From 5975a3dbfcddbfd66a8481842fe0e63718bc2829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 13 Sep 2021 23:33:12 +0200 Subject: [PATCH] Use model of ComboBox to set active entry The value of the ComboBox must be set using index instead of relying on the displayed value because it may not be unique and the popdown must preselect the current value. issue10725 review338851002 --- .../view_form/view/form_gtk/dictionary.py | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/tryton/gui/window/view_form/view/form_gtk/dictionary.py b/tryton/gui/window/view_form/view/form_gtk/dictionary.py index 2f91c22155..163360c1e6 100644 --- a/tryton/gui/window/view_form/view/form_gtk/dictionary.py +++ b/tryton/gui/window/view_form/view/form_gtk/dictionary.py @@ -112,17 +112,15 @@ class DictSelectionEntry(DictEntry): selection_shortcuts(widget) # setting completion and selection - model = Gtk.ListStore(GObject.TYPE_STRING) - model.append(('',)) - self._selection = {'': None} + model = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_PYOBJECT) + model.append(('', "")) width = 10 selection = self.definition['selection'] if self.definition.get('sort', True): selection.sort(key=operator.itemgetter(1)) for value, name in selection: name = str(name) - self._selection[name] = value - model.append((name,)) + model.append((name, value)) width = max(width, len(name)) widget.set_model(model) widget.set_entry_text_column(0) @@ -132,29 +130,36 @@ class DictSelectionEntry(DictEntry): completion.set_model(model) child.set_completion(completion) completion.set_text_column(0) + completion.connect('match-selected', self.match_selected) return widget + def match_selected(self, completion, model, iter_): + value, = model.get(iter_, 1) + model = self.widget.get_model() + for i, selection in enumerate(model): + if selection[1] == value: + GLib.idle_add(self.widget.set_active, i) + break + def get_value(self): - child = self.widget.get_child() - if not child: # widget is destroyed - return - text = child.get_text() - value = None - if text: - for txt, val in list(self._selection.items()): - if not val: - continue - if txt[:len(text)].lower() == text.lower(): - value = val - if len(txt) == len(text): - break - return value + active = self.widget.get_active() + if active < 0: + return None + else: + model = self.widget.get_model() + return model[active][1] def set_value(self, value): - values = dict(self.definition['selection']) - child = self.widget.get_child() - child.set_text(values.get(value, '')) - reset_position(child) + active = -1 + model = self.widget.get_model() + for i, selection in enumerate(model): + if selection[1] == value: + active = i + break + self.widget.set_active(active) + if active == -1: + # When setting no item GTK doesn't clear the entry + self.widget.get_child().set_text('') def set_readonly(self, readonly): self.widget.set_sensitive(not readonly)