[gnome-maps] Add via entries by adding points to RouteQuery



commit 2725e2d85dcb2381e3284150210f5e59cf4f7dae
Author: Jonas Danielsson <jonas danielsson threetimestwo org>
Date:   Thu Oct 2 05:28:08 2014 -0400

    Add via entries by adding points to RouteQuery
    
    Refactor code so that adding a point to the route query
    will add an entry in the sidebar. This allows us to add via
    entries from other places than the sidebar module.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=737775

 src/routeQuery.js |   23 +++++++++++++++++++----
 src/sidebar.js    |   38 ++++++++++++++++++++++++++++----------
 2 files changed, 47 insertions(+), 14 deletions(-)
---
diff --git a/src/routeQuery.js b/src/routeQuery.js
index fa7099c..e549ec8 100644
--- a/src/routeQuery.js
+++ b/src/routeQuery.js
@@ -43,7 +43,9 @@ const RouteQuery = new Lang.Class({
     Name: 'RouteQuery',
     Extends: GObject.Object,
     Signals: {
-        'reset': { }
+        'reset': { },
+        'point-added': { param_types: [GObject.TYPE_OBJECT, GObject.TYPE_INT] },
+        'point-removed': { param_types: [GObject.TYPE_OBJECT, GObject.TYPE_INT] }
     },
     Properties: {
         'points': GObject.ParamSpec.object('points',
@@ -83,16 +85,29 @@ const RouteQuery = new Lang.Class({
         this.reset();
     },
 
-    addPoint: function(point, index) {
+    addPoint: function(index) {
+        let point = new QueryPoint();
+
+        if (index === -1)
+            index = this.points.length - 1;
+
         this._points.splice(index, 0, point);
         point.connect('notify::place', (function() {
             this.notify('points');
         }).bind(this));
+        this.emit('point-added', point, index);
+
+        return point;
     },
 
     removePoint: function(index) {
-        this._points.splice(index, 1);
-        this.notify('points');
+        let removedPoints = this._points.splice(index, 1);
+        let point = removedPoints ? removedPoints[0] : null;
+
+        if (point) {
+            this.notify('points');
+            this.emit('point-removed', point, index);
+        }
     },
 
     set transportation(transportation) {
diff --git a/src/sidebar.js b/src/sidebar.js
index 9529e06..870a0a7 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -99,15 +99,22 @@ const Sidebar = new Lang.Class({
                                         ui.modeBikeToggle,
                                         ui.modeCarToggle);
 
+        let query = Application.routeService.query;
+
+        query.addPoint(0);
         let fromEntry = this._initRouteEntry(ui.fromEntryGrid, 0);
+
+        query.addPoint(1);
         this._initRouteEntry(ui.toEntryGrid, 1);
 
+        this._initQuerySignals(ui.viaGridContainer);
+
         this.bind_property('child-revealed',
                            fromEntry, 'has_focus',
                            GObject.BindingFlags.DEFAULT);
 
         ui.viaAddButton.connect('clicked', (function() {
-            this._createViaRow(ui.viaGridContainer);
+            query.addPoint(-1);
         }).bind(this));
 
         this.add(ui.sidebar);
@@ -143,6 +150,21 @@ const Sidebar = new Lang.Class({
         query.connect('notify::transportation', setToggles);
     },
 
+    _initQuerySignals: function(listbox) {
+        let query = Application.routeService.query;
+
+        // Do nothing for the From and To points.
+        query.connect('point-added', (function(obj, point, index) {
+            if (index !== 0 && index !== query.points.length - 1)
+                this._createViaRow(listbox, index);
+        }).bind(this));
+
+        query.connect('point-removed', (function(obj, point, index) {
+            let row = listbox.get_row_at_index(index - 1);
+            row.destroy();
+        }).bind(this));
+    },
+
     _createPlaceEntry: function() {
         return new PlaceEntry.PlaceEntry({ visible: true,
                                            can_focus: true,
@@ -152,21 +174,19 @@ const Sidebar = new Lang.Class({
                                            parseOnFocusOut: true });
     },
 
-    _createViaRow: function(listbox) {
+    _createViaRow: function(listbox, index) {
         let ui = Utils.getUIObject('route-via-row', [ 'via-grid',
                                                       'via-remove-button',
                                                       'via-entry-grid' ]);
+        let insertIndex = index - 1;
+        let entry = this._createPlaceEntry();
 
-        // Always insert before 'To'
-        let insertIndex = Application.routeService.query.points.length - 1;
+        this._initRouteEntry(ui.viaEntryGrid, index);
         listbox.insert(ui.viaGrid, insertIndex);
-        this._initRouteEntry(ui.viaEntryGrid, insertIndex);
 
         ui.viaRemoveButton.connect('clicked', function() {
             let row = ui.viaGrid.get_parent();
             let pointIndex = row.get_index();
-
-            listbox.remove(row);
             Application.routeService.query.removePoint(pointIndex + 1);
         });
     },
@@ -175,11 +195,10 @@ const Sidebar = new Lang.Class({
         let entry = this._createPlaceEntry();
         container.add(entry);
 
-        let point = new RouteQuery.QueryPoint();
+        let point = Application.routeService.query.points[pointIndex];
         entry.bind_property('place',
                             point, 'place',
                             GObject.BindingFlags.BIDIRECTIONAL);
-        Application.routeService.query.addPoint(point, pointIndex);
 
         return entry;
     },
@@ -193,7 +212,6 @@ const Sidebar = new Lang.Class({
             this._instructionStack.visible_child = this._instructionWindow;
             this._viaGridContainer.get_children().forEach((function(row) {
                 query.removePoint(row.get_index() + 1);
-                row.destroy();
             }).bind(this));
         }).bind(this));
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]