[gnome-boxes] collection-view: use a filtered model



commit 32fa094b3a08995afa07cd1f90dfca8a14bbbac3
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Thu Aug 2 20:05:19 2012 +0200

    collection-view: use a filtered model
    
    Learn to filter the view model by using the model_visible () callback.
    
    The Boxes.CollectionFilter contains the current filtering criterias.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=681089

 src/app.vala             |    3 ++-
 src/collection-view.vala |   34 +++++++++++++++++++++++++++++-----
 src/collection.vala      |   12 ++++++++++++
 3 files changed, 43 insertions(+), 6 deletions(-)
---
diff --git a/src/app.vala b/src/app.vala
index 0fe8d38..f15576b 100644
--- a/src/app.vala
+++ b/src/app.vala
@@ -42,6 +42,7 @@ private class Boxes.App: Boxes.UI {
     public DisplayPage display_page;
     public string? uri { get; set; }
     public Collection collection;
+    public CollectionFilter filter;
     public GLib.SimpleAction action_properties;
     public GLib.SimpleAction action_fullscreen;
     public GLib.SimpleAction action_shutdown;
@@ -65,7 +66,7 @@ private class Boxes.App: Boxes.UI {
         settings = new GLib.Settings ("org.gnome.boxes");
         connections = new HashTable<string, GVir.Connection> (str_hash, str_equal);
         sources = new HashTable<string,CollectionSource> (str_hash, str_equal);
-
+        filter = new Boxes.CollectionFilter ();
         var action = new GLib.SimpleAction ("quit", null);
         action.activate.connect (() => { quit (); });
         application.add_action (action);
diff --git a/src/collection-view.vala b/src/collection-view.vala
index 8ca58a6..2c96bf0 100644
--- a/src/collection-view.vala
+++ b/src/collection-view.vala
@@ -22,6 +22,7 @@ private class Boxes.CollectionView: Boxes.UI {
         ITEM
     }
     private Gtk.ListStore model;
+    private Gtk.TreeModelFilter model_filter;
 
     public bool visible {
         set { icon_view.visible = value; }
@@ -221,19 +222,40 @@ private class Boxes.CollectionView: Boxes.UI {
         var iter = item.get_data<Gtk.TreeIter?> ("iter");
         if (iter == null)
             return null;
-        return model.get_path (iter);
+
+        Gtk.TreeIter filter_iter;
+        if (!model_filter.convert_child_iter_to_iter (out filter_iter, iter))
+            return null;
+
+        return model_filter.get_path (filter_iter);
     }
 
-    private CollectionItem get_item_for_path (Gtk.TreePath path) {
-        Gtk.TreeIter iter;
+    private CollectionItem get_item_for_iter (Gtk.TreeIter iter) {
         GLib.Value value;
 
-        model.get_iter (out iter, path);
         model.get_value (iter, ModelColumns.ITEM, out value);
 
         return (CollectionItem) value;
     }
 
+
+    private CollectionItem get_item_for_path (Gtk.TreePath path) {
+        Gtk.TreeIter filter_iter, iter;
+
+        model_filter.get_iter (out filter_iter, path);
+        model_filter.convert_iter_to_child_iter (out iter, filter_iter);
+
+        return get_item_for_iter (iter);
+    }
+
+    private bool model_visible (Gtk.TreeModel model, Gtk.TreeIter iter) {
+        return App.app.filter.filter (get_item_for_iter (iter));
+    }
+
+    public void refilter () {
+        model_filter.refilter ();
+    }
+
     private void setup_view () {
         model = new Gtk.ListStore (3,
                                    typeof (Gdk.Pixbuf),
@@ -251,8 +273,10 @@ private class Boxes.CollectionView: Boxes.UI {
             return item_a.name.collate (item_b.name);
         });
         model.set_sort_column_id (Gtk.SortColumn.DEFAULT, Gtk.SortType.ASCENDING);
+        model_filter = new Gtk.TreeModelFilter (model, null);
+        model_filter.set_visible_func (model_visible);
 
-        icon_view = new Gtk.IconView.with_model (model);
+        icon_view = new Gtk.IconView.with_model (model_filter);
         icon_view.get_style_context ().add_class ("boxes-bg");
         icon_view.button_press_event.connect ((event) => {
             if (App.app.selection_mode)
diff --git a/src/collection.vala b/src/collection.vala
index 9cea481..2cc5ac6 100644
--- a/src/collection.vala
+++ b/src/collection.vala
@@ -28,6 +28,18 @@ private class Boxes.Collection: GLib.Object {
     }
 }
 
+private class Boxes.CollectionFilter: GLib.Object {
+    public string text;
+
+    public bool filter (CollectionItem item) {
+        if (text == null)
+            return true;
+
+        var text = text.casefold ();
+        return item.name.casefold ().index_of (text) != -1;
+    }
+}
+
 private class Boxes.Category: GLib.Object {
     public enum Kind {
         USER,



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