From 2d42a8cd46fa633315700672cf5a121cb44f9573 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Sat, 1 Apr 2023 18:23:49 +0200 Subject: [PATCH] ellipses done --- src/gui_qt/_py/ellipsewidget.py | 55 ++++++++++--------- src/gui_qt/graphs/draw_inputs.py | 15 +++++- src/gui_qt/graphs/draw_objects.py | 79 ++++++++++++++++++--------- src/resources/_ui/ellipsewidget.ui | 86 +++++++++++++++++------------- 4 files changed, 146 insertions(+), 89 deletions(-) diff --git a/src/gui_qt/_py/ellipsewidget.py b/src/gui_qt/_py/ellipsewidget.py index b75a353..3b78a27 100644 --- a/src/gui_qt/_py/ellipsewidget.py +++ b/src/gui_qt/_py/ellipsewidget.py @@ -17,32 +17,38 @@ class Ui_ellipsewidget(object): ellipsewidget.resize(400, 300) self.gridLayout = QtWidgets.QGridLayout(ellipsewidget) self.gridLayout.setObjectName("gridLayout") - spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) - self.gridLayout.addItem(spacerItem, 3, 0, 1, 1) + self.height_line = QtWidgets.QLineEdit(ellipsewidget) + self.height_line.setObjectName("height_line") + self.gridLayout.addWidget(self.height_line, 3, 2, 1, 1) + self.center_x = QtWidgets.QLineEdit(ellipsewidget) + self.center_x.setObjectName("center_x") + self.gridLayout.addWidget(self.center_x, 2, 1, 1, 1) + self.axes_label = QtWidgets.QLabel(ellipsewidget) + self.axes_label.setObjectName("axes_label") + self.gridLayout.addWidget(self.axes_label, 3, 0, 1, 1) self.color_label = QtWidgets.QLabel(ellipsewidget) self.color_label.setObjectName("color_label") self.gridLayout.addWidget(self.color_label, 0, 0, 1, 1) - self.center_x = QtWidgets.QLineEdit(ellipsewidget) - self.center_x.setObjectName("center_x") - self.gridLayout.addWidget(self.center_x, 1, 1, 1, 1) self.center_y = QtWidgets.QLineEdit(ellipsewidget) self.center_y.setObjectName("center_y") - self.gridLayout.addWidget(self.center_y, 1, 2, 1, 1) - self.label = QtWidgets.QLabel(ellipsewidget) - self.label.setObjectName("label") - self.gridLayout.addWidget(self.label, 1, 0, 1, 1) + self.gridLayout.addWidget(self.center_y, 2, 2, 1, 1) + spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) + self.gridLayout.addItem(spacerItem, 4, 0, 1, 1) + self.center_label = QtWidgets.QLabel(ellipsewidget) + self.center_label.setObjectName("center_label") + self.gridLayout.addWidget(self.center_label, 2, 0, 1, 1) + self.width_line = QtWidgets.QLineEdit(ellipsewidget) + self.width_line.setObjectName("width_line") + self.gridLayout.addWidget(self.width_line, 3, 1, 1, 1) self.color_box = ColorListEditor(ellipsewidget) self.color_box.setObjectName("color_box") self.gridLayout.addWidget(self.color_box, 0, 1, 1, 2) - self.axes_label = QtWidgets.QLabel(ellipsewidget) - self.axes_label.setObjectName("axes_label") - self.gridLayout.addWidget(self.axes_label, 2, 0, 1, 1) - self.width_line = QtWidgets.QLineEdit(ellipsewidget) - self.width_line.setObjectName("width_line") - self.gridLayout.addWidget(self.width_line, 2, 1, 1, 1) - self.height_line = QtWidgets.QLineEdit(ellipsewidget) - self.height_line.setObjectName("height_line") - self.gridLayout.addWidget(self.height_line, 2, 2, 1, 1) + self.fill_box = ColorListEditor(ellipsewidget) + self.fill_box.setObjectName("fill_box") + self.gridLayout.addWidget(self.fill_box, 1, 1, 1, 2) + self.fill_label = QtWidgets.QLabel(ellipsewidget) + self.fill_label.setObjectName("fill_label") + self.gridLayout.addWidget(self.fill_label, 1, 0, 1, 1) self.retranslateUi(ellipsewidget) QtCore.QMetaObject.connectSlotsByName(ellipsewidget) @@ -50,11 +56,12 @@ class Ui_ellipsewidget(object): def retranslateUi(self, ellipsewidget): _translate = QtCore.QCoreApplication.translate ellipsewidget.setWindowTitle(_translate("ellipsewidget", "Form")) - self.color_label.setText(_translate("ellipsewidget", "Color")) - self.center_x.setPlaceholderText(_translate("ellipsewidget", "x")) - self.center_y.setPlaceholderText(_translate("ellipsewidget", "y")) - self.label.setText(_translate("ellipsewidget", "Center")) - self.axes_label.setText(_translate("ellipsewidget", "Axes")) - self.width_line.setPlaceholderText(_translate("ellipsewidget", "axis 1")) self.height_line.setPlaceholderText(_translate("ellipsewidget", "axis 2")) + self.center_x.setPlaceholderText(_translate("ellipsewidget", "x")) + self.axes_label.setText(_translate("ellipsewidget", "Axes")) + self.color_label.setText(_translate("ellipsewidget", "Color")) + self.center_y.setPlaceholderText(_translate("ellipsewidget", "y")) + self.center_label.setText(_translate("ellipsewidget", "Center")) + self.width_line.setPlaceholderText(_translate("ellipsewidget", "axis 1")) + self.fill_label.setText(_translate("ellipsewidget", "Fill color")) from gui_qt.lib.delegates import ColorListEditor diff --git a/src/gui_qt/graphs/draw_inputs.py b/src/gui_qt/graphs/draw_inputs.py index b824c91..815ddf2 100644 --- a/src/gui_qt/graphs/draw_inputs.py +++ b/src/gui_qt/graphs/draw_inputs.py @@ -137,8 +137,6 @@ 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), @@ -154,11 +152,23 @@ class EllipseWidget(QtWidgets.QWidget, Ui_ellipsewidget): super().__init__(parent=parent) self.setupUi(self) + self.fill_box.insertItem(0, 'No fill', None) + self.fill_box.setCurrentIndex(0) + self.center_x.setValidator(QtGui.QDoubleValidator()) self.center_y.setValidator(QtGui.QDoubleValidator()) self.width_line.setValidator(QtGui.QDoubleValidator()) self.height_line.setValidator(QtGui.QDoubleValidator()) + def set_args(self, center: tuple = (0, 0), axes: tuple = (1, 1), color=None, fill=None): + self.center_x.setText(str(center[0])) + self.center_y.setText(str(center[1])) + self.width_line.setText(str(axes[0])) + self.width_line.setText(str(axes[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: centre = parse_point(self.center_x, self.center_y) if centre is None: @@ -169,6 +179,7 @@ class EllipseWidget(QtWidgets.QWidget, Ui_ellipsewidget): dic = { 'color': self.color_box.currentData(QtCore.Qt.UserRole), + 'fill': self.fill_box.currentData(QtCore.Qt.UserRole), 'center': centre, 'axes': axes, } diff --git a/src/gui_qt/graphs/draw_objects.py b/src/gui_qt/graphs/draw_objects.py index c5c9086..eed47c8 100644 --- a/src/gui_qt/graphs/draw_objects.py +++ b/src/gui_qt/graphs/draw_objects.py @@ -222,31 +222,47 @@ class TextObject(BaseObject): class EllipseObject(BaseObject): - def __init__(self, center: tuple, axes: tuple, **kwargs): + def __init__(self, center: tuple, axes: tuple, fill: BaseColor, **kwargs): super().__init__(**kwargs) self.center = center self.axes = axes + self.fill = fill - self.drawing = Ellipse(center, axes) + self.drawing = Ellipse(self.center, self.axes, self.color, self.fill) def __str__(self): return f'Ellipse at {self.center}' + def get_values(self): + return {'type': 'ellipse', 'color': self.color, 'center': self.center, 'axes': self.axes, 'fill': self.fill} + + def set_values(self, center=None, axes=None, color=None, fill=None): + if center is not None: + self.center = center + + if axes is not None: + self.axes = axes + + if fill is not None: + self.fill = fill + + if color is not None: + self.color = color + + self.drawing.setData(self.center, self.axes, self.color, self.fill) + class Ellipse(GraphicsObject): # adapted from pyqtgraph example on custom objects - def __init__(self, center: tuple, axes: tuple): + def __init__(self, center: tuple, axes: tuple, color: BaseColor, fill: BaseColor): GraphicsObject.__init__(self) - self.center = center - self.ax = axes - - self.center_disp = self.center - self.ax_disp = self.ax - self.log_mode = [False, False] + + self.setData(center, axes, color, fill) + self.generatePicture() def generatePicture(self): @@ -254,8 +270,9 @@ class Ellipse(GraphicsObject): ## rather than re-drawing the shapes every time. self.picture = QtGui.QPicture() p = QtGui.QPainter(self.picture) - p.setPen(mkPen('w')) - p.setBrush(mkBrush('r')) + p.setPen(mkPen(self.color.rgb())) + if self.fill is not None: + p.setBrush(mkBrush(self.fill.rgb())) p.drawEllipse(QtCore.QPointF(*self.center_disp), *self.ax_disp) p.end() @@ -274,23 +291,23 @@ class Ellipse(GraphicsObject): if self.log_mode[0] != x_mode: if x_mode: - x0 = self.p0[0] - xmax = log10(x0+self.delta[0]) - self.x_disp = log10(x0) - self.dx = xmax - self.x_disp + x0 = self.center[0] + xmax = log10(x0+self.ax[0]) + self.center_disp = (log10(x0), self.center_disp[1]) + self.ax_disp = (xmax, self.ax_disp[1]) else: - self.x_disp = self.p0[0] - self.dx = self.delta[0] + self.center_disp = (self.center[0], self.center_disp[1]) + self.ax_disp = (self.ax[0], self.ax_disp[1]) if self.log_mode[1] != y_mode: if y_mode: - y0 = self.p0[1] - ymax = log10(y0 + self.delta[1]) - self.y_disp = log10(y0) - self.dy = ymax - self.y_disp + y0 = self.center[1] + ymax = log10(y0+self.ax[1]) + self.center_disp = (self.center_disp[0], log10(y0)) + self.ax_disp = (self.ax_disp[0], ymax) else: - self.y_disp = self.p0[1] - self.dy = self.delta[1] + self.ax_disp = (self.ax_disp[0], self.ax[1]) + self.center_disp = (self.center_disp[0], self.center[1]) self.generatePicture() @@ -298,5 +315,17 @@ class Ellipse(GraphicsObject): self.informViewBoundsChanged() - def get_state(self): - return {'center': self.center, 'axes': self.ax} + def setData(self, center, axes, color, fill): + self.center = center + self.ax = axes + + self.center_disp = self.center + self.ax_disp = self.ax + + self.color = color + self.fill = fill + + l = [self.log_mode[0], self.log_mode[1]] + self.log_mode = [None, None] + + self.setLogMode(*l) diff --git a/src/resources/_ui/ellipsewidget.ui b/src/resources/_ui/ellipsewidget.ui index 595083f..95aa01f 100644 --- a/src/resources/_ui/ellipsewidget.ui +++ b/src/resources/_ui/ellipsewidget.ui @@ -14,7 +14,42 @@ Form + + + + axis 2 + + + + + + + x + + + + + + Axes + + + + + + + Color + + + + + + + y + + + + Qt::Vertical @@ -27,55 +62,30 @@ - - - - Color - - - - - - - x - - - - - - - y - - - - - + + Center - - - - - - - Axes - - - - + axis 1 - - - - axis 2 + + + + + + + + + + Fill color