Skip to content

Commit

Permalink
Lists: Improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
infirit committed Dec 25, 2024
1 parent 6c6fbfa commit 6e24e30
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
12 changes: 6 additions & 6 deletions blueman/gui/DeviceList.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ def do_cache(self, tree_iter: Gtk.TreeIter, kwargs: dict[str, Any]) -> None:
self.path_to_row[object_path] = Gtk.TreeRowReference.new(self.liststore,
self.liststore.get_path(tree_iter))

def append(self, **columns: object) -> Gtk.TreeIter:
tree_iter = super().append(**columns)
self.do_cache(tree_iter, columns)
def append(self, **list_columns: object) -> Gtk.TreeIter:
tree_iter = super().append(**list_columns)
self.do_cache(tree_iter, list_columns)
return tree_iter

def prepend(self, **columns: object) -> Gtk.TreeIter:
tree_iter = super().prepend(**columns)
self.do_cache(tree_iter, columns)
def prepend(self, **list_columns: object) -> Gtk.TreeIter:
tree_iter = super().prepend(**list_columns)
self.do_cache(tree_iter, list_columns)
return tree_iter

def set(self, tree_iter: Gtk.TreeIter, **kwargs: object) -> None:
Expand Down
75 changes: 38 additions & 37 deletions blueman/gui/GenericList.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,47 @@ class ListDataDict(_ListDataDictBase, total=False):

# noinspection PyAttributeOutsideInit
class GenericList(Gtk.TreeView):
def __init__(self, data: Iterable[ListDataDict], headers_visible: bool = True, visible: bool = False) -> None:
def __init__(self, rowdata: Iterable[ListDataDict], headers_visible: bool = True, visible: bool = False) -> None:
super().__init__(headers_visible=headers_visible, visible=visible)
self.set_name("GenericList")
self.selection = self.get_selection()
self._load(data)
self._load(rowdata)

def _load(self, data: Iterable[ListDataDict]) -> None:
self.ids: dict[str, int] = {}
self.columns: dict[str, Gtk.TreeViewColumn] = {}
# Mapping of internal rowdata id to the ListStore column number and TreeViewColumn
self.list_col_order: dict[str, int] = {}
self.view_columns: dict[str, Gtk.TreeViewColumn] = {}

types = [row["type"] for row in data]
list_col_types = [row["type"] for row in data]

self.liststore = Gtk.ListStore(*types)
self.liststore = Gtk.ListStore(*list_col_types)
self.filter = self.liststore.filter_new()
self.set_model(self.filter)

for i, row in enumerate(data):
self.ids[row["id"]] = i
for list_col_num, row in enumerate(data):
self.list_col_order[row["id"]] = list_col_num

if "renderer" not in row:
continue

column = Gtk.TreeViewColumn()
column.pack_start(row["renderer"], True)
column.set_attributes(row["renderer"], **row["render_attrs"])
view_column = Gtk.TreeViewColumn()
view_column.pack_start(row["renderer"], True)
view_column.set_attributes(row["renderer"], **row["render_attrs"])

if "view_props" in row:
column.set_properties(**row["view_props"])
view_column.set_properties(**row["view_props"])

if "celldata_func" in row:
func, user_data = row["celldata_func"]
column.set_cell_data_func(row["renderer"], func, user_data)
view_column.set_cell_data_func(row["renderer"], func, user_data)

self.columns[row["id"]] = column
self.append_column(column)
self.view_columns[row["id"]] = view_column
self.append_column(view_column)

def selected(self) -> Gtk.TreeIter | None:
model, tree_iter = self.selection.get_selected()
list_model, tree_iter = self.selection.get_selected()
if tree_iter is not None:
tree_iter = model.convert_iter_to_child_iter(tree_iter)
tree_iter = list_model.convert_iter_to_child_iter(tree_iter)
return tree_iter

def delete(self, tree_iter: Gtk.TreeIter) -> bool:
Expand All @@ -70,41 +71,41 @@ def delete(self, tree_iter: Gtk.TreeIter) -> bool:
else:
return False

def _add(self, **columns: object) -> Collection[object]:
def _add(self, **list_columns: object) -> Collection[object]:
items: dict[int, object] = {}
for k, v in self.ids.items():
items[v] = None
for col_id, list_col_num in self.list_col_order.items():
items[list_col_num] = None

for k, val in columns.items():
if k in self.ids:
items[self.ids[k]] = val
for col_id, col_value in list_columns.items():
if col_id in self.list_col_order:
items[self.list_col_order[col_id]] = col_value
else:
raise Exception(f"Invalid key {k}")
raise Exception(f"Invalid key {col_id}")

return items.values()

def append(self, **columns: object) -> Gtk.TreeIter:
vals = self._add(**columns)
def append(self, **list_columns: object) -> Gtk.TreeIter:
vals = self._add(**list_columns)
return self.liststore.append(vals)

def prepend(self, **columns: object) -> Gtk.TreeIter:
vals = self._add(**columns)
def prepend(self, **list_columns: object) -> Gtk.TreeIter:
vals = self._add(**list_columns)
return self.liststore.prepend(vals)

def set(self, tree_iter: Gtk.TreeIter, **cols: object) -> None:
for k, v in cols.items():
self.liststore.set(tree_iter, self.ids[k], v)
def set(self, tree_iter: Gtk.TreeIter, **list_columns: object) -> None:
for col_id, col_value in list_columns.items():
self.liststore.set(tree_iter, self.list_col_order[col_id], col_value)

def get(self, tree_iter: Gtk.TreeIter, *items: str) -> dict[str, Any]:
row_data = {}
data = {}
if not items:
columns = [(name, self.ids[name]) for name in self.ids]
columns = [(col_id, self.list_col_order[col_id]) for col_id in self.list_col_order]
else:
columns = [(name, self.ids[name]) for name in items if name in self.ids]
columns = [(col_id, self.list_col_order[col_id]) for col_id in items if col_id in self.list_col_order]

for name, colid in columns:
row_data[name] = self.liststore.get_value(tree_iter, colid)
return row_data
for col_id, list_col_num in columns:
data[col_id] = self.liststore.get_value(tree_iter, list_col_num)
return data

def get_iter(self, path: Gtk.TreePath | None) -> Gtk.TreeIter | None:
if path is None:
Expand Down
20 changes: 10 additions & 10 deletions blueman/gui/manager/ManagerDeviceList.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ def _on_settings_changed(self, settings: Gio.Settings, key: str) -> None:
else:
sort_type = Gtk.SortType.DESCENDING

column_id = self.ids.get(sort_by)
list_col_num = self.list_col_order.get(sort_by)

if column_id:
self.liststore.set_sort_column_id(column_id, sort_type)
if list_col_num:
self.liststore.set_sort_column_id(list_col_num, sort_type)

def on_icon_theme_changed(self, _icon_them: Gtk.IconTheme) -> None:
for row in self.liststore:
Expand Down Expand Up @@ -560,7 +560,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk
self.tooltip_col = path[1]
return False

if path[1] == self.columns["device_surface"]:
if path[1] == self.view_columns["device_surface"]:
tree_iter = self.get_iter(path[0])
assert tree_iter is not None

Expand All @@ -585,9 +585,9 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk
self.tooltip_col = path[1]
return True

elif path[1] == self.columns["battery_pb"] \
or path[1] == self.columns["tpl_pb"] \
or path[1] == self.columns["rssi_pb"]:
elif path[1] == self.view_columns["battery_pb"] \
or path[1] == self.view_columns["tpl_pb"] \
or path[1] == self.view_columns["rssi_pb"]:
tree_iter = self.get_iter(path[0])
assert tree_iter is not None

Expand All @@ -602,7 +602,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk
tpl = self.get(tree_iter, "tpl")["tpl"]

if battery != 0:
if path[1] == self.columns["battery_pb"]:
if path[1] == self.view_columns["battery_pb"]:
lines.append(f"<b>Battery: {int(battery)}%</b>")
else:
lines.append(f"Battery: {int(battery)}%")
Expand All @@ -619,7 +619,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk
else:
rssi_state = _("Too much")

if path[1] == self.columns["rssi_pb"]:
if path[1] == self.view_columns["rssi_pb"]:
lines.append(_("<b>Received Signal Strength: %(rssi)u%%</b> <i>(%(rssi_state)s)</i>") %
{"rssi": rssi, "rssi_state": rssi_state})
else:
Expand All @@ -638,7 +638,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk
else:
tpl_state = _("Very High")

if path[1] == self.columns["tpl_pb"]:
if path[1] == self.view_columns["tpl_pb"]:
lines.append(_("<b>Transmit Power Level: %(tpl)u%%</b> <i>(%(tpl_state)s)</i>") %
{"tpl": tpl, "tpl_state": tpl_state})
else:
Expand Down

0 comments on commit 6e24e30

Please sign in to comment.