1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
|
# Copyright (C) 2009-2010 Mathias Brodala
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import copy
import glib
import gobject
import gtk
import pango
from xl import (
event,
formatter,
player,
settings
)
from xl.nls import gettext as _
from xlgui import main
from xlgui.guiutil import (
get_workarea_size,
ProgressBarFormatter
)
from xlgui.playlist import Playlist
from xlgui.widgets import info, rating
class MenuItem(gtk.ImageMenuItem):
"""
Convenience wrapper, allows switching to mini mode
"""
def __init__(self, callback):
gtk.ImageMenuItem.__init__(self, stock_id='exaile-minimode')
self.child.set_label(_('Mini Mode'))
self._activate_id = self.connect('activate', callback)
def destroy(self):
"""
Various cleanups
"""
self.disconnect(self._activate_id)
gtk.ImageMenuItem.destroy(self)
class KeyExistsError(KeyError):
def __init__(self, error):
KeyError.__init__(self, error)
class WidgetBox(gtk.HBox):
"""
Wrapper class, allows for identification
of and simple access to contained widgets
Keeps track of registered types
"""
def __init__(self, homogeneous=False, spacing=0):
gtk.HBox.__init__(self, homogeneous, spacing)
self.__register = {}
self.__widgets = {}
def destroy(self):
"""
Various cleanups
"""
for id, widget in self.__widgets.iteritems():
widget.destroy()
gtk.HBox.destroy(self)
def register_widget(self, id, type, arguments=[]):
"""
Registers a widget
"""
if id in self.__register:
raise KeyExistsError, id
self.__register[id] = (type, arguments)
def register_widgets(self, widgets):
"""
Registers multiple widgets at once
"""
for id, (type, arguments) in widgets.iteritems():
self.register_widget(id, type, arguments)
def add_widget(self, id):
"""
Adds a widget to the box, moves
it to the end if already present
"""
if id not in self.__register:
raise KeyError, id
if id in self.__widgets:
widget = self.__widgets[id]
self.reorder_child(widget, -1)
else:
type, arguments = self.__register[id]
widget = type(*arguments)
self.__widgets[id] = widget
self.pack_start(widget, expand=False, fill=False)
def remove_widget(self, id):
"""
Removes a widget from the box
"""
if id not in self.__register:
raise KeyError, id
if id not in self.__widgets:
raise KeyError, id
self.remove(self.__widgets[id])
self.__widgets[id].destroy()
del self.__widgets[id]
def update_widgets(self, ids):
"""
Updates display and order of widgets
"""
for id in self.__register.iterkeys():
if id not in ids:
try:
self.remove_widget(id)
except KeyError:
pass
for id in ids:
try:
self.add_widget(id)
except KeyError:
pass
def __getitem__(self, id):
"""
Returns a widget, allows for box['id']
"""
return self.__widgets[id]
class Button(gtk.Button):
"""
Convenience wrapper around gtk.Button
"""
def __init__(self, stock_id, tooltip_text, callback):
gtk.Button.__init__(self)
self.image = gtk.image_new_from_stock(stock_id, gtk.ICON_SIZE_BUTTON)
self.set_image(self.image)
self.set_tooltip_text(tooltip_text)
self.set_relief(gtk.RELIEF_NONE)
self.set_focus_on_click(False)
if callback is not None:
self.connect('clicked', callback)
class PlayPauseButton(Button):
"""
Button which automatically sets its appearance
depending on the current playback state
"""
def __init__(self, player, callback):
Button.__init__(self, gtk.STOCK_MEDIA_PLAY,
_('Start Playback'), callback)
self.player = player
self.update_state()
event.add_callback(self.on_playback_state_change, 'playback_player_end')
event.add_callback(self.on_playback_state_change, 'playback_track_start')
event.add_callback(self.on_playback_state_change, 'playback_toggle_pause')
def destroy(self):
"""
Various cleanups
"""
event.remove_callback(self.on_playback_state_change, 'playback_player_end')
event.remove_callback(self.on_playback_state_change, 'playback_track_start')
event.remove_callback(self.on_playback_state_change, 'playback_toggle_pause')
Button.destroy(self)
def update_state(self):
"""
Updates the appearance of this button
"""
stock_id = gtk.STOCK_MEDIA_PLAY
tooltip_text = _('Start Playback')
if self.player.current is not None:
if self.player.is_paused():
tooltip_text = _('Continue Playback')
elif self.player.is_playing():
stock_id = gtk.STOCK_MEDIA_PAUSE
tooltip_text = _('Pause Playback')
self.image.set_from_stock(stock_id, gtk.ICON_SIZE_BUTTON)
self.set_tooltip_text(tooltip_text)
def on_playback_state_change(self, event, player, track):
"""
Updates appearance on playback state change
"""
self.update_state()
class StopButton(Button):
"""
Button which allows to stop the playback
as well as trigger the SPAT feature
"""
def __init__(self, player, queue):
Button.__init__(self, gtk.STOCK_MEDIA_STOP,
_('Stop Playback\n\n'
'Right Click for Stop After Track Feature'), None)
self.player = player
self.queue = queue
self.add_events(gtk.gdk.POINTER_MOTION_MASK)
self.connect('motion-notify-event',
self.on_motion_notify_event)
self.connect('leave-notify-event',
self.on_leave_notify_event)
self.connect('key-press-event',
self.on_key_press_event)
self.connect('key-release-event',
self.on_key_release_event)
self.connect('focus-out-event',
self.on_focus_out_event)
self.connect('button-press-event',
self.on_button_press_event)
def on_motion_notify_event(self, widget, event):
"""
Sets the hover state and shows SPAT icon
"""
self.set_data('hovered', True)
if event.state & gtk.gdk.SHIFT_MASK:
self.set_image(gtk.image_new_from_stock(
gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON))
else:
self.set_image(gtk.image_new_from_stock(
gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_BUTTON))
def on_leave_notify_event(self, widget, event):
"""
Unsets the hover state and resets the button icon
"""
self.set_data('hovered', False)
if not self.is_focus() and \
~(event.state & gtk.gdk.SHIFT_MASK):
self.set_image(gtk.image_new_from_stock(
gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_BUTTON))
def on_key_press_event(self, widget, event):
"""
Shows SPAT icon on Shift key press
"""
if event.keyval in (gtk.keysyms.Shift_L, gtk.keysyms.Shift_R):
self.set_image(gtk.image_new_from_stock(
gtk.STOCK_STOP, gtk.ICON_SIZE_BUTTON))
self.set_data('toggle_spat', True)
if event.keyval in (gtk.keysyms.space, gtk.keysyms.Return):
if self.get_data('toggle_spat'):
self.set_spat()
else:
self.player.stop()
pass
def on_key_release_event(self, widget, event):
"""
Resets the button icon
"""
if event.keyval in (gtk.keysyms.Shift_L, gtk.keysyms.Shift_R):
self.set_image(gtk.image_new_from_stock(
gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_BUTTON))
self.set_data('toggle_spat', False)
def on_focus_out_event(self, widget, event):
"""
Resets the button icon unless
the button is still hovered
"""
if not self.get_data('hovered'):
self.set_image(gtk.image_new_from_stock(
gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_BUTTON))
def on_button_press_event(self, widget, event):
"""
Called when the user clicks on the stop button
"""
if event.button == 1:
if event.state & gtk.gdk.SHIFT_MASK:
self.set_spat()
else:
self.player.stop()
elif event.button == 3:
menu = guiutil.Menu()
menu.append(_("Toggle: Stop after Selected Track"),
self.clicked,
gtk.STOCK_STOP)
menu.popup(None, None, None, event.button, event.time)
def set_spat(self):
"""
Called when the user clicks on the SPAT item
"""
tracks = main.get_selected_playlist().get_selected_tracks()
if not tracks:
return
track = tracks[0]
if track == self.queue.stop_track:
self.queue.stop_track = None
else:
self.queue.stop_track = track
main.get_selected_playlist().list.queue_draw()
class RestoreButton(Button):
"""
Button which allows to restore the main window
"""
def __init__(self, accel_group, callback):
Button.__init__(self, gtk.STOCK_LEAVE_FULLSCREEN,
_('Restore main window'), callback)
key, modifier = gtk.accelerator_parse('<Control><Alt>M')
self.add_accelerator('clicked', accel_group,
key, modifier, gtk.ACCEL_VISIBLE)
class VolumeButton(gtk.VolumeButton):
"""
Wrapper class around gtk.VolumeButton
"""
def __init__(self, player, callback):
gtk.VolumeButton.__init__(self)
self.player = player
adjustment = gtk.Adjustment(upper=1, step_incr=0.1, page_incr=0.2)
self.set_adjustment(adjustment)
self._changed_callback = callback
self._value_changed_id = self.connect('value-changed', self.on_change)
self._expose_event_id = self.connect('expose-event', self.on_expose)
def destroy(self):
"""
Various cleanups
"""
self.disconnect(self._value_changed_id)
self.disconnect(self._expose_event_id)
gtk.VolumeButton.destroy(self)
def on_change(self, *e):
"""
Wrapper function to prevent race conditions
"""
if not self._updating:
self._changed_callback(*e)
def on_expose(self, volume_button, event):
"""
Updates value on repaint requests
"""
self._updating = True
self.set_value(self.player.get_volume() / 100.0)
self._updating = False
class AttachedWindow(gtk.Window):
"""
A window attachable to arbitrary widgets,
follows the movement of its parent
"""
def __init__(self, parent):
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
self.set_decorated(False)
self.set_property('skip-taskbar-hint', True)
self.realize()
self.window.set_functions(gtk.gdk.FUNC_RESIZE) # Only allow resizing
self.parent_widget = parent
self._configure_id = None
self._parent_realize_id = self.parent_widget.connect(
'realize', self.on_parent_realize)
def destroy(self):
"""
Various cleanups
"""
if self._configure_id is not None:
self.disconnect(self._configure_id)
self.disconnect(self._parent_realize_id)
gtk.Window.destroy(self)
def update_location(self):
"""
Makes sure the window is
always fully visible
"""
workarea_width, workarea_height = get_workarea_size() # 1280, 974
width, height = self.size_request() # 350, 400
# FIXME: AttributeError: 'NoneType' object has no attribute 'get_origin'
parent_window_x, parent_window_y = self.parent_widget.get_window().get_origin()
parent_x, parent_y, parent_width, parent_height = self.parent_widget.get_allocation()
parent_x, parent_y = parent_window_x + parent_x, parent_window_y + parent_y
# E.g. 1280 - 1000 < 350
if workarea_width - parent_x < width:
# 1000 + 150 - 350 = 800
x = parent_x + parent_width - width # Aligned right
else:
x = parent_x # Aligned left
# E.g. 974 - 800 < 400
if workarea_height - parent_y < height:
# 800 - 400 = 400
y = parent_y - height # Aligned top
else:
y = parent_y + parent_height # Aligned bottom
self.move(x, y)
def on_parent_realize(self, parent):
"""
Prepares the window to
follow its parent window
"""
if self._configure_id is None:
self._configure_id = parent.get_toplevel().connect(
'configure-event', self.on_parent_window_configure)
def on_parent_window_configure(self, *e):
"""
Handles movement of the topmost window
"""
self.update_location()
class PlaylistButton(gtk.ToggleButton):
"""
Displays the current track title and
the current playlist on activation
Also allows for drag and drop of files
to add them to the playlist
"""
__gsignals__ = {
'track-changed': (
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_PYOBJECT, )
)
}
def __init__(self, main, player, queue, formatter, change_callback):
gtk.ToggleButton.__init__(self, '')
self.set_focus_on_click(False)
self.set_size_request(150, -1)
box = gtk.HBox()
self.arrow = gtk.Arrow(gtk.ARROW_RIGHT, gtk.SHADOW_OUT)
box.pack_start(self.arrow, expand=False)
self.label = gtk.Label('')
self.label.set_property('ellipsize', pango.ELLIPSIZE_END)
box.pack_start(self.label)
self.remove(self.child)
self.add(box)
self.main = main
self.formatter = formatter
playlist = self.main.get_selected_playlist()
self.playlist = Playlist(main, queue, playlist.playlist)
self.playlist.model = playlist.model
self.playlist.list.set_model(self.playlist.model)
self.playlist.scroll.set_property('shadow-type', gtk.SHADOW_IN)
self.popup = AttachedWindow(self)
self.popup.resize(
settings.get_option('plugin/minimode/playlist_button_popup_width', 350),
settings.get_option('plugin/minimode/playlist_button_popup_height', 400)
)
self.popup.add(self.playlist)
self.popup.connect('configure-event', self.on_popup_configure_event)
self.tooltip = info.TrackToolTip(self, auto_update=True)
self._dirty = False
self._drag_shown = False
self._parent_hide_id = None
self._drag_motion_id = None
self.drag_dest_set(gtk.DEST_DEFAULT_ALL,
self.playlist.list.targets,
gtk.gdk.ACTION_COPY |
gtk.gdk.ACTION_DEFAULT |
gtk.gdk.ACTION_MOVE)
self.connect('track-changed', change_callback)
self.connect('scroll-event', self.on_scroll)
self.connect('toggled', self.on_toggled)
self.connect('drag-leave', self.on_drag_leave)
self.connect('drag-motion', self.on_drag_motion)
self.playlist.list.connect('drag-data-received',
self.on_playlist_drag_data_received)
self.playlist.connect('track-count-changed',
self.on_track_count_changed)
self.main.playlist_notebook.connect('switch-page',
self.on_playlist_notebook_switch)
self.formatter.connect('format-changed',
self.on_format_changed)
event.add_callback(self.on_playback_player_end,
'playback_player_end')
event.add_callback(self.on_playback_track_start,
'playback_track_start')
if player.current is not None:
self.on_playback_track_start('playback_track_start',
player, player.current)
def destroy(self):
"""
Various cleanups
"""
event.remove_callback(self.on_playback_player_end, 'playback_player_end')
event.remove_callback(self.on_playback_track_start, 'playback_track_start')
gtk.ToggleButton.destroy(self)
def set_label(self, text):
"""
Sets the label of the button
"""
self.label.set_text(text)
def set_arrow_direction(self, direction):
"""
Sets the direction of the arrow
"""
self.arrow.set(direction, gtk.SHADOW_OUT)
def on_format_changed(self, formatter, format):
"""
Updates the playlist button
on track title format changes
"""
track = self.playlist.playlist.get_current()
if track:
self.set_label(self.formatter.format(track))
def on_playback_player_end(self, event, player, track):
"""
Clears label
"""
self.set_label('')
def on_playback_track_start(self, event, player, track):
"""
Updates appearance and cursor position
"""
self.set_label(self.formatter.format(track))
if track in self.playlist.playlist.ordered_tracks:
path = (self.playlist.playlist.index(track),)
if settings.get_option('gui/ensure_visible', True):
self.playlist.list.scroll_to_cell(path)
glib.idle_add(self.playlist.list.set_cursor, path)
def on_parent_hide(self, parent):
"""
Makes sure to hide the popup
"""
self.set_active(False)
def on_scroll(self, togglebutton, event):
"""
Switches tracks on scrolling
"""
track = None
if event.direction == gtk.gdk.SCROLL_UP:
track = self.playlist.playlist.prev()
elif event.direction == gtk.gdk.SCROLL_DOWN:
track = self.playlist.playlist.next()
self.emit('track-changed', track)
def on_toggled(self, togglebutton):
"""
Displays or hides the playlist on toggle
"""
if self.popup.get_transient_for() is None:
self.popup.set_transient_for(self.get_toplevel())
if self._parent_hide_id is None:
self._parent_hide_id = self.get_toplevel().connect(
'hide', self.on_parent_hide)
if self.get_active():
self.set_arrow_direction(gtk.ARROW_DOWN)
self.popup.update_location()
self.popup.show()
else:
self.popup.hide()
self.set_arrow_direction(gtk.ARROW_RIGHT)
def on_drag_leave(self, togglebutton, context, timestamp):
"""
Prevents showing the playlist if the
pointer leaves the button prematurely
"""
if self._drag_motion_id is not None:
glib.source_remove(self._drag_motion_id)
self._drag_motion_id = None
def on_drag_motion(self, togglebutton, context, x, y, timestamp):
"""
Sets up a timeout to show the playlist
"""
if self._drag_motion_id is None:
self._drag_motion_id = glib.timeout_add(500,
self.drag_motion_finish)
def drag_motion_finish(self):
"""
Shows the playlist
"""
self.set_active(True)
self._drag_shown = True
def on_playlist_drag_data_received(self, *e):
"""
Hides the playlist if it has
been opened via drag events
"""
if self._drag_shown:
self.set_active(False)
self._drag_shown = False
def on_track_count_changed(self, playlist, track_count):
"""
Synchronizes models
"""
self.playlist.model = playlist.model
self.playlist.list.set_model(self.playlist.model)
def on_playlist_notebook_switch(self, notebook, page, page_num):
"""
Updates the internal playlist
"""
playlist = notebook.get_nth_page(page_num)
if playlist is not None:
self.playlist.model = playlist.model
self.playlist.list.set_model(self.playlist.model)
def on_popup_configure_event(self, widget, event):
"""
Saves the window size after resizing
"""
width = settings.get_option(
'plugin/minimode/playlist_button_popup_width', 350)
height = settings.get_option(
'plugin/minimode/playlist_button_popup_height', 400)
if event.width != width:
settings.set_option('plugin/minimode/playlist_button_popup_width',
event.width)
if event.height != height:
settings.set_option('plugin/minimode/playlist_button_popup_height',
event.height)
class RatingWidget(rating.RatingWidget):
"""
A wrapper which updates the current
track on rating selection
"""
def __init__(self):
rating.RatingWidget.__init__(self)
self.connect('rating-changed', self.on_rating_changed)
def on_rating_changed(self, widget, rating):
"""
Updates the rating of the currently playing track
"""
if self.player.current is not None:
self.player.current.set_rating(rating)
maximum = settings.get_option('rating/maximum', 5)
event.log_event('rating_changed', self, rating / maximum * 100)
class ProgressButton(gtk.EventBox):
"""
A button which allows displaying a playlist
via click and seeking via right-click.
Scrolling to switch tracks is also supported
as well as dropping media files to add them
to the playlist. Behold!
"""
__gsignals__ = {
'seeked': (
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, )
),
'track-changed': (
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_PYOBJECT, )
)
}
def __init__(self, change_callback, seek_callback):
gtk.EventBox.__init__(self)
self.set_visible_window(False)
self.set_above_child(True)
self.set_size_request(300, -1)
self._dirty = False
self._drag_shown = False
self._parent_hide_id = None
self._drag_motion_id = None
self._timer = None
title_format = settings.get_option(
'plugin/minimode/progress_button_title_format',
_('$title ($current_time / $total_time)'))
self.track_formatter = formatter.TrackFormatter('')
self.progress_formatter = formatter.ProgressTextFormatter(title_format)
self.button = gtk.ToggleButton()
self.arrow = gtk.Arrow(gtk.ARROW_RIGHT, gtk.SHADOW_IN)
self.progress = gtk.ProgressBar()
self.progress.set_size_request(self.progress.size_request()[0], 1)
self.progress.set_text(_('Not Playing'))
self.progress.set_ellipsize(pango.ELLIPSIZE_END)
self.progress.add_events(gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.POINTER_MOTION_MASK)
self.tooltip = info.TrackToolTip(self, auto_update=True)
playlist = main.get_selected_playlist()
self.playlist = Playlist(main.mainwindow(), player.QUEUE, playlist.playlist)
self.playlist.model = playlist.model
self.playlist.list.set_model(self.playlist.model)
self.playlist.scroll.props.shadow_type = gtk.SHADOW_IN
self.popup = AttachedWindow(self)
self.popup.resize(
settings.get_option('plugin/minimode/progress_button_popup_width', 350),
settings.get_option('plugin/minimode/progress_button_popup_height', 400)
)
self.popup.add(self.playlist)
self.popup.connect('configure-event', self.on_popup_configure_event)
self.tooltip = info.TrackToolTip(self, auto_update=True)
self.drag_dest_set(gtk.DEST_DEFAULT_ALL,
self.playlist.list.targets,
gtk.gdk.ACTION_COPY |
gtk.gdk.ACTION_DEFAULT |
gtk.gdk.ACTION_MOVE)
box = gtk.HBox(spacing=6)
box.pack_start(self.arrow, expand=False)
box.pack_start(self.progress)
self.button.add(box)
self.add(self.button)
self.connect('button-press-event', self.on_button_press_event)
self.connect('button-release-event', self.on_button_release_event)
self.connect('motion-notify-event', self.on_motion_notify_event)
self.connect('track-changed', change_callback)
self.connect('seeked', seek_callback)
self.connect('scroll-event', self.on_scroll_event)
self.connect('drag-leave', self.on_drag_leave)
self.connect('drag-motion', self.on_drag_motion)
self.button.connect('toggled', self.on_button_toggled)
self.playlist.list.connect('drag-data-received',
self.on_playlist_drag_data_received)
self.playlist.connect('track-count-changed',
self.on_track_count_changed)
main.get_playlist_notebook().connect('switch-page',
self.on_playlist_notebook_switch)
event.add_callback(self.on_playback_track_start,
'playback_track_start')
event.add_callback(self.on_playback_player_end,
'playback_player_end')
event.add_callback(self.on_option_set,
'plugin_minimode_option_set')
if player.PLAYER.current is not None:
self.on_playback_track_start('playback_track_start',
player.PLAYER, player.PLAYER.current)
def destroy():
"""
Various cleanups
"""
event.remove_callback(self.on_playback_player_end, 'playback_player_end')
event.remove_callback(self.on_playback_track_start, 'playback_track_start')
gtk.EventBox.destroy(self)
def translate_coordinates(self, child, x, y, clamp=True):
"""
Translates coordinates to child coordinates
:param clamp: Use allocation to limit the values
"""
x, y = gtk.EventBox.translate_coordinates(self, child, x, y)
if clamp:
allocation = child.get_allocation()
x = max(0, x)
x = min(x, allocation.width)
y = max(0, y)
y = min(y, allocation.height)
return x, y
def set_label_from_track(self, track):
"""
Updates label based on data taken from track
:param track: the track object
:type track: :class:`xl.trax.Track`
"""
if not self.get_data('seeking'):
text = _('Not Playing')
if track is not None:
text = self.progress_formatter.format()
self.track_formatter.props.format = text
text = self.track_formatter.format(player.PLAYER.current)
self.progress.set_fraction(player.PLAYER.get_progress())
self.progress.set_text(text)
return True
def enable_timer(self):
"""
Enables the timer, if not already
"""
if self._timer is not None:
return
milliseconds = settings.get_option('gui/progress_update/millisecs', 1000)
if milliseconds % 1000 == 0:
self._timer = glib.timeout_add_seconds(milliseconds / 1000,
self.set_label_from_track, player.PLAYER.current)
else:
self._timer = glib.timeout_add(milliseconds,
self.set_label_from_track, player.PLAYER.current)
def disable_timer(self):
"""
Disables the timer, if not already
"""
if self._timer is None:
return
glib.source_remove(self._timer)
self._timer = None
def on_button_press_event(self, widget, event):
"""
Shows the attached window or starts
seeking to the desired position
"""
if event.button == 1:
self.button.set_active(not self.button.get_active())
self.set_data('seeking', False)
elif event.button == 3:
if player.PLAYER.current is None:
return True
allocation = self.progress.get_allocation()
x, y = self.translate_coordinates(
self.progress, int(event.x), int(event.y))
fraction = float(x) / allocation.width
self.progress.set_fraction(fraction)
length = player.PLAYER.current.get_tag_raw('__length')
seek_position = formatter.LengthTagFormatter.format_value(length * fraction)
self.progress.set_text(_('Seeking: %s') % seek_position)
self.set_data('seeking', True)
def on_button_release_event(self, widget, event):
"""
Seeks to the desired position
"""
if event.button == 3 and self.get_data('seeking'):
allocation = self.progress.get_allocation()
x, y = self.translate_coordinates(
self.progress, int(event.x), int(event.y))
self.emit('seeked', float(x) / allocation.width)
self.set_data('seeking', False)
def on_motion_notify_event(self, widget, event):
"""
Visualizes seeking
"""
if self.get_data('seeking'):
press_event = gtk.gdk.Event(gtk.gdk.BUTTON_PRESS)
press_event.button = 3
press_event.x = event.x
press_event.y = event.y
self.emit('button-press-event', press_event)
def on_scroll_event(self, widget, event):
"""
Switches to the previous or next track
"""
track = None
if event.direction == gtk.gdk.SCROLL_UP:
track = self.playlist.playlist.prev()
elif event.direction == gtk.gdk.SCROLL_DOWN:
track = self.playlist.playlist.next()
self.emit('track-changed', track)
def on_drag_leave(self, widget, event):
"""
Prevents showing the playlist if the
pointer leaves the button prematurely
"""
if self._drag_motion_id is not None:
glib.source_remove(self._drag_motion_id)
self._drag_motion_id = None
def on_drag_motion(self, widget, event):
"""
Sets up a timeout to show the playlist
"""
if self._drag_motion_id is None:
self._drag_motion_id = glib.timeout_add(500,
self.drag_motion_finish)
def drag_motion_finish(self):
"""
Shows the playlist
"""
self.set_active(True)
self._drag_shown = True
def on_playlist_drag_data_received(self, *e):
"""
Hides the playlist if it has
been opened via drag events
"""
if self._drag_shown:
self.set_active(False)
self._drag_shown = False
def on_track_count_changed(self, playlist, track_count):
"""
Synchronizes models
"""
self.playlist.model = playlist.model
self.playlist.list.set_model(self.playlist.model)
def on_playlist_notebook_switch(self, notebook, page, page_num):
"""
Updates the internal playlist
"""
playlist = notebook.get_nth_page(page_num)
if playlist is not None:
self.playlist.model = playlist.model
self.playlist.list.set_model(self.playlist.model)
def on_button_toggled(self, button):
"""
Changes the arrow and shows
or hides the attached window
"""
if self.popup.get_transient_for() is None:
self.popup.set_transient_for(self.get_toplevel())
if self._parent_hide_id is None:
self._parent_hide_id = self.get_toplevel().connect(
'hide', self.on_parent_hide)
if button.get_active():
self.arrow.props.arrow_type = gtk.ARROW_DOWN
self.popup.update_location()
self.popup.show()
else:
self.popup.hide()
self.arrow.props.arrow_type = gtk.ARROW_RIGHT
def on_popup_configure_event(self, widget, event):
"""
Saves the window size after resizing
"""
width = settings.get_option(
'plugin/minimode/progress_button_popup_width', 350)
height = settings.get_option(
'plugin/minimode/progress_button_popup_height', 400)
if event.width != width:
settings.set_option('plugin/minimode/progress_button_popup_width',
event.width)
if event.height != height:
settings.set_option('plugin/minimode/progress_button_popup_height',
event.height)
def on_parent_hide(self, parent):
"""
Makes sure to hide the popup
"""
self.button.set_active(False)
def on_format_changed(self, formatter, format):
"""
Updates the playlist button
on track title format changes
"""
track = self.playlist.playlist.get_current()
if track:
self.set_label_from_track(track)
def on_playback_track_start(self, event, player, track):
"""
Updates appearance and cursor position
"""
self.set_label_from_track(track)
self.enable_timer()
if track in self.playlist.playlist.ordered_tracks:
path = (self.playlist.playlist.index(track),)
if settings.get_option('gui/ensure_visible', True):
self.playlist.list.scroll_to_cell(path)
glib.idle_add(self.playlist.list.set_cursor, path)
def on_playback_player_end(self, event, player, track):
"""
Clears label
"""
self.disable_timer()
self.set_label_from_track(None)
def on_option_set(self, event, settings, option):
"""
Updates the title format
"""
if option == 'plugin/minimode/progress_button_title_format':
title_format = settings.get_option(option,
_('$title ($current_time / $total_time)'))
self.progress_formatter.props.format = title_format
self.set_label_from_track(player.PLAYER.current)
class TrackSelector(gtk.ComboBox):
"""
Control which updates its content automatically
on playlist actions, track display is configurable
"""
__gsignals__ = {
'track-changed': (
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_PYOBJECT, )
)
}
def __init__(self, main, queue, formatter, changed_callback):
gtk.ComboBox.__init__(self)
self.main = main
self.queue = queue
self.formatter = formatter
self.list = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
self.set_model(self.list)
self.set_size_request(150, 0)
textrenderer = gtk.CellRendererText()
self.pack_start(textrenderer, expand=True)
self.set_attributes(textrenderer, text=0)
self.set_cell_data_func(textrenderer, self.text_data_func)
self._updating = False
self._dirty = False
self.update_track_list(self.queue.current_playlist)
self._track_changed_id = self.connect('track-changed',
changed_callback)
self._expose_event_id = self.connect('expose-event',
self.on_expose)
self._changed_id = self.connect('changed',
self.on_change)
self._switch_page_id = self.main.playlist_notebook.connect('switch-page',
self.on_playlist_notebook_switch)
self._format_changed_id = self.formatter.connect('format-changed',
self.on_format_changed)
event.add_callback(self.on_playlist_current_changed, 'playlist_current_changed')
event.add_callback(self.on_tracks_added, 'tracks_added')
event.add_callback(self.on_tracks_removed, 'tracks_removed')
event.add_callback(self.on_tracks_reordered, 'tracks_reordered')
def destroy(self):
"""
Various cleanups
"""
self.disconnect(self._track_changed_id)
self.disconnect(self._expose_event_id)
self.disconnect(self._changed_id)
self.main.playlist_notebook.disconnect(self._switch_page_id)
self.formatter.disconnect(self._format_changed_id)
event.remove_callback(self.on_playlist_current_changed, 'playlist_current_changed')
event.remove_callback(self.on_tracks_added, 'tracks_added')
event.remove_callback(self.on_tracks_removed, 'tracks_removed')
event.remove_callback(self.on_tracks_reordered, 'tracks_reordered')
gtk.ComboBox.destroy(self)
def update_track_list(self, playlist=None, tracks=None):
"""
Populates the track list based
on the current playlist
"""
if playlist is None:
playlist = self.queue.current_playlist
if playlist is None:
self.list.clear()
return
tracks = playlist.get_tracks()
if tracks is None:
tracks = playlist.get_tracks()
current_track = playlist.get_current()
self._updating = True
self.list.clear()
for track in tracks:
iter = self.list.append([track, self.formatter.format(track)])
if track == current_track:
self.set_active_iter(iter)
self._updating = False
def get_active_track(self):
"""
Returns the currently selected track
"""
try:
iter = self.get_active_iter()
return self.list.get_value(iter, 0)
except TypeError:
return None
def set_active_track(self, active_track):
"""
Sets the currently selected track
"""
iter = self.list.get_iter_first()
self._updating = True
for row in self.list:
if row[0] == active_track:
self.set_active_iter(row.iter)
self._updating = False
def text_data_func(self, celllayout, cell, model, iter):
"""
Updates track titles and highlights
the current track if the popup is shown
"""
active_iter = self.get_active_iter()
if active_iter is not None:
track = model.get_value(iter, 1)
active_track = model.get_value(active_iter, 1)
weight = pango.WEIGHT_NORMAL
if self.get_property('popup-shown'):
if track == active_track:
weight = pango.WEIGHT_BOLD
cell.set_property('weight', weight)
def on_expose(self, widget, event):
"""
Performs deferred tasks
"""
if self._dirty:
self.update_track_list()
self._dirty = False
def on_change(self, *e):
"""
Wrapper function to prevent race conditions
"""
if not self._updating:
self.emit('track-changed', self.get_active_track())
def on_format_changed(self, formatter, format):
"""
Updates the track list
on track title format changes
"""
self.update_track_list()
def on_playlist_current_changed(self, event, playlist, track):
"""
Updates the currently selected track
"""
self.set_active_track(track)
def on_tracks_added(self, event, playlist, tracks):
"""
Triggers update of the track list on track addition
"""
if self.get_toplevel().get_property('visible'):
self.update_track_list(playlist, tracks)
else:
self._dirty = True
def on_tracks_removed(self, event, playlist, (start, end, removed)):
"""
Triggers update of the track list on track removal
"""
if self.get_toplevel().get_property('visible'):
self.update_track_list(playlist)
else:
self._dirty = True
def on_tracks_reordered(self, event, playlist, tracks):
"""
Triggers update of the track list on track reordering
"""
if self.get_toplevel().get_property('visible'):
self.update_track_list(playlist, tracks)
else:
self._dirty = True
def on_playlist_notebook_switch(self, notebook, page, page_num):
"""
Updates the internal playlist
"""
if self.get_toplevel().get_property('visible'):
page = notebook.get_nth_page(page_num)
if page is not None:
self.update_track_list(page.playlist)
else:
self._dirty = True
class ProgressBar(gtk.Alignment):
"""
Wrapper class which updates itself
based on the current track
"""
__gsignals__ = {
'seeked': (
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, )
)
}
def __init__(self, player, callback):
gtk.Alignment.__init__(self, xscale=1, yscale=1)
self.set_padding(3, 3, 0, 0)
self.bar = gtk.ProgressBar()
self.bar.set_size_request(150, -1)
self.add(self.bar)
self.player = player
self.formatter = ProgressBarFormatter()
self._timer = None
self._press_event = None
self.update_state()
self.bar.add_events(gtk.gdk.BUTTON_PRESS_MASK |
gtk.gdk.BUTTON_RELEASE_MASK |
gtk.gdk.POINTER_MOTION_MASK)
self._seeked_id = self.connect('seeked',
callback)
self._button_press_event_id = self.bar.connect('button-press-event',
self.on_button_press)
self._button_release_event_id = self.bar.connect('button-release-event',
self.on_button_release)
self._motion_notify_event_id = self.bar.connect('motion-notify-event',
self.on_motion_notify)
event.add_callback(self.on_playback_state_change, 'playback_track_start')
event.add_callback(self.on_playback_state_change, 'playback_toggle_pause')
event.add_callback(self.on_playback_state_change, 'playback_track_end')
def destroy(self):
"""
Various cleanups
"""
event.remove_callback(self.on_playback_state_change, 'playback_track_start')
event.remove_callback(self.on_playback_state_change, 'playback_toggle_pause')
event.remove_callback(self.on_playback_state_change, 'playback_track_end')
self.disconnect(self._seeked_id)
self.bar.disconnect(self._button_press_event_id)
self.bar.disconnect(self._button_release_event_id)
self.bar.disconnect(self._motion_notify_event_id)
gtk.Alignment.destroy(self)
def update_state(self):
"""
Updates the appearance of this progress bar
"""
fraction = 0.0
text = _('Not Playing')
track = self.player.current
if track is not None:
total = track.get_tag_raw('__length')
if total is not None:
text = self.formatter.format()
if self.player.is_paused():
self.disable_timer()
fraction = self.bar.get_fraction()
elif self.player.is_playing():
self.enable_timer()
fraction = self.player.get_progress()
elif not track.is_local():
self.disable_timer()
text = _('Streaming...')
self.bar.set_fraction(fraction)
self.bar.set_text(text)
return True
def enable_timer(self):
"""
Enables the timer, if not already
"""
if self._timer is not None:
return
milliseconds = settings.get_option('gui/progress_update/millisecs', 1000)
if milliseconds % 1000 == 0:
self._timer = glib.timeout_add_seconds(milliseconds / 1000,
self.update_state)
else:
self._timer = glib.timeout_add(milliseconds,
self.update_state)
def disable_timer(self):
"""
Disables the timer, if not already
"""
if self._timer is None:
return
glib.source_remove(self._timer)
self._timer = None
def on_playback_state_change(self, event, player, track):
"""
Updates appearance on playback state change
"""
self.update_state()
def on_button_press(self, widget, event):
"""
Prepares seeking
"""
if self.player.current is None:
return True
if not self.player.current.is_local() and \
not self.player.current.get_tag_raw('__length'):
return True
if event.button == 1:
self._press_event = event.copy()
def on_button_release(self, widget, event):
"""
Completes seeking and emits the 'seeked' signal
"""
if self._press_event is None:
return True
posx, posy, width, height = self.bar.get_allocation()
event.x = float(max(0, event.x))
event.x = float(min(event.x, width - 1))
self.bar.set_fraction(event.x / width)
self.emit('seeked', event.x / width)
self._press_event = None
self.enable_timer()
def on_motion_notify(self, widget, event):
"""
Visually changes the current seeking
status in the progress bar
"""
if self._press_event is None:
return True
posx, posy, width, height = self.bar.get_allocation()
event.x = float(max(0, event.x))
event.x = float(min(event.x, width))
if event.x == self._press_event.x:
return True
self.disable_timer()
total = self.player.current.get_tag_raw('__length')
seekpos = (event.x / width) * total
text = _('Seeking: ')
text += '%d:%02d' % (seekpos // 60, seekpos % 60)
self.bar.set_fraction(event.x / width)
self.bar.set_text(text)
# vim: et sts=4 sw=4
|