changeset 19d46cf0e25c in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset&node=19d46cf0e25c
description:
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
diffstat:
tryton/gui/window/view_form/view/form_gtk/dictionary.py | 51 +++++++++-------
1 files changed, 28 insertions(+), 23 deletions(-)
diffs (79 lines):
diff -r 941b90300b84 -r 19d46cf0e25c
tryton/gui/window/view_form/view/form_gtk/dictionary.py
--- a/tryton/gui/window/view_form/view/form_gtk/dictionary.py Fri Sep 10
19:37:47 2021 +0200
+++ b/tryton/gui/window/view_form/view/form_gtk/dictionary.py Mon Sep 13
23:33:12 2021 +0200
@@ -112,17 +112,15 @@
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 @@
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)