|
5 | 5 |
|
6 | 6 | from meld.conf import _ |
7 | 7 | from meld.ui._gtktemplate import Template |
8 | | -from meld.ui.listselector import FilteredListSelector |
9 | 8 |
|
10 | 9 | # TODO: Current pygobject support for templates excludes subclassing of |
11 | 10 | # templated classes, which is why we have two near-identical UI files |
12 | 11 | # here, and why we can't subclass Gtk.Grid directly in |
13 | 12 | # FilteredListSelector. |
14 | 13 |
|
| 14 | + |
| 15 | +class FilteredListSelector: |
| 16 | + |
| 17 | + # FilteredListSelector was intially based on gedit's |
| 18 | + # GeditHighlightModeSelector |
| 19 | + # Copyright (C) 2013 - Ignacio Casal Quinteiro |
| 20 | + # Python translation and adaptations |
| 21 | + # Copyright (C) 2015, 2017 Kai Willadsen <kai.willadsen@gmail.com> |
| 22 | + |
| 23 | + __gtype_name__ = 'FilteredListSelector' |
| 24 | + |
| 25 | + NAME_COLUMN, VALUE_COLUMN = 0, 1 |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + super().__init__() |
| 29 | + self.init_template() |
| 30 | + |
| 31 | + self.treeview_selection = self.treeview.get_selection() |
| 32 | + # FIXME: Should be able to access as a template child, but can't. |
| 33 | + self.listfilter = self.treeview.get_model() |
| 34 | + self.liststore = self.listfilter.get_model() |
| 35 | + |
| 36 | + self.populate_model() |
| 37 | + self.filter_string = '' |
| 38 | + self.entry.connect('changed', self.on_entry_changed) |
| 39 | + self.listfilter.set_visible_func(self.name_filter) |
| 40 | + |
| 41 | + self.entry.connect('activate', self.on_activate) |
| 42 | + self.treeview.connect('row-activated', self.on_activate) |
| 43 | + |
| 44 | + def populate_model(self): |
| 45 | + raise NotImplementedError |
| 46 | + |
| 47 | + def select_value(self, value): |
| 48 | + if not value: |
| 49 | + return |
| 50 | + |
| 51 | + new_value_getter = getattr(value, self.value_accessor) |
| 52 | + for row in self.liststore: |
| 53 | + row_value = row[self.VALUE_COLUMN] |
| 54 | + if not row_value: |
| 55 | + continue |
| 56 | + old_value_getter = getattr(row_value, self.value_accessor) |
| 57 | + if old_value_getter() != new_value_getter(): |
| 58 | + continue |
| 59 | + self.treeview_selection.select_path(row.path) |
| 60 | + self.treeview.scroll_to_cell(row.path, None, True, 0.5, 0) |
| 61 | + |
| 62 | + def name_filter(self, model, it, *args): |
| 63 | + if not self.filter_string: |
| 64 | + return True |
| 65 | + name = model.get_value(it, self.NAME_COLUMN).lower() |
| 66 | + return self.filter_string.lower() in name |
| 67 | + |
| 68 | + def on_entry_changed(self, entry): |
| 69 | + self.filter_string = entry.get_text() |
| 70 | + self.listfilter.refilter() |
| 71 | + first = self.listfilter.get_iter_first() |
| 72 | + if first: |
| 73 | + self.treeview_selection.select_iter(first) |
| 74 | + |
| 75 | + def on_activate(self, *args): |
| 76 | + model, it = self.treeview_selection.get_selected() |
| 77 | + if not it: |
| 78 | + return |
| 79 | + value = model.get_value(it, self.VALUE_COLUMN) |
| 80 | + self.emit(self.change_signal_name, value) |
| 81 | + |
| 82 | + |
15 | 83 | # The subclassing here is weird; the Selector must directly subclass |
16 | 84 | # Gtk.Grid; we can't do this on the FilteredListSelector. Likewise, the |
17 | 85 | # Template.Child attributes must be per-class, because of how they're |
|
0 commit comments