Add CityButton class to replace departure / arrival and vicinity picker buttons
authorPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 28 May 2010 18:43:01 +0000 (20:43 +0200)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Fri, 28 May 2010 20:18:34 +0000 (22:18 +0200)
Makefile.am
src/city-button.vala [new file with mode: 0644]
src/query-window.vala

index d172235..0b76648 100644 (file)
@@ -30,6 +30,7 @@ icon64_DATA = \
 beifahrer_SOURCES = \
        src/beifahrer.c \
        src/adac-mitfahrclub.c \
+       src/city-button.c \
        src/orientation.c \
        src/query-window.c \
        src/lift-list-window.c \
@@ -39,6 +40,7 @@ beifahrer_SOURCES = \
 beifahrer_VALASOURCES = \
        src/beifahrer.vala \
        src/adac-mitfahrclub.vala \
+       src/city-button.vala \
        src/orientation.vala \
        src/query-window.vala \
        src/lift-list-window.vala \
diff --git a/src/city-button.vala b/src/city-button.vala
new file mode 100644 (file)
index 0000000..d1a2a63
--- /dev/null
@@ -0,0 +1,121 @@
+/* This file is part of Beifahrer.
+ *
+ * Copyright (C) 2010 Philipp Zabel
+ *
+ * Beifahrer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Beifahrer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Gtk;
+using Hildon;
+
+class CityButton : Hildon.Button {
+       private enum Response {
+               RADIUS = 1
+       }
+
+       Gtk.Dialog dialog;
+       TouchSelector city_selector;
+       PickerButton radius_button;
+       int active = 0;
+       int radius = 0;
+
+       public CityButton (SizeType size, ButtonArrangement arrangement, List<City> city_list) {
+               GLib.Object (arrangement: arrangement, size: size);
+               set_style (ButtonStyle.PICKER);
+
+               city_selector = new TouchSelectorEntry.text ();
+               foreach (unowned City city in city_list)
+                       city_selector.append_text (city.name);
+
+               clicked.connect (on_clicked);
+       }
+
+       public void set_active (int _active) {
+               active = _active;
+               city_selector.set_active (0, active);
+               update_value ();
+       }
+
+       public int get_active () {
+               return active;
+       }
+
+       public void set_radius (int _radius) {
+               radius = _radius;
+               update_value ();
+       }
+
+       public int get_radius () {
+               return radius;
+       }
+
+       private void on_clicked () {
+               dialog = new Gtk.Dialog ();
+               dialog.set_transient_for (find_parent_window ());
+
+               var content_area = (Box) dialog.get_content_area ();
+               content_area.set_size_request (-1, 5*70);
+               content_area.pack_start (city_selector, true, true, 0);
+
+               var radius_selector = new TouchSelector.text ();
+               for (int km = 0; km <= 50; km += 10)
+                       radius_selector.append_text ("%d km".printf (km));
+
+               radius_button = new PickerButton (SizeType.FINGER_HEIGHT,
+                                                 ButtonArrangement.VERTICAL);
+               radius_button.set_selector (radius_selector);
+               radius_button.set_title (_("Radius"));
+               radius_button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
+               radius_button.set_active (radius / 10);
+               dialog.add_action_widget (radius_button, Response.RADIUS);
+
+               var action_area = (ButtonBox) dialog.get_action_area ();
+               action_area.set_child_secondary (radius_button, true);
+
+               dialog.add_button (_("Done"), Gtk.ResponseType.OK);
+
+               dialog.response.connect (on_response);
+
+               dialog.show_all ();
+       }
+
+       private void on_response (int response_id) {
+               if (response_id == Gtk.ResponseType.OK) {
+                       active = city_selector.get_active (0);
+                       radius = radius_button.get_active () * 10;
+                       update_value ();
+               }
+               if (response_id != Response.RADIUS) {
+                       // Unlink the city_selector so it doesn't get destroyed with the dialog
+                       var content_area = (Box) dialog.get_content_area ();
+                       content_area.remove (city_selector);
+                       dialog.destroy ();
+                       dialog = null;
+               }
+       }
+
+       private void update_value () {
+               if (get_radius () == 0)
+                       set_value (city_selector.get_current_text ());
+               else
+                       set_value ("%s, radius %d km".printf (city_selector.get_current_text (), radius));
+       }
+
+       private Gtk.Window? find_parent_window () {
+               for (Gtk.Widget p = parent; p != null; p = p.parent)
+                       if (p is Gtk.Window)
+                               return (Gtk.Window) p;
+               return null;
+       }
+}
index 1ab7e49..7286df4 100644 (file)
@@ -25,10 +25,8 @@ public class QueryWindow : StackableWindow {
        public const string GCONF_KEY_USE_LOCATION = "/apps/beifahrer/use_location";
 
        AdacMitfahrclub adac;
-       TouchSelector city_from_selector;
-       TouchSelector city_to_selector;
-       TouchSelector umkreis_from_selector;
-       TouchSelector umkreis_to_selector;
+       CityButton departure_button;
+       CityButton arrival_button;
        DateButton date;
        TouchSelector tolerance_selector;
        Location.GPSDControl control;
@@ -49,20 +47,6 @@ public class QueryWindow : StackableWindow {
                adac = new AdacMitfahrclub ();
                gconf = GConf.Client.get_default ();
 
-               city_from_selector = new TouchSelectorEntry.text ();
-               city_to_selector = new TouchSelectorEntry.text ();
-               foreach (unowned City city in adac.get_city_list ()) {
-                       city_from_selector.append_text (city.name);
-                       city_to_selector.append_text (city.name);
-               }
-
-               umkreis_from_selector = new TouchSelector.text ();
-               umkreis_to_selector = new TouchSelector.text ();
-               for (int km = 0; km <= 50; km += 10) {
-                       umkreis_from_selector.append_text ("%d km".printf (km));
-                       umkreis_to_selector.append_text ("%d km".printf (km));
-               }
-
                tolerance_selector = new TouchSelector.text ();
                for (int days = 0; days <= 4; days += 1)
                        tolerance_selector.append_text (_("+/- %d days").printf (days));
@@ -73,59 +57,41 @@ public class QueryWindow : StackableWindow {
                alignment.right_padding = MARGIN_DOUBLE;
                var table = new Table (5, 2, false);
 
-               var button = new PickerButton (SizeType.FINGER_HEIGHT,
-                                              ButtonArrangement.VERTICAL);
-               button.set_selector (city_from_selector);
-               button.set_title (_("Departure"));
-               button.set_value (_("Please select"));
-               button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
-               table.attach (button, 0, 1, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
+               departure_button = new CityButton (SizeType.FINGER_HEIGHT,
+                                                  ButtonArrangement.VERTICAL, adac.get_city_list ());
+               departure_button.set_title (_("Departure"));
+               departure_button.set_value (_("Please select"));
+               departure_button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
+               table.attach (departure_button, 0, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
 
                try {
-                       button.set_active (gconf.get_int ("/apps/beifahrer/departure"));
+                       departure_button.set_active (gconf.get_int ("/apps/beifahrer/departure"));
                } catch (Error e) {
                }
 
-               button = new PickerButton (SizeType.FINGER_HEIGHT,
-                                          ButtonArrangement.VERTICAL);
-               button.set_selector (umkreis_from_selector);
-               button.set_title (_("Vicinity"));
-               button.set_value ("0 km");
-               button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
-               table.attach (button, 1, 2, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
-
-               button = new PickerButton (SizeType.FINGER_HEIGHT,
-                                          ButtonArrangement.VERTICAL);
-               button.set_selector (city_to_selector);
-               button.set_title (_("Arrival"));
-               button.set_value (_("Please select"));
-               button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
-               table.attach (button, 0, 1, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
+               arrival_button = new CityButton (SizeType.FINGER_HEIGHT,
+                                                ButtonArrangement.VERTICAL, adac.get_city_list ());
+               arrival_button.set_title (_("Arrival"));
+               arrival_button.set_value (_("Please select"));
+               arrival_button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
+               table.attach (arrival_button, 0, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
 
                try {
-                       button.set_active (gconf.get_int ("/apps/beifahrer/arrival"));
+                       arrival_button.set_active (gconf.get_int ("/apps/beifahrer/arrival"));
                } catch (Error e) {
                }
 
-               button = new PickerButton (SizeType.FINGER_HEIGHT,
-                                          ButtonArrangement.VERTICAL);
-               button.set_selector (umkreis_to_selector);
-               button.set_title (_("Vicinity"));
-               button.set_value ("0 km");
-               button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
-               table.attach (button, 1, 2, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
-
                var switch_button = new Gtk.Button.with_label (_("Switch departure and arrival"));
                Hildon.gtk_widget_set_theme_size (switch_button, SizeType.FINGER_HEIGHT);
                switch_button.set_alignment (0.0f, 0.5f);
-               table.attach (switch_button, 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
+               table.attach (switch_button, 0, 2, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
 
                date = new DateButton (SizeType.FINGER_HEIGHT,
                                       ButtonArrangement.VERTICAL);
                date.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
                table.attach (date, 0, 1, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
 
-               button = new PickerButton (SizeType.FINGER_HEIGHT,
+               var button = new PickerButton (SizeType.FINGER_HEIGHT,
                                           ButtonArrangement.VERTICAL);
                button.set_selector (tolerance_selector);
                button.set_title (_("Tolerance"));
@@ -199,17 +165,17 @@ public class QueryWindow : StackableWindow {
                        unowned City city = adac.find_nearest_city (device.fix.latitude, device.fix.longitude);
                        if (city != null) {
                                int n = adac.get_city_list ().index (city);
-                               int n_from = city_from_selector.get_active (0);
-                               int n_to = city_to_selector.get_active (0);
+                               int n_from = departure_button.get_active ();
+                               int n_to = arrival_button.get_active ();
 
                                print ("city(%d): %s (%d)\n", n, city.name, city.number);
 
                                if (n != n_from) {
-                                       city_from_selector.set_active (0, n);
+                                       departure_button.set_active (n);
 
                                        // If we are at the previous point of arrival, prepare for return trip
                                        if (n == n_to)
-                                               city_to_selector.set_active (0, n_from);
+                                               arrival_button.set_active (n_from);
                                }
                        }
 
@@ -221,7 +187,7 @@ public class QueryWindow : StackableWindow {
        void on_search_button_clicked (Gtk.Button button) {
                int n;
 
-               n = city_from_selector.get_active (0);
+               n = departure_button.get_active ();
                if (n == -1)
                        return;
                string city_from = adac.get_city_list ().nth_data (n).name;
@@ -231,7 +197,7 @@ public class QueryWindow : StackableWindow {
                } catch (Error e) {
                }
 
-               n = city_to_selector.get_active (0);
+               n = arrival_button.get_active ();
                if (n == -1)
                        return;
                string city_to = adac.get_city_list ().nth_data (n).name;
@@ -257,12 +223,12 @@ public class QueryWindow : StackableWindow {
 
        // Switch departure and arrival
        void on_switch_button_clicked () {
-               int n = city_from_selector.get_active (0);
-               city_from_selector.set_active (0, city_to_selector.get_active (0));
-               city_to_selector.set_active (0, n);
-               n = umkreis_from_selector.get_active (0);
-               umkreis_from_selector.set_active (0, umkreis_to_selector.get_active (0));
-               umkreis_to_selector.set_active (0, n);
+               int n = departure_button.get_active ();
+               departure_button.set_active (arrival_button.get_active ());
+               arrival_button.set_active (n);
+               n = departure_button.get_radius ();
+               departure_button.set_radius (arrival_button.get_radius ());
+               arrival_button.set_radius (n);
        }
 
        void on_settings_clicked () {