From 06b797202233fa2ff77e41391bd56581cd49cdfa Mon Sep 17 00:00:00 2001 From: Martin <martin.hogstedt@hotmail.com> Date: Thu, 1 Aug 2024 12:00:57 +0200 Subject: [PATCH] for some reason the lines are not drawn properly --- src/simudator/gui/signal_graphics_item.py | 76 ++++++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/src/simudator/gui/signal_graphics_item.py b/src/simudator/gui/signal_graphics_item.py index cbed8b9..f8a48c4 100644 --- a/src/simudator/gui/signal_graphics_item.py +++ b/src/simudator/gui/signal_graphics_item.py @@ -2,9 +2,9 @@ import itertools import math from qtpy import QtCore -from qtpy.QtCore import QPointF, Qt -from qtpy.QtGui import QCursor -from qtpy.QtWidgets import QGraphicsItem, QGraphicsLineItem, QGraphicsSceneMouseEvent +from qtpy.QtCore import QPoint, QPointF, QRectF, Qt +from qtpy.QtGui import QCursor, QPainter +from qtpy.QtWidgets import QGraphicsLineItem, QGraphicsSceneMouseEvent, QGraphicsWidget from simudator.gui.color_scheme import ColorScheme from simudator.gui.port_graphics_item import PortGraphicsItem @@ -14,7 +14,7 @@ LONGEST_CLICK_DIST = 20 MERGE_THRESHOLD = 25 -class SignalGraphicsItem(QGraphicsItem): +class SignalGraphicsItem(QGraphicsWidget): """ Class used to draw and update signals between modules, represented as orthogonal line segments that zig-zag from one module to another. @@ -29,7 +29,7 @@ class SignalGraphicsItem(QGraphicsItem): start_port: PortGraphicsItem, end_port: PortGraphicsItem, parent=None, - display_name: str = None, + display_name: str | None = None, ): super().__init__(parent) self.start_port = start_port @@ -44,11 +44,13 @@ class SignalGraphicsItem(QGraphicsItem): self.points: list[QPointF] = [] self.lines = [] + self.line_segments: list[LineSegment] = [] + # Variables and flags for handling mouse events self.left_click = False self.last_line_start_i = None self.last_line_end_i = None - self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable) + self.setFlag(QGraphicsWidget.GraphicsItemFlag.ItemIsMovable) # Initialise the visual representation of the signal as a zig-zag # between the ports of the signal @@ -64,6 +66,22 @@ class SignalGraphicsItem(QGraphicsItem): self.points.append(end_point) self.drawSignal() + # 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) + + self.line_segments.append(LineSegment(local_start, local_end, self)) + """ + self.line_segments.append( + LineSegment( + QPointF(half_x, start_point.y()), QPointF(half_x, end_point.y()), self + ) + ) + self.line_segments.append( + LineSegment(QPointF(half_x, end_point.y()), end_point, self) + )""" + # Used to lock layout self.is_locked = False @@ -103,6 +121,9 @@ class SignalGraphicsItem(QGraphicsItem): to another segment at its end points, except for the two end segments which are connected to module ports. """ + for line in self.line_segments: + line.paint(QPainter(), None, None) + return # Remove old lines from the GUI while self.lines: item = self.lines.pop() @@ -150,7 +171,7 @@ class SignalGraphicsItem(QGraphicsItem): else: bot_corner = QPointF(half_point.x(), next_half_point.y()) - # Remove the old corner and insteart new points in the correct order + # Remove the old corner and insteart new points in the correct288.5 order self.points.pop(end_point_index) self.points.insert(end_point_index, next_half_point) self.points.insert(end_point_index, bot_corner) @@ -184,7 +205,7 @@ class SignalGraphicsItem(QGraphicsItem): event.ignore() # Save whether the pressed line segment is vertical or horisontal - # for convenience + # for convenience288.5 if line_start.x() == line_end.x(): self.last_line_vertical = True elif line_start.y() == line_end.y(): @@ -229,7 +250,7 @@ class SignalGraphicsItem(QGraphicsItem): if line_end == self.points[0] or line_end == self.points[-1]: return - # If the line segment is vertical, move it horisontally + # If the line segment is vertical, move it horisontally288.5 if self.last_line_vertical: line_start.setX(current_mouse_point.x()) line_end.setX(current_mouse_point.x()) @@ -364,7 +385,7 @@ class SignalGraphicsItem(QGraphicsItem): else: distance = math.inf - # Horisontal line segment + # Horizontal line segment elif line_start.y() == line_end.y(): # Make sure the closest point is not outside the line leftmost_x = min(line_start.x(), line_end.x()) @@ -434,3 +455,38 @@ class SignalGraphicsItem(QGraphicsItem): # Redraw the signal self.drawSignal() + + +class LineSegment(QGraphicsWidget): + """""" + + def __init__( + self, start_pos: QPointF, end_pos: QPointF, parent: SignalGraphicsItem + ): + super().__init__(parent) + start_pos = self.mapFromItem(parent, start_pos) + end_pos = self.mapFromItem(parent, end_pos) + self._start_pos = start_pos + self._end_pos = end_pos + + # Vertical line + if self._start_pos.x() == self._end_pos.x(): + x = self._start_pos.x() + y = min(self._start_pos.y(), self._end_pos.y()) + w = 10 + h = max(self._start_pos.y(), self._end_pos.y()) - y + + # Horizontal line + else: + x = min(self._start_pos.x(), self._end_pos.x()) + y = self._start_pos.y() + w = max(self._start_pos.y(), self._end_pos.y()) - x + h = 10 + + self.setGeometry(x, y, w, h) + + def paint(self, painter: QPainter, option, widget): + painter.drawLine(self._start_pos, self._end_pos) + + def boundingRect(self): + return self.geometry() -- GitLab