Hide the route column when the lift list window is opened in portrait mode
[beifahrer] / src / lift-list-window.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Beifahrer is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 public class LiftListWindow : StackableWindow {
23         AdacMitfahrclub adac;
24         ListStore store;
25         TreeView tree;
26         TreeViewColumn route_column;
27         Label no_lifts;
28
29         public LiftListWindow (AdacMitfahrclub _adac) {
30                 adac = _adac;
31         }
32
33         construct {
34                 set_title ("Beifahrer");
35
36                 store = new ListStore (6, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (Lift));
37
38                 tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
39                 Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
40
41                 tree.set_headers_visible (false);
42                 tree.set_rules_hint (true);
43
44                 // Tree selection object
45                 var selection = tree.get_selection ();
46                 selection.set_mode (SelectionMode.SINGLE);
47
48                 // Source and destination column
49                 route_column = new TreeViewColumn.with_attributes ("Route", new CellRendererText (), "text", 0);
50                 route_column.set_reorderable (false);
51                 route_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
52                 route_column.set_expand (true);
53                 tree.append_column (route_column);
54
55                 // Date and time column
56                 var datetime_column = new TreeViewColumn.with_attributes ("DateTime", new CellRendererText (), "text", 1);
57                 datetime_column.set_reorderable (false);
58                 datetime_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
59                 datetime_column.set_expand (true);
60                 tree.append_column (datetime_column);
61
62                 // Free places column
63                 var places_column = new TreeViewColumn.with_attributes ("Places", new CellRendererText (), "text", 2);
64                 places_column.set_reorderable (false);
65                 places_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
66                 places_column.set_expand (true);
67                 tree.append_column (places_column);
68
69                 // Price column
70                 var price_column = new TreeViewColumn.with_attributes ("Price", new CellRendererText (), "text", 3);
71                 price_column.set_reorderable (false);
72                 price_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
73                 price_column.set_expand (true);
74                 tree.append_column (price_column);
75
76                 // Smoker/Non-smoker column
77                 var smoke_column = new TreeViewColumn.with_attributes ("Smoker", new CellRendererPixbuf (), "icon-name", 4);
78                 smoke_column.set_reorderable (false);
79                 smoke_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
80                 smoke_column.set_expand (false);
81                 tree.append_column (smoke_column);
82
83                 var pannable = new PannableArea ();
84                 pannable.add (tree);
85
86                 no_lifts = new Label (_("No lifts"));
87                 Hildon.helper_set_logical_font (no_lifts, "LargeSystemFont");
88                 Hildon.helper_set_logical_color (no_lifts, RcFlags.FG, StateType.NORMAL, "SecondaryTextColor");
89                 no_lifts.set_size_request (-1, 6 * 70);
90                 no_lifts.set_alignment ((float) 0.5, (float) 0.42);
91
92                 var vbox = new VBox (false, 0);
93                 vbox.pack_start (pannable, true, true, 0);
94                 vbox.pack_start (no_lifts, false, false, 0);
95
96                 add (vbox);
97
98                 vbox.show_all ();
99                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
100
101                 tree.row_activated.connect (this.on_row_activated);
102                 BeifahrerProgram.orientation.changed.connect (this.on_orientation_changed);
103         }
104
105         public void find_lifts (string city_from, string city_to, Date date, int tolerance = 0) {
106                 set_title ("%s - %s".printf (city_from, city_to));
107
108                 var lift_list = adac.get_lift_list (city_from, city_to, date, tolerance);
109                 foreach (Lift lift in lift_list) {
110                         TreeIter iter;
111                         string icon_name = null;
112                         if (LiftFlags.SMOKER in lift.flags)
113                                 icon_name = "beifahrer_smoker";
114                         else if (LiftFlags.NON_SMOKER in lift.flags)
115                                 icon_name = "beifahrer_non_smoker";
116                         store.insert_with_values (out iter, -1, 0, lift.city_from + " - " + lift.city_to,
117                                                                 1, (lift.time != null) ? (lift.date + ", " + lift.time) : lift.date,
118                                                                 2, _("%d pl.").printf (lift.places),
119                                                                 3, lift.price,
120                                                                 4, icon_name,
121                                                                 5, lift);
122                         // (LiftFlags.WOMEN_ONLY,ADAC_MEMBER in lift.flags)
123                 }
124                 if (lift_list.length () > 0)
125                         no_lifts.hide ();
126         }
127
128         private void on_row_activated (TreeView tree, TreePath /*_*/path, TreeViewColumn column) {
129                 TreeModel model = tree.model;
130                 TreeIter iter;
131                 Lift lift;
132
133                 if (model.get_iter (out iter, path)) {
134                         model.get (iter, 5, out lift);
135
136                         if (lift.description == null)
137                                 Hildon.gtk_window_set_progress_indicator (this, 1);
138                         var window = new LiftDetailWindow (adac, lift);
139                         window.show_all ();
140
141                         // TODO - this should be async
142                         if (lift.description == null) {
143                                 adac.update_lift_details (lift);
144                                 window.update_lift_details ();
145                                 Hildon.gtk_window_set_progress_indicator (this, 0);
146                         }
147                 }
148         }
149
150         private void on_orientation_changed () {
151                 route_column.set_visible (!BeifahrerProgram.orientation.portrait);
152         }
153 }