[gnome-maps] Move search result model knowledge to SearchPopup



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

    Move search result model knowledge to SearchPopup
    
    This allows easy use of the SearchPopup from more places than
    mainwindow.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=723895

 src/mainWindow.js  |   67 +------------------------------------------------
 src/searchPopup.js |   70 +++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 60 insertions(+), 77 deletions(-)
---
diff --git a/src/mainWindow.js b/src/mainWindow.js
index a359685..8b764d3 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 = 20;
-
-const SearchResults = {
-    COL_ICON:         0,
-    COL_DESCRIPTION:  1,
-    COL_PLACE:        2
-};
-
 const MainWindow = new Lang.Class({
     Name: 'MainWindow',
 
@@ -99,11 +89,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',
@@ -251,10 +236,7 @@ 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);
-
+    _onSearchPopupSelected: function(widget, place) {
         this.mapView.showNGotoLocation(place);
 
         this._placeStore.addRecent(place);
@@ -265,63 +247,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.updateResult(places, this._searchEntry.get_text());
         this._searchPopup.showResult();
     },
 
diff --git a/src/searchPopup.js b/src/searchPopup.js
index 97ebc14..201d510 100644
--- a/src/searchPopup.js
+++ b/src/searchPopup.js
@@ -19,15 +19,21 @@
  */
 
 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 = 20;
+
 const SearchPopup = new Lang.Class({
     Name: 'SearchPopup',
     Extends: Gtk.Popover,
@@ -45,11 +51,16 @@ 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,
@@ -74,7 +85,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 +99,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 +109,7 @@ const SearchPopup = new Lang.Class({
             if (!iter_valid)
                 return;
 
-            this.emit('selected', iter);
+            this.emit('selected', model.get_value(iter, Columns.PLACE));
         }
     },
 
@@ -132,12 +143,47 @@ const SearchPopup = new Lang.Class({
         this.parent();
     },
 
-    setModel: function(model) {
-        this._treeView.set_model(model);
-    },
+    updateResult: 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]