UI: Added about, search, results
[maevies] / ui / maeviesui / maeviesui / gui.py
1 # -*- coding: utf-8 -*-
2
3 ###########################################################################
4 #    Maevies
5 #    Copyright (C) 2010 Simón Pena <spenap@gmail.com>
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 ###########################################################################
20
21 import hildon
22 import pygtk
23 pygtk.require("2.0")
24 import gtk
25
26 class Maevies(hildon.StackableWindow):
27
28     ACTION_SEARCH = 0
29     ACTION_ABOUT = 1
30     ACTION_THEATERS = 2
31     ACTION_FAVORITES = 3
32
33     def __init__(self):
34         super(Maevies, self).__init__()
35         self.set_title("Maevies - 0.1")
36         self.connect('delete-event',
37                      lambda widget, event: gtk.main_quit())
38
39         self.add(self._create_contents())
40         self.set_app_menu(self._create_app_menu())
41
42         self.show_all()
43
44     def _create_box(self, title, action):
45         box = gtk.VBox()
46         box.set_border_width(20)
47
48         button = gtk.Button()
49         button.connect("clicked", self._button_clicked, action)
50         foot_label = gtk.Label()
51         foot_label.set_text(title)
52
53         box.pack_start(button,
54                              expand = True, fill = True)
55         box.pack_start(foot_label,
56                              expand = False, fill = False)
57
58         return box
59
60     def _create_contents(self):
61         contents = gtk.HBox()
62         contents.set_border_width(60)
63         contents.set_homogeneous(True)
64         contents.pack_start(self._create_box(title = "On Theaters",
65                                              action = self.ACTION_THEATERS),
66                             expand = True, fill = True)
67         contents.pack_start(self._create_box(title = "Favorites",
68                                              action = self.ACTION_FAVORITES),
69                             expand = True, fill = True)
70         contents.pack_start(self._create_box(title = "Search",
71                                              action = self.ACTION_SEARCH),
72                             expand = True, fill = True)
73
74         return contents;
75
76     def _button_clicked(self, button, action):
77         if action == self.ACTION_SEARCH:
78             search_dialog = SearchDialog(self)
79             if search_dialog.run() == gtk.RESPONSE_ACCEPT:
80                 ResultsWindow(search_term = search_dialog.get_search_term(),
81                               search_category = search_dialog.get_search_category())
82             search_dialog.destroy()
83         elif action == self.ACTION_ABOUT:
84             about_dialog = AboutDialog(self)
85             about_dialog.run()
86             about_dialog.destroy()
87
88     def _create_app_menu(self):
89         menu = hildon.AppMenu()
90
91         about = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
92         about.set_label("About")
93         about.connect("clicked", self._button_clicked, self.ACTION_ABOUT)
94
95         menu.append(about)
96
97         menu.show_all()
98
99         return menu
100
101     def run(self):
102         gtk.main()
103
104 class SearchDialog(gtk.Dialog):
105
106     fields = [
107            "Movies",
108            "People",
109            ]
110
111     def __init__(self, parent):
112         super(SearchDialog, self).__init__(parent = parent,
113                                             flags = gtk.DIALOG_DESTROY_WITH_PARENT)
114         self.set_title("Enter search terms")
115
116         self.vbox.pack_start(self._create_contents(), True, False, 0)
117         self.add_button(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)
118
119         self.show_all()
120
121     def _create_contents(self):
122         self._search_entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
123         search_button = self._create_picker_button()
124
125         search_contents = gtk.VBox(False, 0)
126
127         search_contents.pack_start(self._search_entry, True, True, 0)
128         search_contents.pack_start(search_button, True, True, 0)
129
130         return search_contents
131
132     def _create_picker_button(self):
133         self._picker_button = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT,
134                                             hildon.BUTTON_ARRANGEMENT_VERTICAL)
135         self._picker_button.set_title("Search for")
136
137         selector = hildon.TouchSelector(text = True)
138         selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
139
140         for field in self.fields:
141             selector.append_text(field)
142
143         self._picker_button.set_selector(selector)
144         self._picker_button.set_active(0)
145
146         return self._picker_button
147
148     def get_search_term(self):
149         return self._search_entry.get_text()
150
151     def get_search_category(self):
152         return self.fields[self._picker_button.get_active()]
153
154 class ResultsWindow(hildon.StackableWindow):
155
156     TIMEOUT_TIME = 2000
157
158     def __init__(self, search_term, search_category):
159         super(ResultsWindow, self).__init__()
160         self.set_title("Search results")
161
162         self.search_term = search_term
163         self.search_category = search_category
164
165         self._show_banner()
166
167         self.show_all()
168
169     def _show_banner(self):
170         message = "Searching <i>%(category)s</i> for <i>%(term)s</i>" % {'category': self.search_category,
171                                                                          'term' : self.search_term}
172         banner = hildon.hildon_banner_show_information_with_markup(self,
173                                                                    "ignored",
174                                                                    message)
175         banner.set_timeout(self.TIMEOUT_TIME)
176         pass
177
178 if __name__ == "__main__":
179     maevies = Maevies()
180     maevies.run()
181
182 class AboutDialog(gtk.Dialog):
183
184     def __init__(self, parent):
185         super(AboutDialog, self).__init__(parent = parent,
186                                           flags = gtk.DIALOG_DESTROY_WITH_PARENT)
187         self.set_title("About Maevies")
188         self.show_all()