From 881eff2770b1b17cbff1db004255c9bb251c5afd Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Wed, 7 Feb 2024 18:11:15 +0000 Subject: [PATCH] update tool tip; fixes #234 Co-authored-by: Dominik Demuth Reviewed-on: https://gitea.pkm.physik.tu-darmstadt.de/IPKM/nmreval/pulls/238 --- src/gui_qt/data/datawidget/datawidget.py | 106 +++++++++++++---------- src/gui_qt/data/datawidget/properties.py | 6 +- 2 files changed, 65 insertions(+), 47 deletions(-) diff --git a/src/gui_qt/data/datawidget/datawidget.py b/src/gui_qt/data/datawidget/datawidget.py index 45ffc52..e24c10d 100644 --- a/src/gui_qt/data/datawidget/datawidget.py +++ b/src/gui_qt/data/datawidget/datawidget.py @@ -49,11 +49,16 @@ class DataTree(QtWidgets.QTreeWidget): def add_graph(self, idd: str, name: str): item = QtWidgets.QTreeWidgetItem() - item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsDropEnabled | QtCore.Qt.ItemIsEditable | - QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable) + item.setFlags( + QtCore.Qt.ItemFlag.ItemIsSelectable | + QtCore.Qt.ItemFlag.ItemIsDropEnabled | + QtCore.Qt.ItemFlag.ItemIsEditable | + QtCore.Qt.ItemFlag.ItemIsEnabled | + QtCore.Qt.ItemFlag.ItemIsUserCheckable + ) item.setText(0, name) - item.setData(0, QtCore.Qt.UserRole, idd) - item.setCheckState(0, QtCore.Qt.Checked) + item.setData(0, QtCore.Qt.ItemDataRole.UserRole, idd) + item.setCheckState(0, QtCore.Qt.CheckState.Checked) self.addTopLevelItem(item) self._checked_graphs.add(idd) @@ -67,14 +72,19 @@ class DataTree(QtWidgets.QTreeWidget): for row in range(self.invisibleRootItem().childCount()): graph = self.invisibleRootItem().child(row) - if graph.data(0, QtCore.Qt.UserRole) == gid: + if graph.data(0, QtCore.Qt.ItemDataRole.UserRole) == gid: for (idd, name, value) in items: item = QtWidgets.QTreeWidgetItem([name]) item.setToolTip(0, f'Value: {value}') - item.setData(0, QtCore.Qt.UserRole, idd) - item.setCheckState(0, QtCore.Qt.Checked) - item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsEditable | - QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable) + item.setData(0, QtCore.Qt.ItemDataRole.UserRole, idd) + item.setCheckState(0, QtCore.Qt.CheckState.Checked) + item.setFlags( + QtCore.Qt.ItemFlag.ItemIsSelectable | + QtCore.Qt.ItemFlag.ItemIsDragEnabled | + QtCore.Qt.ItemFlag.ItemIsEditable | + QtCore.Qt.ItemFlag.ItemIsEnabled | + QtCore.Qt.ItemFlag.ItemIsUserCheckable + ) graph.addChild(item) self._checked_sets.add(idd) @@ -85,8 +95,8 @@ class DataTree(QtWidgets.QTreeWidget): @QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem) def data_change(self, item: QtWidgets.QTreeWidgetItem, emit: bool = True) -> tuple[set, set]: - idd = item.data(0, QtCore.Qt.UserRole) - is_selected = item.checkState(0) == QtCore.Qt.Checked + idd = item.data(0, QtCore.Qt.ItemDataRole.UserRole) + is_selected = item.checkState(0) == QtCore.Qt.CheckState.Checked to_be_hidden = set() to_be_shown = set() @@ -104,9 +114,9 @@ class DataTree(QtWidgets.QTreeWidget): self.blockSignals(True) for i in range(item.childCount()): child = item.child(i) - child.setCheckState(0, QtCore.Qt.Checked) - to_be_shown.add(child.data(0, QtCore.Qt.UserRole)) - self._checked_sets.add(child.data(0, QtCore.Qt.UserRole)) + child.setCheckState(0, QtCore.Qt.CheckState.Checked) + to_be_shown.add(child.data(0, QtCore.Qt.ItemDataRole.UserRole)) + self._checked_sets.add(child.data(0, QtCore.Qt.ItemDataRole.UserRole)) self.blockSignals(False) # check state change to unchecked @@ -115,10 +125,10 @@ class DataTree(QtWidgets.QTreeWidget): self.blockSignals(True) for i in range(item.childCount()): child = item.child(i) - child.setCheckState(0, QtCore.Qt.Unchecked) - to_be_hidden.add(child.data(0, QtCore.Qt.UserRole)) + child.setCheckState(0, QtCore.Qt.CheckState.Unchecked) + to_be_hidden.add(child.data(0, QtCore.Qt.ItemDataRole.UserRole)) try: - self._checked_sets.remove(child.data(0, QtCore.Qt.UserRole)) + self._checked_sets.remove(child.data(0, QtCore.Qt.ItemDataRole.UserRole)) except KeyError: pass self.blockSignals(False) @@ -153,7 +163,7 @@ class DataTree(QtWidgets.QTreeWidget): @QtCore.pyqtSlot(QtWidgets.QTreeWidgetItem) def new_selection(self, item: QtWidgets.QTreeWidgetItem): if item.parent() is None: - self.management.select_window(item.data(0, QtCore.Qt.UserRole)) + self.management.select_window(item.data(0, QtCore.Qt.ItemDataRole.UserRole)) def dropEvent(self, evt: QtGui.QDropEvent): dropped_index = self.indexAt(evt.pos()) @@ -179,7 +189,7 @@ class DataTree(QtWidgets.QTreeWidget): from_parent.removeChild(it) tobemoved.append(it) - take_from.append(from_parent.data(0, QtCore.Qt.UserRole)) + take_from.append(from_parent.data(0, QtCore.Qt.ItemDataRole.UserRole)) pos = QtCore.QModelIndex(persistent_drop) if self.dropIndicatorPosition() == QtWidgets.QAbstractItemView.BelowItem: @@ -191,8 +201,8 @@ class DataTree(QtWidgets.QTreeWidget): else: to_parent.insertChildren(row, tobemoved) - self.management.move_sets([it.data(0, QtCore.Qt.UserRole) for it in tobemoved], - to_parent.data(0, QtCore.Qt.UserRole), take_from, + self.management.move_sets([it.data(0, QtCore.Qt.ItemDataRole.UserRole) for it in tobemoved], + to_parent.data(0, QtCore.Qt.ItemDataRole.UserRole), take_from, pos=-1 if append else row) self.update_indexes() @@ -207,7 +217,7 @@ class DataTree(QtWidgets.QTreeWidget): while iterator.value(): item = iterator.value() if item is not None: - data = item.data(0, QtCore.Qt.UserRole) + data = item.data(0, QtCore.Qt.ItemDataRole.UserRole) if data == gid_out: from_parent = item @@ -231,7 +241,7 @@ 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) + graph_id = graph_item.data(0, QtCore.Qt.ItemDataRole.UserRole) sets = self.management.get_attributes(graph_id, mode) sets = [el[0] for el in sorted(sets.items(), key=lambda x: x[1])] @@ -243,7 +253,7 @@ class DataTree(QtWidgets.QTreeWidget): for s in sets: for c in children: - if c.data(0, QtCore.Qt.UserRole) == s: + if c.data(0, QtCore.Qt.ItemDataRole.UserRole) == s: graph_item.addChild(c) self.update_indexes() @@ -276,7 +286,7 @@ class DataTree(QtWidgets.QTreeWidget): while iterator.value(): item = iterator.value() if item is not None: - data = item.data(0, QtCore.Qt.UserRole) + data = item.data(0, QtCore.Qt.ItemDataRole.UserRole) if data == sid: if name != item.text(0): item.setText(0, name) @@ -285,7 +295,7 @@ class DataTree(QtWidgets.QTreeWidget): iterator += 1 def keyPressEvent(self, evt: QtGui.QKeyEvent): - if evt.key() == QtCore.Qt.Key_Delete: + if evt.key() == QtCore.Qt.Key.Key_Delete: rm_sets = [] rm_graphs = [] for idx in self.selectedIndexes(): @@ -296,20 +306,20 @@ class DataTree(QtWidgets.QTreeWidget): if item.parent() is None: for c_i in range(item.childCount()): # add sets inside graph to removal - child_data = item.child(c_i).data(0, QtCore.Qt.UserRole) + child_data = item.child(c_i).data(0, QtCore.Qt.ItemDataRole.UserRole) if child_data not in rm_sets: rm_sets.append(child_data) - rm_graphs.append(item.data(0, QtCore.Qt.UserRole)) + rm_graphs.append(item.data(0, QtCore.Qt.ItemDataRole.UserRole)) else: - item_data = item.data(0, QtCore.Qt.UserRole) + item_data = item.data(0, QtCore.Qt.ItemDataRole.UserRole) if item_data not in rm_sets: rm_sets.append(item_data) # self.deleteItem.emit(rm_sets+rm_graphs) self.management.delete_sets(rm_sets+rm_graphs) - elif evt.key() == QtCore.Qt.Key_Space: + elif evt.key() == QtCore.Qt.Key.Key_Space: sets = [] from_parent = [] @@ -329,7 +339,7 @@ class DataTree(QtWidgets.QTreeWidget): for it in sets: if it in from_parent: continue - it.setCheckState(0, QtCore.Qt.Unchecked if it.checkState(0) == QtCore.Qt.Checked else QtCore.Qt.Checked) + it.setCheckState(0, QtCore.Qt.CheckState.Unchecked if it.checkState(0) == QtCore.Qt.CheckState.Checked else QtCore.Qt.CheckState.Checked) s1, s2 = self.data_change(it, emit=False) to_be_hidden |= s2 to_be_shown |= s1 @@ -353,7 +363,7 @@ class DataTree(QtWidgets.QTreeWidget): # find all items that have to be removed while iterator.value(): item = iterator.value() - _id = item.data(0, QtCore.Qt.UserRole) + _id = item.data(0, QtCore.Qt.ItemDataRole.UserRole) if _id in ids: try: item_parent = item.parent() @@ -431,7 +441,7 @@ class DataTree(QtWidgets.QTreeWidget): if i.column() == 0: continue items.append(self.itemFromIndex(i)) - graphs.append(self.itemFromIndex(i).data(0, QtCore.Qt.UserRole)) + graphs.append(self.itemFromIndex(i).data(0, QtCore.Qt.ItemDataRole.UserRole)) if action == del_action: for gid in graphs: @@ -473,12 +483,12 @@ class DataTree(QtWidgets.QTreeWidget): continue else: - graph_id = parent.data(0, QtCore.Qt.UserRole) + graph_id = parent.data(0, QtCore.Qt.ItemDataRole.UserRole) if graph_id not in idx: idx[graph_id] = [] # collect sets in their graph - idx[graph_id].append(item.data(0, QtCore.Qt.UserRole)) - data = self.management[item.data(0, QtCore.Qt.UserRole)] + idx[graph_id].append(item.data(0, QtCore.Qt.ItemDataRole.UserRole)) + data = self.management[item.data(0, QtCore.Qt.ItemDataRole.UserRole)] if data.mode == 'fit': has_fits = True @@ -523,7 +533,7 @@ class DataTree(QtWidgets.QTreeWidget): while iterator.value(): item = iterator.value() if item is not None: - if item.data(0, QtCore.Qt.UserRole) == gid: + if item.data(0, QtCore.Qt.ItemDataRole.UserRole) == gid: item.setBackground(0, QtGui.QBrush(QtGui.QColor('gray'))) else: item.setBackground(0, QtGui.QBrush()) @@ -536,10 +546,10 @@ class DataTree(QtWidgets.QTreeWidget): while iterator.value(): item = iterator.value() if item is not None: - if item.data(0, QtCore.Qt.UserRole) in sets: - item.setCheckState(0, QtCore.Qt.Unchecked) + if item.data(0, QtCore.Qt.ItemDataRole.UserRole) in sets: + item.setCheckState(0, QtCore.Qt.CheckState.Unchecked) else: - self._checked_sets.add(item.data(0, QtCore.Qt.UserRole)) + self._checked_sets.add(item.data(0, QtCore.Qt.ItemDataRole.UserRole)) iterator += 1 self.blockSignals(False) @@ -594,7 +604,7 @@ class DataWidget(QtWidgets.QWidget, Ui_DataWidget): sid = [] for i in self.tree.selectedIndexes(): if i.column() == 0: - sid.append(i.data(role=QtCore.Qt.UserRole)) + sid.append(i.data(role=QtCore.Qt.ItemDataRole.UserRole)) self.startShowProperty.emit(sid) @@ -603,15 +613,23 @@ class DataWidget(QtWidgets.QWidget, Ui_DataWidget): self.proptable.populate(props) def change_property(self, key1, key2, value): - ids = [item.data(0, QtCore.Qt.UserRole) for item in self.tree.selectedItems()] if key2 == 'Value': try: value = float(value) except ValueError: - QtWidgets.QMessageBox.warning(self, 'Invalid entry', - 'Value %r is not a valid number for `value`.' % value) + QtWidgets.QMessageBox.warning( + self, + 'Invalid entry', + f'Value {value!r} is not a valid number for `value`.') return + ids = [] + for item in self.tree.selectedItems(): + ids.append(item.data(0, QtCore.Qt.ItemDataRole.UserRole)) + item.setToolTip(0, str(value)) + else: + ids = [item.data(0, QtCore.Qt.ItemDataRole.UserRole) for item in self.tree.selectedItems()] + self.propertyChanged.emit(ids, key1, key2, value) def uncheck_sets(self, sets: list[str]): diff --git a/src/gui_qt/data/datawidget/properties.py b/src/gui_qt/data/datawidget/properties.py index dd07087..21e3e2d 100644 --- a/src/gui_qt/data/datawidget/properties.py +++ b/src/gui_qt/data/datawidget/properties.py @@ -41,7 +41,7 @@ class PropWidget(QtWidgets.QWidget): idx = table.indexFromItem(item) self.propertyChanged.emit(self.tab.tabText(tab_idx), table.item(idx.row(), idx.column()-1).text(), - item.data(QtCore.Qt.DisplayRole)) + item.data(QtCore.Qt.ItemDataRole.DisplayRole)) @QtCore.pyqtSlot(int) def tab_change(self, idx: int): @@ -66,10 +66,10 @@ class PropTable(QtWidgets.QTableWidget): self.blockSignals(True) for k, v in prop.items(): value_item = QtWidgets.QTableWidgetItem('') - value_item.setData(QtCore.Qt.DisplayRole, v) + value_item.setData(QtCore.Qt.ItemDataRoleDisplayRole, v) key_item = QtWidgets.QTableWidgetItem(k) - key_item.setFlags(QtCore.Qt.NoItemFlags) + key_item.setFlags(QtCore.Qt.ItemDataRole.NoItemFlags) key_item.setForeground(QtGui.QBrush(QtGui.QColor(0, 0, 0))) self.setRowCount(self.rowCount()+1)