[gnome-clocks] Encapsulate ListStore usage in the ContentView widget



commit f6c129c9da54b2e48694822ef1a7dc356b10602e
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Feb 24 15:24:02 2013 +0100

    Encapsulate ListStore usage in the ContentView widget
    
    First step in hiding usage of ListStor/IconView from alarm and world.
    Once the two clocks are not aware of the implementation it should be
    easier to experiment with EggFlowBox.

 src/alarm.vala   |   35 +++++++++++------------------------
 src/widgets.vala |   36 ++++++++++++++++++++++++++++++++++--
 src/world.vala   |   31 +++++++++----------------------
 3 files changed, 54 insertions(+), 48 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index fd50325..ec7560f 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -476,36 +476,23 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
             }
         });
 
-        icon_view.item_activated.connect ((path) => {
-            var list_store = (Gtk.ListStore) icon_view.model;
-            Gtk.TreeIter i;
-            if (list_store.get_iter (out i, path)) {
-                Item alarm;
-                list_store.get (i, IconView.Column.ITEM, out alarm);
-                if (alarm.state == Item.State.SNOOZING) {
-                    show_ringing_panel (alarm);
-                } else {
-                    edit (alarm);
-                }
-            }
-        });
-
         var builder = Utils.load_ui ("alarm.ui");
         var empty_view = builder.get_object ("empty_panel") as Gtk.Widget;
         content_view = new ContentView (empty_view, icon_view, toolbar);
         add (content_view);
 
+        content_view.item_activated.connect ((item) => {
+            Item alarm = (Item) item;
+            if (alarm.state == Item.State.SNOOZING) {
+                show_ringing_panel (alarm);
+            } else {
+                edit (alarm);
+            }
+        });
+
         content_view.delete_selected.connect (() => {
-            // FIXME: this is not efficient, but we have few itesm and
-            // we are probably going to drop the TreeModel soon
-            var list_store = (Gtk.ListStore) icon_view.model;
-            foreach (Gtk.TreePath path in icon_view.get_selected_items ()) {
-                Gtk.TreeIter i;
-                if (list_store.get_iter (out i, path)) {
-                    Item location;
-                    list_store.get (i, IconView.Column.ITEM, out location);
-                    alarms.remove (location);
-                }
+            foreach (Object i in content_view.get_selected_items ()) {
+                alarms.remove ((Item) i);
             }
             icon_view.remove_selected ();
             save ();
diff --git a/src/widgets.vala b/src/widgets.vala
index 5df7297..89a764d 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -301,8 +301,8 @@ public class IconView : Gtk.IconView {
 
     // Redefine selection handling methods since we handle selection manually
 
-    public new List<Gtk.TreePath>? get_selected_items () {
-        List<Gtk.TreePath>? items = null;
+    public new List<Gtk.TreePath> get_selected_items () {
+        var items = new List<Gtk.TreePath> ();
         model.foreach ((model, path, iter) => {
             bool selected;
             ((Gtk.ListStore) model).get (iter, Column.SELECTED, out selected);
@@ -407,9 +407,21 @@ public class ContentView : Gtk.Bin {
             selection_toolbar.set_visible (n_items != 0);
         });
 
+        icon_view.item_activated.connect ((path) => {
+            var store = (Gtk.ListStore) icon_view.model;
+            Gtk.TreeIter i;
+            if (store.get_iter (out i, path)) {
+                Object item;
+                store.get (i, IconView.Column.ITEM, out item);
+                item_activated (item);
+            }
+        });
+
         add (empty_page);
     }
 
+    public signal void item_activated (Object item);
+
     public signal void delete_selected ();
 
     private Gtk.Toolbar create_selection_toolbar () {
@@ -463,6 +475,26 @@ public class ContentView : Gtk.Bin {
         show_all ();
     }
 
+    // Note: this is not efficient: we first walk the model to collect
+    // a list then the caller has to walk this list and then it has to
+    // delete the items from the view, which walks the model again...
+    // Our models are small enough that it does not matter and hopefully
+    // we will get rid of GtkListStore/GtkIconView soon.
+    public List<Object>? get_selected_items () {
+        var items = new List<Object> ();
+        var store = (Gtk.ListStore) icon_view.model;
+        foreach (Gtk.TreePath path in icon_view.get_selected_items ()) {
+            Gtk.TreeIter i;
+            if (store.get_iter (out i, path)) {
+                Object item;
+                store.get (i, IconView.Column.ITEM, out item);
+                items.prepend (item);
+            }
+        }
+        items.reverse ();
+        return (owned) items;
+    }
+
     public void update_toolbar () {
         switch (main_toolbar.mode) {
         case Toolbar.Mode.SELECTION:
diff --git a/src/world.vala b/src/world.vala
index 2f0bbfb..4216c0b 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -254,34 +254,21 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
             }
         });
 
-        icon_view.item_activated.connect ((path) => {
-            var list_store = (Gtk.ListStore) icon_view.model;
-            Gtk.TreeIter i;
-            if (list_store.get_iter (out i, path)) {
-                Item location;
-                list_store.get (i, IconView.Column.ITEM, out location);
-                standalone.location = location;
-                standalone.update ();
-                visible_child = standalone;
-            }
-        });
-
         var builder = Utils.load_ui ("world.ui");
         var empty_view = builder.get_object ("empty_panel") as Gtk.Widget;
         content_view = new ContentView (empty_view, icon_view, toolbar);
         add (content_view);
 
+        content_view.item_activated.connect ((item) => {
+            Item location = (Item) item;
+            standalone.location = location;
+            standalone.update ();
+            visible_child = standalone;
+        });
+
         content_view.delete_selected.connect (() => {
-            // FIXME: this is not efficient, but we have few itesm and
-            // we are probably going to drop the TreeModel soon
-            var list_store = (Gtk.ListStore) icon_view.model;
-            foreach (Gtk.TreePath path in icon_view.get_selected_items ()) {
-                Gtk.TreeIter i;
-                if (list_store.get_iter (out i, path)) {
-                    Item location;
-                    list_store.get (i, IconView.Column.ITEM, out location);
-                    locations.remove (location);
-                }
+            foreach (Object i in content_view.get_selected_items ()) {
+                locations.remove ((Item) i);
             }
             icon_view.remove_selected ();
             save ();


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