Switching over to the nav box plus a bug fix
[watersofshiloah] / src / playcontrol.py
1 import logging
2
3 import gobject
4 import gtk
5
6 import gtk_toolbox
7 import hildonize
8 import util.misc as misc_utils
9
10 import presenter
11
12
13 _moduleLogger = logging.getLogger(__name__)
14
15
16 class NavControl(gobject.GObject):
17
18
19         __gsignals__ = {
20                 'home' : (
21                         gobject.SIGNAL_RUN_LAST,
22                         gobject.TYPE_NONE,
23                         (),
24                 ),
25                 'jump-to' : (
26                         gobject.SIGNAL_RUN_LAST,
27                         gobject.TYPE_NONE,
28                         (gobject.TYPE_PYOBJECT, ),
29                 ),
30         }
31
32         def __init__(self, player, store):
33                 gobject.GObject.__init__(self)
34
35                 self._store = store
36
37                 self._player = player
38                 self._player.connect("state-change", self._on_player_state_change)
39                 self._player.connect("title-change", self._on_player_title_change)
40
41                 self._controlButton = store.get_image_from_store(store.STORE_LOOKUP["small_play"])
42
43                 self._controlBox = presenter.NavigationBox()
44                 self._controlBox.toplevel.add(self._controlButton)
45                 self._controlBox.connect("action", self._on_nav_action)
46                 self._controlBox.connect("navigating", self._on_navigating)
47
48                 self._titleButton = gtk.Label("")
49
50                 self._displayBox = presenter.NavigationBox()
51                 self._displayBox.toplevel.add(self._titleButton)
52                 self._displayBox.connect("action", self._on_nav_action)
53                 self._displayBox.connect("navigating", self._on_navigating)
54
55                 self._layout = gtk.HBox()
56                 self._layout.pack_start(self._controlBox.toplevel, False, False)
57                 self._layout.pack_start(self._displayBox.toplevel, True, True)
58
59         def refresh(self):
60                 if not self._player.title:
61                         self.toplevel.hide()
62
63         @property
64         def toplevel(self):
65                 return self._layout
66
67         def set_orientation(self, orientation):
68                 pass
69
70         @misc_utils.log_exception(_moduleLogger)
71         def _on_player_state_change(self, player, newState):
72                 if self._controlBox.is_active() or self._displayBox.is_active():
73                         return
74
75                 if newState == "play":
76                         stateImage = self._store.STORE_LOOKUP["small_play"]
77                         self._store.set_image_from_store(self._controlButton, stateImage)
78                         self.toplevel.show()
79                 elif newState == "pause":
80                         stateImage = self._store.STORE_LOOKUP["small_pause"]
81                         self._store.set_image_from_store(self._controlButton, stateImage)
82                         self.toplevel.show()
83                 elif newState == "stop":
84                         self._titleButton.set_label("")
85                         self.toplevel.hide()
86                 else:
87                         _moduleLogger.info("Unhandled player state %s" % newState)
88                         stateImage = self._store.STORE_LOOKUP["small_pause"]
89                         self._store.set_image_from_store(self._controlButton, stateImage)
90
91         @misc_utils.log_exception(_moduleLogger)
92         def _on_player_title_change(self, player, newState):
93                 self._titleButton.set_label(self._player.title)
94
95         @misc_utils.log_exception(_moduleLogger)
96         def _on_navigating(self, widget, navState):
97                 if navState == "down":
98                         imageName = "small_home"
99                 elif navState == "up":
100                         imageName = "small_play"
101                 elif navState == "clicking" or not self._player.can_navigate:
102                         if widget is self._controlBox:
103                                 if self._player.state == "play":
104                                         imageName = "small_pause"
105                                 else:
106                                         imageName = "small_play"
107                         elif widget is self._displayBox:
108                                 if self._player.state == "play":
109                                         imageName = "small_play"
110                                 else:
111                                         imageName = "small_pause"
112                         else:
113                                 raise NotImplementedError()
114                 elif navState == "left":
115                         imageName = "small_next"
116                 elif navState == "right":
117                         imageName = "small_prev"
118
119                 imagePath = self._store.STORE_LOOKUP[imageName]
120                 self._store.set_image_from_store(self._controlButton, imagePath)
121
122         @misc_utils.log_exception(_moduleLogger)
123         def _on_nav_action(self, widget, navState):
124                 if self._player.state == "play":
125                         imageName = "small_play"
126                 else:
127                         imageName = "small_pause"
128                 imagePath = self._store.STORE_LOOKUP[imageName]
129                 self._store.set_image_from_store(self._controlButton, imagePath)
130
131                 if navState == "clicking":
132                         if widget is self._controlBox:
133                                 if self._player.state == "play":
134                                         self._player.pause()
135                                 else:
136                                         self._player.play()
137                         elif widget is self._displayBox:
138                                 self.emit("jump-to", self._player.node)
139                         else:
140                                 raise NotImplementedError()
141                 elif navState == "down":
142                         self.emit("home")
143                 elif navState == "up":
144                         pass
145                 elif navState == "left":
146                         self._player.next()
147                 elif navState == "right":
148                         self._player.back()
149
150
151 gobject.type_register(NavControl)
152
153
154 class PlayControl(object):
155
156         def __init__(self, player, store):
157                 self._isPortrait = True
158
159                 self._store = store
160
161                 self._player = player
162                 self._player.connect("state-change", self._on_player_state_change)
163                 self._player.connect("title-change", self._on_player_nav_change)
164
165                 img = store.get_image_from_store(store.STORE_LOOKUP["small_prev"])
166                 self._back = gtk.Button()
167                 self._back.set_image(img)
168                 self._back.connect("clicked", self._on_back_clicked)
169
170                 img = store.get_image_from_store(store.STORE_LOOKUP["small_stop"])
171                 self._stop = gtk.Button()
172                 self._stop.set_image(img)
173                 self._stop.connect("clicked", self._on_stop_clicked)
174
175                 img = store.get_image_from_store(store.STORE_LOOKUP["small_pause"])
176                 self._pause = gtk.Button()
177                 self._pause.set_image(img)
178                 self._pause.connect("clicked", self._on_pause_clicked)
179
180                 img = store.get_image_from_store(store.STORE_LOOKUP["small_play"])
181                 self._play = gtk.Button()
182                 self._play.set_image(img)
183                 self._play.connect("clicked", self._on_play_clicked)
184
185                 img = store.get_image_from_store(store.STORE_LOOKUP["small_next"])
186                 self._next = gtk.Button()
187                 self._next.set_image(img)
188                 self._next.connect("clicked", self._on_next_clicked)
189
190                 self._controls = gtk.HBox()
191                 self._controls.pack_start(self._back)
192                 self._controls.pack_start(self._stop)
193                 self._controls.pack_start(self._pause)
194                 self._controls.pack_start(self._play)
195                 self._controls.pack_start(self._next)
196
197                 self._layout = gtk.VBox()
198                 self._layout.pack_start(self._controls)
199
200         def refresh(self):
201                 if not self._player.title:
202                         self.toplevel.hide()
203                 self._set_navigate(self._player.can_navigate)
204                 self._set_state(self._player.state)
205
206         @property
207         def toplevel(self):
208                 return self._layout
209
210         def set_orientation(self, orientation):
211                 if orientation == gtk.ORIENTATION_VERTICAL:
212                         if self._isPortrait:
213                                 return
214                         self._isPortrait = True
215
216                         self._controls.remove(self._back)
217                         self._controls.remove(self._stop)
218                         self._controls.remove(self._pause)
219                         self._controls.remove(self._play)
220                         self._controls.remove(self._next)
221                         self._layout.remove(self._controls)
222
223                         self._controls = gtk.HBox()
224                         self._controls.pack_start(self._back)
225                         self._controls.pack_start(self._stop)
226                         self._controls.pack_start(self._pause)
227                         self._controls.pack_start(self._play)
228                         self._controls.pack_start(self._next)
229                         self._layout.pack_start(self._controls)
230                 elif orientation == gtk.ORIENTATION_HORIZONTAL:
231                         if not self._isPortrait:
232                                 return
233                         self._isPortrait = False
234
235                         self._controls.remove(self._back)
236                         self._controls.remove(self._stop)
237                         self._controls.remove(self._pause)
238                         self._controls.remove(self._play)
239                         self._controls.remove(self._next)
240                         self._layout.remove(self._controls)
241
242                         self._controls = gtk.VBox()
243                         self._controls.pack_start(self._back)
244                         self._controls.pack_start(self._stop)
245                         self._controls.pack_start(self._pause)
246                         self._controls.pack_start(self._play)
247                         self._controls.pack_start(self._next)
248                         self._layout.pack_start(self._controls)
249                 else:
250                         raise NotImplementedError(orientation)
251
252         def _set_navigate(self, canNavigate):
253                 if canNavigate:
254                         self._back.show()
255                         self._next.show()
256                 else:
257                         self._back.hide()
258                         self._next.hide()
259
260         def _set_state(self, newState):
261                 if newState == "play":
262                         self._pause.show()
263                         self._play.hide()
264                         self.toplevel.show()
265                 elif newState == "pause":
266                         self._pause.hide()
267                         self._play.show()
268                         self.toplevel.show()
269                 elif newState == "stop":
270                         self._pause.hide()
271                         self._play.show()
272                         self.toplevel.hide()
273
274         @misc_utils.log_exception(_moduleLogger)
275         def _on_player_state_change(self, player, newState):
276                 self._set_state(newState)
277
278         @misc_utils.log_exception(_moduleLogger)
279         def _on_player_nav_change(self, player, newState):
280                 self._set_navigate(player.can_navigate)
281
282         @misc_utils.log_exception(_moduleLogger)
283         def _on_back_clicked(self, *args):
284                 self._player.back()
285
286                 parent = gtk_toolbox.find_parent_window(self._layout)
287                 hildonize.show_information_banner(parent, self._player.title)
288
289         @misc_utils.log_exception(_moduleLogger)
290         def _on_stop_clicked(self, *args):
291                 self._pause.hide()
292                 self._play.show()
293                 self._player.stop()
294
295         @misc_utils.log_exception(_moduleLogger)
296         def _on_pause_clicked(self, *args):
297                 self._pause.show()
298                 self._play.hide()
299                 self._player.pause()
300
301         @misc_utils.log_exception(_moduleLogger)
302         def _on_play_clicked(self, *args):
303                 self._pause.hide()
304                 self._play.show()
305                 self._player.play()
306
307         @misc_utils.log_exception(_moduleLogger)
308         def _on_next_clicked(self, *args):
309                 self._player.next()
310
311                 parent = gtk_toolbox.find_parent_window(self._layout)
312                 hildonize.show_information_banner(parent, self._player.title)