[gnome-clocks/wip/vala] More work on the AlarmDialog port (in progress; to be squashed)



commit ca5e36cdf94a41220ba891a210e62ec55b07dee7
Author: Volker Sobek <reklov live com>
Date:   Tue Feb 12 13:53:03 2013 +0100

    More work on the AlarmDialog port (in progress; to be squashed)

 src/alarm.ui   |    2 +-
 src/alarm.vala |   92 +++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 76 insertions(+), 18 deletions(-)
---
diff --git a/src/alarm.ui b/src/alarm.ui
index 9b8c4df..a11c7b1 100644
--- a/src/alarm.ui
+++ b/src/alarm.ui
@@ -152,7 +152,7 @@
       </packing>
     </child>
     <child>
-      <object class="GtkBox" id="time_entry">
+      <object class="GtkBox" id="time_box">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="spacing">6</property>
diff --git a/src/alarm.vala b/src/alarm.vala
index 163c362..6da2609 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -202,6 +202,8 @@ private class SetupDialog : Gtk.Dialog {
     private Gtk.SpinButton h_spinbutton;
     private Gtk.SpinButton m_spinbutton;
     private AmPmToggleButton am_pm_button;
+    private Gtk.Entry name_entry;
+    private Utils.WallClock.Format format;
 
     public SetupDialog (Gtk.Window parent, Item? alarm) {
         Object (transient_for: parent, modal: true, title: _("New Alarm"));
@@ -210,32 +212,89 @@ private class SetupDialog : Gtk.Dialog {
         set_default_response (1);
 
         var builder = Utils.load_ui ("alarm.ui");
-
         var grid = builder.get_object ("setup_dialog_content") as Gtk.Grid;
-        get_content_area ().add (grid);
+        var time_box = builder.get_object ("time_box") as Gtk.Box;
+        var wc = Utils.WallClock.get_default ();
+
         h_spinbutton = builder.get_object ("h_spinbutton") as Gtk.SpinButton;
         m_spinbutton = builder.get_object ("m_spinbutton") as Gtk.SpinButton;
+        h_spinbutton.output.connect (show_leading_zeros);
+        m_spinbutton.output.connect (show_leading_zeros);
+        name_entry = builder.get_object ("name_entry") as Gtk.Entry;
         am_pm_button = new AmPmToggleButton ();
-        var time_entry = builder.get_object ("time_entry") as Gtk.Box;
+        format = wc.format;
 
-        var wc = Utils.WallClock.get_default ();
-        if (wc.format == Utils.WallClock.Format.TWENTYFOUR)
+        if (format == Utils.WallClock.Format.TWENTYFOUR)
+            // 12h format
             h_spinbutton.set_range (0, 23);
         else {
+            // 24h format
             h_spinbutton.set_range (1, 12);
+            time_box.pack_end (am_pm_button);
         }
 
-        h_spinbutton.output.connect (show_leading_zeros);
-        m_spinbutton.output.connect (show_leading_zeros);
-        time_entry.pack_end (am_pm_button);
+        get_content_area ().add (grid);
+        set_alarm_item (alarm);
+
+    }
+
+    public void set_alarm_item (Item? alarm) {
+        string name;
+        bool active;
+        int hour;
+        int minute;
+        int[] days;
+        var wc = Utils.WallClock.get_default ();
+
+        if (alarm == null) {
+            name = _("New Alarm");
+            hour = wc.date_time.get_hour();
+            minute = wc.date_time.get_minute();
+            days = null;
+        } else {
+            name = alarm.name;
+            hour = alarm.hour;
+            minute = alarm.minute;
+            days = alarm.days;
+        }
+
+        if (format == Utils.WallClock.Format.TWELVE) {
+            if (hour < 12) {
+                am_pm_button.choice = AmPmToggleButton.AmPm.AM;
+            } else {
+                am_pm_button.choice = AmPmToggleButton.AmPm.PM;
+                hour -= 12;
+            }
+
+            if (hour == 0) {
+                hour = 12;
+            }
+        }
+
+        h_spinbutton.set_value(hour);
+        m_spinbutton.set_value(minute);
+        name_entry.set_text (name != null ? name : "");
+
+        // TODO: set day buttons
     }
 
-    // TODO: use the spin buttons
-    // public Item get_alarm_item () {
-    //     var h = h_entry.get_text ();
-    //     var m = h_entry.get_text ();
-    //     return new Item ("Alarm", true, int.parse (h), int.parse (m), null);
-    // }
+    public Item get_alarm_item () {
+        var name = name_entry.get_text();
+        var hour = h_spinbutton.get_value_as_int ();
+        var minute = m_spinbutton.get_value_as_int ();
+        if (format == Utils.WallClock.Format.TWELVE) {
+            var choice = am_pm_button.choice;
+            if (choice == AmPmToggleButton.AmPm.AM && hour == 12) {
+                hour = 0;
+            } else if (choice == AmPmToggleButton.AmPm.PM && hour != 12) {
+                hour += 12;
+            }
+        }
+
+        // TODO: get days from day buttons
+
+        return new Item (name, true, hour, minute, null);
+    }
 
     private bool show_leading_zeros (Gtk.SpinButton spin_button) {
         spin_button.set_text ("%02i".printf (spin_button.get_value_as_int ()));
@@ -349,9 +408,8 @@ public class MainPanel : Gd.Stack, Clocks.Clock {
         var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), null);
         dialog.response.connect ((dialog, response) => {
             if (response == 1) {
-                // TODO: uncomment when implemented
-                //var alarm = ((SetupDialog) dialog).get_alarm_item ();
-                //add_alarm (alarm);
+                var alarm = ((SetupDialog) dialog).get_alarm_item ();
+                add_alarm (alarm);
             }
             dialog.destroy ();
         });


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