From 07d275c0dc7228d033b3f699e6b4051861c18560 Mon Sep 17 00:00:00 2001 From: Matthias Hagmann <16444067+MattHag@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:13:38 +0100 Subject: [PATCH] refactor: Make _populate_model a function Related #2378 --- lib/solaar/ui/diversion_rules.py | 64 ++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/lib/solaar/ui/diversion_rules.py b/lib/solaar/ui/diversion_rules.py index 2aecf2c968..74fb2f497e 100644 --- a/lib/solaar/ui/diversion_rules.py +++ b/lib/solaar/ui/diversion_rules.py @@ -22,7 +22,7 @@ from copy import copy from dataclasses import dataclass, field from shlex import quote as shlex_quote -from typing import Dict +from typing import Any, Dict, Optional from gi.repository import Gdk, GObject, Gtk from logitech_receiver import diversion as _DIV @@ -124,6 +124,36 @@ def _menu_do_copy(_mitem: Gtk.MenuItem, m: Gtk.TreeStore, it: Gtk.TreeIter): _rule_component_clipboard = _DIV.RuleComponent().compile(c.data()) +def _populate_model( + model: Gtk.TreeStore, + it: Gtk.TreeIter, + rule_component: Any, + level: int = 0, + pos: int = -1, + editable: Optional[bool] = None, +): + if isinstance(rule_component, list): + for c in rule_component: + _populate_model(model, it, c, level=level, pos=pos, editable=editable) + if pos >= 0: + pos += 1 + return + if editable is None: + editable = model[it][0].editable if it is not None else False + if isinstance(rule_component, _DIV.Rule): + editable = editable or (rule_component.source is not None) + wrapped = RuleComponentWrapper(rule_component, level, editable=editable) + piter = model.insert(it, pos, (wrapped,)) + if isinstance(rule_component, (_DIV.Rule, _DIV.And, _DIV.Or, _DIV.Later)): + for c in rule_component.components: + ed = editable or (isinstance(c, _DIV.Rule) and c.source is not None) + _populate_model(model, piter, c, level + 1, editable=ed) + if len(rule_component.components) == 0: + _populate_model(model, piter, None, level + 1, editable=editable) + elif isinstance(rule_component, _DIV.Not): + _populate_model(model, piter, rule_component.component, level + 1, editable=editable) + + class DiversionDialog: def __init__(self): window = Gtk.Window() @@ -248,7 +278,7 @@ def _create_model(self): if len(_DIV.rules.components) == 1: # only built-in rules - add empty user rule list _DIV.rules.components.insert(0, _DIV.Rule([], source=_DIV._file_path)) - self._populate_model(model, None, _DIV.rules.components) + _populate_model(model, None, _DIV.rules.components) return model def _create_view_columns(self): @@ -267,28 +297,6 @@ def _create_view_columns(self): ) return col1, col2 - def _populate_model(self, model, it, rule_component, level=0, pos=-1, editable=None): - if isinstance(rule_component, list): - for c in rule_component: - self._populate_model(model, it, c, level=level, pos=pos, editable=editable) - if pos >= 0: - pos += 1 - return - if editable is None: - editable = model[it][0].editable if it is not None else False - if isinstance(rule_component, _DIV.Rule): - editable = editable or (rule_component.source is not None) - wrapped = RuleComponentWrapper(rule_component, level, editable=editable) - piter = model.insert(it, pos, (wrapped,)) - if isinstance(rule_component, (_DIV.Rule, _DIV.And, _DIV.Or, _DIV.Later)): - for c in rule_component.components: - ed = editable or (isinstance(c, _DIV.Rule) and c.source is not None) - self._populate_model(model, piter, c, level + 1, editable=ed) - if len(rule_component.components) == 0: - self._populate_model(model, piter, None, level + 1, editable=editable) - elif isinstance(rule_component, _DIV.Not): - self._populate_model(model, piter, rule_component.component, level + 1, editable=editable) - def on_update(self): self.view.queue_draw() self.dirty = True @@ -485,7 +493,7 @@ def _menu_do_flatten(self, _mitem, m, it): parent_c.components = [*parent_c.components[:idx], *c.components, *parent_c.components[idx + 1 :]] children = [child[0].component for child in m[it].iterchildren()] m.remove(it) - self._populate_model(m, parent_it, children, level=wrapped.level, pos=idx) + _populate_model(m, parent_it, children, level=wrapped.level, pos=idx) new_iter = m.iter_nth_child(parent_it, idx) self.view.expand_row(m.get_path(parent_it), True) self.view.get_selection().select_iter(new_iter) @@ -510,7 +518,7 @@ def _menu_do_insert(self, _mitem, m, it, new_c, below=False): new_c.source = _DIV._file_path # new rules will be saved to the YAML file idx += int(below) parent_c.components.insert(idx, new_c) - self._populate_model(m, parent_it, new_c, level=wrapped.level, pos=idx) + _populate_model(m, parent_it, new_c, level=wrapped.level, pos=idx) self.on_update() if len(parent_c.components) == 1: m.remove(it) # remove placeholder in the end @@ -599,7 +607,7 @@ def _menu_do_delete(self, _mitem, m, it): idx = parent_c.components.index(c) parent_c.components.pop(idx) if len(parent_c.components) == 0: # placeholder - self._populate_model(m, parent_it, None, level=wrapped.level) + _populate_model(m, parent_it, None, level=wrapped.level) m.remove(it) self.view.get_selection().select_iter(m.iter_nth_child(parent_it, max(0, min(idx, len(parent_c.components) - 1)))) self.on_update() @@ -642,7 +650,7 @@ def _menu_do_wrap(self, _mitem, m, it, cls): new_c = cls([c], warn=False) parent_c.component = new_c m.remove(it) - self._populate_model(m, parent_it, new_c, level=wrapped.level, pos=0) + _populate_model(m, parent_it, new_c, level=wrapped.level, pos=0) self.view.expand_row(m.get_path(parent_it), True) self.view.get_selection().select_iter(m.iter_nth_child(parent_it, 0)) else: