Propogating fullscreen
[watersofshiloah] / src / presenter.py
1 import logging
2
3 import gobject
4 import pango
5 import cairo
6 import gtk
7
8 import util.misc as misc_utils
9
10
11 _moduleLogger = logging.getLogger(__name__)
12
13
14 class NavigationBox(gobject.GObject):
15
16         __gsignals__ = {
17                 'action' : (
18                         gobject.SIGNAL_RUN_LAST,
19                         gobject.TYPE_NONE,
20                         (gobject.TYPE_STRING, ),
21                 ),
22                 'navigating' : (
23                         gobject.SIGNAL_RUN_LAST,
24                         gobject.TYPE_NONE,
25                         (gobject.TYPE_STRING, ),
26                 ),
27         }
28
29         MINIMUM_MOVEMENT = 32
30
31         _NO_POSITION = -1, -1
32
33         def __init__(self):
34                 gobject.GObject.__init__(self)
35                 self._eventBox = gtk.EventBox()
36                 self._eventBox.connect("button_press_event", self._on_button_press)
37                 self._eventBox.connect("button_release_event", self._on_button_release)
38                 self._eventBox.connect("motion_notify_event", self._on_motion_notify)
39
40                 self._isPortrait = True
41                 self._clickPosition = self._NO_POSITION
42
43         @property
44         def toplevel(self):
45                 return self._eventBox
46
47         def set_orientation(self, orientation):
48                 if orientation == gtk.ORIENTATION_VERTICAL:
49                         self._isPortrait = True
50                 elif orientation == gtk.ORIENTATION_HORIZONTAL:
51                         self._isPortrait = False
52                 else:
53                         raise NotImplementedError(orientation)
54
55         def is_active(self):
56                 return self._clickPosition != self._NO_POSITION
57
58         def get_state(self, newCoord):
59                 if self._clickPosition == self._NO_POSITION:
60                         return ""
61
62                 delta = (
63                         newCoord[0] - self._clickPosition[0],
64                         - (newCoord[1] - self._clickPosition[1])
65                 )
66                 absDelta = (abs(delta[0]), abs(delta[1]))
67                 if max(*absDelta) < self.MINIMUM_MOVEMENT:
68                         return "clicking"
69
70                 if absDelta[0] < absDelta[1]:
71                         if 0 < delta[1]:
72                                 return "up"
73                         else:
74                                 return "down"
75                 else:
76                         if 0 < delta[0]:
77                                 return "right"
78                         else:
79                                 return "left"
80
81         @misc_utils.log_exception(_moduleLogger)
82         def _on_button_press(self, widget, event):
83                 if self._clickPosition != self._NO_POSITION:
84                         _moduleLogger.debug("Ignoring double click")
85                 self._clickPosition = event.get_coords()
86
87                 self.emit("navigating", "clicking")
88
89         @misc_utils.log_exception(_moduleLogger)
90         def _on_button_release(self, widget, event):
91                 assert self._clickPosition != self._NO_POSITION
92                 try:
93                         mousePosition = event.get_coords()
94                         state = self.get_state(mousePosition)
95                         assert state
96                 finally:
97                         self._clickPosition = self._NO_POSITION
98                 self.emit("action", state)
99
100         @misc_utils.log_exception(_moduleLogger)
101         def _on_motion_notify(self, widget, event):
102                 if self._clickPosition == self._NO_POSITION:
103                         return
104
105                 mousePosition = event.get_coords()
106                 newState = self.get_state(mousePosition)
107                 self.emit("navigating", newState)
108
109
110 gobject.type_register(NavigationBox)
111
112
113 class StreamPresenter(object):
114
115         def __init__(self, store):
116                 self._store = store
117
118                 self._image = gtk.DrawingArea()
119                 self._image.connect("expose_event", self._on_expose)
120
121                 self._isPortrait = True
122
123                 self._backgroundImage = None
124                 self._title = ""
125                 self._subtitle = ""
126                 self._buttonImage = None
127                 self._imageName = ""
128                 self._dims = 0, 0
129
130         @property
131         def toplevel(self):
132                 return self._image
133
134         def set_orientation(self, orientation):
135                 if orientation == gtk.ORIENTATION_VERTICAL:
136                         self._isPortrait = True
137                 elif orientation == gtk.ORIENTATION_HORIZONTAL:
138                         self._isPortrait = False
139                 else:
140                         raise NotImplementedError(orientation)
141
142                 self._image.queue_draw()
143
144         def set_state(self, stateImage):
145                 if stateImage == self._imageName:
146                         return
147                 self._imageName = stateImage
148                 self._buttonImage = self._store.get_surface_from_store(stateImage)
149
150                 self._image.queue_draw()
151
152         def set_context(self, backgroundImage, title, subtitle):
153                 self._backgroundImage = self._store.get_surface_from_store(backgroundImage)
154                 self._title = title
155                 self._subtitle = subtitle
156
157                 backWidth = self._backgroundImage.get_width()
158                 backHeight = self._backgroundImage.get_height()
159                 self._image.set_size_request(backWidth, backHeight)
160
161                 self._image.queue_draw()
162
163         @misc_utils.log_exception(_moduleLogger)
164         def _on_expose(self, widget, event):
165                 cairoContext = self._image.window.cairo_create()
166                 self._draw_presenter(cairoContext)
167
168         def _draw_presenter(self, cairoContext):
169                 rect = self._image.get_allocation()
170                 self._dims = rect.width, rect.height
171
172                 # Blank things
173                 cairoContext.rectangle(
174                         0,
175                         0,
176                         rect.width,
177                         rect.height,
178                 )
179                 cairoContext.set_source_rgb(0, 0, 0)
180                 cairoContext.fill()
181
182                 # Draw Background
183                 if self._backgroundImage is not None:
184                         cairoContext.set_source_surface(
185                                 self._backgroundImage,
186                                 0,
187                                 0,
188                         )
189                         cairoContext.paint()
190
191                 pangoContext = self._image.create_pango_context()
192
193                 titleLayout = pango.Layout(pangoContext)
194                 titleLayout.set_markup("<i>%s</i>" % self._subtitle)
195                 textWidth, textHeight = titleLayout.get_pixel_size()
196                 subtitleTextX = self._dims[0] / 2 - textWidth / 2
197                 subtitleTextY = self._dims[1] - textHeight - self._buttonImage.get_height() + 10
198
199                 subtitleLayout = pango.Layout(pangoContext)
200                 subtitleLayout.set_markup("<b>%s</b>" % self._title)
201                 textWidth, textHeight = subtitleLayout.get_pixel_size()
202                 textX = self._dims[0] / 2 - textWidth / 2
203                 textY = subtitleTextY - textHeight
204
205                 xPadding = min((self._dims[0] - textWidth) / 2 - 5, 5)
206                 yPadding = 5
207                 startContent = xPadding, textY - yPadding
208                 endContent = self._dims[0] - xPadding,  self._dims[1] - yPadding
209
210                 # Control background
211                 cairoContext.rectangle(
212                         startContent[0],
213                         startContent[1],
214                         endContent[0] - startContent[0],
215                         endContent[1] - startContent[1],
216                 )
217                 cairoContext.set_source_rgba(0.9, 0.9, 0.9, 0.75)
218                 cairoContext.fill()
219
220                 # title
221                 if self._title or self._subtitle:
222                         cairoContext.move_to(subtitleTextX, subtitleTextY)
223                         cairoContext.set_source_rgb(0, 0, 0)
224                         cairoContext.show_layout(titleLayout)
225
226                         cairoContext.move_to(textX, textY)
227                         cairoContext.set_source_rgb(0, 0, 0)
228                         cairoContext.show_layout(subtitleLayout)
229
230                 self._draw_state(cairoContext)
231
232         def _draw_state(self, cairoContext):
233                 if self._backgroundImage is None or self._buttonImage is None:
234                         return
235                 cairoContext.set_source_surface(
236                         self._buttonImage,
237                         self._dims[0] / 2 - self._buttonImage.get_width() / 2,
238                         self._dims[1] - self._buttonImage.get_height() + 5,
239                 )
240                 cairoContext.paint()
241
242
243 class StreamMiniPresenter(object):
244
245         def __init__(self, store):
246                 self._store = store
247
248                 self._button = gtk.Image()
249
250         @property
251         def toplevel(self):
252                 return self._button
253
254         def set_orientation(self, orientation):
255                 pass
256
257         def set_state(self, stateImage):
258                 self._store.set_image_from_store(self._button, stateImage)
259
260         def set_context(self, backgroundImage, title, subtitle):
261                 pass