From 9479364a643d557ff5836d2a0f045000d36727df Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Thu, 27 Apr 2023 19:08:14 +0200 Subject: [PATCH] delete item after the iteration to find its index; closes #52 --- src/gui_qt/data/datawidget/datawidget.py | 34 ++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/gui_qt/data/datawidget/datawidget.py b/src/gui_qt/data/datawidget/datawidget.py index 1051bb8..0bd67b8 100644 --- a/src/gui_qt/data/datawidget/datawidget.py +++ b/src/gui_qt/data/datawidget/datawidget.py @@ -1,6 +1,5 @@ from __future__ import annotations -from nmreval.lib.logger import logger from nmreval.lib.colors import available_cycles from .properties import PropWidget @@ -253,6 +252,7 @@ class DataTree(QtWidgets.QTreeWidget): graph_cnt = -1 set_cnt = 0 iterator = QtWidgets.QTreeWidgetItemIterator(self) + self.blockSignals(True) while iterator.value(): item = iterator.value() if item is not None: @@ -267,6 +267,7 @@ class DataTree(QtWidgets.QTreeWidget): iterator += 1 self.resizeColumnToContents(1) + self.blockSignals(False) def set_name(self, sid, name): iterator = QtWidgets.QTreeWidgetItemIterator(self) @@ -345,28 +346,45 @@ class DataTree(QtWidgets.QTreeWidget): def remove_item(self, ids: list[str]): iterator = QtWidgets.QTreeWidgetItemIterator(self) + + toberemoved = [] + graph_removal = [] + + # find all items that have to be removed while iterator.value(): item = iterator.value() _id = item.data(0, QtCore.Qt.UserRole) if _id in ids: try: - if item.parent() is None: + item_parent = item.parent() + if item_parent is None: raise AttributeError - idx = item.parent().indexOfChild(item) - item.parent().takeChild(idx) + idx = item_parent.indexOfChild(item) + # item.parent().takeChild(idx) + + toberemoved.append((item_parent, idx)) + if _id in self._checked_sets: self._checked_sets.remove(_id) + except AttributeError: idx = self.invisibleRootItem().indexOfChild(item) - self.invisibleRootItem().takeChild(idx) - try: + # self.invisibleRootItem().takeChild(idx) + + graph_removal.append(idx) + + if _id in self._checked_graphs: self._checked_graphs.remove(_id) - except KeyError: - logger.warn(f'Graph {_id} already removed, skip') iterator += 1 + for (item, set_idx) in sorted(toberemoved, key=lambda x: x[1], reverse=True): + item.takeChild(set_idx) + + for graph_idx in sorted(graph_removal, reverse=True): + self.invisibleRootItem().takeChild(graph_idx) + self.update_indexes() def contextMenuEvent(self, evt):