diff --git a/src/simudator/gui/signal_graphics_item.py b/src/simudator/gui/signal_graphics_item.py index 37398aa9efab93e6310640fbbea3eaa21408fe71..ecbc0b1b12d3318eb2eb3bf0542453c6faf1829f 100644 --- a/src/simudator/gui/signal_graphics_item.py +++ b/src/simudator/gui/signal_graphics_item.py @@ -67,9 +67,7 @@ class SignalGraphicsItem(QGraphicsWidget): local_start = self.mapFromScene(start_point) local_end = self.mapFromScene(end_point) - self._init_line_points(start_point, end_point) - - self.drawSignal() + self._init_line_points(local_start, local_end) # Used to lock layout self.is_locked = False @@ -120,41 +118,6 @@ class SignalGraphicsItem(QGraphicsWidget): self.setVisible(True) self.drawSignal() - def drawSignal(self) -> None: - """ - Draws the signal as a collection of orthogonal line segments which - can be moved and split into smaller segments. Each segment is joined - to another segment at its end points, except for the two end segments - which are connected to module ports. - """ - - # Remove old lines from the GUI - while self.lines: - item = self.lines.pop() - item.setParentItem(None) - - # Remove old lines form the GUI - while self._line_segments: - item = self._line_segments.pop() - item.setParentItem(None) - - # Draw lines from the first to the second point, from the second - # to the third point and so on - prev_point = self.points[0] - for point in self.points[1:]: - # Coordinates - # Bounded box for clicking - x1 = prev_point.x() - y1 = prev_point.y() - x2 = point.x() - y2 = point.y() - # line = QGraphicsLineItem(x1, y1, x2, y2, self) - # line.setPen(ColorScheme.Signal) - segment = LineSegment(prev_point, point, self) - self._line_segments.append(segment) - # self.lines.append(line) - prev_point = point - def splitLine(self, start_point_index: int, end_point_index: int) -> None: """ Splits an existing line of two segments into a new line of four line @@ -350,6 +313,32 @@ class SignalGraphicsItem(QGraphicsWidget): def paint(self, *args): # This avoids errors, it is a pure virtual function that # needs an implementation + # Remove old lines from the GUI + while self.lines: + item = self.lines.pop() + item.setParentItem(None) + + # Remove old lines form the GUI + while self._line_segments: + item = self._line_segments.pop() + item.setParentItem(None) + + # Draw lines from the first to the second point, from the second + # to the third point and so on + prev_point = self.points[0] + for point in self.points[1:]: + # Coordinates + # Bounded box for clicking + x1 = prev_point.x() + y1 = prev_point.y() + x2 = point.x() + y2 = point.y() + # line = QGraphicsLineItem(x1, y1, x2, y2, self) + # line.setPen(ColorScheme.Signal) + segment = LineSegment(prev_point, point, self) + self._line_segments.append(segment) + # self.lines.append(line) + prev_point = point pass def distance_sq(self, point1, point2): @@ -444,8 +433,6 @@ class SignalGraphicsItem(QGraphicsWidget): end_point.setX(new_end_point.x()) neighbour_point.setX(new_end_point.x()) - self.drawSignal() - def toggleVisibility(self) -> None: """ Toggles the visibility of the signal. @@ -464,9 +451,6 @@ class SignalGraphicsItem(QGraphicsWidget): def setPoints(self, points: list[QPointF]) -> None: self.points = points - # Redraw the signal - self.drawSignal() - class LineSegment(QGraphicsWidget): """""" @@ -478,15 +462,14 @@ class LineSegment(QGraphicsWidget): self._parent = parent - self._start_pos = start_pos - self._end_pos = end_pos + self._margin = 5 self._calc_area(start_pos, end_pos) def _calc_area(self, start_pos: QPointF, end_pos: QPointF): - # start_pos = self.mapFromScene(start_pos) - # end_pos = self.mapFromScene(end_pos) + start_pos = self.mapFromItem(self._parent, start_pos) + end_pos = self.mapFromItem(self._parent, end_pos) self._start_pos = start_pos self._end_pos = end_pos @@ -497,7 +480,12 @@ class LineSegment(QGraphicsWidget): ) 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() - self.setGeometry(top_left.x() - 5, top_left.y() - 5, width + 10, height + 10) + self.setGeometry( + top_left.x() - self._margin, + top_left.y() - self._margin, + width + 2 * self._margin, + height + 2 * self._margin, + ) def update_position(self, start_pos: QPointF, end_pos: QPointF): self._calc_area(start_pos, end_pos)