diff --git a/src/gui_qt/graphs/draw_inputs.py b/src/gui_qt/graphs/draw_inputs.py index c0547ff..b824c91 100644 --- a/src/gui_qt/graphs/draw_inputs.py +++ b/src/gui_qt/graphs/draw_inputs.py @@ -119,6 +119,15 @@ class RectangleWidget(QtWidgets.QWidget, Ui_rectanglewidget): self.right_x.setValidator(QtGui.QDoubleValidator()) self.right_y.setValidator(QtGui.QDoubleValidator()) + def set_args(self, left: tuple = (0, 0), right: tuple = (1, 1), color=None, fill=None): + self.left_x.setText(str(left[0])) + self.left_y.setText(str(left[1])) + self.right_x.setText(str(right[0])) + self.right_y.setText(str(right[1])) + + self.color_box.setCurrentIndex(self.color_box.findData(color, QtCore.Qt.UserRole)) + self.fill_box.setCurrentIndex(self.fill_box.findData(fill, QtCore.Qt.UserRole)) + def collect_args(self) -> dict | None: left = parse_point(self.left_x, self.left_y) if left is None: @@ -128,6 +137,8 @@ class RectangleWidget(QtWidgets.QWidget, Ui_rectanglewidget): if right is None: return + print(self.fill_box.currentIndex(), self.fill_box.currentData(QtCore.Qt.UserRole)) + dic = { 'color': self.color_box.currentData(QtCore.Qt.UserRole), 'fill': self.fill_box.currentData(QtCore.Qt.UserRole), diff --git a/src/gui_qt/graphs/draw_objects.py b/src/gui_qt/graphs/draw_objects.py index 29b3582..c5c9086 100644 --- a/src/gui_qt/graphs/draw_objects.py +++ b/src/gui_qt/graphs/draw_objects.py @@ -11,6 +11,8 @@ from gui_qt.lib.pg_objects import LogInfiniteLine, PlotItem __all__ = ['LineObject', 'MultipointObject', 'RectangleObject', 'TextObject', 'EllipseObject'] +from nmreval.lib.colors import BaseColor + class BaseObject: def __init__(self, color=None): @@ -45,11 +47,12 @@ class LineObject(BaseObject): return {'type': 'line', 'color': self.color, 'pos': self.pos, 'angle': self.angle} def set_values(self, pos=None, angle=None, color=None): - if pos != self.pos: self.drawing.setPos(pos) + if angle != self.angle: self.drawing.setAngle(angle) + if color != self.color: self.drawing.setPen(mkPen(color.rgb())) @@ -94,30 +97,27 @@ class MultipointObject(BaseObject): self._x += (x[0],) self._y += (y[0],) + self.drawing.setPos(self._x, self._y) + + class Rectangle(GraphicsObject): # adapted from pyqtgraph example on custom objects - def __init__(self, left: tuple, right: tuple): + def __init__(self, left: tuple, right: tuple, color: BaseColor, fill: BaseColor): GraphicsObject.__init__(self) - self.p0 = left - self.delta = [right[0]-self.p0[0], right[1]-self.p0[1]] - - self.x_disp = self.p0[0] - self.dx = self.delta[0] - self.y_disp = self.p0[1] - self.dy = self.delta[1] - self.log_mode = [False, False] - self.generatePicture() + self.setData(left, right, color, fill) def generatePicture(self): ## pre-computing a QPicture object allows paint() to run much more quickly, ## rather than re-drawing the shapes every time. + print(self.x_disp, self.y_disp, self.dx, self.dy, self.color, self.fill) self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) - p.setPen(mkPen('w')) - p.setBrush(mkBrush('r')) + if self.fill is not None: + p.setBrush(mkBrush(self.fill.rgb())) + p.setPen(mkPen(self.color.rgb())) p.drawRect(QtCore.QRectF(self.x_disp, self.y_disp, self.dx, self.dy)) p.end() @@ -160,18 +160,54 @@ class Rectangle(GraphicsObject): self.informViewBoundsChanged() -class RectangleObject(BaseObject): - def __init__(self, left: tuple, right: tuple, **kwargs): - super().__init__(**kwargs) + def setData(self, left, right, color, fill): + self.p0 = left + self.delta = [right[0]-self.p0[0], right[1]-self.p0[1]] + self.x_disp = self.p0[0] + self.dx = self.delta[0] + self.y_disp = self.p0[1] + self.dy = self.delta[1] + + self.color = color + self.fill = fill + + l = [self.log_mode[0], self.log_mode[1]] + self.log_mode = [None, None] + + self.setLogMode(*l) + + +class RectangleObject(BaseObject): + def __init__(self, left: tuple, right: tuple, fill: BaseColor, **kwargs): + super().__init__(**kwargs) + self.fill = fill self.left = left self.right = right - self.drawing = Rectangle(left, right) + self.drawing = Rectangle(left, right, self.color, self.fill) def __str__(self): return f'Rectangle {self.left} to {self.right}' + def get_values(self): + return {'type': 'rectangle', 'color': self.color, 'left': self.left, 'right': self.right, 'fill': self.fill} + + def set_values(self, left=None, right=None, color=None, fill=None): + if left is not None: + self.left = left + + if right is not None: + self.right = right + + if fill is not None: + self.fill = fill + + if color is not None: + self.color = color + + self.drawing.setData(self.left, self.right, self.color, self.fill) + class TextObject(BaseObject): def __init__(self, text, pos, **kwargs): @@ -197,6 +233,7 @@ class EllipseObject(BaseObject): def __str__(self): return f'Ellipse at {self.center}' + class Ellipse(GraphicsObject): # adapted from pyqtgraph example on custom objects diff --git a/src/gui_qt/graphs/drawings.py b/src/gui_qt/graphs/drawings.py index 81067fb..3c0c242 100644 --- a/src/gui_qt/graphs/drawings.py +++ b/src/gui_qt/graphs/drawings.py @@ -62,6 +62,8 @@ class DrawingsWidget(QtWidgets.QWidget, Ui_Form): graph_id, obj = self.get_item() + print(dic) + if self._editmode: obj.set_values(**dic)