| 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 Atk |
| 28 |
from gi.repository import Gdk |
| 29 |
from gi.repository import GLib |
| 30 |
from gi.repository import GObject |
| 31 |
from gi.repository import Gtk |
| 32 |
from gi.repository import Pango |
| 33 |
|
| 34 |
from xl import event, formatter, player, providers, settings |
| 35 |
from xl.common import clamp |
| 36 |
from xl.nls import gettext as _ |
| 37 |
from xlgui.widgets import menu |
| 38 |
|
| 39 |
from xlgui.guiutil import GtkTemplate |
| 40 |
|
| 41 |
import logging |
| 42 |
|
| 43 |
logger = logging.getLogger(__name__) |
| 44 |
|
| 45 |
|
| 46 |
class ProgressBarFormatter(formatter.ProgressTextFormatter): |
| 47 |
""" |
| 48 |
A formatter for progress bars |
| 49 |
""" |
| 50 |
|
| 51 |
def __init__(self, player): |
| 52 |
formatter.ProgressTextFormatter.__init__(self, '', player) |
| 53 |
|
| 54 |
self.on_option_set('gui_option_set', settings, 'gui/progress_bar_text_format') |
| 55 |
event.add_ui_callback(self.on_option_set, 'gui_option_set') |
| 56 |
|
| 57 |
def on_option_set(self, event, settings, option): |
| 58 |
""" |
| 59 |
Updates the internal format on setting change |
| 60 |
""" |
| 61 |
if option == 'gui/progress_bar_text_format': |
| 62 |
self.props.format = settings.get_option( |
| 63 |
'gui/progress_bar_text_format', '$current_time / $remaining_time' |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
class PlaybackProgressBar(Gtk.ProgressBar): |
| 68 |
""" |
| 69 |
Progress bar which automatically follows playback |
| 70 |
""" |
| 71 |
|
| 72 |
def __init__(self, player): |
| 73 |
Gtk.ProgressBar.__init__(self) |
| 74 |
self.__player = player |
| 75 |
|
| 76 |
try: |
| 77 |
awidget = self.get_accessible() |
| 78 |
awidget.set_role(Atk.Role.AUDIO) |
| 79 |
except Exception: |
| 80 |
logger.debug("Exception setting ATK role", exc_info=True) |
| 81 |
|
| 82 |
self.set_show_text(True) |
| 83 |
|
| 84 |
self.reset() |
| 85 |
|
| 86 |
self.formatter = ProgressBarFormatter(player) |
| 87 |
self.__timer_id = None |
| 88 |
self.__events = ( |
| 89 |
'playback_track_start', |
| 90 |
'playback_player_end', |
| 91 |
'playback_toggle_pause', |
| 92 |
'playback_error', |
| 93 |
) |
| 94 |
|
| 95 |
for e in self.__events: |
| 96 |
event.add_ui_callback(getattr(self, 'on_%s' % e), e, self.__player) |
| 97 |
|
| 98 |
def destroy(self): |
| 99 |
""" |
| 100 |
Cleanups |
| 101 |
""" |
| 102 |
for e in self.__events: |
| 103 |
event.remove_callback(getattr(self, 'on_%s' % e), e, self.__player) |
| 104 |
|
| 105 |
def reset(self): |
| 106 |
""" |
| 107 |
Resets the progress bar appearance |
| 108 |
""" |
| 109 |
self.set_fraction(0) |
| 110 |
self.set_text(_('Not Playing')) |
| 111 |
|
| 112 |
def __enable_timer(self): |
| 113 |
""" |
| 114 |
Enables the update timer |
| 115 |
""" |
| 116 |
if self.__timer_id is not None: |
| 117 |
return |
| 118 |
|
| 119 |
interval = settings.get_option('gui/progress_update_millisecs', 1000) |
| 120 |
|
| 121 |
if interval % 1000 == 0: |
| 122 |
self.__timer_id = GLib.timeout_add_seconds(interval // 1000, self.on_timer) |
| 123 |
else: |
| 124 |
self.__timer_id = GLib.timeout_add(interval, self.on_timer) |
| 125 |
|
| 126 |
self.on_timer() |
| 127 |
|
| 128 |
def __disable_timer(self): |
| 129 |
""" |
| 130 |
Disables the update timer |
| 131 |
""" |
| 132 |
if self.__timer_id is not None: |
| 133 |
GLib.source_remove(self.__timer_id) |
| 134 |
self.__timer_id = None |
| 135 |
|
| 136 |
def on_timer(self): |
| 137 |
""" |
| 138 |
Updates progress bar appearance |
| 139 |
""" |
| 140 |
if self.__player.current is None: |
| 141 |
self.__disable_timer() |
| 142 |
self.reset() |
| 143 |
return False |
| 144 |
|
| 145 |
self.set_fraction(self.__player.get_progress()) |
| 146 |
self.set_text(self.formatter.format()) |
| 147 |
|
| 148 |
return True |
| 149 |
|
| 150 |
def on_playback_track_start(self, event_type, player, track): |
| 151 |
""" |
| 152 |
Starts update timer |
| 153 |
""" |
| 154 |
self.reset() |
| 155 |
self.__enable_timer() |
| 156 |
|
| 157 |
def on_playback_player_end(self, event_type, player, track): |
| 158 |
""" |
| 159 |
Stops update timer |
| 160 |
""" |
| 161 |
self.__disable_timer() |
| 162 |
self.reset() |
| 163 |
|
| 164 |
def on_playback_toggle_pause(self, event_type, player, track): |
| 165 |
""" |
| 166 |
Starts or stops update timer |
| 167 |
""" |
| 168 |
if player.is_playing(): |
| 169 |
self.__enable_timer() |
| 170 |
elif player.is_paused(): |
| 171 |
self.__disable_timer() |
| 172 |
|
| 173 |
def on_playback_error(self, event_type, player, message): |
| 174 |
""" |
| 175 |
Stops update timer |
| 176 |
""" |
| 177 |
self.__disable_timer() |
| 178 |
self.reset() |
| 179 |
|
| 180 |
|
| 181 |
class Anchor(int): |
| 182 |
__gtype__ = GObject.TYPE_INT |
| 183 |
|
| 184 |
|
| 185 |
for i, a in enumerate( |
| 186 |
'CENTER NORTH NORTH_WEST NORTH_EAST SOUTH SOUTH_WEST SOUTH_EAST WEST EAST'.split() |
| 187 |
): |
| 188 |
setattr(Anchor, a, Anchor(i)) |
| 189 |
|
| 190 |
|
| 191 |
class Marker(GObject.Object): |
| 192 |
""" |
| 193 |
A marker pointing to a playback position |
| 194 |
""" |
| 195 |
|
| 196 |
__gproperties__ = { |
| 197 |
'anchor': ( |
| 198 |
Anchor, |
| 199 |
'anchor position', |
| 200 |
'The position the marker will be anchored', |
| 201 |
Anchor.CENTER, |
| 202 |
Anchor.EAST, |
| 203 |
Anchor.SOUTH, |
| 204 |
GObject.ParamFlags.READWRITE, |
| 205 |
), |
| 206 |
'color': ( |
| 207 |
Gdk.RGBA, |
| 208 |
'marker color', |
| 209 |
'Override color of the marker', |
| 210 |
GObject.ParamFlags.READWRITE, |
| 211 |
), |
| 212 |
'label': ( |
| 213 |
GObject.TYPE_STRING, |
| 214 |
'marker label', |
| 215 |
'Textual description of the marker', |
| 216 |
None, |
| 217 |
GObject.ParamFlags.READWRITE, |
| 218 |
), |
| 219 |
'position': ( |
| 220 |
GObject.TYPE_FLOAT, |
| 221 |
'marker position', |
| 222 |
'Relative position of the marker', |
| 223 |
0, |
| 224 |
1, |
| 225 |
0, |
| 226 |
GObject.ParamFlags.READWRITE, |
| 227 |
), |
| 228 |
'state': ( |
| 229 |
Gtk.StateType, |
| 230 |
'marker state', |
| 231 |
'The state of the marker', |
| 232 |
Gtk.StateType.NORMAL, |
| 233 |
GObject.ParamFlags.READWRITE, |
| 234 |
), |
| 235 |
} |
| 236 |
__gsignals__ = {'reached': (GObject.SignalFlags.RUN_LAST, None, ())} |
| 237 |
|
| 238 |
def __init__(self, position: float = 0): |
| 239 |
GObject.Object.__init__(self) |
| 240 |
|
| 241 |
self.__values = { |
| 242 |
'anchor': Anchor.SOUTH, |
| 243 |
'color': None, |
| 244 |
'label': None, |
| 245 |
'position': 0, |
| 246 |
'state': Gtk.StateType.NORMAL, |
| 247 |
} |
| 248 |
|
| 249 |
self.props.position = position |
| 250 |
|
| 251 |
def __str__(self): |
| 252 |
""" |
| 253 |
Informal representation |
| 254 |
""" |
| 255 |
if self.props.label is not None: |
| 256 |
text = '%s (%g)' % (self.props.label, self.props.position) |
| 257 |
else: |
| 258 |
text = '%g' % self.props.position |
| 259 |
|
| 260 |
return text |
| 261 |
|
| 262 |
def __lt__(self, other): |
| 263 |
""" |
| 264 |
Compares positions |
| 265 |
""" |
| 266 |
return self.props.position < other.props.position |
| 267 |
|
| 268 |
def __gt__(self, other): |
| 269 |
""" |
| 270 |
Compares positions |
| 271 |
""" |
| 272 |
return self.props.position > other.props.position |
| 273 |
|
| 274 |
def do_get_property(self, gproperty): |
| 275 |
""" |
| 276 |
Gets a GObject property |
| 277 |
""" |
| 278 |
try: |
| 279 |
return self.__values[gproperty.name] |
| 280 |
except KeyError: |
| 281 |
raise AttributeError('unknown property %s' % gproperty.name) |
| 282 |
|
| 283 |
def do_set_property(self, gproperty, value): |
| 284 |
""" |
| 285 |
Sets a GObject property |
| 286 |
""" |
| 287 |
if gproperty.name not in self.__values: |
| 288 |
raise AttributeError('unknown property %s' % gproperty.name) |
| 289 |
self.__values[gproperty.name] = value |
| 290 |
|
| 291 |
|
| 292 |
class MarkerManager(providers.ProviderHandler): |
| 293 |
""" |
| 294 |
Enables management of playback markers; namely simple |
| 295 |
adding, removing and finding. It also takes care of |
| 296 |
emitting signals when a marker is reached during playback. |
| 297 |
|
| 298 |
TODO: This presumes there is only one player object present |
| 299 |
in exaile, and that markers can only be associated with |
| 300 |
the single player object. This class should probably be |
| 301 |
changed to be associated with a particular player (which |
| 302 |
requires some changes to the marker class) |
| 303 |
""" |
| 304 |
|
| 305 |
def __init__(self): |
| 306 |
providers.ProviderHandler.__init__(self, 'playback-markers') |
| 307 |
|
| 308 |
self.__events = ('playback_track_start', 'playback_track_end') |
| 309 |
self.__timeout_id = None |
| 310 |
|
| 311 |
for e in self.__events: |
| 312 |
event.add_ui_callback(getattr(self, 'on_%s' % e), e) |
| 313 |
|
| 314 |
def destroy(self): |
| 315 |
""" |
| 316 |
Cleanups |
| 317 |
""" |
| 318 |
for e in self.__events: |
| 319 |
event.remove_callback(getattr(self, 'on_%s' % e), e) |
| 320 |
|
| 321 |
def add_marker(self, position: float) -> Marker: |
| 322 |
""" |
| 323 |
Creates a new marker for a playback position |
| 324 |
|
| 325 |
:param position: the playback position [0..1] |
| 326 |
:returns: the new marker |
| 327 |
""" |
| 328 |
marker = Marker(position) |
| 329 |
# Provider compatibility |
| 330 |
marker.name = 'marker' |
| 331 |
providers.register('playback-markers', marker) |
| 332 |
|
| 333 |
return marker |
| 334 |
|
| 335 |
def remove_marker(self, marker: Marker) -> None: |
| 336 |
""" |
| 337 |
Removes a playback marker |
| 338 |
|
| 339 |
:param marker: the marker |
| 340 |
""" |
| 341 |
providers.unregister('playback-markers', marker) |
| 342 |
|
| 343 |
def get_markers_at(self, position): |
| 344 |
""" |
| 345 |
Gets all markers located at a position |
| 346 |
|
| 347 |
:param position: the mark position |
| 348 |
:type position: float |
| 349 |
:returns: (m1, m2, ...) |
| 350 |
:rtype: (:class:`Marker`, ...) |
| 351 |
|
| 352 |
* *m1*: the first marker |
| 353 |
* *m2*: the second marker |
| 354 |
* ... |
| 355 |
""" |
| 356 |
# Reproduce value modifications |
| 357 |
position = Marker(position).props.position |
| 358 |
markers = () |
| 359 |
|
| 360 |
for marker in providers.get('playback-markers'): |
| 361 |
if marker.props.position == position: |
| 362 |
markers += (marker,) |
| 363 |
|
| 364 |
return markers |
| 365 |
|
| 366 |
def on_playback_track_start(self, event, player, track): |
| 367 |
""" |
| 368 |
Starts marker watching |
| 369 |
""" |
| 370 |
if self.__timeout_id is not None: |
| 371 |
GLib.source_remove(self.__timeout_id) |
| 372 |
|
| 373 |
self.__timeout_id = GLib.timeout_add_seconds(1, self.on_timeout, player) |
| 374 |
|
| 375 |
def on_playback_track_end(self, event, player, track): |
| 376 |
""" |
| 377 |
Stops marker watching |
| 378 |
""" |
| 379 |
if self.__timeout_id is not None: |
| 380 |
GLib.source_remove(self.__timeout_id) |
| 381 |
self.__timeout_id = None |
| 382 |
|
| 383 |
def on_timeout(self, player): |
| 384 |
""" |
| 385 |
Triggers "reached" signal of markers |
| 386 |
""" |
| 387 |
|
| 388 |
if player.current is None: |
| 389 |
self.__timeout_id = None |
| 390 |
return |
| 391 |
|
| 392 |
track_length = player.current.get_tag_raw('__length') |
| 393 |
|
| 394 |
if track_length is None: |
| 395 |
return True |
| 396 |
|
| 397 |
playback_time = int(player.get_time()) |
| 398 |
reached_markers = ( |
| 399 |
m |
| 400 |
for m in providers.get('playback-markers') |
| 401 |
if int(m.props.position * track_length) == playback_time |
| 402 |
) |
| 403 |
|
| 404 |
for marker in reached_markers: |
| 405 |
marker.emit('reached') |
| 406 |
|
| 407 |
return True |
| 408 |
|
| 409 |
|
| 410 |
__MARKERMANAGER = MarkerManager() |
| 411 |
add_marker = __MARKERMANAGER.add_marker |
| 412 |
remove_marker = __MARKERMANAGER.remove_marker |
| 413 |
get_markers_at = __MARKERMANAGER.get_markers_at |
| 414 |
|
| 415 |
|
| 416 |
class _SeekInternalProgressBar(PlaybackProgressBar): |
| 417 |
def __init__(self, player, points, marker_scale): |
| 418 |
PlaybackProgressBar.__init__(self, player) |
| 419 |
self._points = points |
| 420 |
self._seeking = False |
| 421 |
self._marker_scale = marker_scale |
| 422 |
|
| 423 |
def do_draw(self, context): |
| 424 |
""" |
| 425 |
Draws markers on top of the progress bar |
| 426 |
""" |
| 427 |
Gtk.ProgressBar.do_draw(self, context) |
| 428 |
|
| 429 |
if not self._points: |
| 430 |
return |
| 431 |
|
| 432 |
context.set_line_width(self._marker_scale / 0.9) |
| 433 |
style = self.get_style_context() |
| 434 |
|
| 435 |
POW_256_2 = 256**2 |
| 436 |
|
| 437 |
for marker, points in self._points.items(): |
| 438 |
for i, (x, y) in enumerate(points): |
| 439 |
if i == 0: |
| 440 |
context.move_to(x, y) |
| 441 |
else: |
| 442 |
context.line_to(x, y) |
| 443 |
context.close_path() |
| 444 |
|
| 445 |
if marker.props.state in (Gtk.StateType.PRELIGHT, Gtk.StateType.ACTIVE): |
| 446 |
c = style.get_color(Gtk.StateFlags.NORMAL) |
| 447 |
context.set_source_rgba(c.red, c.green, c.blue, c.alpha) |
| 448 |
else: |
| 449 |
if marker.props.color is not None: |
| 450 |
base = marker.props.color |
| 451 |
else: |
| 452 |
base = style.get_color(marker.props.state) |
| 453 |
|
| 454 |
context.set_source_rgba( |
| 455 |
base.red / POW_256_2, |
| 456 |
base.green / POW_256_2, |
| 457 |
base.blue / POW_256_2, |
| 458 |
0.7, |
| 459 |
) |
| 460 |
context.fill_preserve() |
| 461 |
|
| 462 |
if marker.props.state in (Gtk.StateType.PRELIGHT, Gtk.StateType.ACTIVE): |
| 463 |
c = style.get_color(Gtk.StateFlags.NORMAL) |
| 464 |
context.set_source_rgba(c.red, c.green, c.blue, c.alpha) |
| 465 |
else: |
| 466 |
foreground = style.get_color(marker.props.state) |
| 467 |
context.set_source_rgba( |
| 468 |
foreground.red / POW_256_2, |
| 469 |
foreground.green / POW_256_2, |
| 470 |
foreground.blue / POW_256_2, |
| 471 |
0.7, |
| 472 |
) |
| 473 |
context.stroke() |
| 474 |
|
| 475 |
def on_timer(self): |
| 476 |
""" |
| 477 |
Prevents update while seeking |
| 478 |
""" |
| 479 |
if self._seeking: |
| 480 |
return True |
| 481 |
|
| 482 |
return PlaybackProgressBar.on_timer(self) |
| 483 |
|
| 484 |
|
| 485 |
class SeekProgressBar(Gtk.EventBox, providers.ProviderHandler): |
| 486 |
""" |
| 487 |
Playback progress bar which allows for seeking |
| 488 |
and setting positional markers |
| 489 |
""" |
| 490 |
|
| 491 |
CSS = Gtk.CssProvider() |
| 492 |
CSS.load_from_data( |
| 493 |
b''' |
| 494 |
/* Make the text easier to read on Adwaita */ |
| 495 |
progressbar { |
| 496 |
color: unset; |
| 497 |
font-size: unset; |
| 498 |
} |
| 499 |
''' |
| 500 |
) |
| 501 |
|
| 502 |
__gproperties__ = { |
| 503 |
'marker-scale': ( |
| 504 |
GObject.TYPE_FLOAT, |
| 505 |
'marker scale', |
| 506 |
'Scaling of markers', |
| 507 |
0, |
| 508 |
1, |
| 509 |
0.7, |
| 510 |
GObject.ParamFlags.READWRITE, |
| 511 |
) |
| 512 |
} |
| 513 |
__gsignals__ = { |
| 514 |
'button-press-event': 'override', |
| 515 |
'button-release-event': 'override', |
| 516 |
'motion-notify-event': 'override', |
| 517 |
'notify': 'override', |
| 518 |
'key-press-event': 'override', |
| 519 |
'key-release-event': 'override', |
| 520 |
'scroll-event': 'override', |
| 521 |
'marker-reached': ( |
| 522 |
GObject.SignalFlags.RUN_LAST, |
| 523 |
GObject.TYPE_BOOLEAN, |
| 524 |
(Marker,), |
| 525 |
GObject.signal_accumulator_true_handled, |
| 526 |
), |
| 527 |
} |
| 528 |
|
| 529 |
def __init__(self, player, use_markers=True): |
| 530 |
""" |
| 531 |
TODO: markers aren't designed for more than one player, once |
| 532 |
they are we can get rid of the use_markers option |
| 533 |
""" |
| 534 |
|
| 535 |
Gtk.EventBox.__init__(self) |
| 536 |
|
| 537 |
points = {} |
| 538 |
|
| 539 |
self.__player = player |
| 540 |
self.__values = {'marker-scale': 0.7} |
| 541 |
self._points = points |
| 542 |
|
| 543 |
self.__progressbar = _SeekInternalProgressBar( |
| 544 |
player, points, self.__values['marker-scale'] |
| 545 |
) |
| 546 |
self._progressbar_menu = None |
| 547 |
|
| 548 |
if use_markers: |
| 549 |
self._progressbar_menu = ProgressBarContextMenu(self) |
| 550 |
|
| 551 |
self._marker_menu = MarkerContextMenu(self) |
| 552 |
self._marker_menu.connect('deactivate', self.on_marker_menu_deactivate) |
| 553 |
|
| 554 |
providers.ProviderHandler.__init__(self, 'playback-markers') |
| 555 |
|
| 556 |
sc = self.__progressbar.get_style_context() |
| 557 |
sc.add_provider(self.CSS, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) |
| 558 |
|
| 559 |
self.add_events( |
| 560 |
Gdk.EventMask.BUTTON_PRESS_MASK |
| 561 |
| Gdk.EventMask.BUTTON_RELEASE_MASK |
| 562 |
| Gdk.EventMask.POINTER_MOTION_MASK |
| 563 |
| Gdk.EventMask.LEAVE_NOTIFY_MASK |
| 564 |
| Gdk.EventMask.SCROLL_MASK |
| 565 |
) |
| 566 |
|
| 567 |
self.set_can_focus(True) |
| 568 |
|
| 569 |
self.connect('hierarchy-changed', self.on_hierarchy_changed) |
| 570 |
self.connect('scroll-event', self.on_scroll_event) |
| 571 |
|
| 572 |
self.add(self.__progressbar) |
| 573 |
self.show_all() |
| 574 |
|
| 575 |
def get_label(self, marker): |
| 576 |
""" |
| 577 |
Builds the most appropriate label |
| 578 |
markup to describe a marker |
| 579 |
|
| 580 |
:param marker: the marker |
| 581 |
:type marker: :class:`Marker` |
| 582 |
:returns: the label |
| 583 |
:rtype: string |
| 584 |
""" |
| 585 |
markup = None |
| 586 |
|
| 587 |
if self.__player.current: |
| 588 |
length = self.__player.current.get_tag_raw('__length') |
| 589 |
|
| 590 |
if length is not None: |
| 591 |
length = length * marker.props.position |
| 592 |
length = formatter.LengthTagFormatter.format_value(length) |
| 593 |
|
| 594 |
if marker.props.label: |
| 595 |
markup = '<b>%s</b> (%s)' % (marker.props.label, length) |
| 596 |
else: |
| 597 |
markup = '%s' % length |
| 598 |
else: |
| 599 |
if marker.props.label: |
| 600 |
markup = '<b>%s</b> (%d%%)' % ( |
| 601 |
marker.props.label, |
| 602 |
int(marker.props.position * 100), |
| 603 |
) |
| 604 |
else: |
| 605 |
markup = '%d%%' % int(marker.props.position * 100) |
| 606 |
|
| 607 |
return markup |
| 608 |
|
| 609 |
def _is_marker_hit(self, marker, check_x, check_y): |
| 610 |
""" |
| 611 |
Checks whether a marker is hit by a point |
| 612 |
|
| 613 |
:param marker: the marker |
| 614 |
:type marker: :class:`Marker` |
| 615 |
:param check_x: the x location to check |
| 616 |
:type check_x: float |
| 617 |
:param check_y: the y location to check |
| 618 |
:type check_y: float |
| 619 |
:returns: whether the marker was hit |
| 620 |
:rtype: bool |
| 621 |
""" |
| 622 |
points = self._points[marker] |
| 623 |
x, y, width, height = self._get_bounding_box(points) |
| 624 |
|
| 625 |
if x <= check_x <= width and y <= check_y <= height: |
| 626 |
return True |
| 627 |
|
| 628 |
return False |
| 629 |
|
| 630 |
def _get_points(self, marker, width=None, height=None): |
| 631 |
""" |
| 632 |
Calculates the points necessary |
| 633 |
to represent a marker |
| 634 |
|
| 635 |
:param marker: the marker |
| 636 |
:type marker: :class:`Marker` |
| 637 |
:param width: area width override |
| 638 |
:type width: int |
| 639 |
:param height: area height override |
| 640 |
:type height: int |
| 641 |
:returns: ((x1, y1), (x2, y2), ...) |
| 642 |
:rtype: ((float, float), ...) |
| 643 |
|
| 644 |
* *x1*: the x coordinate of the first point |
| 645 |
* *y1*: the y coordinate of the first point |
| 646 |
* *x2*: the x coordinate of the second point |
| 647 |
* *y2*: the y coordinate of the second point |
| 648 |
* ... |
| 649 |
""" |
| 650 |
points = () |
| 651 |
alloc = self.get_allocation() |
| 652 |
width = width or alloc.width |
| 653 |
height = height or alloc.height |
| 654 |
position = width * marker.props.position |
| 655 |
marker_scale = int(height * self.props.marker_scale) |
| 656 |
# Adjustment by half of the line width |
| 657 |
offset = self.props.marker_scale / 0.9 / 2 |
| 658 |
|
| 659 |
if marker.props.anchor == Anchor.NORTH_WEST: |
| 660 |
points = ( |
| 661 |
(position - offset, offset), |
| 662 |
(position + marker_scale * 0.75 - offset, offset), |
| 663 |
(position - offset, marker_scale * 0.75 + offset), |
| 664 |
) |
| 665 |
elif marker.props.anchor == Anchor.NORTH: |
| 666 |
points = ( |
| 667 |
(position - offset, marker_scale // 2 + offset), |
| 668 |
(position + marker_scale // 2 - offset, offset), |
| 669 |
(position - marker_scale // 2 - offset, offset), |
| 670 |
) |
| 671 |
elif marker.props.anchor == Anchor.NORTH_EAST: |
| 672 |
points = ( |
| 673 |
(position - marker_scale * 0.75 - offset, offset), |
| 674 |
(position - offset, offset), |
| 675 |
(position - offset, marker_scale * 0.75 + offset), |
| 676 |
) |
| 677 |
elif marker.props.anchor == Anchor.EAST: |
| 678 |
points = ( |
| 679 |
(position - marker_scale // 2 - offset, height // 2 + offset), |
| 680 |
(position - offset, height // 2 - marker_scale // 2 + offset), |
| 681 |
(position - offset, height // 2 + marker_scale // 2 + offset), |
| 682 |
) |
| 683 |
elif marker.props.anchor == Anchor.SOUTH_EAST: |
| 684 |
points = ( |
| 685 |
(position - offset, height - offset), |
| 686 |
(position - offset, height - marker_scale * 0.75 - offset), |
| 687 |
(position - marker_scale * 0.75 - offset, height - offset), |
| 688 |
) |
| 689 |
elif marker.props.anchor == Anchor.SOUTH: |
| 690 |
points = ( |
| 691 |
(position - offset, height - marker_scale // 2 - offset), |
| 692 |
(position + marker_scale // 2 - offset, height - offset), |
| 693 |
(position - marker_scale // 2 - offset, height - offset), |
| 694 |
) |
| 695 |
elif marker.props.anchor == Anchor.SOUTH_WEST: |
| 696 |
points = ( |
| 697 |
(position - offset, height - offset), |
| 698 |
(position + marker_scale * 0.75 - offset, height - offset), |
| 699 |
(position - offset, height - marker_scale * 0.75 - offset), |
| 700 |
) |
| 701 |
elif marker.props.anchor == Anchor.WEST: |
| 702 |
points = ( |
| 703 |
(position + marker_scale // 2 - offset, height // 2 + offset), |
| 704 |
(position - offset, height // 2 - marker_scale // 2 + offset), |
| 705 |
(position - offset, height // 2 + marker_scale // 2 + offset), |
| 706 |
) |
| 707 |
elif marker.props.anchor == Anchor.CENTER: |
| 708 |
points = ( |
| 709 |
(position - offset, height // 2 - marker_scale // 2 + offset), |
| 710 |
(position + marker_scale // 2 - offset, height // 2 + offset), |
| 711 |
(position - offset, height // 2 + marker_scale // 2 + offset), |
| 712 |
(position - marker_scale // 2 - offset, height // 2 + offset), |
| 713 |
) |
| 714 |
|
| 715 |
return points |
| 716 |
|
| 717 |
def _get_bounding_box(self, points): |
| 718 |
""" |
| 719 |
Calculates the axis aligned bounding box |
| 720 |
of a sequence of points |
| 721 |
|
| 722 |
:param points: ((x1, y1), (x2, y2), ...) |
| 723 |
:type points: ((float, float), ...) |
| 724 |
:returns: (x, y, width, height) |
| 725 |
:rtype: (float, float, float, float) |
| 726 |
|
| 727 |
* *x*: the x coordinate of the box |
| 728 |
* *y*: the y coordinate of the box |
| 729 |
* *width*: the width of the box |
| 730 |
* *height*: the height of the box |
| 731 |
""" |
| 732 |
xs, ys = zip(*points) |
| 733 |
return min(xs), min(ys), max(xs), max(ys) |
| 734 |
|
| 735 |
def seek(self, position): |
| 736 |
""" |
| 737 |
Seeks within the current track |
| 738 |
""" |
| 739 |
if self.__player.current: |
| 740 |
self.__player.set_progress(position) |
| 741 |
self.update_progress() |
| 742 |
|
| 743 |
def update_progress(self): |
| 744 |
""" |
| 745 |
Updates the progress bar and the time with data from the player |
| 746 |
""" |
| 747 |
|
| 748 |
if self.__player.current: |
| 749 |
length = self.__player.current.get_tag_raw('__length') |
| 750 |
|
| 751 |
if length is not None: |
| 752 |
position = float(self.__player.get_time()) / length |
| 753 |
self.__progressbar.set_fraction(position) |
| 754 |
self.__progressbar.set_text( |
| 755 |
self.__progressbar.formatter.format(current_time=length * position) |
| 756 |
) |
| 757 |
|
| 758 |
def do_get_property(self, gproperty): |
| 759 |
""" |
| 760 |
Gets a GObject property |
| 761 |
""" |
| 762 |
try: |
| 763 |
return self.__values[gproperty.name] |
| 764 |
except KeyError: |
| 765 |
raise AttributeError('unknown property %s' % property.name) |
| 766 |
|
| 767 |
def do_set_property(self, gproperty, value): |
| 768 |
""" |
| 769 |
Sets a GObject property |
| 770 |
""" |
| 771 |
try: |
| 772 |
self.__values[gproperty.name] = value |
| 773 |
except KeyError: |
| 774 |
raise AttributeError('unknown property %s' % property.name) |
| 775 |
|
| 776 |
def do_notify(self, gproperty): |
| 777 |
""" |
| 778 |
Reacts to GObject property changes |
| 779 |
""" |
| 780 |
if gproperty.name == 'marker-scale': |
| 781 |
for marker in self._points: |
| 782 |
self._points[marker] = self._get_points(marker) |
| 783 |
self.__progressbar._marker_scale = self.__values['marker-scale'] |
| 784 |
self.__progressbar.queue_draw() |
| 785 |
|
| 786 |
def do_size_allocate(self, allocation): |
| 787 |
""" |
| 788 |
Recalculates the marker points |
| 789 |
""" |
| 790 |
oldallocation = self.get_allocation() |
| 791 |
|
| 792 |
Gtk.EventBox.do_size_allocate(self, allocation) |
| 793 |
|
| 794 |
if allocation != oldallocation: |
| 795 |
for marker in self._points: |
| 796 |
self._points[marker] = self._get_points(marker) |
| 797 |
|
| 798 |
def do_button_press_event(self, event): |
| 799 |
""" |
| 800 |
Prepares seeking |
| 801 |
""" |
| 802 |
event = event.button |
| 803 |
hit_markers = [] |
| 804 |
|
| 805 |
for marker in self._points: |
| 806 |
if self._is_marker_hit(marker, event.x, event.y): |
| 807 |
if marker.props.state in (Gtk.StateType.NORMAL, Gtk.StateType.PRELIGHT): |
| 808 |
marker.props.state = Gtk.StateType.ACTIVE |
| 809 |
hit_markers += [marker] |
| 810 |
|
| 811 |
hit_markers.sort() |
| 812 |
|
| 813 |
if event.button == Gdk.BUTTON_PRIMARY: |
| 814 |
if self.__player.current is None: |
| 815 |
return True |
| 816 |
|
| 817 |
length = self.__player.current.get_tag_raw('__length') |
| 818 |
|
| 819 |
if length is None: |
| 820 |
return True |
| 821 |
|
| 822 |
if len(hit_markers) > 0: |
| 823 |
self.seek(hit_markers[0].props.position) |
| 824 |
else: |
| 825 |
fraction = event.x / self.get_allocation().width |
| 826 |
fraction = clamp(fraction, 0, 1) |
| 827 |
|
| 828 |
self.__progressbar.set_fraction(fraction) |
| 829 |
self.__progressbar.set_text( |
| 830 |
_('Seeking: %s') |
| 831 |
% self.__progressbar.formatter.format( |
| 832 |
current_time=length * fraction |
| 833 |
) |
| 834 |
) |
| 835 |
self.__progressbar._seeking = True |
| 836 |
elif event.triggers_context_menu(): |
| 837 |
if len(hit_markers) > 0: |
| 838 |
self._marker_menu.popup(event, tuple(hit_markers)) |
| 839 |
elif self._progressbar_menu is not None: |
| 840 |
self._progressbar_menu.popup(event) |
| 841 |
|
| 842 |
def do_button_release_event(self, event): |
| 843 |
""" |
| 844 |
Completes seeking |
| 845 |
""" |
| 846 |
event = event.button |
| 847 |
|
| 848 |
for marker in self._points: |
| 849 |
if marker.props.state == Gtk.StateType.ACTIVE: |
| 850 |
marker.props.state = Gtk.StateType.PRELIGHT |
| 851 |
|
| 852 |
if event.button == Gdk.BUTTON_PRIMARY and self.__progressbar._seeking: |
| 853 |
fraction = event.x / self.get_allocation().width |
| 854 |
fraction = clamp(fraction, 0, 1) |
| 855 |
|
| 856 |
self.seek(fraction) |
| 857 |
self.__progressbar._seeking = False |
| 858 |
|
| 859 |
def do_motion_notify_event(self, event): |
| 860 |
""" |
| 861 |
Updates progress bar while seeking |
| 862 |
and updates marker states on hover |
| 863 |
""" |
| 864 |
self.set_tooltip_markup(None) |
| 865 |
|
| 866 |
if self.__progressbar._seeking: |
| 867 |
press_event = Gdk.EventButton.new(Gdk.EventType.BUTTON_PRESS) |
| 868 |
press_event.button = Gdk.BUTTON_PRIMARY |
| 869 |
press_event.x = event.x |
| 870 |
press_event.y = event.y |
| 871 |
|
| 872 |
self.emit('button-press-event', press_event) |
| 873 |
else: |
| 874 |
hit_markers = [] |
| 875 |
|
| 876 |
for marker in self._points: |
| 877 |
if self._is_marker_hit(marker, event.x, event.y): |
| 878 |
if marker.props.state == Gtk.StateType.NORMAL: |
| 879 |
marker.props.state = Gtk.StateType.PRELIGHT |
| 880 |
hit_markers += [marker] |
| 881 |
else: |
| 882 |
if marker.props.state == Gtk.StateType.PRELIGHT: |
| 883 |
marker.props.state = Gtk.StateType.NORMAL |
| 884 |
|
| 885 |
if len(hit_markers) > 0: |
| 886 |
hit_markers.sort() |
| 887 |
markup = ', '.join([self.get_label(m) for m in hit_markers]) |
| 888 |
self.set_tooltip_markup(markup) |
| 889 |
self.trigger_tooltip_query() |
| 890 |
|
| 891 |
def do_leave_notify_event(self, event): |
| 892 |
""" |
| 893 |
Resets marker states |
| 894 |
""" |
| 895 |
for marker in self._points: |
| 896 |
# Leave other states intact |
| 897 |
if marker.props.state == Gtk.StateType.PRELIGHT: |
| 898 |
marker.props.state = Gtk.StateType.NORMAL |
| 899 |
|
| 900 |
def do_key_press_event(self, event): |
| 901 |
""" |
| 902 |
Prepares seeking via keyboard interaction |
| 903 |
* Alt+Up/Right: seek 1% forward |
| 904 |
* Alt+Down/Left: seek 1% backward |
| 905 |
""" |
| 906 |
_, state = event.get_state() |
| 907 |
if state & Gtk.StateType.INSENSITIVE: |
| 908 |
return |
| 909 |
if not state & Gdk.ModifierType.MOD1_MASK: |
| 910 |
return |
| 911 |
|
| 912 |
if event.keyval in (Gdk.KEY_Up, Gdk.KEY_Right): |
| 913 |
direction = 1 |
| 914 |
elif event.keyval in (Gdk.KEY_Down, Gdk.KEY_Left): |
| 915 |
direction = -1 |
| 916 |
else: |
| 917 |
return |
| 918 |
|
| 919 |
press_event = Gdk.Event.new(Gdk.EventType.BUTTON_PRESS) |
| 920 |
press_event.button = Gdk.BUTTON_PRIMARY |
| 921 |
new_fraction = self.__progressbar.get_fraction() + 0.01 * direction |
| 922 |
alloc = self.get_allocation() |
| 923 |
press_event.x = alloc.width * new_fraction |
| 924 |
press_event.y = float(alloc.y) |
| 925 |
|
| 926 |
self.emit('button-press-event', press_event) |
| 927 |
|
| 928 |
def do_key_release_event(self, event): |
| 929 |
""" |
| 930 |
Completes seeking via keyboard interaction |
| 931 |
""" |
| 932 |
_, state = event.get_state() |
| 933 |
if not state & Gdk.ModifierType.MOD1_MASK: |
| 934 |
return |
| 935 |
|
| 936 |
if event.keyval in (Gdk.KEY_Up, Gdk.KEY_Right): |
| 937 |
direction = 1 |
| 938 |
elif event.keyval in (Gdk.KEY_Down, Gdk.KEY_Left): |
| 939 |
direction = -1 |
| 940 |
else: |
| 941 |
return |
| 942 |
|
| 943 |
release_event = Gdk.Event.new(Gdk.EventType.BUTTON_RELEASE) |
| 944 |
release_event.button = Gdk.BUTTON_PRIMARY |
| 945 |
new_fraction = self.__progressbar.get_fraction() + 0.01 * direction |
| 946 |
alloc = self.get_allocation() |
| 947 |
release_event.x = alloc.width * new_fraction |
| 948 |
release_event.y = float(alloc.y) |
| 949 |
|
| 950 |
self.emit('button-release-event', release_event) |
| 951 |
|
| 952 |
def on_scroll_event(self, widget, event): |
| 953 |
""" |
| 954 |
Seek on scroll as VLC does |
| 955 |
""" |
| 956 |
if not self.__player.current: |
| 957 |
return True |
| 958 |
if self.__player.current.get_tag_raw('__length') is None: |
| 959 |
return True |
| 960 |
|
| 961 |
progress = self.__player.get_progress() |
| 962 |
progress_delta = 0.05 # 5% of track length |
| 963 |
progress_delta_small = 0.005 # 0.5% of track length |
| 964 |
|
| 965 |
if ( |
| 966 |
event.direction == Gdk.ScrollDirection.DOWN |
| 967 |
or event.direction == Gdk.ScrollDirection.LEFT |
| 968 |
): |
| 969 |
if event.get_state() & Gdk.ModifierType.SHIFT_MASK: |
| 970 |
new_progress = progress - progress_delta_small |
| 971 |
else: |
| 972 |
new_progress = progress - progress_delta |
| 973 |
elif ( |
| 974 |
event.direction == Gdk.ScrollDirection.UP |
| 975 |
or event.direction == Gdk.ScrollDirection.RIGHT |
| 976 |
): |
| 977 |
if event.get_state() & Gdk.ModifierType.SHIFT_MASK: |
| 978 |
new_progress = progress + progress_delta_small |
| 979 |
else: |
| 980 |
new_progress = progress + progress_delta |
| 981 |
elif event.direction == Gdk.ScrollDirection.SMOOTH: |
| 982 |
if event.get_state() & Gdk.ModifierType.SHIFT_MASK: |
| 983 |
new_progress = progress + progress_delta_small * ( |
| 984 |
event.deltax + event.deltay |
| 985 |
) |
| 986 |
else: |
| 987 |
new_progress = progress + progress_delta * (event.deltax + event.deltay) |
| 988 |
|
| 989 |
self.__player.set_progress(clamp(new_progress, 0, 1)) |
| 990 |
self.update_progress() |
| 991 |
|
| 992 |
return True |
| 993 |
|
| 994 |
def on_hierarchy_changed(self, widget, old_toplevel): |
| 995 |
""" |
| 996 |
Sets up editing cancel on toplevel focus out |
| 997 |
""" |
| 998 |
# Disconnect from previous toplevel. |
| 999 |
prev_conn = getattr(self, '_SeekProgressBar__prev_focus_out_conn', None) |
| 1000 |
if prev_conn: |
| 1001 |
prev_conn[0].disconnect(prev_conn[1]) |
| 1002 |
del self.__prev_focus_out_conn |
| 1003 |
|
| 1004 |
# Connect to new toplevel and store the connection, but only if it's an |
| 1005 |
# actual toplevel window. |
| 1006 |
toplevel = self.get_toplevel() |
| 1007 |
if toplevel.is_toplevel(): |
| 1008 |
conn = toplevel.connect( |
| 1009 |
'focus-out-event', lambda w, e: self.emit('focus-out-event', e.copy()) |
| 1010 |
) |
| 1011 |
self.__prev_focus_out_conn = (toplevel, conn) |
| 1012 |
|
| 1013 |
def on_marker_menu_deactivate(self, menu): |
| 1014 |
""" |
| 1015 |
Makes sure to reset states of |
| 1016 |
previously selected markers |
| 1017 |
""" |
| 1018 |
for marker in self._points: |
| 1019 |
marker.props.state = Gtk.StateType.NORMAL |
| 1020 |
self.__progressbar.queue_draw() |
| 1021 |
|
| 1022 |
def on_marker_notify(self, marker, gproperty): |
| 1023 |
""" |
| 1024 |
Recalculates marker points on position changes |
| 1025 |
""" |
| 1026 |
if gproperty.name in ('anchor', 'position'): |
| 1027 |
self._points[marker] = self._get_points(marker) |
| 1028 |
self.__progressbar.queue_draw() |
| 1029 |
|
| 1030 |
def on_provider_added(self, marker): |
| 1031 |
""" |
| 1032 |
Calculates points after marker addition |
| 1033 |
|
| 1034 |
:param marker: the new marker |
| 1035 |
:type marker: :class:`Marker` |
| 1036 |
""" |
| 1037 |
notify_id = marker.connect('notify', self.on_marker_notify) |
| 1038 |
setattr(marker, '%s_notify_id' % id(self), notify_id) |
| 1039 |
self._points[marker] = self._get_points(marker) |
| 1040 |
self.__progressbar.queue_draw() |
| 1041 |
|
| 1042 |
def on_provider_removed(self, marker): |
| 1043 |
""" |
| 1044 |
Removes points from internal cache |
| 1045 |
|
| 1046 |
:param marker: the marker |
| 1047 |
:type marker: :class:`Marker` |
| 1048 |
""" |
| 1049 |
notify_id = getattr(marker, '%s_notify_id' % id(self)) |
| 1050 |
if notify_id is not None: |
| 1051 |
marker.disconnect(notify_id) |
| 1052 |
|
| 1053 |
del self._points[marker] |
| 1054 |
self.__progressbar.queue_draw() |
| 1055 |
|
| 1056 |
# HACK: These methods implement the PlaybackAdapter interface (passing the |
| 1057 |
# calls to the internal progress bar, which is an actual PlaybackAdapter). |
| 1058 |
# This class only pretends to be a PlaybackAdapter because we don't want |
| 1059 |
# the mixin behavior here. |
| 1060 |
|
| 1061 |
def on_playback_track_start(self, event, player, track): |
| 1062 |
self.__progressbar.on_playback_track_start(event, player, track) |
| 1063 |
|
| 1064 |
def on_playback_track_end(self, event, player, track): |
| 1065 |
self.__progressbar.on_playback_track_end(event, player, track) |
| 1066 |
|
| 1067 |
def on_playback_player_end(self, event, player, track): |
| 1068 |
self.__progressbar.on_playback_player_end(event, player, track) |
| 1069 |
|
| 1070 |
def on_playback_toggle_pause(self, event, player, track): |
| 1071 |
self.__progressbar.on_playback_toggle_pause(event, player, track) |
| 1072 |
|
| 1073 |
def on_playback_error(self, event, player, message): |
| 1074 |
self.__progressbar.on_playback_error(event, player, message) |
| 1075 |
|
| 1076 |
|
| 1077 |
class ProgressBarContextMenu(menu.ProviderMenu): |
| 1078 |
""" |
| 1079 |
Progress bar specific context menu |
| 1080 |
""" |
| 1081 |
|
| 1082 |
def __init__(self, progressbar): |
| 1083 |
""" |
| 1084 |
:param progressbar: the progress bar |
| 1085 |
:type progressbar: :class:`PlaybackProgressBar` |
| 1086 |
""" |
| 1087 |
menu.ProviderMenu.__init__(self, 'progressbar-context-menu', progressbar) |
| 1088 |
|
| 1089 |
self._position = -1 |
| 1090 |
|
| 1091 |
def get_context(self): |
| 1092 |
""" |
| 1093 |
Retrieves the context |
| 1094 |
""" |
| 1095 |
context = {'current-position': self._position} |
| 1096 |
|
| 1097 |
return context |
| 1098 |
|
| 1099 |
def popup(self, event): |
| 1100 |
""" |
| 1101 |
Pops up the menu |
| 1102 |
|
| 1103 |
:param event: an event |
| 1104 |
:type event: :class:`Gdk.Event` |
| 1105 |
""" |
| 1106 |
self._position = event.x / self._parent.get_allocation().width |
| 1107 |
|
| 1108 |
menu.ProviderMenu.popup(self, event) |
| 1109 |
|
| 1110 |
|
| 1111 |
class MarkerContextMenu(menu.ProviderMenu): |
| 1112 |
""" |
| 1113 |
Marker specific context menu |
| 1114 |
""" |
| 1115 |
|
| 1116 |
def __init__(self, markerbar): |
| 1117 |
""" |
| 1118 |
:param markerbar: the marker capable progress bar |
| 1119 |
:type markerbar: :class:`SeekProgressBar` |
| 1120 |
""" |
| 1121 |
menu.ProviderMenu.__init__(self, 'playback-marker-context-menu', markerbar) |
| 1122 |
|
| 1123 |
self._markers = () |
| 1124 |
self._position = -1 |
| 1125 |
|
| 1126 |
def regenerate_menu(self): |
| 1127 |
""" |
| 1128 |
Builds the menu, with submenu if appropriate |
| 1129 |
""" |
| 1130 |
for marker in self._markers: |
| 1131 |
label = self._parent.get_label(marker) |
| 1132 |
|
| 1133 |
if label is None: |
| 1134 |
continue |
| 1135 |
|
| 1136 |
markup_data = Pango.parse_markup(label, -1, '0') |
| 1137 |
label_item = Gtk.MenuItem.new_with_mnemonic(markup_data[2]) |
| 1138 |
self.append(label_item) |
| 1139 |
|
| 1140 |
if len(self._markers) > 1: |
| 1141 |
item_menu = Gtk.Menu() |
| 1142 |
label_item.set_submenu(item_menu) |
| 1143 |
else: |
| 1144 |
item_menu = self |
| 1145 |
label_item.set_sensitive(False) |
| 1146 |
self.append(Gtk.SeparatorMenuItem()) |
| 1147 |
|
| 1148 |
context = { |
| 1149 |
'current-marker': marker, |
| 1150 |
'selected-markers': self._markers, |
| 1151 |
'current-position': self._position, |
| 1152 |
} |
| 1153 |
|
| 1154 |
for item in self._items: |
| 1155 |
i = item.factory(self, self._parent, context) |
| 1156 |
item_menu.append(i) |
| 1157 |
|
| 1158 |
self.show_all() |
| 1159 |
|
| 1160 |
def popup(self, event, markers): |
| 1161 |
""" |
| 1162 |
Pops up the menu |
| 1163 |
|
| 1164 |
:param event: an event |
| 1165 |
:type event: :class:`Gdk.Event` |
| 1166 |
:param markers: (m1, m2, ...) |
| 1167 |
:type markers: (:class:`Marker`, ...) |
| 1168 |
""" |
| 1169 |
self._markers = markers |
| 1170 |
self._position = event.x / self._parent.get_allocation().width |
| 1171 |
|
| 1172 |
menu.ProviderMenu.popup(self, event) |
| 1173 |
|
| 1174 |
|
| 1175 |
class MoveMarkerMenuItem(menu.MenuItem): |
| 1176 |
""" |
| 1177 |
Menu item allowing for movement of markers |
| 1178 |
""" |
| 1179 |
|
| 1180 |
def __init__(self, name, after, display_name=_('Move'), icon_name=None): |
| 1181 |
menu.MenuItem.__init__(self, name, None, after) |
| 1182 |
|
| 1183 |
self._parent = None |
| 1184 |
self._display_name = display_name |
| 1185 |
self._icon_name = icon_name |
| 1186 |
self._marker = None |
| 1187 |
self._reset_position = -1 |
| 1188 |
|
| 1189 |
def factory(self, menu, parent, context): |
| 1190 |
""" |
| 1191 |
Generates the menu item |
| 1192 |
""" |
| 1193 |
self._parent = parent |
| 1194 |
|
| 1195 |
item = Gtk.ImageMenuItem.new_with_mnemonic(self._display_name) |
| 1196 |
|
| 1197 |
if self._icon_name is not None: |
| 1198 |
item.set_image( |
| 1199 |
Gtk.Image.new_from_icon_name(self._icon_name, Gtk.IconSize.MENU) |
| 1200 |
) |
| 1201 |
|
| 1202 |
item.connect('activate', self.on_activate, parent, context) |
| 1203 |
|
| 1204 |
parent.connect('button-press-event', self.on_parent_button_press_event) |
| 1205 |
parent.connect('motion-notify-event', self.on_parent_motion_notify_event) |
| 1206 |
parent.connect('focus-out-event', self.on_parent_focus_out_event) |
| 1207 |
|
| 1208 |
return item |
| 1209 |
|
| 1210 |
def move_begin(self, marker): |
| 1211 |
""" |
| 1212 |
Captures the current marker for movement |
| 1213 |
|
| 1214 |
:param marker: the marker |
| 1215 |
:type marker: :class:`Marker` |
| 1216 |
:returns: whether a marker could be captured |
| 1217 |
:rtype: bool |
| 1218 |
""" |
| 1219 |
self.move_cancel() |
| 1220 |
|
| 1221 |
if marker is not None: |
| 1222 |
self._marker = marker |
| 1223 |
self._marker.props.state = Gtk.StateType.ACTIVE |
| 1224 |
self._reset_position = marker.props.position |
| 1225 |
self._parent.props.window.set_cursor( |
| 1226 |
Gdk.Cursor.new(Gdk.CursorType.SB_H_DOUBLE_ARROW) |
| 1227 |
) |
| 1228 |
|
| 1229 |
return True |
| 1230 |
|
| 1231 |
return False |
| 1232 |
|
| 1233 |
def move_update(self, position): |
| 1234 |
""" |
| 1235 |
Moves the marker |
| 1236 |
|
| 1237 |
:param position: the current marker position |
| 1238 |
:type position: float |
| 1239 |
:returns: whether a marker could be moved |
| 1240 |
:rtype: bool |
| 1241 |
""" |
| 1242 |
if self._marker is not None: |
| 1243 |
self._marker.props.position = position |
| 1244 |
label = self._parent.get_label(self._marker) |
| 1245 |
self._parent.set_tooltip_markup(label) |
| 1246 |
|
| 1247 |
return True |
| 1248 |
|
| 1249 |
return False |
| 1250 |
|
| 1251 |
def move_finish(self): |
| 1252 |
""" |
| 1253 |
Finishes movement and releases the marker |
| 1254 |
|
| 1255 |
:returns: whether the movement could be finished |
| 1256 |
:rtype: bool |
| 1257 |
""" |
| 1258 |
if self._marker is not None: |
| 1259 |
self._marker.props.state = Gtk.StateType.NORMAL |
| 1260 |
self._marker = None |
| 1261 |
self._reset_position = -1 |
| 1262 |
self._parent.props.window.set_cursor(None) |
| 1263 |
|
| 1264 |
return True |
| 1265 |
|
| 1266 |
return False |
| 1267 |
|
| 1268 |
def move_cancel(self): |
| 1269 |
""" |
| 1270 |
Cancels movement and releases the marker |
| 1271 |
|
| 1272 |
:returns: whether the movement could be cancelled |
| 1273 |
:rtype: bool |
| 1274 |
""" |
| 1275 |
if self._marker is not None: |
| 1276 |
self._marker.props.position = self._reset_position |
| 1277 |
self._marker.props.state = Gtk.StateType.NORMAL |
| 1278 |
self._marker = None |
| 1279 |
self._reset_position = -1 |
| 1280 |
self._parent.props.window.set_cursor(None) |
| 1281 |
|
| 1282 |
return True |
| 1283 |
|
| 1284 |
return False |
| 1285 |
|
| 1286 |
def on_activate(self, widget, parent, context): |
| 1287 |
""" |
| 1288 |
Starts movement of markers |
| 1289 |
""" |
| 1290 |
self.move_begin(context.get('current-marker', None)) |
| 1291 |
|
| 1292 |
def on_parent_button_press_event(self, widget, event): |
| 1293 |
""" |
| 1294 |
Finishes or cancels movement of markers |
| 1295 |
""" |
| 1296 |
if event.button == Gdk.BUTTON_PRIMARY: |
| 1297 |
return self.move_finish() |
| 1298 |
elif event.triggers_context_menu(): |
| 1299 |
return self.move_cancel() |
| 1300 |
|
| 1301 |
return False |
| 1302 |
|
| 1303 |
def on_parent_motion_notify_event(self, widget, event): |
| 1304 |
""" |
| 1305 |
Moves markers |
| 1306 |
""" |
| 1307 |
position = event.x / widget.get_allocation().width |
| 1308 |
|
| 1309 |
return self.move_update(position) |
| 1310 |
|
| 1311 |
def on_parent_focus_out_event(self, widget, event): |
| 1312 |
""" |
| 1313 |
Cancels movement of markers |
| 1314 |
""" |
| 1315 |
self.move_cancel() |
| 1316 |
|
| 1317 |
|
| 1318 |
class NewMarkerMenuItem(MoveMarkerMenuItem): |
| 1319 |
""" |
| 1320 |
Menu item allowing for insertion |
| 1321 |
and instant movement of a marker |
| 1322 |
""" |
| 1323 |
|
| 1324 |
def __init__(self, name, after): |
| 1325 |
MoveMarkerMenuItem.__init__(self, name, after, _('New Marker'), 'list-add') |
| 1326 |
|
| 1327 |
def move_cancel(self): |
| 1328 |
""" |
| 1329 |
Cancels movement and insertion of the marker |
| 1330 |
|
| 1331 |
:param parent: the parent |
| 1332 |
:type parent: :class:`SeekProgressBar` |
| 1333 |
:returns: whether the movement could be cancelled |
| 1334 |
:rtype: bool |
| 1335 |
""" |
| 1336 |
if self._marker is not None: |
| 1337 |
remove_marker(self._marker) |
| 1338 |
self._marker = None |
| 1339 |
self._reset_position = -1 |
| 1340 |
self._parent.props.window.set_cursor(None) |
| 1341 |
|
| 1342 |
return True |
| 1343 |
|
| 1344 |
return False |
| 1345 |
|
| 1346 |
def on_activate(self, widget, parent, context): |
| 1347 |
""" |
| 1348 |
Inserts a new marker and starts movement |
| 1349 |
""" |
| 1350 |
context['current-marker'] = add_marker(context['current-position']) |
| 1351 |
MoveMarkerMenuItem.on_activate(self, widget, parent, context) |
| 1352 |
|
| 1353 |
|
| 1354 |
# XXX: Example implementation only |
| 1355 |
# Bookmarks: "Add bookmark" (1 new marker) |
| 1356 |
# A-B-Repeat: "Repeat" (2 new marker, NW, NE) |
| 1357 |
|
| 1358 |
|
| 1359 |
def __create_progressbar_context_menu(): |
| 1360 |
items = [] |
| 1361 |
|
| 1362 |
items.append(NewMarkerMenuItem('new-marker', [])) |
| 1363 |
|
| 1364 |
for item in items: |
| 1365 |
providers.register('progressbar-context-menu', item) |
| 1366 |
|
| 1367 |
|
| 1368 |
__create_progressbar_context_menu() |
| 1369 |
|
| 1370 |
|
| 1371 |
def __create_marker_context_menu(): |
| 1372 |
items = [] |
| 1373 |
|
| 1374 |
def on_jumpto_item_activate(widget, name, parent, context): |
| 1375 |
# parent.seek(context['current-marker'].props.position) |
| 1376 |
position = context['current-marker'].props.position |
| 1377 |
player.PLAYER.set_progress(position) |
| 1378 |
|
| 1379 |
def on_remove_item_activate(widget, name, parent, context): |
| 1380 |
providers.unregister('playback-markers', context['current-marker']) |
| 1381 |
|
| 1382 |
items.append( |
| 1383 |
menu.simple_menu_item( |
| 1384 |
'jumpto-marker', [], _("_Jump to"), 'go-jump', on_jumpto_item_activate |
| 1385 |
) |
| 1386 |
) |
| 1387 |
items.append(MoveMarkerMenuItem('move-marker', [items[-1].name])) |
| 1388 |
items.append( |
| 1389 |
menu.simple_menu_item( |
| 1390 |
'remove-marker', |
| 1391 |
[items[-1].name], |
| 1392 |
_("_Remove Marker"), |
| 1393 |
'list-remove', |
| 1394 |
on_remove_item_activate, |
| 1395 |
) |
| 1396 |
) |
| 1397 |
|
| 1398 |
for item in items: |
| 1399 |
providers.register('playback-marker-context-menu', item) |
| 1400 |
|
| 1401 |
|
| 1402 |
__create_marker_context_menu() |
| 1403 |
|
| 1404 |
|
| 1405 |
@GtkTemplate('ui', 'widgets', 'volume_control.ui') |
| 1406 |
class VolumeControl(Gtk.Box): |
| 1407 |
""" |
| 1408 |
Encapsulates a button and a slider to |
| 1409 |
control the volume indicating the current |
| 1410 |
status via icon and tooltip |
| 1411 |
""" |
| 1412 |
|
| 1413 |
__gtype_name__ = 'VolumeControl' |
| 1414 |
|
| 1415 |
(button, slider, button_image, slider_adjustment) = GtkTemplate.Child.widgets(4) |
| 1416 |
|
| 1417 |
def __init__(self, player): |
| 1418 |
Gtk.Box.__init__(self) |
| 1419 |
self.init_template() |
| 1420 |
|
| 1421 |
self.button.add_events(Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.SCROLL_MASK) |
| 1422 |
|
| 1423 |
self.__volume_setting = '%s/volume' % player._name |
| 1424 |
self.restore_volume = settings.get_option(self.__volume_setting, 1) |
| 1425 |
self.icon_names = ['low', 'medium', 'high'] |
| 1426 |
self.__update(self.restore_volume) |
| 1427 |
|
| 1428 |
event.add_ui_callback(self.on_option_set, '%s_option_set' % player._name) |
| 1429 |
|
| 1430 |
def __update(self, volume): |
| 1431 |
""" |
| 1432 |
Sets the volume level indicator |
| 1433 |
""" |
| 1434 |
icon_name = 'audio-volume-muted' |
| 1435 |
tooltip = _('Muted') |
| 1436 |
|
| 1437 |
if volume > 0: |
| 1438 |
i = clamp(int(round(volume * 2)), 0, len(self.icon_names) - 1) |
| 1439 |
icon_name = 'audio-volume-%s' % self.icon_names[i] |
| 1440 |
# TRANSLATORS: Volume percentage |
| 1441 |
tooltip = _('%d%%') % (volume * 100) |
| 1442 |
else: |
| 1443 |
volume = 0 |
| 1444 |
|
| 1445 |
if volume == 1.0: |
| 1446 |
tooltip = _('Full Volume') |
| 1447 |
|
| 1448 |
if volume > 0: |
| 1449 |
self.button.set_active(False) |
| 1450 |
|
| 1451 |
self.button_image.set_from_icon_name(icon_name, Gtk.IconSize.BUTTON) |
| 1452 |
self.button.set_tooltip_text(tooltip) |
| 1453 |
self.slider.set_value(volume) |
| 1454 |
self.slider.set_tooltip_text(tooltip) |
| 1455 |
|
| 1456 |
@GtkTemplate.Callback |
| 1457 |
def on_scroll_event(self, widget, event): |
| 1458 |
""" |
| 1459 |
Changes the volume on scrolling |
| 1460 |
""" |
| 1461 |
page_increment = self.slider_adjustment.props.page_increment |
| 1462 |
step_increment = self.slider_adjustment.props.step_increment |
| 1463 |
value = self.slider.get_value() |
| 1464 |
|
| 1465 |
if event.direction in (Gdk.ScrollDirection.DOWN, Gdk.ScrollDirection.LEFT): |
| 1466 |
if event.get_state() & Gdk.ModifierType.SHIFT_MASK: |
| 1467 |
self.slider.set_value(value - page_increment) |
| 1468 |
else: |
| 1469 |
self.slider.set_value(value - step_increment) |
| 1470 |
return True |
| 1471 |
elif event.direction in (Gdk.ScrollDirection.UP, Gdk.ScrollDirection.RIGHT): |
| 1472 |
if event.get_state() & Gdk.ModifierType.SHIFT_MASK: |
| 1473 |
self.slider.set_value(value + page_increment) |
| 1474 |
else: |
| 1475 |
self.slider.set_value(value + step_increment) |
| 1476 |
return True |
| 1477 |
elif event.direction == Gdk.ScrollDirection.SMOOTH: |
| 1478 |
delta = event.delta_x - event.delta_y |
| 1479 |
if event.get_state() & Gdk.ModifierType.SHIFT_MASK: |
| 1480 |
self.slider.set_value(value + delta * page_increment) |
| 1481 |
else: |
| 1482 |
self.slider.set_value(value + delta * step_increment) |
| 1483 |
return True |
| 1484 |
|
| 1485 |
return False |
| 1486 |
|
| 1487 |
@GtkTemplate.Callback |
| 1488 |
def on_button_toggled(self, button): |
| 1489 |
""" |
| 1490 |
Mutes or unmutes the volume |
| 1491 |
""" |
| 1492 |
if button.get_active(): |
| 1493 |
self.restore_volume = settings.get_option(self.__volume_setting, 1) |
| 1494 |
volume = 0 |
| 1495 |
else: |
| 1496 |
volume = self.restore_volume |
| 1497 |
|
| 1498 |
if self.restore_volume > 0: |
| 1499 |
settings.set_option(self.__volume_setting, volume) |
| 1500 |
|
| 1501 |
@GtkTemplate.Callback |
| 1502 |
def on_slider_value_changed(self, slider): |
| 1503 |
""" |
| 1504 |
Stores the preferred volume |
| 1505 |
""" |
| 1506 |
settings.set_option(self.__volume_setting, slider.get_value()) |
| 1507 |
|
| 1508 |
@GtkTemplate.Callback |
| 1509 |
def on_slider_key_press_event(self, slider, event): |
| 1510 |
""" |
| 1511 |
Changes the volume on key press |
| 1512 |
while the slider is focused |
| 1513 |
""" |
| 1514 |
page_increment = slider.get_adjustment().props.page_increment |
| 1515 |
step_increment = slider.get_adjustment().props.step_increment |
| 1516 |
value = slider.get_value() |
| 1517 |
|
| 1518 |
if event.keyval == Gdk.KEY_Down: |
| 1519 |
slider.set_value(value - step_increment) |
| 1520 |
return True |
| 1521 |
elif event.keyval == Gdk.KEY_Page_Down: |
| 1522 |
slider.set_value(value - page_increment) |
| 1523 |
return True |
| 1524 |
elif event.keyval == Gdk.KEY_Up: |
| 1525 |
slider.set_value(value + step_increment) |
| 1526 |
return True |
| 1527 |
elif event.keyval == Gdk.KEY_Page_Up: |
| 1528 |
slider.set_value(value + page_increment) |
| 1529 |
return True |
| 1530 |
|
| 1531 |
return False |
| 1532 |
|
| 1533 |
def on_option_set(self, event, sender, option): |
| 1534 |
""" |
| 1535 |
Updates the volume indication |
| 1536 |
""" |
| 1537 |
if option == self.__volume_setting: |
| 1538 |
self.__update(settings.get_option(option, 1)) |
| 1539 |
|
| 1540 |
|
| 1541 |
def playpause(player): |
| 1542 |
if player.get_state() in ('playing', 'paused'): |
| 1543 |
player.toggle_pause() |
| 1544 |
else: |
| 1545 |
from xlgui import main |
| 1546 |
|
| 1547 |
page = main.get_selected_playlist() |
| 1548 |
if page: |
| 1549 |
pl = page.playlist |
| 1550 |
if len(pl) == 0: |
| 1551 |
return |
| 1552 |
try: |
| 1553 |
idx = page.view.get_selected_paths()[0][0] |
| 1554 |
except IndexError: |
| 1555 |
idx = 0 |
| 1556 |
player.queue.set_current_playlist(pl) |
| 1557 |
pl.current_position = idx |
| 1558 |
player.queue.play(track=pl.current) |
| 1559 |
|
| 1560 |
|
| 1561 |
def PlayPauseMenuItem(name, player, after): |
| 1562 |
def factory(name, after, player): |
| 1563 |
if player.is_playing(): |
| 1564 |
icon_name = 'media-playback-pause' |
| 1565 |
label = _("_Pause") |
| 1566 |
else: |
| 1567 |
icon_name = 'media-playback-start' |
| 1568 |
label = _("P_lay") |
| 1569 |
return menu.simple_menu_item( |
| 1570 |
name, after, label, icon_name, callback=lambda *args: playpause(player) |
| 1571 |
) |
| 1572 |
|
| 1573 |
return factory(name, after, player) |
| 1574 |
|
| 1575 |
|
| 1576 |
def NextMenuItem(name, player, after): |
| 1577 |
return menu.simple_menu_item( |
| 1578 |
name, |
| 1579 |
after, |
| 1580 |
_("_Next Track"), |
| 1581 |
'media-skip-forward', |
| 1582 |
callback=lambda *args: player.queue.next(), |
| 1583 |
) |
| 1584 |
|
| 1585 |
|
| 1586 |
def PrevMenuItem(name, player, after): |
| 1587 |
return menu.simple_menu_item( |
| 1588 |
name, |
| 1589 |
after, |
| 1590 |
_("_Previous Track"), |
| 1591 |
'media-skip-backward', |
| 1592 |
callback=lambda *args: player.queue.prev(), |
| 1593 |
) |
| 1594 |
|
| 1595 |
|
| 1596 |
def StopMenuItem(name, player, after): |
| 1597 |
return menu.simple_menu_item( |
| 1598 |
name, |
| 1599 |
after, |
| 1600 |
_("_Stop"), |
| 1601 |
'media-playback-stop', |
| 1602 |
callback=lambda *args: player.stop(), |
| 1603 |
) |