Skip to content
Snippets Groups Projects
Commit 06b79720 authored by Martin's avatar Martin
Browse files

for some reason the lines are not drawn properly

parent fa064bdf
No related branches found
No related tags found
No related merge requests found
Pipeline #134583 failed
...@@ -2,9 +2,9 @@ import itertools ...@@ -2,9 +2,9 @@ import itertools
import math import math
from qtpy import QtCore from qtpy import QtCore
from qtpy.QtCore import QPointF, Qt from qtpy.QtCore import QPoint, QPointF, QRectF, Qt
from qtpy.QtGui import QCursor from qtpy.QtGui import QCursor, QPainter
from qtpy.QtWidgets import QGraphicsItem, QGraphicsLineItem, QGraphicsSceneMouseEvent from qtpy.QtWidgets import QGraphicsLineItem, QGraphicsSceneMouseEvent, QGraphicsWidget
from simudator.gui.color_scheme import ColorScheme from simudator.gui.color_scheme import ColorScheme
from simudator.gui.port_graphics_item import PortGraphicsItem from simudator.gui.port_graphics_item import PortGraphicsItem
...@@ -14,7 +14,7 @@ LONGEST_CLICK_DIST = 20 ...@@ -14,7 +14,7 @@ LONGEST_CLICK_DIST = 20
MERGE_THRESHOLD = 25 MERGE_THRESHOLD = 25
class SignalGraphicsItem(QGraphicsItem): class SignalGraphicsItem(QGraphicsWidget):
""" """
Class used to draw and update signals between modules, represented Class used to draw and update signals between modules, represented
as orthogonal line segments that zig-zag from one module to another. as orthogonal line segments that zig-zag from one module to another.
...@@ -29,7 +29,7 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -29,7 +29,7 @@ class SignalGraphicsItem(QGraphicsItem):
start_port: PortGraphicsItem, start_port: PortGraphicsItem,
end_port: PortGraphicsItem, end_port: PortGraphicsItem,
parent=None, parent=None,
display_name: str = None, display_name: str | None = None,
): ):
super().__init__(parent) super().__init__(parent)
self.start_port = start_port self.start_port = start_port
...@@ -44,11 +44,13 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -44,11 +44,13 @@ class SignalGraphicsItem(QGraphicsItem):
self.points: list[QPointF] = [] self.points: list[QPointF] = []
self.lines = [] self.lines = []
self.line_segments: list[LineSegment] = []
# Variables and flags for handling mouse events # Variables and flags for handling mouse events
self.left_click = False self.left_click = False
self.last_line_start_i = None self.last_line_start_i = None
self.last_line_end_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 # Initialise the visual representation of the signal as a zig-zag
# between the ports of the signal # between the ports of the signal
...@@ -64,6 +66,22 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -64,6 +66,22 @@ class SignalGraphicsItem(QGraphicsItem):
self.points.append(end_point) self.points.append(end_point)
self.drawSignal() 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 # Used to lock layout
self.is_locked = False self.is_locked = False
...@@ -103,6 +121,9 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -103,6 +121,9 @@ class SignalGraphicsItem(QGraphicsItem):
to another segment at its end points, except for the two end segments to another segment at its end points, except for the two end segments
which are connected to module ports. which are connected to module ports.
""" """
for line in self.line_segments:
line.paint(QPainter(), None, None)
return
# Remove old lines from the GUI # Remove old lines from the GUI
while self.lines: while self.lines:
item = self.lines.pop() item = self.lines.pop()
...@@ -150,7 +171,7 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -150,7 +171,7 @@ class SignalGraphicsItem(QGraphicsItem):
else: else:
bot_corner = QPointF(half_point.x(), next_half_point.y()) 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.pop(end_point_index)
self.points.insert(end_point_index, next_half_point) self.points.insert(end_point_index, next_half_point)
self.points.insert(end_point_index, bot_corner) self.points.insert(end_point_index, bot_corner)
...@@ -184,7 +205,7 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -184,7 +205,7 @@ class SignalGraphicsItem(QGraphicsItem):
event.ignore() event.ignore()
# Save whether the pressed line segment is vertical or horisontal # Save whether the pressed line segment is vertical or horisontal
# for convenience # for convenience288.5
if line_start.x() == line_end.x(): if line_start.x() == line_end.x():
self.last_line_vertical = True self.last_line_vertical = True
elif line_start.y() == line_end.y(): elif line_start.y() == line_end.y():
...@@ -229,7 +250,7 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -229,7 +250,7 @@ class SignalGraphicsItem(QGraphicsItem):
if line_end == self.points[0] or line_end == self.points[-1]: if line_end == self.points[0] or line_end == self.points[-1]:
return 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: if self.last_line_vertical:
line_start.setX(current_mouse_point.x()) line_start.setX(current_mouse_point.x())
line_end.setX(current_mouse_point.x()) line_end.setX(current_mouse_point.x())
...@@ -364,7 +385,7 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -364,7 +385,7 @@ class SignalGraphicsItem(QGraphicsItem):
else: else:
distance = math.inf distance = math.inf
# Horisontal line segment # Horizontal line segment
elif line_start.y() == line_end.y(): elif line_start.y() == line_end.y():
# Make sure the closest point is not outside the line # Make sure the closest point is not outside the line
leftmost_x = min(line_start.x(), line_end.x()) leftmost_x = min(line_start.x(), line_end.x())
...@@ -434,3 +455,38 @@ class SignalGraphicsItem(QGraphicsItem): ...@@ -434,3 +455,38 @@ class SignalGraphicsItem(QGraphicsItem):
# Redraw the signal # Redraw the signal
self.drawSignal() 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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment