From 46952e1648fbecac6adaaeef0283f6817ab40ff5 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Sat, 7 Jan 2023 18:00:38 +0100 Subject: [PATCH] Sort option to context menu in data tree added; fixes T226 --- src/gui_qt/data/datawidget/datawidget.py | 45 +++++++++++++++++++----- src/gui_qt/main/management.py | 5 ++- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/gui_qt/data/datawidget/datawidget.py b/src/gui_qt/data/datawidget/datawidget.py index 0a73161..8823c7f 100644 --- a/src/gui_qt/data/datawidget/datawidget.py +++ b/src/gui_qt/data/datawidget/datawidget.py @@ -1,4 +1,4 @@ -from typing import List, Tuple, Union +from __future__ import annotations from nmreval.lib.colors import available_cycles @@ -43,7 +43,6 @@ class DataTree(QtWidgets.QTreeWidget): header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch) header.setVisible(False) header.moveSection(1, 0) - # self.setColumnWidth(1, 16) self.setItemDelegateForColumn(1, HeaderDelegate()) def add_graph(self, idd: str, name: str): @@ -60,7 +59,7 @@ class DataTree(QtWidgets.QTreeWidget): self.update_indexes() - def add_item(self, items: Union[tuple, List[tuple]], gid: str): + def add_item(self, items: (tuple | list[tuple]), gid: str): if isinstance(items, tuple): items = [items] @@ -227,6 +226,27 @@ class DataTree(QtWidgets.QTreeWidget): self.blockSignals(False) + def sort(self, graph_item: QtWidgets.QTreeWidgetItem, mode: str = 'value'): + graph_id = graph_item.data(0, QtCore.Qt.UserRole) + sets = self.management.get_attributes(graph_id, mode) + sets = [el[0] for el in sorted(sets.items(), key=lambda x: x[1])] + + self.management.move_sets(sets, graph_id, graph_id, pos=-1) + + self.blockSignals(True) + + children = graph_item.takeChildren() + + for s in sets: + for c in children: + if c.data(0, QtCore.Qt.UserRole) == s: + graph_item.addChild(c) + + self.update_indexes() + + self.blockSignals(False) + + def update_indexes(self): graph_cnt = -1 set_cnt = 0 @@ -358,19 +378,24 @@ class DataTree(QtWidgets.QTreeWidget): def ctx_graphs(self, evt, menu): del_action = menu.addAction('Exterminate graph!') + + sort_menu = menu.addMenu('Sort sets') + for label in ['By name', 'By value']: + sort_menu.addAction(label) + col_menu = menu.addMenu('Color cycle') for c in available_cycles.keys(): col_menu.addAction(c) graphs = [] - + items = [] for i in self.selectedIndexes(): if i.column() == 0: continue - + items.append(self.itemFromIndex(i)) graphs.append(self.itemFromIndex(i).data(0, QtCore.Qt.UserRole)) - action = menu.exec_(evt.globalPos()) + action = menu.exec(evt.globalPos()) if action == del_action: for gid in graphs: self.management.delete_graph(gid) @@ -379,6 +404,10 @@ class DataTree(QtWidgets.QTreeWidget): for gid in graphs: self.management.set_cycle(self.management.graphs[gid].sets, action.text()) + elif action.parent() == sort_menu: + for i in items: + self.sort(i, mode=action.text().split()[1]) + evt.accept() def ctx_sets(self, evt, menu): @@ -456,7 +485,7 @@ class DataTree(QtWidgets.QTreeWidget): item.setBackground(0, QtGui.QBrush()) iterator += 1 - def uncheck_sets(self, sets: List[str]): + def uncheck_sets(self, sets: list[str]): self.blockSignals(True) iterator = QtWidgets.QTreeWidgetItemIterator(self) while iterator.value(): @@ -537,7 +566,7 @@ class DataWidget(QtWidgets.QWidget, Ui_DataWidget): self.propertyChanged.emit(ids, key1, key2, value) - def uncheck_sets(self, sets: List[str]): + def uncheck_sets(self, sets: list[str]): self.tree.uncheck_sets(sets) def set_name(self, sid, value): diff --git a/src/gui_qt/main/management.py b/src/gui_qt/main/management.py index af2c62a..018ff84 100644 --- a/src/gui_qt/main/management.py +++ b/src/gui_qt/main/management.py @@ -144,6 +144,9 @@ class UpperManagement(QtCore.QObject): def active_sets(self): return self.graphs.active(self.current_graph) + def get_attributes(self, graph_id: str, attr: str) -> dict[str, Any]: + return {self.data[i].id: getattr(self.data[i], attr) for i in self.graphs[graph_id].sets} + def add(self, data, **kwargs): _id = str(uuid.uuid4()) self.__setitem__(_id, data, **kwargs) @@ -230,7 +233,7 @@ class UpperManagement(QtCore.QObject): self.graphs[self.data[key].graph].remove(key) @QtCore.pyqtSlot(list, str, str) - def move_sets(self, sets: list, dest: str, src, pos: int = -1): + def move_sets(self, sets: list, dest: str, src: (str|list), pos: int = -1): if isinstance(src, str): src = [src]*len(sets)