[gnome-clocks/wip/vala] Cleanup



commit 643c7e56f97e36fc44b01e702a41c3596dd2d5fc
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Feb 18 21:42:40 2013 +0100

    Cleanup

 src/alarm.vala   |   30 ++++++++----------------------
 src/widgets.vala |   36 +++++++++++++++++++++++++-----------
 src/world.vala   |   30 ++++++++----------------------
 3 files changed, 41 insertions(+), 55 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index 483f992..bd619b9 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -412,19 +412,11 @@ private class StandalonePanel : Gtk.EventBox {
 }
 
 public class MainPanel : Gd.Stack, Clocks.Clock {
-    private enum Column {
-        SELECTED,
-        NAME,
-        ITEM,
-        COLUMNS
-    }
-
     public string label { get; construct set; }
     public Toolbar toolbar { get; construct set; }
 
     private List<Item> alarms;
     private GLib.Settings settings;
-    private Gtk.ListStore list_store;
     private IconView icon_view;
     private ContentView content_view;
     private StandalonePanel standalone;
@@ -435,10 +427,9 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         alarms = new List<Item> ();
         settings = new GLib.Settings("org.gnome.clocks");
 
-        list_store = new Gtk.ListStore (Column.COLUMNS, typeof (bool), typeof (string), typeof (Object));
-        icon_view = new IconView (list_store, Column.SELECTED, Column.NAME, (column, cell, model, iter) => {
+        icon_view = new IconView ((column, cell, model, iter) => {
             Item alarm;
-            model.get (iter, Column.ITEM, out alarm);
+            model.get (iter, IconView.Column.ITEM, out alarm);
             var renderer = (DigitalClockRenderer) cell;
             if (alarm.state == Item.State.SNOOZING) {
                 renderer.text = alarm.snooze_time_label;
@@ -452,10 +443,11 @@ 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, Column.ITEM, out alarm);
+                list_store.get (i, IconView.Column.ITEM, out alarm);
                 var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), alarm);
                 dialog.response.connect ((dialog, response) => {
                     if (response == 1) {
@@ -477,11 +469,12 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         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, Column.ITEM, out location);
+                    list_store.get (i, IconView.Column.ITEM, out location);
                     alarms.remove (location);
                 }
             }
@@ -538,7 +531,7 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         foreach (var a in settings.get_value ("alarms")) {
             Item alarm = Item.deserialize (a);
             alarms.prepend (alarm);
-            add_alarm_to_store (alarm);
+            icon_view.add_item (alarm.name, alarm);
         }
         alarms.reverse ();
     }
@@ -551,13 +544,6 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         settings.set_value ("alarms", builder.end ());
     }
 
-    private void add_alarm_to_store (Item alarm) {
-        var label = "<b>%s</b>".printf (GLib.Markup.escape_text (alarm.name));
-        Gtk.TreeIter i;
-        list_store.append (out i);
-        list_store.set (i, Column.SELECTED, false, Column.NAME, label, Column.ITEM, alarm);
-    }
-
     public void activate_new () {
         var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), null);
         dialog.response.connect ((dialog, response) => {
@@ -565,7 +551,7 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
                 var alarm = new Item ();
                 ((SetupDialog) dialog).apply_to_alarm (alarm);
                 alarms.append (alarm);
-                add_alarm_to_store (alarm);
+                icon_view.add_item (alarm.name, alarm);
                 alarm.reset();
                 save ();
             }
diff --git a/src/widgets.vala b/src/widgets.vala
index adf4510..2bec91d 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -218,6 +218,13 @@ public class IconView : Gtk.IconView {
         SELECTION
     }
 
+    public enum Column {
+        SELECTED,
+        LABEL,
+        ITEM,
+        COLUMNS
+    }
+
     public Mode mode {
         get {
             return _mode;
@@ -238,13 +245,12 @@ public class IconView : Gtk.IconView {
     }
 
     private Mode _mode;
-    private int selection_col;
     private DigitalClockRenderer thumb_renderer;
 
-    public IconView (Gtk.TreeModel model, int selection_column, int text_column, owned 
Gtk.CellLayoutDataFunc thumb_data_func) {
-        Object (model: model, selection_mode: Gtk.SelectionMode.NONE, mode: Mode.NORMAL);
+    public IconView (owned Gtk.CellLayoutDataFunc thumb_data_func) {
+        Object (selection_mode: Gtk.SelectionMode.NONE, mode: Mode.NORMAL);
 
-        selection_col = selection_column;
+        model = new Gtk.ListStore (Column.COLUMNS, typeof (bool), typeof (string), typeof (Object));
 
         get_style_context ().add_class ("content-view");
         set_column_spacing (20);
@@ -254,7 +260,7 @@ public class IconView : Gtk.IconView {
         thumb_renderer.set_alignment (0.5f, 0.5f);
         thumb_renderer.set_fixed_size (160, 160);
         pack_start (thumb_renderer, false);
-        add_attribute (thumb_renderer, "active", selection_col);
+        add_attribute (thumb_renderer, "active", Column.SELECTED);
         set_cell_data_func (thumb_renderer, (owned) thumb_data_func);
 
         var text_renderer = new Gtk.CellRendererText ();
@@ -263,7 +269,7 @@ public class IconView : Gtk.IconView {
         text_renderer.wrap_width = 140;
         text_renderer.wrap_mode = Pango.WrapMode.WORD_CHAR;
         pack_start (text_renderer, true);
-        add_attribute (text_renderer, "markup", text_column);
+        add_attribute (text_renderer, "markup", Column.LABEL);
     }
 
     public override bool button_press_event (Gdk.EventButton event) {
@@ -274,8 +280,8 @@ public class IconView : Gtk.IconView {
                 Gtk.TreeIter i;
                 if (store.get_iter (out i, path)) {
                     bool selected;
-                    store.get (i, selection_col, out selected);
-                    store.set (i, selection_col, !selected);
+                    store.get (i, Column.SELECTED, out selected);
+                    store.set (i, Column.SELECTED, !selected);
                     selection_changed ();
                 }
             } else {
@@ -286,13 +292,21 @@ public class IconView : Gtk.IconView {
         return false;
     }
 
+    public void add_item (string name, Object item) {
+        var store = (Gtk.ListStore) model;
+        var label = GLib.Markup.escape_text (name);
+        Gtk.TreeIter i;
+        store.append (out i);
+        store.set (i, Column.SELECTED, false, Column.LABEL, label, Column.ITEM, item);
+    }
+
     // Redefine selection handling methods since we handle selection manually
 
     public new List<Gtk.TreePath>? get_selected_items () {
         List<Gtk.TreePath>? items = null;
         model.foreach ((model, path, iter) => {
             bool selected;
-            ((Gtk.ListStore) model).get (iter, selection_col, out selected);
+            ((Gtk.ListStore) model).get (iter, Column.SELECTED, out selected);
             if (selected) {
                 items.prepend (path);
             }
@@ -305,7 +319,7 @@ public class IconView : Gtk.IconView {
     public new void select_all () {
         var model = get_model () as Gtk.ListStore;
         model.foreach ((model, path, iter) => {
-            ((Gtk.ListStore) model).set (iter, selection_col, true);
+            ((Gtk.ListStore) model).set (iter, Column.SELECTED, true);
             return false;
         });
         selection_changed ();
@@ -314,7 +328,7 @@ public class IconView : Gtk.IconView {
     public new void unselect_all () {
         var model = get_model () as Gtk.ListStore;
         model.foreach ((model, path, iter) => {
-            ((Gtk.ListStore) model).set (iter, selection_col, false);
+            ((Gtk.ListStore) model).set (iter, Column.SELECTED, false);
             return false;
         });
         selection_changed ();
diff --git a/src/world.vala b/src/world.vala
index e489b99..0e0f7ae 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -219,13 +219,6 @@ private class StandalonePanel : Gtk.EventBox {
 }
 
 public class MainPanel : Gd.Stack, Clocks.Clock {
-    private enum Column {
-        SELECTED,
-        NAME,
-        ITEM,
-        COLUMNS
-    }
-
     public string label { get; construct set; }
     public Toolbar toolbar { get; construct set; }
 
@@ -233,7 +226,6 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
     private GLib.Settings settings;
     private Gdk.Pixbuf? day_pixbuf;
     private Gdk.Pixbuf? night_pixbuf;
-    private Gtk.ListStore list_store;
     private IconView icon_view;
     private ContentView content_view;
     private StandalonePanel standalone;
@@ -247,10 +239,9 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         day_pixbuf = Utils.load_image ("day.png");
         night_pixbuf = Utils.load_image ("night.png");
 
-        list_store = new Gtk.ListStore (Column.COLUMNS, typeof (bool), typeof (string), typeof (Object));
-        icon_view = new IconView (list_store, Column.SELECTED, Column.NAME, (column, cell, model, iter) => {
+        icon_view = new IconView ((column, cell, model, iter) => {
             Item location;
-            model.get (iter, Column.ITEM, out location);
+            model.get (iter, IconView.Column.ITEM, out location);
             var renderer = (DigitalClockRenderer) cell;
             renderer.text = location.time_label;
             renderer.subtext = location.day_label;
@@ -264,10 +255,11 @@ 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, Column.ITEM, out location);
+                list_store.get (i, IconView.Column.ITEM, out location);
                 standalone.location = location;
                 standalone.update ();
                 visible_child = standalone;
@@ -282,11 +274,12 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         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, Column.ITEM, out location);
+                    list_store.get (i, IconView.Column.ITEM, out location);
                     locations.remove (location);
                 }
             }
@@ -319,7 +312,7 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         foreach (var l in settings.get_value ("world-clocks")) {
             Item location = Item.deserialize (l);
             locations.prepend (location);
-            add_location_to_store (location);
+            icon_view.add_item (location.name, location);
         }
         locations.reverse ();
     }
@@ -332,13 +325,6 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         settings.set_value ("world-clocks", builder.end ());
     }
 
-    private void add_location_to_store (Item location) {
-        var label = "<b>%s</b>".printf (GLib.Markup.escape_text (location.name));
-        Gtk.TreeIter i;
-        list_store.append (out i);
-        list_store.set (i, Column.SELECTED, false, Column.NAME, label, Column.ITEM, location);
-    }
-
     public void activate_new () {
         var dialog = new LocationDialog ((Gtk.Window) get_toplevel ());
 
@@ -346,7 +332,7 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
             if (response == 1) {
                 var location = ((LocationDialog) dialog).get_location ();
                 locations.append (location);
-                add_location_to_store (location);
+                icon_view.add_item (location.name, location);
                 save ();
             }
             dialog.destroy ();


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