[gnome-maps/wip/routing: 34/35] Move model knowledge to searchpopup



commit b022f06dc9e5a3b843cd322c1b7158c9d211099a
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Mon Feb 3 01:40:06 2014 +0100

    Move model knowledge to searchpopup

 src/mainWindow.js  |   92 +++++++++++++++------------------------------------
 src/searchPopup.js |   89 +++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 93 insertions(+), 88 deletions(-)
---
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 3f9fc96..1e8d712 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -22,10 +22,8 @@
  */
 
 const Gdk = imports.gi.Gdk;
-const GdkPixbuf = imports.gi.GdkPixbuf;
 const GLib = imports.gi.GLib;
 const Gtk = imports.gi.Gtk;
-const GObject = imports.gi.GObject;
 const Champlain = imports.gi.Champlain;
 
 const Lang = imports.lang;
@@ -39,6 +37,7 @@ const PlaceStore = imports.placeStore;
 const Utils = imports.utils;
 const Config = imports.config;
 const ZoomControl = imports.zoomControl;
+const Sidebar = imports.sidebar;
 
 const _ = imports.gettext.gettext;
 
@@ -46,14 +45,6 @@ const _CONFIGURE_ID_TIMEOUT = 100; // msecs
 const _WINDOW_MIN_WIDTH = 600;
 const _WINDOW_MIN_HEIGHT = 500;
 
-const _PLACE_ICON_SIZE = 20;
-
-const SearchResults = {
-    COL_ICON:         0,
-    COL_DESCRIPTION:  1,
-    COL_PLACE:        2
-};
-
 const MainWindow = new Lang.Class({
     Name: 'MainWindow',
 
@@ -73,15 +64,18 @@ const MainWindow = new Lang.Class({
 
         this.mapView.gotoUserLocation(false);
 
+        this._initPlaces();
+
         this._contextMenu = new ContextMenu.ContextMenu(this.mapView);
+        this._sidebar = new Sidebar.Sidebar(this.mapView, this._placeStore);
 
-        this._initPlaces();
         this._initSearchWidgets();
         this._initActions();
         this._initSignals();
         this._restoreWindowGeometry();
 
         ui.windowContent.add_overlay(new ZoomControl.ZoomControl(this.mapView));
+        ui.windowContent.add_overlay(this._sidebar);
 
         ui.windowContent.show_all();
     },
@@ -99,11 +93,6 @@ const MainWindow = new Lang.Class({
     _initSearchWidgets: function() {
         this._searchPopup = new SearchPopup.SearchPopup(this._searchEntry, 10);
 
-        let model = new Gtk.ListStore();
-        model.set_column_types([GdkPixbuf.Pixbuf,
-                                GObject.TYPE_STRING,
-                                GObject.TYPE_OBJECT]);
-        this._searchPopup.setModel(model);
         this._searchPopup.connect('selected',
                                   this._onSearchPopupSelected.bind(this));
         this.mapView.view.connect('button-press-event',
@@ -160,6 +149,9 @@ const MainWindow = new Lang.Class({
             }, {
                 properties: { name: 'goto-user-location' },
                 signalHandlers: { activate: this._onGotoUserLocationActivate }
+            }, {
+                properties: { name: 'route-mode-close' },
+                signalHandlers: { activate: this._onRouteModeCloseActivate }
             }
         ], this);
     },
@@ -173,6 +165,19 @@ const MainWindow = new Lang.Class({
         this.window.connect('key-press-event',
                             this._onKeyPressEvent.bind(this));
 
+        this._sidebar.connect('instruction-selected', (function(sidebar, instruction) {
+            if(instruction && instruction.coordinate)
+                this.mapView.showTurnPoint(instruction.coordinate);
+            else
+                log("No coordinate for this turn instruction");
+        }).bind(this));
+
+        this.mapView.connect('got-route', (function(mapView, route) {
+            this._sidebar.open();
+            mapView.showRoute(route);
+            this._sidebar.addInstructions(route.instructions);
+        }).bind(this));
+
         this._searchEntry.connect('activate',
                                   this._onSearchActivate.bind(this));
         this._viewMovedId = 0;
@@ -251,10 +256,12 @@ const MainWindow = new Lang.Class({
         return false;
     },
 
-    _onSearchPopupSelected: function(widget, iter) {
-        let model = this._searchPopup.getModel();
-        let place = model.get_value(iter, SearchResults.COL_PLACE);
+    _onRouteModeCloseActivate: function() {
+        this._sidebar.close();
+        this.mapView.clearRouteLayers();
+    },
 
+    _onSearchPopupSelected: function(widget, place) {
         this.mapView.showNGotoLocation(place);
 
         this._placeStore.addRecent(place);
@@ -265,63 +272,18 @@ const MainWindow = new Lang.Class({
         let searchString = this._searchEntry.get_text();
 
         if (searchString.length > 0) {
-            let model = this._searchPopup.getModel();
-
-            model.clear();
             this._searchPopup.showSpinner();
             this.mapView.geocodeSearch(searchString,
                                        this._showSearchResults.bind(this));
         }
     },
 
-    // We want to match case insensitive but present in the correct case.
-    _boldMatch: function(description, searchStringLower) {
-        let index = description.toLowerCase().indexOf(searchStringLower);
-
-        if (index !== -1) {
-            let substring = description.substring(index,
-                                                  index + searchStringLower.length);
-
-            description = description.replace(substring, substring.bold());
-        }
-
-        return description;
-    },
-
     _showSearchResults: function(places) {
-        let model = this._searchPopup.getModel();
-
         if (places === null) {
             this._searchPopup.hide();
             return;
         }
-
-        // Lower case to match case insensitive
-        let searchStringLower = this._searchEntry.text.toLowerCase();
-
-        places.forEach((function(place) {
-            let iter = model.append();
-            let location = place.get_location();
-            let icon = place.icon;
-
-            if (location == null)
-                return;
-
-            let description = GLib.markup_escape_text(location.description, -1);
-            description = this._boldMatch(description, searchStringLower);
-
-            model.set(iter,
-                      [SearchResults.COL_DESCRIPTION,
-                       SearchResults.COL_PLACE],
-                      [description,
-                       place]);
-
-            if (icon !== null) {
-                Utils.load_icon(icon, _PLACE_ICON_SIZE, function(pixbuf) {
-                    model.set(iter, [SearchResults.COL_ICON], [pixbuf]);
-                });
-            }
-        }).bind(this));
+        this._searchPopup.update(places, this._searchEntry.get_text());
         this._searchPopup.showResult();
     },
 
diff --git a/src/searchPopup.js b/src/searchPopup.js
index 97ebc14..1b43102 100644
--- a/src/searchPopup.js
+++ b/src/searchPopup.js
@@ -19,21 +19,34 @@
  */
 
 const Gtk = imports.gi.Gtk;
+const GLib = imports.gi.GLib;
+const GdkPixbuf = imports.gi.GdkPixbuf;
+const GObject = imports.gi.GObject;
 
 const Lang = imports.lang;
 const Utils = imports.utils;
 
 const Columns = {
-    ICON: 0,
-    TEXT: 1
+    ICON:         0,
+    DESCRIPTION:  1,
+    PLACE:        2
 };
 
+const _PLACE_ICON_SIZE = 24;
+
 const SearchPopup = new Lang.Class({
     Name: 'SearchPopup',
     Extends: Gtk.Popover,
 
     _init: function(relativeTo, numVisible) {
-        this._numVisible = numVisible;
+        let widthRequest = relativeTo.get_preferred_width()[1];
+
+        this.parent({ relative_to: relativeTo,
+                      width_request: widthRequest,
+                      can_default: true,
+                      can_focus: true,
+                      no_show_all: true,
+                      visible: true });
 
         let ui = Utils.getUIObject('search-popup', ['scrolled-window',
                                                     'stack',
@@ -45,18 +58,18 @@ const SearchPopup = new Lang.Class({
         this._spinner = ui.spinner;
         this._treeView = ui.treeview;
 
+        let model = new Gtk.ListStore();
+        model.set_column_types([GdkPixbuf.Pixbuf,
+                                GObject.TYPE_STRING,
+                                GObject.TYPE_OBJECT]);
+        this._treeView.model = model;
+
         this._treeView.connect('button-press-event',
                                this._onListButtonPress.bind(this));
         this._initList();
-
-        this.height_request = this._cellHeight * this._numVisible;
+        this.height_request = this._cellHeight * numVisible;
         this._scrolledWindow.set_min_content_height(this.height_request);
 
-        this.parent({ relative_to: relativeTo,
-                      width_request: 500,
-                      no_show_all: true,
-                      visible: true });
-
         this.get_style_context().add_class('maps-popover');
         this.add(this._stack);
         this.hide();
@@ -74,7 +87,7 @@ const SearchPopup = new Lang.Class({
         cell = new Gtk.CellRendererText({ xpad: 8,
                                           ypad: 8 });
         column.pack_start(cell, true);
-        column.add_attribute(cell, 'markup', Columns.TEXT);
+        column.add_attribute(cell, 'markup', Columns.DESCRIPTION);
 
         this._cellHeight = column.cell_get_size(null)[3];
         this._cellHeight += cell.get_preferred_height(this._treeView)[0];
@@ -88,7 +101,7 @@ const SearchPopup = new Lang.Class({
         [path_valid, path] = this._treeView.get_path_at_pos(coordX, coordY,
                                                             null, null, null);
         if (path_valid) {
-            let model = this.getModel();
+            let model = this._treeView.model;
             let iter_valid, iter;
 
             if (model === null)
@@ -98,7 +111,7 @@ const SearchPopup = new Lang.Class({
             if (!iter_valid)
                 return;
 
-            this.emit('selected', iter);
+            this.emit('selected', model.get_value(iter, Columns.PLACE));
         }
     },
 
@@ -120,11 +133,6 @@ const SearchPopup = new Lang.Class({
             this.show();
     },
 
-    vfunc_show: function() {
-        this._treeView.columns_autosize();
-        this.parent();
-    },
-
     vfunc_hide: function() {
         if (this._spinner.active)
             this._spinner.stop();
@@ -132,12 +140,47 @@ const SearchPopup = new Lang.Class({
         this.parent();
     },
 
-    setModel: function(model) {
-        this._treeView.set_model(model);
-    },
+    update: function(places, searchString) {
+        let model = this._treeView.get_model();
+
+        model.clear();
 
-    getModel: function() {
-        return this._treeView.get_model();
+        places.forEach((function(place) {
+            if (!place.location)
+                return;
+
+            let iter = model.append();
+            let location = place.get_location();
+            let icon = place.icon;
+
+            let description = GLib.markup_escape_text(location.description, -1);
+            description = this._boldMatch(description, searchString);
+
+            model.set(iter,
+                      [Columns.DESCRIPTION,
+                       Columns.PLACE],
+                      [description,
+                       place]);
+
+            if (icon !== null) {
+                Utils.load_icon(icon, _PLACE_ICON_SIZE, function(pixbuf) {
+                    model.set(iter, [Columns.ICON], [pixbuf]);
+                });
+            }
+        }).bind(this));
     },
+
+    _boldMatch: function(description, searchString) {
+        searchString = searchString.toLowerCase();
+
+        let index = description.toLowerCase().indexOf(searchString);
+
+        if (index !== -1) {
+            let substring = description.substring(index,
+                                                  index + searchString.length);
+            description = description.replace(substring, substring.bold());
+        }
+        return description;
+    }
 });
 Utils.addSignalMethods(SearchPopup.prototype);


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