diff --git a/src/simudator/gui/signal_graphics_item.py b/src/simudator/gui/signal_graphics_item.py index ecbc0b1b12d3318eb2eb3bf0542453c6faf1829f..a0a4320ec5f0ff0e3c7b114b70b9113ba33728fd 100644 --- a/src/simudator/gui/signal_graphics_item.py +++ b/src/simudator/gui/signal_graphics_item.py @@ -4,7 +4,12 @@ import math from qtpy import QtCore from qtpy.QtCore import QPoint, QPointF, QRectF, Qt from qtpy.QtGui import QCursor, QPainter, QPen -from qtpy.QtWidgets import QGraphicsLineItem, QGraphicsSceneMouseEvent, QGraphicsWidget +from qtpy.QtWidgets import ( + QGraphicsItem, + QGraphicsLineItem, + QGraphicsSceneMouseEvent, + QGraphicsWidget, +) from simudator.gui.color_scheme import ColorScheme from simudator.gui.port_graphics_item import PortGraphicsItem @@ -64,17 +69,20 @@ class SignalGraphicsItem(QGraphicsWidget): # local_start = self.mapFromItem(start_port, start_point) # local_end = self.mapFromItem(end_port, end_point) - local_start = self.mapFromScene(start_point) - local_end = self.mapFromScene(end_point) + # local_start = self.mapFromScene(start_point) + # local_end = self.mapFromScene(end_point) + local_start = start_point - self.start_port.scenePos() + local_end = end_point - self.end_port.scenePos() - self._init_line_points(local_start, local_end) + self._init_line_points(start_point, end_point) + + self.setPos(0, 0) # Used to lock layout self.is_locked = False def _init_line_points(self, start_point: QPointF, end_point: QPointF) -> None: """ """ - # TODO: work on self._points # |---- port # | @@ -159,6 +167,7 @@ class SignalGraphicsItem(QGraphicsWidget): no line segment will be chosen as pressed and the mouse press event is ignored. """ + print(892734) # Calculate and save which line segment was pressed (choose the one # closest to the point pressed) press_point = event.scenePos() @@ -190,6 +199,7 @@ class SignalGraphicsItem(QGraphicsWidget): Calls the functions 'handleLeftClick' or 'handleRightClick' if the user left or right clicked. """ + print(1111111111111) if event.button() == Qt.MouseButton.LeftButton: self.handleLeftClick() @@ -203,6 +213,7 @@ class SignalGraphicsItem(QGraphicsWidget): The two line sgments that make up the ends of the signal are not moved if pressed. """ + print(22222222222222222222) if not self.left_click or self.is_locked: return @@ -310,10 +321,14 @@ class SignalGraphicsItem(QGraphicsWidget): def boundingRect(self): return self.childrenBoundingRect() - def paint(self, *args): + def paint(self, painter, *args): # This avoids errors, it is a pure virtual function that # needs an implementation # Remove old lines from the GUI + + painter.setPen(QPen(QtCore.Qt.red)) + painter.drawLine(0, 0, 5, 0) + while self.lines: item = self.lines.pop() item.setParentItem(None) @@ -339,7 +354,6 @@ class SignalGraphicsItem(QGraphicsWidget): self._line_segments.append(segment) # self.lines.append(line) prev_point = point - pass def distance_sq(self, point1, point2): dx = point1.x() - point2.x() @@ -459,6 +473,7 @@ class LineSegment(QGraphicsWidget): self, start_pos: QPointF, end_pos: QPointF, parent: SignalGraphicsItem ): super().__init__(parent) + self.setFlag(QGraphicsItem.ItemIsMovable) self._parent = parent @@ -466,6 +481,8 @@ class LineSegment(QGraphicsWidget): self._calc_area(start_pos, end_pos) + self.setPos(self._start_pos) + def _calc_area(self, start_pos: QPointF, end_pos: QPointF): start_pos = self.mapFromItem(self._parent, start_pos) @@ -490,13 +507,24 @@ class LineSegment(QGraphicsWidget): def update_position(self, start_pos: QPointF, end_pos: QPointF): self._calc_area(start_pos, end_pos) - def paint(self, painter: QPainter, option, widget): + def paint(self, painter: QPainter, *args): painter.drawLine(self._start_pos, self._end_pos) painter.setPen(QPen(QtCore.Qt.red)) painter.drawRect(self.boundingRect()) def boundingRect(self): - return self.geometry() + top_left = QPointF( + min(self._start_pos.x(), self._end_pos.x()), + min(self._start_pos.y(), self._end_pos.y()), + ) + width = max(self._start_pos.x(), self._end_pos.x()) - top_left.x() + height = max(self._start_pos.y(), self._end_pos.y()) - top_left.y() + return QRectF( + top_left.x() - self._margin, + top_left.y() - self._margin, + width + 2 * self._margin, + height + 2 * self._margin, + ) def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent) -> None: """ @@ -510,3 +538,9 @@ class LineSegment(QGraphicsWidget): elif event.button() == Qt.MouseButton.RightButton: self.handleRightClick() + + def mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None: + print(321) + + def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent) -> None: + print(654)