| 1 |
# Copyright (C) 2008-2010 Adam Olsen |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
from gi.repository import GObject |
| 28 |
from gi.repository import Gdk |
| 29 |
from gi.repository import Gtk |
| 30 |
|
| 31 |
from xl import event, settings |
| 32 |
from xl.common import clamp |
| 33 |
from xl.nls import gettext as _ |
| 34 |
|
| 35 |
from xlgui import icons |
| 36 |
|
| 37 |
|
| 38 |
class RatingWidget(Gtk.EventBox): |
| 39 |
""" |
| 40 |
A rating widget which displays a row of |
| 41 |
images and allows for selecting the rating |
| 42 |
""" |
| 43 |
|
| 44 |
__gproperties__ = { |
| 45 |
'rating': ( |
| 46 |
GObject.TYPE_INT, |
| 47 |
'rating', |
| 48 |
'The selected rating', |
| 49 |
0, # Minimum |
| 50 |
65535, # Maximum |
| 51 |
0, # Default |
| 52 |
GObject.ParamFlags.READWRITE, |
| 53 |
) |
| 54 |
} |
| 55 |
__gsignals__ = { |
| 56 |
'rating-changed': (GObject.SignalFlags.RUN_LAST, None, (GObject.TYPE_INT,)) |
| 57 |
} |
| 58 |
|
| 59 |
def __init__(self, rating=0, player=None): |
| 60 |
""" |
| 61 |
:param rating: the optional initial rating |
| 62 |
:type rating: int |
| 63 |
:param player: If not None, this rating widget will automatically |
| 64 |
update to reflect the rating of the current song |
| 65 |
:type player: xl.player.ExailePlayer |
| 66 |
""" |
| 67 |
Gtk.EventBox.__init__(self) |
| 68 |
self._player = player |
| 69 |
|
| 70 |
self.set_visible_window(False) |
| 71 |
self.set_above_child(True) |
| 72 |
self.add_events(Gdk.EventMask.POINTER_MOTION_MASK) |
| 73 |
self.set_can_focus(True) |
| 74 |
|
| 75 |
self._image = Gtk.Image() |
| 76 |
self.add(self._image) |
| 77 |
|
| 78 |
self._rating = -1 |
| 79 |
self.props.rating = rating |
| 80 |
|
| 81 |
if self._player is not None: |
| 82 |
event.add_ui_callback( |
| 83 |
self.on_rating_update, 'playback_track_start', self._player |
| 84 |
) |
| 85 |
event.add_ui_callback( |
| 86 |
self.on_rating_update, 'playback_track_end', self._player |
| 87 |
) |
| 88 |
event.add_ui_callback(self.on_rating_update, 'rating_changed') |
| 89 |
|
| 90 |
self.on_rating_update('rating_changed', None, None) |
| 91 |
|
| 92 |
def destroy(self): |
| 93 |
""" |
| 94 |
Cleanups |
| 95 |
""" |
| 96 |
if self._player is not None: |
| 97 |
event.remove_callback( |
| 98 |
self.on_rating_update, 'playback_track_start', self._player |
| 99 |
) |
| 100 |
event.remove_callback( |
| 101 |
self.on_rating_update, 'playback_track_end', self._player |
| 102 |
) |
| 103 |
event.remove_callback(self.on_rating_update, 'rating_changed') |
| 104 |
|
| 105 |
def do_get_property(self, property): |
| 106 |
""" |
| 107 |
Getter for custom properties |
| 108 |
""" |
| 109 |
if property.name == 'rating': |
| 110 |
return self._rating |
| 111 |
else: |
| 112 |
raise AttributeError('unknown property %s' % property.name) |
| 113 |
|
| 114 |
def do_set_property(self, property, value): |
| 115 |
""" |
| 116 |
Setter for custom properties |
| 117 |
""" |
| 118 |
if property.name == 'rating': |
| 119 |
if value == self._rating: |
| 120 |
value = 0 |
| 121 |
else: |
| 122 |
value = clamp(value, 0, settings.get_option('rating/maximum', 5)) |
| 123 |
|
| 124 |
self._rating = value |
| 125 |
self._image.set_from_pixbuf(icons.MANAGER.pixbuf_from_rating(value).pixbuf) |
| 126 |
|
| 127 |
self.emit('rating-changed', value) |
| 128 |
else: |
| 129 |
raise AttributeError('unknown property %s' % property.name) |
| 130 |
|
| 131 |
def do_expose_event(self, event): |
| 132 |
""" |
| 133 |
Takes care of painting the focus indicator |
| 134 |
""" |
| 135 |
if self.is_focus(): |
| 136 |
self.style.paint_focus( |
| 137 |
window=self.window, |
| 138 |
state_type=self.get_state(), |
| 139 |
area=event.area, |
| 140 |
widget=self, |
| 141 |
detail='button', # Borrow style from GtkButton |
| 142 |
x=event.area.x, |
| 143 |
y=event.area.y, |
| 144 |
width=event.area.width, |
| 145 |
height=event.area.height, |
| 146 |
) |
| 147 |
|
| 148 |
Gtk.EventBox.do_expose_event(self, event) |
| 149 |
|
| 150 |
def do_motion_notify_event(self, event): |
| 151 |
""" |
| 152 |
Temporarily updates the displayed rating |
| 153 |
""" |
| 154 |
if self.get_state_flags() & Gtk.StateType.INSENSITIVE: |
| 155 |
return |
| 156 |
|
| 157 |
allocation = self.get_allocation() |
| 158 |
maximum = settings.get_option('rating/maximum', 5) |
| 159 |
pixbuf_width = self._image.get_pixbuf().get_width() |
| 160 |
# Activate pixbuf if half of it has been passed |
| 161 |
threshold = (pixbuf_width / maximum) / 2 |
| 162 |
position = (event.x + threshold) / allocation.width |
| 163 |
rating = int(position * maximum) |
| 164 |
|
| 165 |
self._image.set_from_pixbuf(icons.MANAGER.pixbuf_from_rating(rating).pixbuf) |
| 166 |
|
| 167 |
def do_leave_notify_event(self, event): |
| 168 |
""" |
| 169 |
Restores the original rating |
| 170 |
""" |
| 171 |
if self.get_state_flags() & Gtk.StateType.INSENSITIVE: |
| 172 |
return |
| 173 |
|
| 174 |
self._image.set_from_pixbuf( |
| 175 |
icons.MANAGER.pixbuf_from_rating(self._rating).pixbuf |
| 176 |
) |
| 177 |
|
| 178 |
def do_button_release_event(self, event): |
| 179 |
""" |
| 180 |
Applies the selected rating |
| 181 |
""" |
| 182 |
if self.get_state_flags() & Gtk.StateType.INSENSITIVE: |
| 183 |
return |
| 184 |
|
| 185 |
allocation = self.get_allocation() |
| 186 |
maximum = settings.get_option('rating/maximum', 5) |
| 187 |
pixbuf_width = self._image.get_pixbuf().get_width() |
| 188 |
# Activate pixbuf if half of it has been passed |
| 189 |
threshold = (pixbuf_width / maximum) / 2 |
| 190 |
position = (event.x + threshold) / allocation.width |
| 191 |
self.props.rating = int(position * maximum) |
| 192 |
|
| 193 |
def do_key_press_event(self, event): |
| 194 |
""" |
| 195 |
Changes the rating on keyboard interaction |
| 196 |
* Alt+Up/Right: increases the rating |
| 197 |
* Alt+Down/Left: decreases the rating |
| 198 |
""" |
| 199 |
if self.get_state_flags() & Gtk.StateType.INSENSITIVE: |
| 200 |
return |
| 201 |
|
| 202 |
if not event.get_state() & Gdk.ModifierType.MOD1_MASK: |
| 203 |
return |
| 204 |
|
| 205 |
if event.keyval in (Gdk.KEY_Up, Gdk.KEY_Right): |
| 206 |
rating = self.props.rating + 1 |
| 207 |
elif event.keyval in (Gdk.KEY_Down, Gdk.KEY_Left): |
| 208 |
rating = self.props.rating - 1 |
| 209 |
else: |
| 210 |
return |
| 211 |
|
| 212 |
rating = max(0, rating) |
| 213 |
|
| 214 |
# Prevents unsetting rating if maximum is reached |
| 215 |
if rating == self.props.rating: |
| 216 |
return |
| 217 |
|
| 218 |
self.props.rating = rating |
| 219 |
|
| 220 |
def on_rating_update(self, event_type, sender, data): |
| 221 |
""" |
| 222 |
Updates the rating from the current track |
| 223 |
""" |
| 224 |
if self._player.current is not None: |
| 225 |
self._rating = self._player.current.get_rating() |
| 226 |
self._image.set_from_pixbuf( |
| 227 |
icons.MANAGER.pixbuf_from_rating(self._rating).pixbuf |
| 228 |
) |
| 229 |
self.set_sensitive(True) |
| 230 |
else: |
| 231 |
self.set_sensitive(False) |
| 232 |
|
| 233 |
|
| 234 |
class RatingMenuItem(Gtk.MenuItem): |
| 235 |
""" |
| 236 |
A menuitem containing a rating widget |
| 237 |
""" |
| 238 |
|
| 239 |
__gproperties__ = { |
| 240 |
'rating': ( |
| 241 |
GObject.TYPE_INT, |
| 242 |
'rating', |
| 243 |
'The selected rating', |
| 244 |
0, # Minimum |
| 245 |
65535, # Maximum |
| 246 |
0, # Default |
| 247 |
GObject.ParamFlags.READWRITE, |
| 248 |
) |
| 249 |
} |
| 250 |
__gsignals__ = { |
| 251 |
'rating-changed': (GObject.SignalFlags.RUN_LAST, None, (GObject.TYPE_INT,)) |
| 252 |
} |
| 253 |
|
| 254 |
def __init__(self, rating=0, player=None): |
| 255 |
""" |
| 256 |
:param rating: the optional initial rating |
| 257 |
:type rating: int |
| 258 |
:param player: If not None, this rating widget will automatically |
| 259 |
update to reflect the rating of the current song |
| 260 |
:type player: xl.player.ExailePlayer |
| 261 |
""" |
| 262 |
Gtk.MenuItem.__init__(self) |
| 263 |
|
| 264 |
box = Gtk.Box(spacing=6) |
| 265 |
box.pack_start(Gtk.Label(label=_('Rating:')), False, False, 0) |
| 266 |
self.rating_widget = RatingWidget(rating, player) |
| 267 |
box.pack_start(self.rating_widget, False, False, 0) |
| 268 |
|
| 269 |
self.add(box) |
| 270 |
|
| 271 |
self.rating_widget.connect('rating-changed', self.on_rating_changed) |
| 272 |
|
| 273 |
def do_get_property(self, property): |
| 274 |
""" |
| 275 |
Getter for custom properties |
| 276 |
""" |
| 277 |
if property.name == 'rating': |
| 278 |
return self.rating_widget.props.rating |
| 279 |
else: |
| 280 |
raise AttributeError('unknown property %s' % property.name) |
| 281 |
|
| 282 |
def do_set_property(self, property, value): |
| 283 |
""" |
| 284 |
Setter for custom properties |
| 285 |
""" |
| 286 |
if property.name == 'rating': |
| 287 |
self.rating_widget.props.rating = value |
| 288 |
else: |
| 289 |
raise AttributeError('unknown property %s' % property.name) |
| 290 |
|
| 291 |
def do_motion_notify_event(self, event): |
| 292 |
""" |
| 293 |
Forwards the event to the rating widget |
| 294 |
""" |
| 295 |
allocation = self.rating_widget.get_allocation() |
| 296 |
|
| 297 |
if allocation.x < event.x < allocation.x + allocation.width: |
| 298 |
x, y = self.translate_coordinates( |
| 299 |
self.rating_widget, int(event.x), int(event.y) |
| 300 |
) |
| 301 |
event.x, event.y = float(x), float(y) |
| 302 |
self.rating_widget.emit('motion-notify-event', event.copy()) |
| 303 |
|
| 304 |
def do_leave_notify_event(self, event): |
| 305 |
""" |
| 306 |
Forwards the event to the rating widget |
| 307 |
""" |
| 308 |
self.rating_widget.emit('leave-notify-event', event.copy()) |
| 309 |
|
| 310 |
def do_button_release_event(self, event): |
| 311 |
""" |
| 312 |
Forwards the event to the rating widget |
| 313 |
""" |
| 314 |
allocation = self.rating_widget.get_allocation() |
| 315 |
|
| 316 |
if allocation.x < event.x < allocation.x + allocation.width: |
| 317 |
x, y = self.translate_coordinates( |
| 318 |
self.rating_widget, int(event.x), int(event.y) |
| 319 |
) |
| 320 |
event.x, event.y = float(x), float(y) |
| 321 |
self.rating_widget.emit('button-release-event', event.copy()) |
| 322 |
|
| 323 |
def on_rating_changed(self, widget, rating): |
| 324 |
""" |
| 325 |
Forwards the event |
| 326 |
""" |
| 327 |
self.emit('rating-changed', rating) |
| 328 |
|
| 329 |
|
| 330 |
class RatingCellRenderer(Gtk.CellRendererPixbuf): |
| 331 |
""" |
| 332 |
A cell renderer drawing rating images |
| 333 |
and allowing for selection of ratings |
| 334 |
""" |
| 335 |
|
| 336 |
__gproperties__ = { |
| 337 |
'rating': ( |
| 338 |
GObject.TYPE_INT, |
| 339 |
'Rating', |
| 340 |
'The selected rating', |
| 341 |
0, # Minimum |
| 342 |
65535, # Maximum |
| 343 |
0, # Default |
| 344 |
GObject.ParamFlags.READWRITE, |
| 345 |
) |
| 346 |
} |
| 347 |
__gsignals__ = { |
| 348 |
'rating-changed': ( |
| 349 |
GObject.SignalFlags.RUN_LAST, |
| 350 |
None, |
| 351 |
(GObject.TYPE_PYOBJECT, GObject.TYPE_INT), |
| 352 |
) |
| 353 |
} |
| 354 |
|
| 355 |
def __init__(self): |
| 356 |
Gtk.CellRendererPixbuf.__init__(self) |
| 357 |
self.props.mode = Gtk.CellRendererMode.ACTIVATABLE |
| 358 |
self.props.xalign = 0 |
| 359 |
|
| 360 |
self.rating = 0 |
| 361 |
self.size_ratio = 1 |
| 362 |
|
| 363 |
def do_get_property(self, property): |
| 364 |
""" |
| 365 |
Getter for GObject properties |
| 366 |
""" |
| 367 |
if property.name == 'rating': |
| 368 |
return self.rating |
| 369 |
else: |
| 370 |
raise AttributeError('unknown property %s' % property.name) |
| 371 |
|
| 372 |
def do_set_property(self, property, value): |
| 373 |
""" |
| 374 |
Setter for GObject properties |
| 375 |
""" |
| 376 |
if property.name == 'rating': |
| 377 |
self.rating = value |
| 378 |
self.props.pixbuf = icons.MANAGER.pixbuf_from_rating( |
| 379 |
self.rating, self.size_ratio |
| 380 |
).pixbuf |
| 381 |
else: |
| 382 |
raise AttributeError('unknown property %s' % property.name) |
| 383 |
|
| 384 |
def do_activate(self, event, widget, path, background_area, cell_area, flags): |
| 385 |
""" |
| 386 |
Checks if a button press event did occur |
| 387 |
within the clickable rating image area |
| 388 |
""" |
| 389 |
if event is None: # Keyboard activation |
| 390 |
return |
| 391 |
|
| 392 |
# Locate click area at zero |
| 393 |
click_area = Gdk.Rectangle() |
| 394 |
click_area.x = 0 |
| 395 |
click_area.y = self.props.ypad |
| 396 |
click_area.width = self.props.pixbuf.get_width() |
| 397 |
click_area.height = self.props.pixbuf.get_height() |
| 398 |
|
| 399 |
# Move event location relative to zero |
| 400 |
event.x -= cell_area.x + click_area.x |
| 401 |
event.y -= cell_area.y + click_area.y |
| 402 |
|
| 403 |
if 0 <= event.x <= click_area.width: |
| 404 |
fraction = event.x / click_area.width |
| 405 |
maximum = settings.get_option('rating/maximum', 5) |
| 406 |
rating = fraction * maximum + 1 |
| 407 |
self.emit('rating-changed', (int(path),), rating) |
| 408 |
|
| 409 |
|
| 410 |
''' |
| 411 |
No longer needed in GTK3? |
| 412 |
|
| 413 |
def do_render(self, context, widget, background_area, |
| 414 |
cell_area, flags): |
| 415 |
""" |
| 416 |
Renders the rating images |
| 417 |
(Overridden since Gtk.CellRendererPixbuf |
| 418 |
fails at vertical padding) |
| 419 |
""" |
| 420 |
cell_area.width *= self.props.xalign |
| 421 |
cell_area.height *= self.props.yalign |
| 422 |
|
| 423 |
pixbuf_width = self.props.pixbuf.get_width() * self.props.xalign |
| 424 |
pixbuf_height = self.props.pixbuf.get_height() * self.props.yalign |
| 425 |
|
| 426 |
x = cell_area.x + cell_area.width - pixbuf_width |
| 427 |
y = cell_area.y + cell_area.height - pixbuf_height |
| 428 |
|
| 429 |
context.set_source_pixbuf(self.props.pixbuf, x, y) |
| 430 |
context.paint() |
| 431 |
''' |