forked from deluge-torrent/deluge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_combo_chooser.py
executable file
·1545 lines (1292 loc) · 60.4 KB
/
path_combo_chooser.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Bro <bro.development@gmail.com>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
from __future__ import print_function
import os
import gobject
import gtk
from gtk import gdk, keysyms
from deluge.common import resource_filename
from deluge.path_chooser_common import get_completion_paths
def is_ascii_value(keyval, ascii_key):
try:
# Set show/hide hidden files
if chr(keyval) == ascii_key:
return True
except ValueError:
# Not in ascii range
pass
return False
def key_is_up(keyval):
return keyval == keysyms.Up or keyval == keysyms.KP_Up
def key_is_down(keyval):
return keyval == keysyms.Down or keyval == keysyms.KP_Down
def key_is_up_or_down(keyval):
return key_is_up(keyval) or key_is_down(keyval)
def key_is_pgup_or_pgdown(keyval):
return keyval == keysyms.Page_Down or keyval == keysyms.Page_Up
def key_is_enter(keyval):
return keyval == keysyms.Return or keyval == keysyms.KP_Enter
def path_without_trailing_path_sep(path):
while path.endswith("/") or path.endswith("\\"):
if path == "/":
return path
path = path[0:-1]
return path
class ValueList(object):
paths_without_trailing_path_sep = False
def get_values_count(self):
return len(self.tree_store)
def get_values(self):
"""
Returns the values in the list.
"""
values = []
for row in self.tree_store:
values.append(row[0])
return values
def add_values(self, paths, append=True, scroll_to_row=False,
clear=False, emit_signal=False):
"""
Add paths to the liststore
:param paths: the paths to add
:type paths: list
:param append: if the values should be appended or inserted
:type append: boolean
:param scroll_to_row: if the treeview should scroll to the new row
:type scroll_to_row: boolean
"""
if clear:
self.tree_store.clear()
for path in paths:
if self.paths_without_trailing_path_sep:
path = path_without_trailing_path_sep(path)
if append:
tree_iter = self.tree_store.append([path])
else:
tree_iter = self.tree_store.insert(0, [path])
if scroll_to_row:
self.treeview.grab_focus()
tree_path = self.tree_store.get_path(tree_iter)
# Scroll to path
self.handle_list_scroll(path=tree_path)
if emit_signal:
self.emit("list-value-added", paths)
self.emit("list-values-changed", self.get_values())
def set_values(self, paths, scroll_to_row=False, preserve_selection=True):
"""
Add paths to the liststore
:param paths: the paths to add
:type paths: list
:param scroll_to_row: if the treeview should scroll to the new row
:type scroll_to_row: boolean
"""
if not (isinstance(paths, list) or isinstance(paths, tuple)):
return
sel = None
if preserve_selection:
sel = self.get_selection_path()
self.add_values(paths, scroll_to_row=scroll_to_row, clear=True)
if sel:
self.treeview.get_selection().select_path(sel)
def get_selection_path(self):
"""Returns the (first) selected path from a treeview"""
tree_selection = self.treeview.get_selection()
model, tree_paths = tree_selection.get_selected_rows()
if len(tree_paths) > 0:
return tree_paths[0]
return None
def get_selected_value(self):
path = self.get_selection_path()
if path:
return self.tree_store[path][0]
return None
def remove_selected_path(self):
path = self.get_selection_path()
if path:
path_value = self.tree_store[path][0]
del self.tree_store[path]
index = path[0]
# The last row was deleted
if index == len(self.tree_store):
index -= 1
if index >= 0:
path = (index, )
self.treeview.set_cursor(path)
self.set_path_selected(path)
self.emit("list-value-removed", path_value)
self.emit("list-values-changed", self.get_values())
def set_selected_value(self, value, select_first=False):
"""
Select the row of the list with value
:param value: the value to be selected
:type value: str
:param select_first: if the first item should be selected if the value if not found.
:type select_first: boolean
"""
for i, row in enumerate(self.tree_store):
if row[0] == value:
self.treeview.set_cursor((i))
return
# The value was not found
if select_first:
self.treeview.set_cursor((0,))
else:
self.treeview.get_selection().unselect_all()
def set_path_selected(self, path):
self.treeview.get_selection().select_path(path)
def on_value_list_treeview_key_press_event(self, widget, event):
"""
Mimics Combobox behavior
Escape or Alt+Up: Close
Enter or Return : Select
"""
keyval = event.keyval
state = event.state & gtk.accelerator_get_default_mod_mask()
if keyval == keysyms.Escape or\
(key_is_up(keyval) and
state == gdk.MOD1_MASK): # ALT Key
self.popdown()
return True
# Set entry value to the selected row
elif key_is_enter(keyval):
path = self.get_selection_path()
if path:
self.set_entry_value(path, popdown=True)
return True
return False
def on_treeview_mouse_button_press_event(self, treeview, event, double_click=False):
"""
When left clicking twice, the row value is set for the text entry
and the popup is closed.
"""
# This is left click
if event.button != 3:
# Double clicked a row, set this as the entry value
# and close the popup
if (double_click and event.type == gtk.gdk._2BUTTON_PRESS) or\
(not double_click and event.type == gtk.gdk.BUTTON_PRESS):
path = self.get_selection_path()
if path:
self.set_entry_value(path, popdown=True)
return True
return False
def handle_list_scroll(self, _next=None, path=None, set_entry=False, swap=False, scroll_window=False):
"""
Handles changes to the row selection.
:param _next: the direction to change selection. True means down and False means up.
None means no change.
:type _next: boolean/None
:param path: the current path. If None, the currently selected path is used.
:type path: tuple
:param set_entry: if the new value should be set in the text entry.
:type set_entry: boolean
:param swap: if the old and new value should be swapped
:type swap: boolean
"""
if scroll_window:
adjustment = self.completion_scrolled_window.get_vadjustment()
visible_rows_height = self.get_values_count()
if visible_rows_height > self.max_visible_rows:
visible_rows_height = self.max_visible_rows
visible_rows_height *= self.row_height
value = adjustment.get_value()
# Max adjustment value
max_value = adjustment.get_upper() - visible_rows_height
# Set adjustment increment to 3 times the row height
adjustment.set_step_increment(self.row_height * 3)
if _next:
# If number of values is less than max rows, no scroll
if self.get_values_count() < self.max_visible_rows:
return
value += adjustment.get_step_increment()
if value > max_value:
value = max_value
else:
value -= adjustment.get_step_increment()
if value < 0:
value = 0
adjustment.set_value(value)
return
if path is None:
path = self.get_selection_path()
if not path:
# These options require a selected path
if set_entry or swap:
return
# This is a regular scroll, not setting value in entry or swapping rows,
# so we find a path value anyways
path = (0, )
cursor = self.treeview.get_cursor()
if cursor is not None and cursor[0] is not None:
path = cursor[0]
else:
# Since cursor is none, we won't advance the index
_next = None
# If _next is None, we won't change the selection
if _next is not None:
# We move the selection either one up or down.
# If we reach end of list, we wrap
index = path[0] if path else 0
index = index + 1 if _next else index - 1
if index >= len(self.tree_store):
index = 0
elif index < 0:
index = len(self.tree_store) - 1
# We have the index for the new path
new_path = (index)
if swap:
p1 = self.tree_store[path][0]
p2 = self.tree_store[new_path][0]
self.tree_store.swap(self.tree_store.get_iter(path),
self.tree_store.get_iter(new_path))
self.emit("list-values-reordered", [p1, p2])
self.emit("list-values-changed", self.get_values())
path = new_path
self.treeview.set_cursor(path)
self.treeview.get_selection().select_path(path)
if set_entry:
self.set_entry_value(path)
class StoredValuesList(ValueList):
def __init__(self):
self.tree_store = self.builder.get_object("stored_values_tree_store")
self.tree_column = self.builder.get_object("stored_values_treeview_column")
self.rendererText = self.builder.get_object("stored_values_cellrenderertext")
self.paths_without_trailing_path_sep = False
# Add signal handlers
self.signal_handlers["on_stored_values_treeview_mouse_button_press_event"] = \
self.on_treeview_mouse_button_press_event
self.signal_handlers["on_stored_values_treeview_key_press_event"] = \
self.on_stored_values_treeview_key_press_event
self.signal_handlers["on_stored_values_treeview_key_release_event"] = \
self.on_stored_values_treeview_key_release_event
self.signal_handlers["on_cellrenderertext_edited"] = self.on_cellrenderertext_edited
def on_cellrenderertext_edited(self, cellrenderertext, path, new_text):
"""
Callback on the 'edited' signal.
Sets the new text in the path and disables editing on the renderer.
"""
new_text = path_without_trailing_path_sep(new_text)
self.tree_store[path][0] = new_text
self.rendererText.set_property('editable', False)
def on_edit_path(self, path, column):
"""
Starts editing on the provided path
:param path: the paths to edit
:type path: tuple
:param column: the column to edit
:type column: gtk.TreeViewColumn
"""
self.rendererText.set_property('editable', True)
self.treeview.grab_focus()
self.treeview.set_cursor(path, focus_column=column, start_editing=True)
def on_treeview_mouse_button_press_event(self, treeview, event):
"""
Shows popup on selected row when right clicking
When left clicking twice, the row value is set for the text entry
and the popup is closed.
"""
# This is left click
if event.button != 3:
super(StoredValuesList, self).on_treeview_mouse_button_press_event(treeview, event, double_click=True)
return False
# This is right click, create popup menu for this row
x = int(event.x)
y = int(event.y)
time = event.time
pthinfo = treeview.get_path_at_pos(x, y)
if pthinfo is not None:
path, col, cellx, celly = pthinfo
treeview.grab_focus()
treeview.set_cursor(path, col, 0)
self.path_list_popup = gtk.Menu()
menuitem_edit = gtk.MenuItem("Edit path")
self.path_list_popup.append(menuitem_edit)
menuitem_remove = gtk.MenuItem("Remove path")
self.path_list_popup.append(menuitem_remove)
def on_edit_clicked(widget, path):
self.on_edit_path(path, self.tree_column)
def on_remove_clicked(widget, path):
self.remove_selected_path()
menuitem_edit.connect("activate", on_edit_clicked, path)
menuitem_remove.connect("activate", on_remove_clicked, path)
self.path_list_popup.popup(None, None, None, event.button, time, data=path)
self.path_list_popup.show_all()
def remove_selected_path(self):
ValueList.remove_selected_path(self)
# Resize popup
PathChooserPopup.popup(self)
def on_stored_values_treeview_key_press_event(self, widget, event):
super(StoredValuesList, self).on_value_list_treeview_key_press_event(widget, event)
# Prevent the default event handler to move the cursor in the list
if key_is_up_or_down(event.keyval):
return True
def on_stored_values_treeview_key_release_event(self, widget, event):
"""
Mimics Combobox behavior
Escape or Alt+Up: Close
Enter or Return : Select
"""
keyval = event.keyval
ctrl = event.state & gtk.gdk.CONTROL_MASK
# Edit selected row
if (keyval in [keysyms.Left, keysyms.Right, keysyms.space]):
path = self.get_selection_path()
if path:
self.on_edit_path(path, self.tree_column)
elif key_is_up_or_down(keyval):
# Swap the row value
if event.state & gtk.gdk.CONTROL_MASK:
self.handle_list_scroll(_next=key_is_down(keyval),
swap=True)
else:
self.handle_list_scroll(_next=key_is_down(keyval))
elif key_is_pgup_or_pgdown(event.keyval):
# The cursor has been changed by the default key-press-event handler
# so set the path of the cursor selected
self.set_path_selected(self.treeview.get_cursor()[0])
elif ctrl:
# Handle key bindings for manipulating the list
# Remove the selected entry
if is_ascii_value(keyval, 'r'):
self.remove_selected_path()
return True
# Add current value to saved list
elif is_ascii_value(keyval, 's'):
super(PathChooserComboBox, self).add_current_value_to_saved_list() # pylint: disable=bad-super-call
return True
# Edit selected value
elif is_ascii_value(keyval, 'e'):
self.edit_selected_path()
return True
class CompletionList(ValueList):
def __init__(self):
self.tree_store = self.builder.get_object("completion_tree_store")
self.tree_column = self.builder.get_object("completion_treeview_column")
self.rendererText = self.builder.get_object("completion_cellrenderertext")
self.completion_scrolled_window = self.builder.get_object("completion_scrolled_window")
self.signal_handlers["on_completion_treeview_key_press_event"] = \
self.on_completion_treeview_key_press_event
self.signal_handlers["on_completion_treeview_motion_notify_event"] = \
self.on_completion_treeview_motion_notify_event
# Add super class signal handler
self.signal_handlers["on_completion_treeview_mouse_button_press_event"] = \
super(CompletionList, self).on_treeview_mouse_button_press_event
def reduce_values(self, prefix):
"""
Reduce the values in the liststore to those starting with the prefix.
:param prefix: the prefix to be matched
:type paths: string
"""
values = self.get_values()
matching_values = []
for v in values:
if v.startswith(prefix):
matching_values.append(v)
self.add_values(matching_values, clear=True)
def on_completion_treeview_key_press_event(self, widget, event):
ret = super(CompletionList, self).on_value_list_treeview_key_press_event(widget, event)
if ret:
return ret
keyval = event.keyval
ctrl = event.state & gtk.gdk.CONTROL_MASK
if key_is_up_or_down(keyval):
self.handle_list_scroll(_next=key_is_down(keyval))
return True
elif ctrl:
# Set show/hide hidden files
if is_ascii_value(keyval, 'h'):
self.path_entry.set_show_hidden_files(not self.path_entry.get_show_hidden_files(), do_completion=True)
return True
def on_completion_treeview_motion_notify_event(self, widget, event):
if event.is_hint:
x, y, state = event.window.get_pointer()
else:
x = event.x
y = event.y
path = self.treeview.get_path_at_pos(int(x), int(y))
if path:
self.handle_list_scroll(path=path[0], _next=None)
class PathChooserPopup(object):
"""
This creates the popop window for the ComboEntry
"""
def __init__(self, min_visible_rows, max_visible_rows, popup_alignment_widget):
self.min_visible_rows = min_visible_rows
# Maximum number of rows to display without scrolling
self.set_max_popup_rows(max_visible_rows)
self.popup_window.realize()
self.alignment_widget = popup_alignment_widget
self.popup_buttonbox = None # If set, the height of this widget is the minimum height
def popup(self):
"""
Makes the popup visible.
"""
# Entry is not yet visible
if not self.path_entry.flags() & gtk.REALIZED:
return
self.set_window_position_and_size()
def popdown(self):
if not self.is_popped_up():
return
if not self.path_entry.flags() & gtk.REALIZED:
return
self.popup_window.grab_remove()
self.popup_window.hide_all()
def is_popped_up(self):
"""
Return True if the window is popped up.
"""
return bool(self.popup_window.flags() & gtk.MAPPED)
def set_window_position_and_size(self):
if len(self.tree_store) < self.min_visible_rows:
return False
x, y, width, height = self.get_position()
self.popup_window.set_size_request(width, height)
self.popup_window.resize(width, height)
self.popup_window.move(x, y)
self.popup_window.show_all()
def get_position(self):
"""
Returns the size of the popup window and the coordinates on the screen.
"""
# Necessary for the first call, to make treeview.size_request give sensible values
# self.popup_window.realize()
self.treeview.realize()
# We start with the coordinates of the parent window
x, y = self.path_entry.window.get_origin()
# Add the position of the alignment_widget relative to the parent window.
x += self.alignment_widget.allocation.x
y += self.alignment_widget.allocation.y
height_extra = 8
buttonbox_width = 0
height = self.popup_window.size_request()[1]
width = self.popup_window.size_request()[0]
if self.popup_buttonbox:
buttonbox_height = max(self.popup_buttonbox.size_request()[1], self.popup_buttonbox.allocation.height)
buttonbox_width = max(self.popup_buttonbox.size_request()[0], self.popup_buttonbox.allocation.width)
treeview_width = self.treeview.size_request()[0]
# After removing an element from the tree store, self.treeview.size_request()[0]
# returns -1 for some reason, so the requested width cannot be used until the treeview
# has been displayed once.
if treeview_width != -1:
width = treeview_width + buttonbox_width
# The list is empty, so ignore initial popup width request
# Will be set to the minimum width next
elif len(self.tree_store) == 0:
width = 0
if width < self.alignment_widget.allocation.width:
width = self.alignment_widget.allocation.width
# 10 is extra spacing
content_width = self.treeview.size_request()[0] + buttonbox_width + 10
# Adjust height according to number of list items
if len(self.tree_store) > 0 and self.max_visible_rows > 0:
# The height for one row in the list
self.row_height = self.treeview.size_request()[1] / len(self.tree_store)
# Set height to number of rows
height = len(self.tree_store) * self.row_height + height_extra
# Adjust the height according to the max number of rows
max_height = self.row_height * self.max_visible_rows
# Restrict height to max_visible_rows
if max_height + height_extra < height:
height = max_height
height += height_extra
# Increase width because of vertical scrollbar
content_width += 15
if self.popup_buttonbox:
# Minimum height is the height of the button box
if height < buttonbox_height + height_extra:
height = buttonbox_height + height_extra
if content_width > width:
width = content_width
screen = self.path_entry.get_screen()
monitor_num = screen.get_monitor_at_window(self.path_entry.window)
monitor = screen.get_monitor_geometry(monitor_num)
if x < monitor.x:
x = monitor.x
elif x + width > monitor.x + monitor.width:
x = monitor.x + monitor.width - width
# Set the position
if y + self.path_entry.allocation.height + height <= monitor.y + monitor.height:
y += self.path_entry.allocation.height
# Not enough space downwards on the screen
elif y - height >= monitor.y:
y -= height
elif (monitor.y + monitor.height - (y + self.path_entry.allocation.height) >
y - monitor.y):
y += self.path_entry.allocation.height
height = monitor.y + monitor.height - y
else:
height = y - monitor.y
y = monitor.y
return x, y, width, height
def popup_grab_window(self):
activate_time = 0
if gdk.pointer_grab(self.popup_window.window, True,
(gdk.BUTTON_PRESS_MASK |
gdk.BUTTON_RELEASE_MASK |
gdk.POINTER_MOTION_MASK),
None, None, activate_time) == 0:
if gdk.keyboard_grab(self.popup_window.window, True, activate_time) == 0:
return True
else:
self.popup_window.window.get_display().pointer_ungrab(activate_time)
return False
return False
def set_entry_value(self, path, popdown=False):
"""
Sets the text of the entry to the value in path
"""
self.path_entry.set_text(self.tree_store[path][0], set_file_chooser_folder=True, trigger_event=True)
if popdown:
self.popdown()
def set_max_popup_rows(self, rows):
try:
int(rows)
except Exception:
self.max_visible_rows = 20
return
self.max_visible_rows = rows
def get_max_popup_rows(self):
return self.max_visible_rows
#################
# Callbacks
#################
def on_popup_window_button_press_event(self, window, event):
# If we're clicking outside of the window close the popup
hide = False
# Also if the intersection of self and the event is empty, hide
# the path_list
if (tuple(self.popup_window.allocation.intersect(
gdk.Rectangle(x=int(event.x), y=int(event.y),
width=1, height=1))) == (0, 0, 0, 0)):
hide = True
# Toplevel is the window that received the event, and parent is the
# path_list window. If they are not the same, means the popup should
# be hidden. This is necessary for when the event happens on another
# widget
toplevel = event.window.get_toplevel()
parent = self.popup_window.window
if toplevel != parent:
hide = True
if hide:
self.popdown()
class StoredValuesPopup(StoredValuesList, PathChooserPopup):
"""
The stored values popup
"""
def __init__(self, builder, path_entry, max_visible_rows, popup_alignment_widget):
self.builder = builder
self.treeview = self.builder.get_object("stored_values_treeview")
self.popup_window = self.builder.get_object("stored_values_popup_window")
self.button_default = self.builder.get_object("button_default")
self.path_entry = path_entry
self.text_entry = path_entry.text_entry
self.signal_handlers = {}
PathChooserPopup.__init__(self, 0, max_visible_rows, popup_alignment_widget)
StoredValuesList.__init__(self)
self.popup_buttonbox = self.builder.get_object("buttonbox")
# Add signal handlers
self.signal_handlers["on_buttonbox_key_press_event"] = \
self.on_buttonbox_key_press_event
self.signal_handlers["on_stored_values_treeview_scroll_event"] = self.on_scroll_event
self.signal_handlers["on_button_toggle_dropdown_scroll_event"] = self.on_scroll_event
self.signal_handlers["on_entry_text_scroll_event"] = self.on_scroll_event
self.signal_handlers["on_stored_values_popup_window_focus_out_event"] = \
self.on_stored_values_popup_window_focus_out_event
# For when clicking outside the popup
self.signal_handlers["on_stored_values_popup_window_button_press_event"] = \
self.on_popup_window_button_press_event
# Buttons for manipulating the list
self.signal_handlers["on_button_add_clicked"] = self.on_button_add_clicked
self.signal_handlers["on_button_edit_clicked"] = self.on_button_edit_clicked
self.signal_handlers["on_button_remove_clicked"] = self.on_button_remove_clicked
self.signal_handlers["on_button_up_clicked"] = self.on_button_up_clicked
self.signal_handlers["on_button_down_clicked"] = self.on_button_down_clicked
self.signal_handlers["on_button_default_clicked"] = self.on_button_default_clicked
self.signal_handlers["on_button_properties_clicked"] = self.path_entry._on_button_properties_clicked
def popup(self):
"""
Makes the popup visible.
"""
# Calling super popup
PathChooserPopup.popup(self)
self.popup_window.grab_focus()
if not self.treeview.flags() & gtk.HAS_FOCUS:
self.treeview.grab_focus()
if not self.popup_grab_window():
self.popup_window.hide()
return
self.popup_window.grab_add()
# Set value selected if it exists
self.set_selected_value(path_without_trailing_path_sep(self.path_entry.get_text()))
#################
# Callbacks
#################
def on_stored_values_popup_window_focus_out_event(self, entry, event):
"""
Popup sometimes loses the focus to the text entry, e.g. when right click
shows a popup menu on a row. This regains the focus.
"""
self.popup_grab_window()
return True
def on_scroll_event(self, widget, event):
"""
Handles scroll events from text entry, toggle button and treeview
"""
swap = event.state & gtk.gdk.CONTROL_MASK
scroll_window = event.state & gtk.gdk.SHIFT_MASK
self.handle_list_scroll(_next=event.direction == gdk.SCROLL_DOWN,
set_entry=widget != self.treeview, swap=swap, scroll_window=scroll_window)
return True
def on_buttonbox_key_press_event(self, widget, event):
"""
Handles when Escape or ALT+arrow up is pressed when focus
is on any of the buttons in the popup
"""
keyval = event.keyval
state = event.state & gtk.accelerator_get_default_mod_mask()
if keyval == keysyms.Escape or (key_is_up(keyval) and state == gdk.MOD1_MASK):
self.popdown()
return True
return False
# --------------------------------------------------
# Funcs and callbacks on the buttons to manipulate the list
# --------------------------------------------------
def add_current_value_to_saved_list(self):
text = self.path_entry.get_text()
text = path_without_trailing_path_sep(text)
values = self.get_values()
if text in values:
# Make the matching value selected
self.set_selected_value(text)
self.handle_list_scroll()
return True
self.add_values([text], scroll_to_row=True, append=False, emit_signal=True)
def edit_selected_path(self):
path = self.get_selection_path()
if path:
self.on_edit_path(path, self.tree_column)
def on_button_add_clicked(self, widget):
self.add_current_value_to_saved_list()
self.popup()
def on_button_edit_clicked(self, widget):
self.edit_selected_path()
def on_button_remove_clicked(self, widget):
self.remove_selected_path()
return True
def on_button_up_clicked(self, widget):
self.handle_list_scroll(_next=False, swap=True)
def on_button_down_clicked(self, widget):
self.handle_list_scroll(_next=True, swap=True)
def on_button_default_clicked(self, widget):
if self.default_text:
self.set_text(self.default_text, trigger_event=True)
class PathCompletionPopup(CompletionList, PathChooserPopup):
"""
The auto completion popup
"""
def __init__(self, builder, path_entry, max_visible_rows):
self.builder = builder
self.treeview = self.builder.get_object("completion_treeview")
self.popup_window = self.builder.get_object("completion_popup_window")
self.path_entry = path_entry
self.text_entry = path_entry.text_entry
self.show_hidden_files = False
self.signal_handlers = {}
PathChooserPopup.__init__(self, 1, max_visible_rows, self.text_entry)
CompletionList.__init__(self)
# Add signal handlers
self.signal_handlers["on_completion_treeview_scroll_event"] = self.on_scroll_event
self.signal_handlers["on_completion_popup_window_focus_out_event"] = \
self.on_completion_popup_window_focus_out_event
# For when clicking outside the popup
self.signal_handlers["on_completion_popup_window_button_press_event"] = \
self.on_popup_window_button_press_event
def popup(self):
"""
Makes the popup visible.
"""
PathChooserPopup.popup(self)
self.popup_window.grab_focus()
if not self.treeview.flags() & gtk.HAS_FOCUS:
self.treeview.grab_focus()
if not self.popup_grab_window():
self.popup_window.hide()
return
self.popup_window.grab_add()
self.text_entry.grab_focus()
self.text_entry.set_position(len(self.path_entry.text_entry.get_text()))
self.set_selected_value(path_without_trailing_path_sep(self.path_entry.get_text()), select_first=True)
#################
# Callbacks
#################
def on_completion_popup_window_focus_out_event(self, entry, event):
"""
Popup sometimes loses the focus to the text entry, e.g. when right click
shows a popup menu on a row. This regains the focus.
"""
self.popup_grab_window()
return True
def on_scroll_event(self, widget, event):
"""
Handles scroll events from the treeview
"""
x, y, state = event.window.get_pointer()
self.handle_list_scroll(_next=event.direction == gdk.SCROLL_DOWN,
set_entry=widget != self.treeview, scroll_window=True)
path = self.treeview.get_path_at_pos(int(x), int(y))
if path:
self.handle_list_scroll(path=path[0], _next=None)
return True
class PathAutoCompleter(object):
def __init__(self, builder, path_entry, max_visible_rows):
self.completion_popup = PathCompletionPopup(builder, path_entry, max_visible_rows)
self.path_entry = path_entry
self.dirs_cache = {}
self.use_popup = False
self.auto_complete_enabled = True
self.signal_handlers = self.completion_popup.signal_handlers
self.signal_handlers["on_completion_popup_window_key_press_event"] = \
self.on_completion_popup_window_key_press_event
self.signal_handlers["on_entry_text_delete_text"] = \
self.on_entry_text_delete_text
self.signal_handlers["on_entry_text_insert_text"] = \
self.on_entry_text_insert_text
self.accelerator_string = gtk.accelerator_name(keysyms.Tab, 0)
def on_entry_text_insert_text(self, entry, new_text, new_text_length, position):
if self.path_entry.flags() & gtk.REALIZED:
cur_text = self.path_entry.get_text()
pos = entry.get_position()
new_complete_text = cur_text[:pos] + new_text + cur_text[pos:]
# Remove all values from the list that do not start with new_complete_text
self.completion_popup.reduce_values(new_complete_text)
self.completion_popup.set_selected_value(new_complete_text, select_first=True)
if self.completion_popup.is_popped_up():
self.completion_popup.set_window_position_and_size()
def on_entry_text_delete_text(self, entry, start, end):
"""
Do completion when characters are removed
"""
if self.completion_popup.is_popped_up():
cur_text = self.path_entry.get_text()
new_complete_text = cur_text[:start] + cur_text[end:]
self.do_completion(value=new_complete_text, forward_completion=False)
def set_use_popup(self, use):
self.use_popup = use
def on_completion_popup_window_key_press_event(self, entry, event):
"""
Handles key pressed events on the auto-completion popup window
"""
# If on_completion_treeview_key_press_event handles the event, do nothing
ret = self.completion_popup.on_completion_treeview_key_press_event(entry, event)
if ret:
return ret
keyval = event.keyval
state = event.state & gtk.accelerator_get_default_mod_mask()
if self.is_auto_completion_accelerator(keyval, state)\
and self.auto_complete_enabled:
values_count = self.completion_popup.get_values_count()
if values_count == 1:
self.do_completion()
else:
self.completion_popup.handle_list_scroll(_next=True)
return True
self.path_entry.text_entry.emit("key-press-event", event)
def is_auto_completion_accelerator(self, keyval, state):
return gtk.accelerator_name(keyval, state.numerator) == self.accelerator_string
def do_completion(self, value=None, forward_completion=True):
if not value:
value = self.path_entry.get_text()
self.path_entry.text_entry.set_position(len(value))
opts = {}
opts["show_hidden_files"] = self.completion_popup.show_hidden_files
opts["completion_text"] = value
opts["forward_completion"] = forward_completion
self._start_completion(opts)
def _start_completion(self, args):
args = get_completion_paths(args)
self._end_completion(args)
def _end_completion(self, args):
value = args["completion_text"]
paths = args["paths"]
if args["forward_completion"]:
common_prefix = os.path.commonprefix(paths)
if len(common_prefix) > len(value):
self.path_entry.set_text(common_prefix, set_file_chooser_folder=True, trigger_event=True)