Skip to content

Commit 1d7275e

Browse files
committed
ui.bufferselectors: Absorb FilteredListSelector
It was mostly separate because of the template hack. Now it seems reasonable to have all this logic together.
1 parent 1029ce2 commit 1d7275e

2 files changed

Lines changed: 69 additions & 68 deletions

File tree

meld/ui/bufferselectors.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,81 @@
55

66
from meld.conf import _
77
from meld.ui._gtktemplate import Template
8-
from meld.ui.listselector import FilteredListSelector
98

109
# TODO: Current pygobject support for templates excludes subclassing of
1110
# templated classes, which is why we have two near-identical UI files
1211
# here, and why we can't subclass Gtk.Grid directly in
1312
# FilteredListSelector.
1413

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+
1583
# The subclassing here is weird; the Selector must directly subclass
1684
# Gtk.Grid; we can't do this on the FilteredListSelector. Likewise, the
1785
# Template.Child attributes must be per-class, because of how they're

meld/ui/listselector.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)