ui: allow editing of rules with parameters

This commit is contained in:
Peter F. Patel-Schneider 2022-12-11 12:15:02 -05:00
parent 39ae8d4b32
commit d6872422fd
1 changed files with 33 additions and 16 deletions

View File

@ -1428,36 +1428,53 @@ class TestUI(ConditionUI):
self.widgets = {} self.widgets = {}
self.label = Gtk.Label(valign=Gtk.Align.CENTER, hexpand=True) self.label = Gtk.Label(valign=Gtk.Align.CENTER, hexpand=True)
self.label.set_text(_('Test condition on notification triggering rule processing.')) self.label.set_text(_('Test condition on notification triggering rule processing.'))
self.widgets[self.label] = (0, 0, 1, 1) self.widgets[self.label] = (0, 0, 4, 1)
self.field = Gtk.ComboBoxText.new_with_entry() lbl = Gtk.Label(_('Test'), halign=Gtk.Align.CENTER, valign=Gtk.Align.END, hexpand=False, vexpand=False)
self.field.append('', '') self.widgets[lbl] = (0, 1, 1, 1)
lbl = Gtk.Label(_('Parameter'), halign=Gtk.Align.CENTER, valign=Gtk.Align.END, hexpand=False, vexpand=False)
self.widgets[lbl] = (2, 1, 1, 1)
self.test = Gtk.ComboBoxText.new_with_entry()
self.test.append('', '')
for t in _DIV.TESTS: for t in _DIV.TESTS:
self.field.append(t, t) self.test.append(t, t)
self.field.set_valign(Gtk.Align.CENTER) self.test.set_halign(Gtk.Align.END)
# self.field.set_vexpand(True) self.test.set_valign(Gtk.Align.CENTER)
self.field.set_size_request(600, 0) self.test.set_hexpand(False)
CompletionEntry.add_completion_to_entry(self.field.get_child(), _DIV.TESTS) self.test.set_size_request(300, 0)
self.field.connect('changed', self._on_update) CompletionEntry.add_completion_to_entry(self.test.get_child(), _DIV.TESTS)
self.widgets[self.field] = (0, 1, 1, 1) self.test.connect('changed', self._on_update)
self.widgets[self.test] = (1, 1, 1, 1)
self.parameter = Gtk.Entry(halign=Gtk.Align.CENTER, valign=Gtk.Align.END, hexpand=True)
self.parameter.set_size_request(150, 0)
self.parameter.connect('changed', self._on_update)
self.widgets[self.parameter] = (3, 1, 1, 1)
def show(self, component, editable): def show(self, component, editable):
super().show(component, editable) super().show(component, editable)
with self.ignore_changes(): with self.ignore_changes():
self.field.set_active_id(component.test or '') self.test.set_active_id(component.test)
self.parameter.set_text(str(component.parameter) if component.parameter is not None else '')
if component.test not in _DIV.TESTS: if component.test not in _DIV.TESTS:
self.field.get_child().set_text(component.test) self.test.get_child().set_text(component.test)
self._change_status_icon() self._change_status_icon()
def collect_value(self): def collect_value(self):
return (self.field.get_active_text() or '').strip() try:
param = int(self.parameter.get_text()) if self.parameter.get_text() else None
except Exception:
param = self.parameter.get_text()
test = (self.test.get_active_text() or '').strip()
return [test, param] if param is not None else [test]
def _on_update(self, *args): def _on_update(self, *args):
super()._on_update(*args) super()._on_update(*args)
self._change_status_icon() self._change_status_icon()
def _change_status_icon(self): def _change_status_icon(self):
icon = 'dialog-warning' if self.component.test not in _DIV.TESTS else '' icon = 'dialog-warning' if (self.test.get_active_text() or '').strip() not in _DIV.TESTS else ''
self.field.get_child().set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, icon) self.test.get_child().set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, icon)
@classmethod @classmethod
def left_label(cls, component): def left_label(cls, component):
@ -1465,7 +1482,7 @@ class TestUI(ConditionUI):
@classmethod @classmethod
def right_label(cls, component): def right_label(cls, component):
return str(component.test) return component.test + (' ' + repr(component.parameter) if component.parameter is not None else '')
_TestBytesElement = namedtuple('TestBytesElement', ['id', 'label', 'min', 'max']) _TestBytesElement = namedtuple('TestBytesElement', ['id', 'label', 'min', 'max'])