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



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

    Move model knowledge to searchpopup

 src/mainWindow.js  |   67 +--------------------------------------
 src/searchPopup.js |   89 ++++++++++++++++++++++++++++++++++++++-------------
 src/sidebar.js     |   19 ++++++++++-
 3 files changed, 86 insertions(+), 89 deletions(-)
---
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 9f23d7b..744782a 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;
@@ -46,14 +44,6 @@ const _CONFIGURE_ID_TIMEOUT = 100; // msecs
 const _WINDOW_MIN_WIDTH = 600;
 const _WINDOW_MIN_HEIGHT = 500;
 
-const _PLACE_ICON_SIZE = 24;
-
-const SearchResults = {
-    COL_ICON:         0,
-    COL_DESCRIPTION:  1,
-    COL_PLACE:        2
-};
-
 const MainWindow = new Lang.Class({
     Name: 'MainWindow',
 
@@ -105,11 +95,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',
@@ -263,10 +248,7 @@ const MainWindow = new Lang.Class({
         this.mapView.clearRoutes();
     },
 
-    _onSearchPopupSelected: function(widget, iter) {
-        let model = this._searchPopup.getModel();
-        let place = model.get_value(iter, SearchResults.COL_PLACE);
-
+    _onSearchPopupSelected: function(widget, place) {
         this.mapView.showNGotoLocation(place.location);
 
         this._placeStore.addRecent(place);
@@ -277,63 +259,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 604fbae..4276f6b 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.get_style_context().add_class('search-popover');
         this.add(this._scrolledWindow);
@@ -75,7 +88,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];
@@ -89,7 +102,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)
@@ -99,7 +112,7 @@ const SearchPopup = new Lang.Class({
             if (!iter_valid)
                 return;
 
-            this.emit('selected', iter);
+            this.emit('selected', model.get_value(iter, Columns.PLACE));
         }
     },
 
@@ -121,11 +134,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();
@@ -133,12 +141,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);
diff --git a/src/sidebar.js b/src/sidebar.js
index 3933463..9704a94 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -36,6 +36,7 @@ const Signals = imports.signals;
 const PlaceStore = imports.placeStore;
 const Path = imports.path;
 const Route = imports.route;
+const SearchPopup = imports.searchPopup;
 const Utils = imports.utils;
 const _ = imports.gettext.gettext;
 
@@ -111,6 +112,8 @@ const Sidebar = new Lang.Class({
         this._ui.toCompletion.set_match_func(PlaceStore.completionMatchFunc);
         this._ui.fromCompletion.set_match_func(PlaceStore.completionMatchFunc);
 
+        this._fromPopover = new SearchPopup.SearchPopup(this._ui.fromEntry, 5);
+        this._toPopover = new SearchPopup.SearchPopup(this._ui.toEntry, 5);
         this._ui.toEntry.connect('activate', this._onSearchActivate.bind(this));
         this._ui.fromEntry.connect('activate', this._onSearchActivate.bind(this));
     },
@@ -119,7 +122,21 @@ const Sidebar = new Lang.Class({
         let searchString = entry.get_text();
 
         if (searchString.length > 0) {
-            /* do search */
+            let popover;
+
+            if (entry === this._ui.toEntry)
+                popover = this._toPopover;
+            else
+                popover = this._fromPopover;
+
+            popover.showSpinner();
+            this._mapView.geocodeSearch(searchString, function(places) {
+                if (places === null)
+                    popover.hide();
+
+                popover.update(places, searchString);
+                popover.showResult();
+            });
         }
     },
 


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