[gnome-maps/wip/mlundblad/transit-routing: 8/8] WIP: Hook up a new toggle button for transit mode routing



commit 642971f2c566b80cd6ebb5b2f3bff14a968c94ba
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Feb 15 23:19:09 2016 +0100

    WIP: Hook up a new toggle button for transit mode routing
    
    Currently only sends out a query to the OTP instance to get a list of
    all routers (public transportation networks) currently available in
    the server when the transit mode button is selected.

 data/ui/sidebar.ui  |   23 ++++++++++++
 src/application.js  |    3 ++
 src/contextMenu.js  |    3 +-
 src/mapView.js      |    5 ++-
 src/routeQuery.js   |    9 ++++-
 src/routeService.js |   17 +++++++--
 src/sidebar.js      |   99 +++++++++++++++++++++++++++++++++++++--------------
 7 files changed, 124 insertions(+), 35 deletions(-)
---
diff --git a/data/ui/sidebar.ui b/data/ui/sidebar.ui
index 77347ed..1524e59 100644
--- a/data/ui/sidebar.ui
+++ b/data/ui/sidebar.ui
@@ -94,6 +94,29 @@
                 </style>
               </object>
             </child>
+            <child>
+              <object class="GtkRadioButton" id="modeTransitToggle">
+                <property name="name">mode-transit-toggle</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="active">True</property>
+                <property name="draw_indicator">False</property>
+                <property name="group">modeBikeToggle</property>
+                <property name="height-request">32</property>
+                <property name="width-request">42</property>
+                <child>
+                  <object class="GtkImage" id="mode-transit-image">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="icon-name">route-transit-symbolic</property>
+                  </object>
+                </child>
+                <style>
+                  <class name="transportation-mode-button"/>
+                </style>
+              </object>
+            </child>
             <style>
               <class name="linked"/>
             </style>
diff --git a/src/application.js b/src/application.js
index 2d6b023..efe9aa4 100644
--- a/src/application.js
+++ b/src/application.js
@@ -37,6 +37,7 @@ const GeocodeService = imports.geocodeService;
 const MainWindow = imports.mainWindow;
 const Maps = imports.gi.GnomeMaps;
 const NotificationManager = imports.notificationManager;
+const OpenTripPlanner = imports.openTripPlanner;
 const OSMEdit = imports.osmEdit;
 const OSMTypeSearchEntry = imports.osmTypeSearchEntry;
 const PlaceStore = imports.placeStore;
@@ -55,6 +56,7 @@ let geocodeService = null;
 let networkMonitor = null;
 let checkInManager = null;
 let contactStore = null;
+let openTripPlanner = null;
 let osmEdit = null;
 let normalStartup = true;
 
@@ -256,6 +258,7 @@ const Application = new Lang.Class({
         contactStore = new Maps.ContactStore();
         contactStore.load();
         osmEdit = new OSMEdit.OSMEdit();
+        openTripPlanner = new OpenTripPlanner.OpenTripPlanner();
     },
 
     _createWindow: function() {
diff --git a/src/contextMenu.js b/src/contextMenu.js
index 874414b..744e281 100644
--- a/src/contextMenu.js
+++ b/src/contextMenu.js
@@ -34,6 +34,7 @@ const OSMAccountDialog = imports.osmAccountDialog;
 const OSMEdit = imports.osmEdit;
 const OSMEditDialog = imports.osmEditDialog;
 const Place = imports.place;
+const RouteQuery = imports.routeQuery;
 const Utils = imports.utils;
 const ZoomInNotification = imports.zoomInNotification;
 
@@ -87,7 +88,7 @@ const ContextMenu = new Lang.Class({
     },
 
     _routingUpdate: function() {
-        let query = Application.routeService.query;
+        let query = RouteQuery.query;
 
         if (query.points.length === 0)
             return;
diff --git a/src/mapView.js b/src/mapView.js
index 2bf8f04..2574a0e 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -39,6 +39,7 @@ const Maps = imports.gi.GnomeMaps;
 const MapWalker = imports.mapWalker;
 const Place = imports.place;
 const PlaceMarker = imports.placeMarker;
+const RouteQuery = imports.routeQuery;
 const ShapeLayer = imports.shapeLayer;
 const StoredRoute = imports.storedRoute;
 const TurnPointMarker = imports.turnPointMarker;
@@ -72,7 +73,7 @@ const MapView = new Lang.Class({
     },
 
     set routeVisible(value) {
-        let isValid = Application.routeService.query.isValid();
+        let isValid = RouteQuery.query.isValid();
 
         this._routeLayer.visible = value && isValid;
         this._instructionMarkerLayer.visible = value && isValid;
@@ -172,7 +173,7 @@ const MapView = new Lang.Class({
 
     _connectRouteSignals: function() {
         let route = Application.routeService.route;
-        let query = Application.routeService.query;
+        let query = RouteQuery.query;
 
         route.connect('update', this.showRoute.bind(this, route));
         route.connect('reset', (function() {
diff --git a/src/routeQuery.js b/src/routeQuery.js
index 2108440..6a84312 100644
--- a/src/routeQuery.js
+++ b/src/routeQuery.js
@@ -27,12 +27,14 @@ const Transportation = {
     CAR:        0,
     BIKE:       1,
     PEDESTRIAN: 2,
+    TRANSIT:    3,
 
     toString: function (transportation) {
         switch(transportation) {
         case Transportation.CAR:        return 'car';
         case Transportation.BIKE:       return 'bike';
         case Transportation.PEDESTRIAN: return 'foot';
+        case Transportation.TRANSIT:    return 'transit';
         default:                        return null;
         }
     }
@@ -87,7 +89,8 @@ const RouteQuery = new Lang.Class({
                                                 GObject.ParamFlags.WRITABLE,
                                                 Transportation.CAR,
                                                 Transportation.PEDESTRIAN,
-                                                Transportation.CAR)
+                                                Transportation.CAR,
+                                                Transportation.TRANSIT)
     },
 
     get points() {
@@ -109,6 +112,8 @@ const RouteQuery = new Lang.Class({
         this.parent(args);
         this._points = [];
         this.reset();
+
+        routeQuery = this;
     },
 
     addPoint: function(index) {
@@ -168,3 +173,5 @@ const RouteQuery = new Lang.Class({
                "\nTransportation: " + this.transportation;
     }
 });
+
+const query = new RouteQuery();
diff --git a/src/routeService.js b/src/routeService.js
index 8424c5e..04a2823 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -39,6 +39,14 @@ const GraphHopper = new Lang.Class({
         return this._query;
     },
 
+    set query(query) {
+        this._query = query;
+        if (query)
+            this._connectQuery();
+        else
+            this._query.disconnect('notify::points');
+    },
+
     get route() {
         return this._route;
     },
@@ -49,16 +57,17 @@ const GraphHopper = new Lang.Class({
         this._baseURL = "https://graphhopper.com/api/1/route?";;
         this._locale  = GLib.get_language_names()[0];
         this._route   = new Route.Route();
-        this._query   = new RouteQuery.RouteQuery();
         this.storedRoute = null;
 
-        this.query.connect('notify::points', (function() {
+        this.parent();
+    },
+
+    _connectQuery: function() {
+        this._query.connect('notify::points', (function() {
             if (this.query.isValid())
                 this.fetchRoute(this.query.filledPoints,
                                 this._query.transportation);
         }).bind(this));
-
-        this.parent();
     },
 
     _updateFromStored: function() {
diff --git a/src/sidebar.js b/src/sidebar.js
index f20d535..6656e55 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -48,6 +48,7 @@ const Sidebar = new Lang.Class({
                         'modeBikeToggle',
                         'modeCarToggle',
                         'modePedestrianToggle',
+                        'modeTransitToggle',
                         'timeInfo' ],
 
     _init: function(mapView) {
@@ -55,33 +56,64 @@ const Sidebar = new Lang.Class({
 
         this._mapView = mapView;
 
+        Utils.debug('before _initInstructionList');
+
+        this._query = RouteQuery.query;
         this._initInstructionList();
 
+        Utils.debug('after _initInstructionList');
+
         this._initTransportationToggles(this._modePedestrianToggle,
                                         this._modeBikeToggle,
-                                        this._modeCarToggle);
+                                        this._modeCarToggle,
+                                        this._modeTransitToggle);
+
         this._initQuerySignals();
 
-        let query = Application.routeService.query;
+        Utils.debug('after creating query');
 
-        query.addPoint(0);
-        query.addPoint(1);
+        this._query.addPoint(0);
+        this._query.addPoint(1);
+        this._switchRoutingMode();
     },
 
-    _initTransportationToggles: function(pedestrian, bike, car) {
-        let query = Application.routeService.query;
+    _initTransportationToggles: function(pedestrian, bike, car, transit) {
         let transport = RouteQuery.Transportation;
 
         let onToggle = function(mode, button) {
-            if (button.active && query.transportation !== mode)
-                query.transportation = mode;
+            Utils.debug('onToggle: ' + mode + ', query.mode: ' + this._query.transportation);
+
+            let previousMode = this._query.transportation;
+            if (button.active && this._query.transportation !== mode)
+                this._query.transportation = mode;
+            /* if the transportation mode changes to/from transit
+               change the routing engine */
+            if (button.active &&
+                ((mode !== transport.TRANSIT
+                  && previousMode === transport.TRANSIT)
+                 || (mode === transport.TRANSIT
+                     && previousMode !== transport.TRANSIT))) {
+                Utils.debug('switching routing mode');
+                this._switchRoutingMode();
+            }
+
+            /*
+            if (button.active && mode === transport.TRANSIT) {
+                let openTripPlanner = Application.openTripPlanner;
+
+                openTripPlanner.fetchRouters(function(status, body) {
+                    Utils.debug('fetched routers: ' + status);
+                });
+            }
+            */
         };
         pedestrian.connect('toggled', onToggle.bind(this, transport.PEDESTRIAN));
         car.connect('toggled', onToggle.bind(this, transport.CAR));
         bike.connect('toggled', onToggle.bind(this, transport.BIKE));
+        transit.connect('toggled', onToggle.bind(this, transport.TRANSIT))
 
         let setToggles = function() {
-            switch(query.transportation) {
+            switch(RouteQuery.query.transportation) {
             case transport.PEDESTRIAN:
                 pedestrian.active = true;
                 break;
@@ -91,21 +123,37 @@ const Sidebar = new Lang.Class({
             case transport.BIKE:
                 bike.active = true;
                 break;
+            case transport.TRANSIT:
+                transit.active = true;
+                break;
             }
         };
 
         setToggles();
-        query.connect('notify::transportation', setToggles);
+        this._query.connect('notify::transportation', setToggles);
     },
 
-    _initQuerySignals: function() {
-        let query = Application.routeService.query;
+    _switchRoutingMode: function() {
+        let graphHopper = Application.routeService;
+        let openTripPlanner = Application.openTripPlanner;
+
+        if (this._query.transportation === RouteQuery.Transportation.TRANSIT) {
+            Utils.debug('switching to transit');
+            graphHopper.query = null;
+            openTripPlanner.query = this._query;
+        } else {
+            Utils.debug('switch from transit');
+            openTripPlanner.query = null;
+            graphHopper.query = this._query;
+        }
+    },
 
-        query.connect('point-added', (function(obj, point, index) {
+    _initQuerySignals: function() {
+        this._query.connect('point-added', (function(obj, point, index) {
             this._createRouteEntry(index, point);
         }).bind(this));
 
-        query.connect('point-removed', (function(obj, point, index) {
+        this._query.connect('point-removed', (function(obj, point, index) {
             let row = this._entryList.get_row_at_index(index);
             row.destroy();
         }).bind(this));
@@ -133,7 +181,7 @@ const Sidebar = new Lang.Class({
         if (type === RouteEntry.Type.FROM) {
             routeEntry.button.connect('clicked', (function() {
                 let lastIndex = this._entryList.get_children().length;
-                Application.routeService.query.addPoint(lastIndex - 1);
+                this._query.addPoint(lastIndex - 1);
             }).bind(this));
 
             this.bind_property('child-revealed',
@@ -142,7 +190,7 @@ const Sidebar = new Lang.Class({
         } else if (type === RouteEntry.Type.VIA) {
             routeEntry.button.connect('clicked', function() {
                 let row = routeEntry.get_parent();
-                Application.routeService.query.removePoint(row.get_index());
+                this._query.removePoint(row.get_index());
             });
         }
 
@@ -151,7 +199,6 @@ const Sidebar = new Lang.Class({
 
     _initInstructionList: function() {
         let route = Application.routeService.route;
-        let query = Application.routeService.query;
 
         route.connect('reset', (function() {
             this._clearInstructions();
@@ -159,12 +206,12 @@ const Sidebar = new Lang.Class({
 
             let length = this._entryList.get_children().length;
             for (let index = 1; index < (length - 1); index++) {
-                query.removePoint(index);
+                this._query.removePoint(index);
             }
         }).bind(this));
 
-        query.connect('notify', (function() {
-            if (query.isValid())
+        this._query.connect('notify', (function() {
+            if (this._query.isValid())
                 this._instructionStack.visible_child = this._instructionSpinner;
             else
                 this._clearInstructions();
@@ -183,11 +230,11 @@ const Sidebar = new Lang.Class({
 
             this._storeRouteTimeoutId = Mainloop.timeout_add(5000, (function() {
                 let placeStore = Application.placeStore;
-                let places = query.filledPoints.map(function(point) {
+                let places = this._query.filledPoints.map(function(point) {
                     return point.place;
                 });
                 let storedRoute = new StoredRoute.StoredRoute({
-                    transportation: query.transportation,
+                    transportation: this._query.transportation,
                     route: route,
                     places: places,
                     geoclue: Application.geoclue
@@ -227,7 +274,6 @@ const Sidebar = new Lang.Class({
 
     // Iterate over points and establish the new order of places
     _reorderRoutePoints: function(srcIndex, destIndex) {
-        let query = Application.routeService.query;
         let points = query.points;
         let srcPlace = this._draggedPoint.place;
 
@@ -236,19 +282,18 @@ const Sidebar = new Lang.Class({
 
         // Hold off on notifying the changes to query.points until
         // we have re-arranged the places.
-        query.freeze_notify();
+        this._query.freeze_notify();
 
         for (let i = destIndex; i !== (srcIndex + step); i += step) {
             // swap
             [points[i].place, srcPlace] = [srcPlace, points[i].place];
         }
 
-        query.thaw_notify();
+        this._query.thaw_notify();
     },
 
     _onDragDrop: function(row, context, x, y, time) {
-        let query = Application.routeService.query;
-        let srcIndex = query.points.indexOf(this._draggedPoint);
+        let srcIndex = this._query.points.indexOf(this._draggedPoint);
         let destIndex = row.get_index();
 
         this._reorderRoutePoints(srcIndex, destIndex);


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