From 3321d8520350bdb258c1314b2959c26c2592f46b Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Thu, 13 Jul 2023 19:51:19 +0200 Subject: [PATCH] set integration limits via keyboard; closes #50 --- src/gui_qt/_py/integral_widget.py | 5 ++-- src/gui_qt/data/integral_widget.py | 39 ++++++++++++++++++++++++---- src/resources/_ui/integral_widget.ui | 3 --- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/gui_qt/_py/integral_widget.py b/src/gui_qt/_py/integral_widget.py index 81ba165..0d5e015 100644 --- a/src/gui_qt/_py/integral_widget.py +++ b/src/gui_qt/_py/integral_widget.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- -# Form implementation generated from reading ui file '/autohome/dominik/nmreval/src/resources/_ui/integral_widget.ui' +# Form implementation generated from reading ui file 'src/resources/_ui/integral_widget.ui' # -# Created by: PyQt5 UI code generator 5.15.4 +# Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. @@ -26,7 +26,6 @@ class Ui_Form(object): self.set_combobox.setObjectName("set_combobox") self.verticalLayout.addWidget(self.set_combobox) self.treeWidget = QtWidgets.QTreeWidget(Form) - self.treeWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) self.treeWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.treeWidget.setHeaderHidden(True) self.treeWidget.setObjectName("treeWidget") diff --git a/src/gui_qt/data/integral_widget.py b/src/gui_qt/data/integral_widget.py index baeaa90..ea938ac 100644 --- a/src/gui_qt/data/integral_widget.py +++ b/src/gui_qt/data/integral_widget.py @@ -42,6 +42,8 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): self.max_y = inf self.min_y = -inf + self.treeWidget.itemChanged.connect(self._update_by_tree) + def __call__(self, graph_name, items): self.label_2.setText(f'Connected to {graph_name}\nChanging tab will remove all integration limits.') @@ -77,7 +79,6 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): for idx, rnge in enumerate(self.ranges): self._update_values(idx, rnge) - def add(self, pos): x = pos[0] self.ranges.append((x, x*1.1)) @@ -108,7 +109,10 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): item_list = [] for text, val in [('Start', pts_i[0]), ('Stop', pts_i[1]), ('Areas', 0), ('Ratio', 1.)]: child = QtWidgets.QTreeWidgetItem() - child.setFlags(QtCore.Qt.NoItemFlags) + if text.startswith('S'): + child.setFlags(child.flags() | QtCore.Qt.ItemIsEditable) + else: + child.setFlags(QtCore.Qt.NoItemFlags) child.setText(0, f'{text}: {val:.5g}') child.setForeground(0, QtGui.QBrush(QtGui.QColor('black'))) @@ -121,8 +125,27 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): self._update_values(len(self.ranges) - 1, pts_i) + def _update_by_tree(self, item: QtWidgets.QTreeWidgetItem) -> None: + parent_item = item.parent() + idx = self.treeWidget.invisibleRootItem().indexOfChild(parent_item) + + is_left_border = parent_item.indexOfChild(item) == 0 + current_region = self.lines[idx][0] + current_limits = current_region.getRegion() + + new_value = item.text(0) + try: + new_value = float(new_value) + if is_left_border: + current_region.setRegion((new_value, current_limits[1])) + else: + current_region.setRegion((current_limits[0], new_value)) + except ValueError: + self._update_values(idx, current_limits) + def _update_integral(self): idx = None + reg = None sender = self.sender() for i, (reg, _) in enumerate(self.lines): if sender == reg: @@ -132,19 +155,20 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): if idx is None: return - self._update_values(idx, sender.getRegion()) + self._update_values(idx, reg.getRegion()) def _update_values(self, idx, new_range): self.ranges[idx] = new_range area = self.make_integral(idx, *new_range) + self.treeWidget.blockSignals(True) + item = self.treeWidget.topLevelItem(idx) item.child(0).setText(0, f'Start: {new_range[0]:.5g}') item.child(1).setText(0, f'Stop: {new_range[1]:.5g}') if area is not None: - self.areas[idx] = area item.child(2).setText(0, f'Area: {area:.5g}') if self.max_area > 0: self._set_ratios(idx, self.max_area) @@ -157,9 +181,12 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): self._set_ratios(i, curr_max) self.max_area = curr_max + self.treeWidget.blockSignals(False) + def _set_ratios(self, idx, max_value): item = self.treeWidget.invisibleRootItem().child(idx) area_i = self.areas[idx] + item.child(3).setText(0, f'Ratio: {area_i / max_value:.3g}') integral_line = self.lines[idx][1] @@ -176,7 +203,9 @@ class IntegralWidget(QtWidgets.QWidget, Ui_Form): if integral.size != 0: area = integral[-1, 1] scale = (self.max_y-self.min_y) / area - self.lines[idx][1].setData(x=integral[:, 0], y=integral[:, 1]*scale + self.min_y) + self.lines[idx][1].setData(x=integral[:, 0], y=integral[:, 1]*scale + self.min_y) + + self.areas[idx] = area return area diff --git a/src/resources/_ui/integral_widget.ui b/src/resources/_ui/integral_widget.ui index 9569a7e..23f95b1 100644 --- a/src/resources/_ui/integral_widget.ui +++ b/src/resources/_ui/integral_widget.ui @@ -41,9 +41,6 @@ - - QAbstractItemView::NoEditTriggers - QAbstractItemView::SelectRows