delete item after the iteration to find its index; closes #52

This commit is contained in:
Dominik Demuth 2023-04-27 19:08:14 +02:00
parent def2a99ed8
commit 9479364a64

View File

@ -1,6 +1,5 @@
from __future__ import annotations from __future__ import annotations
from nmreval.lib.logger import logger
from nmreval.lib.colors import available_cycles from nmreval.lib.colors import available_cycles
from .properties import PropWidget from .properties import PropWidget
@ -253,6 +252,7 @@ class DataTree(QtWidgets.QTreeWidget):
graph_cnt = -1 graph_cnt = -1
set_cnt = 0 set_cnt = 0
iterator = QtWidgets.QTreeWidgetItemIterator(self) iterator = QtWidgets.QTreeWidgetItemIterator(self)
self.blockSignals(True)
while iterator.value(): while iterator.value():
item = iterator.value() item = iterator.value()
if item is not None: if item is not None:
@ -267,6 +267,7 @@ class DataTree(QtWidgets.QTreeWidget):
iterator += 1 iterator += 1
self.resizeColumnToContents(1) self.resizeColumnToContents(1)
self.blockSignals(False)
def set_name(self, sid, name): def set_name(self, sid, name):
iterator = QtWidgets.QTreeWidgetItemIterator(self) iterator = QtWidgets.QTreeWidgetItemIterator(self)
@ -345,28 +346,45 @@ class DataTree(QtWidgets.QTreeWidget):
def remove_item(self, ids: list[str]): def remove_item(self, ids: list[str]):
iterator = QtWidgets.QTreeWidgetItemIterator(self) iterator = QtWidgets.QTreeWidgetItemIterator(self)
toberemoved = []
graph_removal = []
# find all items that have to be removed
while iterator.value(): while iterator.value():
item = iterator.value() item = iterator.value()
_id = item.data(0, QtCore.Qt.UserRole) _id = item.data(0, QtCore.Qt.UserRole)
if _id in ids: if _id in ids:
try: try:
if item.parent() is None: item_parent = item.parent()
if item_parent is None:
raise AttributeError raise AttributeError
idx = item.parent().indexOfChild(item) idx = item_parent.indexOfChild(item)
item.parent().takeChild(idx) # item.parent().takeChild(idx)
toberemoved.append((item_parent, idx))
if _id in self._checked_sets: if _id in self._checked_sets:
self._checked_sets.remove(_id) self._checked_sets.remove(_id)
except AttributeError: except AttributeError:
idx = self.invisibleRootItem().indexOfChild(item) idx = self.invisibleRootItem().indexOfChild(item)
self.invisibleRootItem().takeChild(idx) # self.invisibleRootItem().takeChild(idx)
try:
graph_removal.append(idx)
if _id in self._checked_graphs:
self._checked_graphs.remove(_id) self._checked_graphs.remove(_id)
except KeyError:
logger.warn(f'Graph {_id} already removed, skip')
iterator += 1 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() self.update_indexes()
def contextMenuEvent(self, evt): def contextMenuEvent(self, evt):