1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
from qtpy.QtCore import QRectF
from qtpy.QtGui import QPainter, QPainterPath
from qtpy.QtWidgets import (QGraphicsBlurEffect, QGraphicsItem,
QGraphicsObject, QGraphicsSceneHoverEvent,
QGraphicsSceneMouseEvent, QStyleOptionGraphicsItem,
QWidget)
from .base import ConnectionBase
from .connection_painter import ConnectionPainter
from .node_connection_interaction import NodeConnectionInteraction
from .port import PortType, opposite_port
debug_drawing = False
class ConnectionGraphicsObject(QGraphicsObject):
def __init__(self, scene, connection):
'''
connection_graphics_object
Parameters
----------
scene : FlowScene
connection : Connection
'''
super().__init__()
self._scene = scene
self._connection = connection
self._geometry = connection.geometry
self._style = connection.style.connection
self._scene.addItem(self)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemIsFocusable, True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setAcceptHoverEvents(True)
# self.add_graphics_effect()
self.setZValue(-1.0)
def _cleanup(self):
if self._scene is not None:
self._scene.removeItem(self)
self._scene = None
@property
def connection(self) -> ConnectionBase:
"""
Connection
Returns
-------
value : Connection
"""
return self._connection
def boundingRect(self) -> QRectF:
"""
boundingRect
Returns
-------
value : QRectF
"""
return self._geometry.bounding_rect
def shape(self) -> QPainterPath:
"""
Shape
Returns
-------
value : QPainterPath
"""
# TODO DEBUG_DRAWING
if debug_drawing:
path = QPainterPath()
path.addRect(self.boundingRect())
return path
return ConnectionPainter.get_painter_stroke(self._geometry)
def set_geometry_changed(self):
self.prepareGeometryChange()
def move(self):
"""
Updates the position of both ends
"""
conn = self._connection
cgo = conn.graphics_object
for port_type in (PortType.input, PortType.output):
node = self._connection.get_node(port_type)
if node is None:
continue
node_graphics = node.graphics_object
node_geom = node.geometry
scene_pos = node_geom.port_scene_position(
port_type, self._connection.get_port_index(port_type),
node_graphics.sceneTransform()
)
inverted, invertible = self.sceneTransform().inverted()
if invertible:
connection_pos = inverted.map(scene_pos)
self._geometry.set_end_point(port_type, connection_pos)
cgo.set_geometry_changed()
cgo.update()
def lock(self, locked: bool):
"""
Lock
Parameters
----------
locked : bool
"""
self.setFlag(QGraphicsItem.ItemIsMovable, not locked)
self.setFlag(QGraphicsItem.ItemIsFocusable, not locked)
self.setFlag(QGraphicsItem.ItemIsSelectable, not locked)
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget):
"""
Paint
Parameters
----------
painter : QPainter
option : QStyleOptionGraphicsItem
widget : QWidget
"""
painter.setClipRect(option.exposedRect)
ConnectionPainter.paint(painter, self._connection, self._style)
def mousePressEvent(self, event: QGraphicsSceneMouseEvent):
"""
mousePressEvent
Parameters
----------
event : QGraphicsSceneMouseEvent
"""
super().mousePressEvent(event)
# event.ignore()
def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent):
"""
mouseMoveEvent
Parameters
----------
event : QGraphicsSceneMouseEvent
"""
self.prepareGeometryChange()
# view = event.widget()
# TODO/BUG: widget is returning QWidget(), not QGraphicsView...
view = self._scene.views()[0]
node = self._scene.locate_node_at(event.scenePos(), view.transform())
self._connection.interact_with_node(node)
state_required = self._connection.required_port
if node:
node.react_to_possible_connection(
state_required,
self._connection.data_type(opposite_port(state_required)),
event.scenePos()
)
# -------------------
offset = event.pos() - event.lastPos()
required_port = self._connection.required_port
if required_port != PortType.none:
self._geometry.move_end_point(required_port, offset)
# -------------------
self.update()
event.accept()
def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent):
"""
mouseReleaseEvent
Parameters
----------
event : QGraphicsSceneMouseEvent
"""
self.ungrabMouse()
event.accept()
node = self._scene.locate_node_at(event.scenePos(), self._scene.views()[0].transform())
interaction = NodeConnectionInteraction(node, self._connection, self._scene)
if node and interaction.try_connect():
node.reset_reaction_to_connection()
if self._connection.requires_port:
self._scene.delete_connection(self._connection)
def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent):
"""
hoverEnterEvent
Parameters
----------
event : QGraphicsSceneHoverEvent
"""
self._geometry.hovered = True
self.update()
self._scene.connection_hovered.emit(self.connection, event.screenPos())
event.accept()
def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent):
"""
hoverLeaveEvent
Parameters
----------
event : QGraphicsSceneHoverEvent
"""
self._geometry.hovered = False
self.update()
self._scene.connection_hover_left.emit(self.connection)
event.accept()
def add_graphics_effect(self):
effect = QGraphicsBlurEffect()
effect.setBlurRadius(5)
self.setGraphicsEffect(effect)
# effect = QGraphicsDropShadowEffect()
# effect = ConnectionBlurEffect(self)
# effect.setOffset(4, 4)
# effect.setColor(QColor(Qt.gray).darker(800))
|