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

the lines refues to cooperate. I sent the king their way >:))

parent 01055c2b
No related branches found
No related tags found
No related merge requests found
Pipeline #134597 failed
...@@ -4,7 +4,12 @@ import math ...@@ -4,7 +4,12 @@ import math
from qtpy import QtCore from qtpy import QtCore
from qtpy.QtCore import QPoint, QPointF, QRectF, Qt from qtpy.QtCore import QPoint, QPointF, QRectF, Qt
from qtpy.QtGui import QCursor, QPainter, QPen 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.color_scheme import ColorScheme
from simudator.gui.port_graphics_item import PortGraphicsItem from simudator.gui.port_graphics_item import PortGraphicsItem
...@@ -64,17 +69,20 @@ class SignalGraphicsItem(QGraphicsWidget): ...@@ -64,17 +69,20 @@ class SignalGraphicsItem(QGraphicsWidget):
# local_start = self.mapFromItem(start_port, start_point) # local_start = self.mapFromItem(start_port, start_point)
# local_end = self.mapFromItem(end_port, end_point) # local_end = self.mapFromItem(end_port, end_point)
local_start = self.mapFromScene(start_point) # local_start = self.mapFromScene(start_point)
local_end = self.mapFromScene(end_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 # Used to lock layout
self.is_locked = False self.is_locked = False
def _init_line_points(self, start_point: QPointF, end_point: QPointF) -> None: def _init_line_points(self, start_point: QPointF, end_point: QPointF) -> None:
""" """ """ """
# TODO: work on self._points
# |---- port # |---- port
# | # |
...@@ -159,6 +167,7 @@ class SignalGraphicsItem(QGraphicsWidget): ...@@ -159,6 +167,7 @@ class SignalGraphicsItem(QGraphicsWidget):
no line segment will be chosen as pressed and the mouse press event no line segment will be chosen as pressed and the mouse press event
is ignored. is ignored.
""" """
print(892734)
# Calculate and save which line segment was pressed (choose the one # Calculate and save which line segment was pressed (choose the one
# closest to the point pressed) # closest to the point pressed)
press_point = event.scenePos() press_point = event.scenePos()
...@@ -190,6 +199,7 @@ class SignalGraphicsItem(QGraphicsWidget): ...@@ -190,6 +199,7 @@ class SignalGraphicsItem(QGraphicsWidget):
Calls the functions 'handleLeftClick' or 'handleRightClick' if Calls the functions 'handleLeftClick' or 'handleRightClick' if
the user left or right clicked. the user left or right clicked.
""" """
print(1111111111111)
if event.button() == Qt.MouseButton.LeftButton: if event.button() == Qt.MouseButton.LeftButton:
self.handleLeftClick() self.handleLeftClick()
...@@ -203,6 +213,7 @@ class SignalGraphicsItem(QGraphicsWidget): ...@@ -203,6 +213,7 @@ class SignalGraphicsItem(QGraphicsWidget):
The two line sgments that make up the ends of the signal are not moved The two line sgments that make up the ends of the signal are not moved
if pressed. if pressed.
""" """
print(22222222222222222222)
if not self.left_click or self.is_locked: if not self.left_click or self.is_locked:
return return
...@@ -310,10 +321,14 @@ class SignalGraphicsItem(QGraphicsWidget): ...@@ -310,10 +321,14 @@ class SignalGraphicsItem(QGraphicsWidget):
def boundingRect(self): def boundingRect(self):
return self.childrenBoundingRect() return self.childrenBoundingRect()
def paint(self, *args): def paint(self, painter, *args):
# This avoids errors, it is a pure virtual function that # This avoids errors, it is a pure virtual function that
# needs an implementation # needs an implementation
# Remove old lines from the GUI # Remove old lines from the GUI
painter.setPen(QPen(QtCore.Qt.red))
painter.drawLine(0, 0, 5, 0)
while self.lines: while self.lines:
item = self.lines.pop() item = self.lines.pop()
item.setParentItem(None) item.setParentItem(None)
...@@ -339,7 +354,6 @@ class SignalGraphicsItem(QGraphicsWidget): ...@@ -339,7 +354,6 @@ class SignalGraphicsItem(QGraphicsWidget):
self._line_segments.append(segment) self._line_segments.append(segment)
# self.lines.append(line) # self.lines.append(line)
prev_point = point prev_point = point
pass
def distance_sq(self, point1, point2): def distance_sq(self, point1, point2):
dx = point1.x() - point2.x() dx = point1.x() - point2.x()
...@@ -459,6 +473,7 @@ class LineSegment(QGraphicsWidget): ...@@ -459,6 +473,7 @@ class LineSegment(QGraphicsWidget):
self, start_pos: QPointF, end_pos: QPointF, parent: SignalGraphicsItem self, start_pos: QPointF, end_pos: QPointF, parent: SignalGraphicsItem
): ):
super().__init__(parent) super().__init__(parent)
self.setFlag(QGraphicsItem.ItemIsMovable)
self._parent = parent self._parent = parent
...@@ -466,6 +481,8 @@ class LineSegment(QGraphicsWidget): ...@@ -466,6 +481,8 @@ class LineSegment(QGraphicsWidget):
self._calc_area(start_pos, end_pos) self._calc_area(start_pos, end_pos)
self.setPos(self._start_pos)
def _calc_area(self, start_pos: QPointF, end_pos: QPointF): def _calc_area(self, start_pos: QPointF, end_pos: QPointF):
start_pos = self.mapFromItem(self._parent, start_pos) start_pos = self.mapFromItem(self._parent, start_pos)
...@@ -490,13 +507,24 @@ class LineSegment(QGraphicsWidget): ...@@ -490,13 +507,24 @@ class LineSegment(QGraphicsWidget):
def update_position(self, start_pos: QPointF, end_pos: QPointF): def update_position(self, start_pos: QPointF, end_pos: QPointF):
self._calc_area(start_pos, end_pos) 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.drawLine(self._start_pos, self._end_pos)
painter.setPen(QPen(QtCore.Qt.red)) painter.setPen(QPen(QtCore.Qt.red))
painter.drawRect(self.boundingRect()) painter.drawRect(self.boundingRect())
def boundingRect(self): 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: def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent) -> None:
""" """
...@@ -510,3 +538,9 @@ class LineSegment(QGraphicsWidget): ...@@ -510,3 +538,9 @@ class LineSegment(QGraphicsWidget):
elif event.button() == Qt.MouseButton.RightButton: elif event.button() == Qt.MouseButton.RightButton:
self.handleRightClick() self.handleRightClick()
def mousePressEvent(self, event: QGraphicsSceneMouseEvent) -> None:
print(321)
def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent) -> None:
print(654)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment