Skip to content
Snippets Groups Projects
Commit a32dbe1c authored by Oscar Gustafsson's avatar Oscar Gustafsson :bicyclist:
Browse files

Merge branch 'orthogonallines' into 'master'

Add support for orthogonal lines

See merge request !104
parents 99e383b1 d49e2ddc
No related branches found
No related tags found
1 merge request!104Add support for orthogonal lines
Pipeline #88076 passed
...@@ -14,3 +14,4 @@ MIN_HEIGHT_SCENE = 520 ...@@ -14,3 +14,4 @@ MIN_HEIGHT_SCENE = 520
# Interface # Interface
LINECOLOR = QColor(*SIGNAL_COLOR) LINECOLOR = QColor(*SIGNAL_COLOR)
GRID = 19
from qtpy.QtCore import QPointF from qtpy.QtCore import QPointF
from qtpy.QtGui import QPen, QPolygonF from qtpy.QtGui import QPen, QPainterPath
from qtpy.QtWidgets import QGraphicsPolygonItem, QMenu from qtpy.QtWidgets import QGraphicsPathItem, QMenu
from b_asic.GUI._preferences import LINECOLOR, PORTHEIGHT, PORTWIDTH from b_asic.GUI._preferences import LINECOLOR, PORTHEIGHT, PORTWIDTH
from b_asic.signal import Signal from b_asic.signal import Signal
class Arrow(QGraphicsPolygonItem): class Arrow(QGraphicsPathItem):
"""Arrow/connection in signal flow graph GUI.""" """Arrow/connection in signal flow graph GUI."""
def __init__(self, source, destination, window, signal=None, parent=None): def __init__(self, source, destination, window, signal=None, parent=None):
...@@ -80,10 +80,29 @@ class Arrow(QGraphicsPolygonItem): ...@@ -80,10 +80,29 @@ class Arrow(QGraphicsPolygonItem):
""" """
Draw a line connecting self.source with self.destination. Used as callback when moving operations. Draw a line connecting self.source with self.destination. Used as callback when moving operations.
""" """
ORTHOGONAL = True
OFFSET = 2 * PORTWIDTH
self.setPen(QPen(LINECOLOR, 3)) self.setPen(QPen(LINECOLOR, 3))
x0 = self.source.operation.x() + self.source.x() + PORTWIDTH x0 = self.source.operation.x() + self.source.x() + PORTWIDTH
y0 = self.source.operation.y() + self.source.y() + PORTHEIGHT/2 y0 = self.source.operation.y() + self.source.y() + PORTHEIGHT / 2
x1 = self.destination.operation.x() + self.destination.x() x1 = self.destination.operation.x() + self.destination.x()
y1 = self.destination.operation.y() + self.destination.y() + PORTHEIGHT/2 y1 = (
p = QPolygonF() << QPointF(x0, y0) << QPointF(x1, y1) self.destination.operation.y()
self.setPolygon(p) + self.destination.y()
+ PORTHEIGHT / 2
)
xmid = (x0 + x1) / 2
ymid = (y0 + y1) / 2
p = QPainterPath(QPointF(x0, y0))
if y0 == y1 or not ORTHOGONAL:
pass
elif abs(x0 - x1) <= OFFSET / 2:
p.lineTo(QPointF(x0 + OFFSET, y0))
p.lineTo(QPointF(x0 + OFFSET, ymid))
p.lineTo(QPointF(x0 - OFFSET, ymid))
p.lineTo(QPointF(x0 - OFFSET, y1))
else:
p.lineTo(QPointF(xmid, y0))
p.lineTo(QPointF(xmid, y1))
p.lineTo(QPointF(x1, y1))
self.setPath(p)
...@@ -12,7 +12,7 @@ from qtpy.QtWidgets import QAction, QMenu, QPushButton ...@@ -12,7 +12,7 @@ from qtpy.QtWidgets import QAction, QMenu, QPushButton
from b_asic.GUI.properties_window import PropertiesWindow from b_asic.GUI.properties_window import PropertiesWindow
from b_asic.GUI.utils import decorate_class, handle_error from b_asic.GUI.utils import decorate_class, handle_error
from b_asic.GUI._preferences import MINBUTTONSIZE from b_asic.GUI._preferences import GRID, MINBUTTONSIZE
@decorate_class(handle_error) @decorate_class(handle_error)
...@@ -105,6 +105,15 @@ class DragButton(QPushButton): ...@@ -105,6 +105,15 @@ class DragButton(QPushButton):
else: else:
self.select_button(event.modifiers()) self.select_button(event.modifiers())
# Snap
point = self.pos()
x = point.x()
y = point.y()
newx = GRID * round(x / GRID)
newy = GRID * round(y / GRID)
self.move(newx, newy)
self._window.scene.update()
self._window.graphic_view.update()
super().mouseReleaseEvent(event) super().mouseReleaseEvent(event)
def _toggle_button(self, pressed=False): def _toggle_button(self, pressed=False):
...@@ -117,7 +126,9 @@ class DragButton(QPushButton): ...@@ -117,7 +126,9 @@ class DragButton(QPushButton):
path_to_image = os.path.join( path_to_image = os.path.join(
os.path.dirname(__file__), os.path.dirname(__file__),
"operation_icons", "operation_icons",
f"{self.operation_path_name}{'_grey.png' if self.pressed else '.png'}", (
f"{self.operation_path_name}{'_grey.png' if self.pressed else '.png'}"
),
) )
self.setIcon(QIcon(path_to_image)) self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(MINBUTTONSIZE, MINBUTTONSIZE)) self.setIconSize(QSize(MINBUTTONSIZE, MINBUTTONSIZE))
......
...@@ -37,6 +37,7 @@ from b_asic.GUI.port_button import PortButton ...@@ -37,6 +37,7 @@ from b_asic.GUI.port_button import PortButton
from b_asic.GUI.select_sfg_window import SelectSFGWindow from b_asic.GUI.select_sfg_window import SelectSFGWindow
from b_asic.GUI._preferences import ( from b_asic.GUI._preferences import (
GAP, GAP,
GRID,
MINBUTTONSIZE, MINBUTTONSIZE,
PORTHEIGHT, PORTHEIGHT,
PORTWIDTH, PORTWIDTH,
...@@ -540,7 +541,7 @@ class MainWindow(QMainWindow): ...@@ -540,7 +541,7 @@ class MainWindow(QMainWindow):
op.graph_id, op, op.type_name().lower(), True, window=self op.graph_id, op, op.type_name().lower(), True, window=self
) )
if position is None: if position is None:
attr_button.move(250, 100) attr_button.move(GRID * 3, GRID * 2)
else: else:
attr_button.move(*position) attr_button.move(*position)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment