forked from fossfreedom/coverart-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverart_widgets.py
1525 lines (1163 loc) · 48.9 KB
/
coverart_widgets.py
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
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2012 - fossfreedom
# Copyright (C) 2012 - Agustin Carrasco
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
from gi.repository import RB
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Notify
import cairo
from coverart_browser_prefs import GSetting
from coverart_external_plugins import ExternalPlugin
import rb
def enum(**enums):
return type('Enum', (object,), enums)
class OptionsWidget(Gtk.Widget):
def __init__(self, *args, **kwargs):
super(OptionsWidget, self).__init__(*args, **kwargs)
self._controller = None
@property
def controller(self):
return self._controller
@controller.setter
def controller(self, controller):
if self._controller:
# disconnect signals
self._controller.disconnect(self._options_changed_id)
self._controller.disconnect(self._current_key_changed_id)
self._controller.disconnect(self._update_image_changed_id)
self._controller = controller
# connect signals
self._options_changed_id = self._controller.connect('notify::options',
self._update_options)
self._current_key_changed_id = self._controller.connect(
'notify::current-key', self._update_current_key)
self._update_image_changed_id = self._controller.connect(
'notify::update-image', self._update_image)
self._visible_changed_id = self._controller.connect(
'notify::enabled', self._update_visibility)
# update the menu and current key
self.update_options()
self.update_current_key()
def _update_visibility(self, *args):
self.set_visible(self._controller.enabled)
def _update_options(self, *args):
self.update_options()
def update_options(self):
pass
def _update_current_key(self, *args):
self.update_current_key()
def update_current_key():
pass
def _update_image(self, *args):
self.update_image()
def update_image(self):
pass
def calc_popup_position(self, widget):
# this calculates the popup positioning - algorithm taken
# from Gtk3.8 gtk/gtkmenubutton.c
toplevel = self.get_toplevel()
toplevel.set_type_hint(Gdk.WindowTypeHint.DROPDOWN_MENU)
menu_req, pref_req = widget.get_preferred_size()
align = widget.get_halign()
direction = self.get_direction()
window = self.get_window()
screen = widget.get_screen()
monitor_num = screen.get_monitor_at_window(window)
if (monitor_num < 0):
monitor_num = 0
monitor = screen.get_monitor_workarea(monitor_num)
allocation = self.get_allocation()
ret, x, y = window.get_origin()
x += allocation.x
y += allocation.y
if allocation.width - menu_req.width > 0:
x += allocation.width - menu_req.width
if ((y + allocation.height + menu_req.height) <= monitor.y + monitor.height):
y += allocation.height
elif ((y - menu_req.height) >= monitor.y):
y -= menu_req.height
else:
y -= menu_req.height
return x, y
class OptionsPopupWidget(OptionsWidget):
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,))
}
def __init__(self, *args, **kwargs):
OptionsWidget.__init__(self, *args, **kwargs)
self._popup_menu = Gtk.Menu()
def update_options(self):
self.clear_popupmenu()
for key in self._controller.options:
self.add_menuitem(key)
def update_current_key(self):
# select the item if it isn't already
item = self.get_menuitems()[self._controller.get_current_key_index()]
if not item.get_active():
item.set_active(True)
def add_menuitem(self, label):
'''
add a new menu item to the popup
'''
if not self._first_menu_item:
new_menu_item = Gtk.RadioMenuItem(label=label)
self._first_menu_item = new_menu_item
else:
new_menu_item = Gtk.RadioMenuItem.new_with_label_from_widget(
group=self._first_menu_item, label=label)
new_menu_item.connect('toggled', self._fire_item_clicked)
new_menu_item.show()
self._popup_menu.append(new_menu_item)
def get_menuitems(self):
return self._popup_menu.get_children()
def clear_popupmenu(self):
'''
reinitialises/clears the current popup menu and associated actions
'''
for menu_item in self._popup_menu:
self._popup_menu.remove(menu_item)
self._first_menu_item = None
def _fire_item_clicked(self, menu_item):
'''
Fires the item-clicked signal if the item is selected, passing the
given value as a parameter. Also updates the current value with the
value of the selected item.
'''
if menu_item.get_active():
self.emit('item-clicked', menu_item.get_label())
def do_item_clicked(self, key):
if self._controller:
# inform the controller
self._controller.option_selected(key)
def _popup_callback(self, *args):
x, y = self.calc_popup_position(self._popup_menu)
return x, y, False, None
def show_popup(self, align=True):
'''
show the current popup menu
'''
if align:
self._popup_menu.popup(None, None, self._popup_callback, self, 0,
Gtk.get_current_event_time())
else:
self._popup_menu.popup(None, None, None, None, 0,
Gtk.get_current_event_time())
def do_delete_thyself(self):
self.clear_popupmenu()
del self._popupmenu
class PressButton(Gtk.Button):
button_relief = GObject.property(type=bool, default=False)
def __init__(self, *args, **kwargs):
super(PressButton, self).__init__(*args, **kwargs)
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
setting.bind(gs.PluginKey.BUTTON_RELIEF, self,
'button_relief', Gio.SettingsBindFlags.GET)
self.connect('notify::button-relief',
self.on_notify_button_relief)
def on_notify_button_relief(self, *arg):
if self.button_relief:
self.set_relief(Gtk.ReliefStyle.NONE)
else:
self.set_relief(Gtk.ReliefStyle.HALF)
def set_image(self, pixbuf):
image = self.get_image()
if not image:
image = Gtk.Image()
super(PressButton, self).set_image(image)
if hasattr(self, "controller.enabled") and not self.controller.enabled:
pixbuf = self._getBlendedPixbuf(pixbuf)
self.get_image().set_from_pixbuf(pixbuf)
self.on_notify_button_relief()
def _getBlendedPixbuf(self, pixbuf):
"""Turn a pixbuf into a blended version of the pixbuf by drawing a
transparent alpha blend on it."""
pixbuf = pixbuf.copy()
w, h = pixbuf.get_width(), pixbuf.get_height()
surface = cairo.ImageSurface(
cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())
context = cairo.Context(surface)
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
context.paint()
context.set_source_rgba(32, 32, 32, 0.4)
context.set_line_width(0)
context.rectangle(0, 0, w, h)
context.fill()
pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h)
return pixbuf
class EnhancedButton(Gtk.ToggleButton):
button_relief = GObject.property(type=bool, default=False)
def __init__(self, *args, **kwargs):
super(EnhancedButton, self).__init__(*args, **kwargs)
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
setting.bind(gs.PluginKey.BUTTON_RELIEF, self,
'button_relief', Gio.SettingsBindFlags.GET)
self.connect('notify::button-relief',
self.on_notify_button_relief)
def on_notify_button_relief(self, *arg):
if self.button_relief:
self.set_relief(Gtk.ReliefStyle.NONE)
else:
self.set_relief(Gtk.ReliefStyle.HALF)
class PixbufButton(EnhancedButton):
button_relief = GObject.property(type=bool, default=False)
def __init__(self, *args, **kwargs):
super(PixbufButton, self).__init__(*args, **kwargs)
def set_image(self, pixbuf):
image = self.get_image()
if not image:
image = Gtk.Image()
super(PixbufButton, self).set_image(image)
if hasattr(self, "controller.enabled") and not self.controller.enabled:
pixbuf = self._getBlendedPixbuf(pixbuf)
self.get_image().set_from_pixbuf(pixbuf)
self.on_notify_button_relief()
def _getBlendedPixbuf(self, pixbuf):
"""Turn a pixbuf into a blended version of the pixbuf by drawing a
transparent alpha blend on it."""
pixbuf = pixbuf.copy()
w, h = pixbuf.get_width(), pixbuf.get_height()
surface = cairo.ImageSurface(
cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height())
context = cairo.Context(surface)
Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0)
context.paint()
context.set_source_rgba(32, 32, 32, 0.4)
context.set_line_width(0)
context.rectangle(0, 0, w, h)
context.fill()
pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h)
return pixbuf
class PopupButton(PixbufButton, OptionsPopupWidget):
__gtype_name__ = "PopupButton"
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,))
}
def __init__(self, *args, **kwargs):
'''
Initializes the button.
'''
PixbufButton.__init__(self, *args, **kwargs)
OptionsPopupWidget.__init__(self, *args, **kwargs)
self._popup_menu.attach_to_widget(self, None) # critical to ensure theming works
self._popup_menu.connect('deactivate', self.popup_deactivate)
# initialise some variables
self._first_menu_item = None
def popup_deactivate(self, *args):
self.set_active(False)
def update_image(self):
super(PopupButton, self).update_image()
self.set_image(self._controller.get_current_image())
def update_current_key(self):
super(PopupButton, self).update_current_key()
# update the current image and tooltip
self.set_image(self._controller.get_current_image())
self.set_tooltip_text(self._controller.get_current_description())
def do_button_press_event(self, event):
'''
when button is clicked, update the popup with the sorting options
before displaying the popup
'''
if (event.button == Gdk.BUTTON_PRIMARY):
self.show_popup()
self.set_active(True)
class TextPopupButton(EnhancedButton, OptionsPopupWidget):
__gtype_name__ = "TextPopupButton"
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,))
}
def __init__(self, *args, **kwargs):
'''
Initializes the button.
'''
EnhancedButton.__init__(self, *args, **kwargs)
OptionsPopupWidget.__init__(self, *args, **kwargs)
self._popup_menu.attach_to_widget(self, None) # critical to ensure theming works
self._popup_menu.connect('deactivate', self.popup_deactivate)
# initialise some variables
self._first_menu_item = None
def popup_deactivate(self, *args):
self.set_active(False)
def do_button_press_event(self, event):
'''
when button is clicked, update the popup with the sorting options
before displaying the popup
'''
if (event.button == Gdk.BUTTON_PRIMARY):
self.show_popup()
self.set_active(True)
class MenuButton(PixbufButton, OptionsPopupWidget):
__gtype_name__ = "MenuButton"
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,))
}
def __init__(self, *args, **kwargs):
'''
Initializes the button.
'''
PixbufButton.__init__(self, *args, **kwargs)
OptionsPopupWidget.__init__(self, *args, **kwargs)
self._popup_menu.attach_to_widget(self, None) # critical to ensure theming works
self._popup_menu.connect('deactivate', self.popup_deactivate)
self._states = {}
def popup_deactivate(self, *args):
self.set_active(False)
def add_menuitem(self, key):
'''
add a new menu item to the popup
'''
label = key.label
menutype = key.menutype
typevalue = key.typevalue
if menutype and menutype == 'separator':
new_menu_item = Gtk.SeparatorMenuItem().new()
elif menutype and menutype == 'check':
new_menu_item = Gtk.CheckMenuItem(label=label)
new_menu_item.set_active(typevalue)
new_menu_item.connect('toggled', self._fire_item_clicked)
else:
new_menu_item = Gtk.MenuItem(label=label)
new_menu_item.connect('activate', self._fire_item_clicked)
new_menu_item.show()
self._popup_menu.append(new_menu_item)
def clear_popupmenu(self):
'''
reinitialises/clears the current popup menu and associated actions
'''
for menu_item in self._popup_menu:
if isinstance(menu_item, Gtk.CheckMenuItem):
self._states[menu_item.get_label()] = menu_item.get_active()
self._popup_menu.remove(menu_item)
self._first_menu_item = None
def update_options(self):
self.clear_popupmenu()
for key in self._controller.options:
self.add_menuitem(key)
self._states = {}
def _fire_item_clicked(self, menu_item):
'''
Fires the item-clicked signal if the item is selected, passing the
given value as a parameter. Also updates the current value with the
value of the selected item.
'''
self.emit('item-clicked', menu_item.get_label())
def update_image(self):
super(MenuButton, self).update_image()
self.set_image(self._controller.get_current_image())
def update_current_key(self):
# select the item if it isn't already
# item = self.get_menuitems()[self._controller.get_current_key_index()]
# update the current image and tooltip
self.set_image(self._controller.get_current_image())
self.set_tooltip_text(self._controller.get_current_description())
def do_button_press_event(self, event):
'''
when button is clicked, update the popup with the sorting options
before displaying the popup
'''
if (event.button == Gdk.BUTTON_PRIMARY):
self.show_popup()
self.set_active(True)
class ImageToggleButton(PixbufButton, OptionsWidget):
__gtype_name__ = "ImageToggleButton"
def __init__(self, *args, **kwargs):
'''
Initializes the button.
'''
PixbufButton.__init__(self, *args, **kwargs)
OptionsWidget.__init__(self, *args, **kwargs)
# initialise some variables
self.image_display = False
self.initialised = False
def update_image(self):
super(ImageToggleButton, self).update_image()
self.set_image(self._controller.get_current_image())
def update_current_key(self):
# update the current image and tooltip
self.set_image(self._controller.get_current_image())
self.set_tooltip_text(self._controller.get_current_description())
def do_clicked(self):
if self._controller:
index = self._controller.get_current_key_index()
index = (index + 1) % len(self._controller.options)
# inform the controller
self._controller.option_selected(
self._controller.options[index])
class ImageRadioButton(Gtk.RadioButton, OptionsWidget):
# this is legacy code that will not as yet work with
# the new toolbar - consider removing this later
__gtype_name__ = "ImageRadioButton"
button_relief = GObject.property(type=bool, default=False)
def __init__(self, *args, **kwargs):
'''
Initializes the button.
'''
Gtk.RadioButton.__init__(self, *args, **kwargs)
OptionsWidget.__init__(self, *args, **kwargs)
gs = GSetting()
setting = gs.get_setting(gs.Path.PLUGIN)
setting.bind(gs.PluginKey.BUTTON_RELIEF, self,
'button_relief', Gio.SettingsBindFlags.GET)
self.connect('notify::button-relief',
self.on_notify_button_relief)
# initialise some variables
self.image_display = False
self.initialised = False
# ensure button appearance rather than standard radio toggle
self.set_mode(False)
#label colours
self._not_active_colour = None
self._active_colour = None
def update_image(self):
super(ImageRadioButton, self).update_image()
# self.set_image(self._controller.get_current_image(Gtk.Buildable.get_name(self)))
def do_toggled(self):
if self.get_active():
self.controller.option_selected(Gtk.Buildable.get_name(self))
def set_image(self, pixbuf):
image = self.get_image()
if not image:
image = Gtk.Image()
super(ImageRadioButton, self).set_image(image)
self.get_image().set_from_pixbuf(pixbuf)
self.on_notify_button_relief()
def on_notify_button_relief(self, *arg):
if self.button_relief:
self.set_relief(Gtk.ReliefStyle.NONE)
else:
self.set_relief(Gtk.ReliefStyle.HALF)
def update_current_key(self):
# update the current image and tooltip
# self.set_image(self._controller.get_current_image(Gtk.Buildable.get_name(self)))
self.set_tooltip_text("") #self._controller.get_current_description())
if self.controller.current_key == Gtk.Buildable.get_name(self):
self.set_active(True)
self._set_colour(Gtk.StateFlags.NORMAL)
else:
self._set_colour(Gtk.StateFlags.INSENSITIVE)
def _set_colour(self, state_flag):
if len(self.get_children()) == 0:
return
def get_standard_colour(label, state_flag):
context = label.get_style_context()
return context.get_color(state_flag)
label0 = self.get_children()[0]
if not self._not_active_colour:
self._not_active_colour = get_standard_colour(label0, Gtk.StateFlags.INSENSITIVE)
if not self._active_colour:
self._active_colour = get_standard_colour(label0, Gtk.StateFlags.NORMAL)
if state_flag == Gtk.StateFlags.INSENSITIVE:
label0.override_color(Gtk.StateType.NORMAL, self._not_active_colour)
else:
label0.override_color(Gtk.StateType.NORMAL, self._active_colour)
class SearchEntry(RB.SearchEntry, OptionsPopupWidget):
__gtype_name__ = "SearchEntry"
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,))
}
def __init__(self, *args, **kwargs):
RB.SearchEntry.__init__(self, *args, **kwargs)
OptionsPopupWidget.__init__(self)
# self.props.explicit_mode = True
@OptionsPopupWidget.controller.setter
def controller(self, controller):
if self._controller:
# disconnect signals
self._controller.disconnect(self._search_text_changed_id)
OptionsPopupWidget.controller.fset(self, controller)
# connect signals
self._search_text_changed_id = self._controller.connect(
'notify::search-text', self._update_search_text)
# update the current text
self._update_search_text()
def _update_search_text(self, *args):
if not self.searching():
self.grab_focus()
self.set_text(self._controller.search_text)
def update_current_key(self):
super(SearchEntry, self).update_current_key()
self.set_placeholder(self._controller.get_current_description())
def do_show_popup(self):
'''
Callback called by the search entry when the magnifier is clicked.
It prompts the user through a popup to select a filter type.
'''
self.show_popup(False)
def do_search(self, text):
'''
Callback called by the search entry when a new search must
be performed.
'''
if self._controller:
self._controller.do_search(text)
class QuickSearchEntry(Gtk.Frame):
__gtype_name__ = "QuickSearchEntry"
# signals
__gsignals__ = {
'quick-search': (GObject.SIGNAL_RUN_LAST, None, (str,)),
'arrow-pressed': (GObject.SIGNAL_RUN_LAST, None, (object,))
}
def __init__(self, *args, **kwargs):
super(QuickSearchEntry, self).__init__(*args, **kwargs)
self._idle = 0
# text entry for the quick search input
text_entry = Gtk.Entry(halign='center', valign='center',
margin=5)
self.add(text_entry)
self.connect_signals(text_entry)
def get_text(self):
return self.get_child().get_text()
def set_text(self, text):
self.get_child().set_text(text)
def connect_signals(self, text_entry):
text_entry.connect('changed', self._on_quick_search)
text_entry.connect('focus-out-event', self._on_focus_lost)
text_entry.connect('key-press-event', self._on_key_pressed)
def _hide_quick_search(self):
self.hide()
def _add_hide_on_timeout(self):
self._idle += 1
def hide_on_timeout(*args):
self._idle -= 1
if not self._idle:
self._hide_quick_search()
return False
Gdk.threads_add_timeout_seconds(GLib.PRIORITY_DEFAULT_IDLE, 4,
hide_on_timeout, None)
def do_parent_set(self, old_parent, *args):
if old_parent:
old_parent.disconnect(self._on_parent_key_press_id)
parent = self.get_parent()
self._on_parent_key_press_id = parent.connect('key-press-event',
self._on_parent_key_press, self.get_child())
def _on_parent_key_press(self, parent, event, entry):
if not self.get_visible() and \
event.keyval not in [Gdk.KEY_Shift_L,
Gdk.KEY_Shift_R,
Gdk.KEY_Control_L,
Gdk.KEY_Control_R,
Gdk.KEY_Escape,
Gdk.KEY_Alt_L,
Gdk.KEY_Super_L,
Gdk.KEY_Super_R]:
# grab focus, redirect the pressed key and make the quick search
# entry visible
entry.set_text('')
entry.grab_focus()
self.show_all()
entry.im_context_filter_keypress(event)
elif self.get_visible() and event.keyval == Gdk.KEY_Escape:
self._hide_quick_search()
return False
def _on_quick_search(self, entry, *args):
if entry.get_visible():
# emit the quick-search signal
search_text = entry.get_text()
self.emit('quick-search', search_text)
# add a timeout to hide the search entry
self._add_hide_on_timeout()
def _on_focus_lost(self, entry, *args):
self._hide_quick_search()
return False
def _on_key_pressed(self, entry, event, *args):
arrow = event.keyval in [Gdk.KEY_Up, Gdk.KEY_Down]
if arrow:
self.emit('arrow-pressed', event.keyval)
self._add_hide_on_timeout()
return arrow
class ProxyPopupButton(Gtk.Frame):
__gtype_name__ = "ProxyPopupButton"
def __init__(self, *args, **kwargs):
super(ProxyPopupButton, self).__init__(*args, **kwargs)
self._delegate = None
@property
def controller(self):
if self._delegate:
return self._delegate.controller
@controller.setter
def controller(self, controller):
if self._delegate:
self.remove(self._delegate)
if len(controller.options) < 25:
self._delegate = PopupButton()
else:
self._delegate = ListViewButton()
self._delegate.set_visible(True)
self._delegate.set_has_tooltip(True)
self._delegate.set_can_focus(False)
self._delegate.controller = controller
self.add(self._delegate)
class OptionsListViewWidget(OptionsWidget):
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,)),
'deactivate': (GObject.SIGNAL_RUN_LAST, None, ())
}
def __init__(self, *args, **kwargs):
OptionsWidget.__init__(self, *args, **kwargs)
self._popup = self
@OptionsWidget.controller.setter
def controller(self, controller):
ui = Gtk.Builder()
ui.add_from_file(rb.find_plugin_file(controller.plugin,
'ui/coverart_listwindow.ui'))
ui.connect_signals(self)
self._listwindow = ui.get_object('listwindow')
self._liststore = ui.get_object('liststore')
self._listwindow.set_size_request(200, 300)
self._treeview = ui.get_object('treeview')
self._scrollwindow = ui.get_object('scrolledwindow')
self._scrolldown_button = ui.get_object('scrolldown_button')
self._increment = False
OptionsWidget.controller.fset(self, controller)
def update_options(self):
self.clear_options()
self.add_options(self._controller.options)
def update_current_key(self):
self.select(self.controller.get_current_key_index())
def do_item_clicked(self, key):
if self._controller:
# inform the controller
self._controller.option_selected(key)
def show_popup(self):
'''
show the listview window either above or below the controlling
widget depending upon where the cursor position is relative to the
screen
params - x & y is the cursor position
'''
pos_x, pos_y = self.calc_popup_position(self._listwindow)
self._listwindow.move(pos_x, pos_y)
self._listwindow.show_all()
def clear_options(self):
self._liststore.clear()
def add_options(self, iterable):
for label in iterable:
self._liststore.append((label,))
def select(self, index):
self._treeview.get_selection().select_iter(self._liststore[index].iter)
self._treeview.scroll_to_cell(self._liststore[index].path)
def on_button_click(self, view, arg):
try:
liststore, viewiter = view.get_selection().get_selected()
label = liststore.get_value(viewiter, 0)
self.emit('item-clicked', label)
except:
pass
self._treeview.set_hover_selection(False)
self._listwindow.hide()
self.emit('deactivate')
def on_scroll_button_enter(self, button):
def scroll(*args):
if self._increment:
if button is self._scrolldown_button:
adjustment.set_value(adjustment.get_value()
+ self._step)
else:
adjustment.set_value(adjustment.get_value()
- self._step)
return self._increment
self._increment = True
adjustment = self._scrollwindow.get_vadjustment()
self.on_scroll_button_released()
Gdk.threads_add_timeout(GLib.PRIORITY_DEFAULT_IDLE, 50,
scroll, None)
def on_scroll_button_leave(self, *args):
self._increment = False
def on_scroll_button_pressed(self, *args):
adjustment = self._scrollwindow.get_vadjustment()
self._step = adjustment.get_page_increment()
def on_scroll_button_released(self, *args):
adjustment = self._scrollwindow.get_vadjustment()
self._step = adjustment.get_step_increment()
def on_treeview_enter_notify_event(self, *args):
self._treeview.set_hover_selection(True)
def on_cancel(self, *args):
self._listwindow.hide()
self.emit('deactivate')
return True
def do_delete_thyself(self):
self.clear_list()
del self._listwindow
class ListViewButton(PixbufButton, OptionsListViewWidget):
__gtype_name__ = "ListViewButton"
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (str,)),
'deactivate': (GObject.SIGNAL_RUN_LAST, None, ())
}
def __init__(self, *args, **kwargs):
'''
Initializes the button.
'''
PixbufButton.__init__(self, *args, **kwargs)
OptionsListViewWidget.__init__(self, *args, **kwargs)
self._popup.connect('deactivate', self.popup_deactivate)
def popup_deactivate(self, *args):
# add a slight delay to allow the click of button to occur
# before the deactivation of the button - this will allow
# us to toggle the popup via the button correctly
def deactivate(*args):
self.set_active(False)
Gdk.threads_add_timeout(GLib.PRIORITY_DEFAULT_IDLE, 50, deactivate, None)
def update_image(self):
super(ListViewButton, self).update_image()
self.set_image(self._controller.get_current_image())
def update_current_key(self):
super(ListViewButton, self).update_current_key()
# update the current image and tooltip
self.set_image(self._controller.get_current_image())
self.set_tooltip_text(self._controller.get_current_description())
def do_button_press_event(self, event):
'''
when button is clicked, update the popup with the sorting options
before displaying the popup
'''
if (event.button == Gdk.BUTTON_PRIMARY and not self.get_active()):
self.show_popup()
self.set_active(True)
else:
self.set_active(False)
class EnhancedIconView(Gtk.IconView):
__gtype_name__ = "EnhancedIconView"
# signals
__gsignals__ = {
'item-clicked': (GObject.SIGNAL_RUN_LAST, None, (object, object))
}
object_column = GObject.property(type=int, default=-1)