[gnome-maps/wip/refactor: 3/4] MapView: move out route layer



commit 66fdac9e15099cae2764765e0e4845a3b3caf31c
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sun Aug 17 21:31:07 2014 +0200

    MapView: move out route layer
    
    Move out the route layer and its associated methods to its own class.

 src/gnome-maps.js.gresource.xml |    1 +
 src/mapView.js                  |   36 +++-------------
 src/routeLayer.js               |   85 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 29 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index ca842c7..cee9118 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -19,6 +19,7 @@
     <file>placeEntry.js</file>
     <file>placeStore.js</file>
     <file>route.js</file>
+    <file>routeLayer.js</file>
     <file>routeQuery.js</file>
     <file>routeService.js</file>
     <file>searchPopup.js</file>
diff --git a/src/mapView.js b/src/mapView.js
index 3e7ea4a..4495575 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -37,6 +37,7 @@ const Application = imports.application;
 const Utils = imports.utils;
 const Path = imports.path;
 const MapLocation = imports.mapLocation;
+const RouteLayer = imports.routeLayer;
 const UserLocation = imports.userLocation;
 const _ = imports.gettext.gettext;
 
@@ -63,8 +64,6 @@ const MapView = new Lang.Class({
         this._updateUserLocation();
         Application.geoclue.connect("location-changed",
                                     this._updateUserLocation.bind(this));
-
-        this._connectRouteSignals(Application.routeService.route);
     },
 
     _initView: function() {
@@ -87,20 +86,21 @@ const MapView = new Lang.Class({
     },
 
     _initLayers: function() {
-        this._routeLayer = new Champlain.PathLayer({ stroke_width: 2.0 });
+        let routeModel = Application.routeService.route;
+        this._routeLayer = new RouteLayer.RouteLayer({ model:   routeModel,
+                                                       mapView: this });
         this.view.add_layer(this._routeLayer);
 
         let mode = Champlain.SelectionMode.SINGLE;
         this._markerLayer = new Champlain.MarkerLayer({ selection_mode: mode });
-        this.view.add_layer(this._markerLayer);
+        this.view.add_layer(this._markerLayer);;
 
         this._userLocationLayer = new Champlain.MarkerLayer({ selection_mode: mode });
         this.view.add_layer(this._userLocationLayer);
     },
 
-    _connectRouteSignals: function(route) {
-        route.connect('update', this.showRoute.bind(this, route));
-        route.connect('reset', this._routeLayer.remove_all.bind(this._routeLayer));
+    get routeLayer() {
+        return this._routeLayer;
     },
 
     setMapType: function(mapType) {
@@ -174,28 +174,6 @@ const MapView = new Lang.Class({
         mapLocation.goTo(true);
     },
 
-    showRoute: function(route) {
-        this._routeLayer.remove_all();
-
-        route.path.forEach(this._routeLayer.add_node.bind(this._routeLayer));
-
-        // Animate to the center of the route bounding box
-        // goto() is currently implemented on mapLocation, so we need to go
-        // through some hoops here.
-        let [lat, lon] = route.bbox.get_center();
-        let place = new Geocode.Place({
-            location     : new Geocode.Location({ latitude  : lat,
-                                                  longitude : lon }),
-            bounding_box : new Geocode.BoundingBox({ top    : route.bbox.top,
-                                                     bottom : route.bbox.bottom,
-                                                     left   : route.bbox.left,
-                                                     right  : route.bbox.right })
-        });
-        let mapLocation = new MapLocation.MapLocation(place, this);
-
-        mapLocation.goTo(true);
-    },
-
     _onViewMoved: function() {
         this.emit('view-moved');
     }
diff --git a/src/routeLayer.js b/src/routeLayer.js
new file mode 100644
index 0000000..55d6903
--- /dev/null
+++ b/src/routeLayer.js
@@ -0,0 +1,85 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2014 Mattias Bengtsson
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
+ */
+
+const Champlain = imports.gi.Champlain;
+const Geocode = imports.gi.GeocodeGlib;
+const Lang = imports.lang;
+
+const MapLocation = imports.mapLocation;
+
+const RouteLayer = new Lang.Class({
+    Name: 'RouteLayer',
+    Extends: Champlain.PathLayer,
+
+    _init: function(props) {
+        props = props || {};
+        
+        let model = props.model;
+        delete props.model;
+
+        this._mapView = props.mapView;
+        delete props.mapView;
+
+        props.stroke_width = 2.0;
+        this.parent(props);
+
+        if(model)
+            this.setModel(model);
+    },
+
+    setModel: function(model) {
+        this._model = model;
+        this._model.connect('update', this._refresh.bind(this));
+        this._model.connect('reset',  this.remove_all.bind(this));
+
+        this._refresh();
+    },
+
+    _refresh: function() {
+        this.remove_all();
+
+        this._model.path.forEach(this.add_node.bind(this));
+        this._goToRoute();
+    },
+
+    // Animate to the center of the route bounding box
+    // goto() is currently implemented on mapLocation, so we need to go
+    // through some hoops here.
+    _goToRoute: function() {
+        if(!this._model.bbox)
+            return;
+
+        let bbox = this._model.bbox;
+        let [lat, lon] = bbox.get_center();
+        let place = new Geocode.Place({
+            location: new Geocode.Location({ latitude:  lat,
+                                             longitude: lon }),
+            bounding_box: new Geocode.BoundingBox({ top:    bbox.top,
+                                                    bottom: bbox.bottom,
+                                                    left:   bbox.left,
+                                                    right:  bbox.right })
+        });
+        let mapLocation = new MapLocation.MapLocation(place, this._mapView);
+
+        mapLocation.goTo(true);
+    }
+});


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