[gnome-maps] Separate module/class for map view, MapView
- From: Zeeshan Ali Khattak <zeeshanak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps] Separate module/class for map view, MapView
- Date: Sun, 31 Mar 2013 02:34:37 +0000 (UTC)
commit dfd32f00fbcd1ee3b64e1cf019773fd644081087
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date: Sun Mar 31 05:23:46 2013 +0300
Separate module/class for map view, MapView
Separate out all the map view related code into another module/class,
MapView.
src/Makefile-js.am | 1 +
src/mainToolbar.js | 66 +------------------------
src/mainWindow.js | 37 ++------------
src/mapView.js | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 143 insertions(+), 96 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 7b1fbe7..2b0d22f 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -4,6 +4,7 @@ dist_js_DATA = \
main.js \
mainWindow.js \
mainToolbar.js \
+ mapView.js \
path.js \
utils.js
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index 3333360..82a52be 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -45,73 +45,11 @@ const MainToolbar = new Lang.Class({
this.widget = new Gd.HeaderBar();
this.widget.set_custom_title(this._entry);
-
- this._markerLayer = new Champlain.MarkerLayer();
- this._markerLayer.set_selection_mode(Champlain.SelectionMode.SINGLE);
- this._mainWindow.view.add_layer(this._markerLayer);
},
_onSearchActivate: function() {
- let str = this._entry.get_text();
- let forward = Geocode.Forward.new_for_string(str);
- forward._searchStr = str;
- this._markerLayer.remove_all();
-
+ let string = this._entry.get_text();
- forward.search_async (null, Lang.bind(this, this._onSearchComplete));
+ this._mainWindow.mapView.geocodeSearch(string);
},
-
- _onSearchComplete: function(forward, res) {
- let locations = [];
-
- try {
- locations = forward.search_finish(res);
- } catch (e) {
- let str = forward.get_data ("string");
- log ("Failed to search '" + forward._searchStr + "': " + e.message);
- return;
- }
- log (locations.length + " locations found");
- if (locations.length == 0)
- return;
-
- locations.forEach(Lang.bind(this,
- function(location) {
- log ("location: " + location);
- let marker = new Champlain.Label();
- marker.set_text(location.description);
- marker.set_location(location.latitude, location.longitude);
- this._markerLayer.add_marker(marker);
- log ("Added marker at " + location.latitude + ", " + location.longitude);
- }));
-
- if (locations.length == 1)
- this._mainWindow.view.go_to(locations[0].latitude, locations[0].longitude);
- else {
- let min_latitude = 90;
- let max_latitude = -90;
- let min_longitude = 180;
- let max_longitude = -180;
-
- locations.forEach(Lang.bind(this,
- function(location) {
- if (location.latitude > max_latitude)
- max_latitude = location.latitude;
- if (location.latitude < min_latitude)
- min_latitude = location.latitude;
- if (location.longitude > max_longitude)
- max_longitude = location.longitude;
- if (location.longitude < min_longitude)
- min_longitude = location.longitude;
- }));
-
- let bbox = new Champlain.BoundingBox();
- bbox.left = min_longitude;
- bbox.right = max_longitude;
- bbox.bottom = min_latitude;
- bbox.top = max_latitude;
-
- this._mainWindow.view.ensure_visible(bbox, true);
- }
- }
});
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 3937411..58c8df0 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -31,6 +31,7 @@ const Mainloop = imports.mainloop;
const Application = imports.application;
const MainToolbar = imports.mainToolbar;
+const MapView = imports.mapView;
const Utils = imports.utils;
const Config = imports.config;
@@ -86,41 +87,13 @@ const MainWindow = new Lang.Class({
grid.set_orientation (Gtk.Orientation.VERTICAL);
this.window.add(grid);
- this._embed = new GtkChamplain.Embed();
- this.view = this._embed.get_view();
- this.view.set_zoom_level(3);
-
- this._toolbar = new MainToolbar.MainToolbar (this);
-
+ this._toolbar = new MainToolbar.MainToolbar(this);
grid.add(this._toolbar.widget);
- grid.add(this._embed);
- grid.show_all();
+ this.mapView = new MapView.MapView();
+ grid.add(this.mapView.widget);
- let ipclient = new Geocode.Ipclient();
- ipclient.server = "http://freegeoip.net/json/";
- ipclient.search_async(null, Lang.bind(this, this._onSearchComplete));
- },
-
- _onSearchComplete: function(ipclient, res) {
- try {
- let [location, accuracy] = ipclient.search_finish(res);
- log(location.description);
-
- let zoom = Utils.getZoomLevelForAccuracy(accuracy);
- this.view.go_to(location.latitude, location.longitude);
- this.view.connect("animation-completed::go-to", Lang.bind(this,
- function() {
- // Apparently the signal is called before animation is really complete so if we don't
- // zoom in idle, we get a crash. Perhaps a bug in libchamplain?
- Mainloop.idle_add(Lang.bind(this,
- function() {
- this.view.set_zoom_level(zoom);
- }));
- }));
- } catch (e) {
- log("Failed to find your location: " + e);
- }
+ grid.show_all();
},
_saveWindowGeometry: function() {
diff --git a/src/mapView.js b/src/mapView.js
new file mode 100644
index 0000000..c9a3be1
--- /dev/null
+++ b/src/mapView.js
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2011, 2012, 2013 Red Hat, Inc.
+ *
+ * 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: Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ */
+
+const Gdk = imports.gi.Gdk;
+const GLib = imports.gi.GLib;
+const Gtk = imports.gi.Gtk;
+const Champlain = imports.gi.Champlain;
+const GtkChamplain = imports.gi.GtkChamplain;
+const Geocode = imports.gi.GeocodeGlib;
+
+const Lang = imports.lang;
+const Mainloop = imports.mainloop;
+
+const Utils = imports.utils;
+const _ = imports.gettext.gettext;
+
+const MapView = new Lang.Class({
+ Name: 'MapView',
+
+ _init: function(app) {
+ this.widget = new GtkChamplain.Embed();
+ this.actor = this.widget.get_view();
+
+ this._view = this.actor;
+ this._view.set_zoom_level(3);
+
+ this._markerLayer = new Champlain.MarkerLayer();
+ this._markerLayer.set_selection_mode(Champlain.SelectionMode.SINGLE);
+ this._view.add_layer(this._markerLayer);
+
+ let ipclient = new Geocode.Ipclient();
+ ipclient.server = "http://freegeoip.net/json/";
+ ipclient.search_async(null, Lang.bind(this, this._onIpclientSearchComplete));
+ },
+
+ geocodeSearch: function(string) {
+ let forward = Geocode.Forward.new_for_string(string);
+ forward._searchString = string;
+ this._markerLayer.remove_all();
+
+ forward.search_async (null, Lang.bind(this, this._onGeocodeSearchComplete));
+ },
+
+ _onIpclientSearchComplete: function(ipclient, res) {
+ try {
+ let [location, accuracy] = ipclient.search_finish(res);
+ log(location.description);
+
+ let zoom = Utils.getZoomLevelForAccuracy(accuracy);
+ this._view.go_to(location.latitude, location.longitude);
+ this._view.connect("animation-completed::go-to", Lang.bind(this,
+ function() {
+ // Apparently the signal is called before animation is really complete so if we don't
+ // zoom in idle, we get a crash. Perhaps a bug in libchamplain?
+ Mainloop.idle_add(Lang.bind(this,
+ function() {
+ this._view.set_zoom_level(zoom);
+ }));
+ }));
+ } catch (e) {
+ log("Failed to find your location: " + e);
+ }
+ },
+
+ _onGeocodeSearchComplete: function(forward, res) {
+ let locations = [];
+
+ try {
+ locations = forward.search_finish(res);
+ } catch (e) {
+ let str = forward.get_data ("string");
+ log ("Failed to search '" + forward._searchString + "': " + e.message);
+ return;
+ }
+ log (locations.length + " locations found");
+ if (locations.length == 0)
+ return;
+
+ locations.forEach(Lang.bind(this,
+ function(location) {
+ log ("location: " + location);
+ let marker = new Champlain.Label();
+ marker.set_text(location.description);
+ marker.set_location(location.latitude, location.longitude);
+ this._markerLayer.add_marker(marker);
+ log ("Added marker at " + location.latitude + ", " + location.longitude);
+ }));
+
+ if (locations.length == 1)
+ this._view.go_to(locations[0].latitude, locations[0].longitude);
+ else {
+ let min_latitude = 90;
+ let max_latitude = -90;
+ let min_longitude = 180;
+ let max_longitude = -180;
+
+ locations.forEach(Lang.bind(this,
+ function(location) {
+ if (location.latitude > max_latitude)
+ max_latitude = location.latitude;
+ if (location.latitude < min_latitude)
+ min_latitude = location.latitude;
+ if (location.longitude > max_longitude)
+ max_longitude = location.longitude;
+ if (location.longitude < min_longitude)
+ min_longitude = location.longitude;
+ }));
+
+ let bbox = new Champlain.BoundingBox();
+ bbox.left = min_longitude;
+ bbox.right = max_longitude;
+ bbox.bottom = min_latitude;
+ bbox.top = max_latitude;
+
+ this._view.ensure_visible(bbox, true);
+ }
+ }
+});
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]