Edge

 1# Edge.py
 2
 3from PySide6.QtWidgets import QGraphicsPathItem
 4from PySide6.QtCore import Qt, QPointF
 5from PySide6.QtGui import QPen, QPainterPath
 6
 7class Edge(QGraphicsPathItem):
 8    def __init__(self, source_port):
 9        super().__init__()
10        self.source_port = source_port
11        self.source_id = source_port.parentItem().data.uniq_id
12        self.setPen(QPen(Qt.black, 2))
13        self.setZValue(-1)
14        self.destination_port = None
15        self.destination_id = None
16        self.update_position()
17        self.setAcceptHoverEvents(True)
18
19    def update_position(self, end_point=None):
20        path = QPainterPath()
21        start_point = self.source_port.scenePos()
22        if end_point:
23            control_point_1 = QPointF(start_point.x() + 50, start_point.y())
24            control_point_2 = QPointF(end_point.x() - 50, end_point.y())
25            path.moveTo(start_point)
26            path.cubicTo(control_point_1, control_point_2, end_point)
27        elif self.destination_port:
28            end_point = self.destination_port.scenePos()
29            control_point_1 = QPointF(start_point.x() + 50, start_point.y())
30            control_point_2 = QPointF(end_point.x() - 50, end_point.y())
31            path.moveTo(start_point)
32            path.cubicTo(control_point_1, control_point_2, end_point)
33        else:
34            path.moveTo(start_point)
35            path.cubicTo(start_point, start_point, start_point)
36        self.setPath(path)
37
38    def set_destination(self, destination_port):
39        self.destination_port = destination_port
40        self.destination_id = destination_port.parentItem().data.uniq_id
41        self.update_position()
42        source_node = self.source_port.parentItem().data
43        dest_node = self.destination_port.parentItem().data
44        source_node.nexts.append(self.destination_id)
45        dest_node.prevs.append(self.source_id)  # Add to prevs
46
47    def remove(self):
48        if self in self.source_port.edges:
49            self.source_port.edges.remove(self)
50        if self in self.destination_port.edges:
51            self.destination_port.edges.remove(self)
52        if self.destination_id in self.source_port.parentItem().data.nexts:
53            self.source_port.parentItem().data.nexts.remove(self.destination_id)
54        if self.source_id in self.destination_port.parentItem().data.prevs:
55            self.destination_port.parentItem().data.prevs.remove(self.source_id)  # Remove from prevs
56        self.scene().removeItem(self)
57        
58    def hoverEnterEvent(self, event):
59        self.setPen(QPen(Qt.red, 2))  # Change color on hover
60        self.setCursor(Qt.PointingHandCursor)
61        super().hoverEnterEvent(event)
62
63    def hoverLeaveEvent(self, event):
64        self.setPen(QPen(Qt.black, 2))  # Revert color on hover exit
65        self.unsetCursor()
66        super().hoverLeaveEvent(event)
class Edge(PySide6.QtWidgets.QGraphicsPathItem):
 8class Edge(QGraphicsPathItem):
 9    def __init__(self, source_port):
10        super().__init__()
11        self.source_port = source_port
12        self.source_id = source_port.parentItem().data.uniq_id
13        self.setPen(QPen(Qt.black, 2))
14        self.setZValue(-1)
15        self.destination_port = None
16        self.destination_id = None
17        self.update_position()
18        self.setAcceptHoverEvents(True)
19
20    def update_position(self, end_point=None):
21        path = QPainterPath()
22        start_point = self.source_port.scenePos()
23        if end_point:
24            control_point_1 = QPointF(start_point.x() + 50, start_point.y())
25            control_point_2 = QPointF(end_point.x() - 50, end_point.y())
26            path.moveTo(start_point)
27            path.cubicTo(control_point_1, control_point_2, end_point)
28        elif self.destination_port:
29            end_point = self.destination_port.scenePos()
30            control_point_1 = QPointF(start_point.x() + 50, start_point.y())
31            control_point_2 = QPointF(end_point.x() - 50, end_point.y())
32            path.moveTo(start_point)
33            path.cubicTo(control_point_1, control_point_2, end_point)
34        else:
35            path.moveTo(start_point)
36            path.cubicTo(start_point, start_point, start_point)
37        self.setPath(path)
38
39    def set_destination(self, destination_port):
40        self.destination_port = destination_port
41        self.destination_id = destination_port.parentItem().data.uniq_id
42        self.update_position()
43        source_node = self.source_port.parentItem().data
44        dest_node = self.destination_port.parentItem().data
45        source_node.nexts.append(self.destination_id)
46        dest_node.prevs.append(self.source_id)  # Add to prevs
47
48    def remove(self):
49        if self in self.source_port.edges:
50            self.source_port.edges.remove(self)
51        if self in self.destination_port.edges:
52            self.destination_port.edges.remove(self)
53        if self.destination_id in self.source_port.parentItem().data.nexts:
54            self.source_port.parentItem().data.nexts.remove(self.destination_id)
55        if self.source_id in self.destination_port.parentItem().data.prevs:
56            self.destination_port.parentItem().data.prevs.remove(self.source_id)  # Remove from prevs
57        self.scene().removeItem(self)
58        
59    def hoverEnterEvent(self, event):
60        self.setPen(QPen(Qt.red, 2))  # Change color on hover
61        self.setCursor(Qt.PointingHandCursor)
62        super().hoverEnterEvent(event)
63
64    def hoverLeaveEvent(self, event):
65        self.setPen(QPen(Qt.black, 2))  # Revert color on hover exit
66        self.unsetCursor()
67        super().hoverLeaveEvent(event)

QGraphicsPathItem(self, parent: Optional[PySide6.QtWidgets.QGraphicsItem] = None) -> None QGraphicsPathItem(self, path: PySide6.QtGui.QPainterPath, parent: Optional[PySide6.QtWidgets.QGraphicsItem] = None) -> None

Edge(source_port)
 9    def __init__(self, source_port):
10        super().__init__()
11        self.source_port = source_port
12        self.source_id = source_port.parentItem().data.uniq_id
13        self.setPen(QPen(Qt.black, 2))
14        self.setZValue(-1)
15        self.destination_port = None
16        self.destination_id = None
17        self.update_position()
18        self.setAcceptHoverEvents(True)
source_port
source_id
destination_port
destination_id
def update_position(self, end_point=None):
20    def update_position(self, end_point=None):
21        path = QPainterPath()
22        start_point = self.source_port.scenePos()
23        if end_point:
24            control_point_1 = QPointF(start_point.x() + 50, start_point.y())
25            control_point_2 = QPointF(end_point.x() - 50, end_point.y())
26            path.moveTo(start_point)
27            path.cubicTo(control_point_1, control_point_2, end_point)
28        elif self.destination_port:
29            end_point = self.destination_port.scenePos()
30            control_point_1 = QPointF(start_point.x() + 50, start_point.y())
31            control_point_2 = QPointF(end_point.x() - 50, end_point.y())
32            path.moveTo(start_point)
33            path.cubicTo(control_point_1, control_point_2, end_point)
34        else:
35            path.moveTo(start_point)
36            path.cubicTo(start_point, start_point, start_point)
37        self.setPath(path)
def set_destination(self, destination_port):
39    def set_destination(self, destination_port):
40        self.destination_port = destination_port
41        self.destination_id = destination_port.parentItem().data.uniq_id
42        self.update_position()
43        source_node = self.source_port.parentItem().data
44        dest_node = self.destination_port.parentItem().data
45        source_node.nexts.append(self.destination_id)
46        dest_node.prevs.append(self.source_id)  # Add to prevs
def remove(self):
48    def remove(self):
49        if self in self.source_port.edges:
50            self.source_port.edges.remove(self)
51        if self in self.destination_port.edges:
52            self.destination_port.edges.remove(self)
53        if self.destination_id in self.source_port.parentItem().data.nexts:
54            self.source_port.parentItem().data.nexts.remove(self.destination_id)
55        if self.source_id in self.destination_port.parentItem().data.prevs:
56            self.destination_port.parentItem().data.prevs.remove(self.source_id)  # Remove from prevs
57        self.scene().removeItem(self)
def hoverEnterEvent(self, event):
59    def hoverEnterEvent(self, event):
60        self.setPen(QPen(Qt.red, 2))  # Change color on hover
61        self.setCursor(Qt.PointingHandCursor)
62        super().hoverEnterEvent(event)
def hoverLeaveEvent(self, event):
64    def hoverLeaveEvent(self, event):
65        self.setPen(QPen(Qt.black, 2))  # Revert color on hover exit
66        self.unsetCursor()
67        super().hoverLeaveEvent(event)
Inherited Members
PySide6.QtWidgets.QGraphicsPathItem
boundingRect
contains
extension
isObscuredBy
opaqueArea
paint
path
setPath
shape
type
PySide6.QtWidgets.QAbstractGraphicsShapeItem
brush
pen
setBrush
setPen
PySide6.QtWidgets.QGraphicsItem
acceptDrops
acceptHoverEvents
acceptTouchEvents
acceptedMouseButtons
addToIndex
advance
boundingRegion
boundingRegionGranularity
cacheMode
childItems
childrenBoundingRect
clearFocus
clipPath
collidesWithItem
collidesWithPath
collidingItems
commonAncestorItem
contextMenuEvent
cursor
data
deviceTransform
dragEnterEvent
dragLeaveEvent
dragMoveEvent
dropEvent
effectiveOpacity
ensureVisible
filtersChildEvents
flags
focusInEvent
focusItem
focusOutEvent
focusProxy
focusScopeItem
grabKeyboard
grabMouse
graphicsEffect
group
handlesChildEvents
hasCursor
hasFocus
hide
hoverMoveEvent
inputMethodEvent
inputMethodHints
inputMethodQuery
installSceneEventFilter
isActive
isAncestorOf
isBlockedByModalPanel
isClipped
isEnabled
isObscured
isPanel
isSelected
isUnderMouse
isVisible
isVisibleTo
isWidget
isWindow
itemChange
itemTransform
keyPressEvent
keyReleaseEvent
mapFromItem
mapFromParent
mapFromScene
mapRectFromItem
mapRectFromParent
mapRectFromScene
mapRectToItem
mapRectToParent
mapRectToScene
mapToItem
mapToParent
mapToScene
mouseDoubleClickEvent
mouseMoveEvent
mousePressEvent
mouseReleaseEvent
moveBy
opacity
panel
panelModality
parentItem
parentObject
parentWidget
pos
prepareGeometryChange
removeFromIndex
removeSceneEventFilter
resetTransform
rotation
scale
scene
sceneBoundingRect
sceneEvent
sceneEventFilter
scenePos
sceneTransform
scroll
setAcceptDrops
setAcceptHoverEvents
setAcceptTouchEvents
setAcceptedMouseButtons
setActive
setBoundingRegionGranularity
setCacheMode
setCursor
setData
setEnabled
setFiltersChildEvents
setFlag
setFlags
setFocus
setFocusProxy
setGraphicsEffect
setGroup
setHandlesChildEvents
setInputMethodHints
setOpacity
setPanelModality
setParentItem
setPos
setRotation
setScale
setSelected
setToolTip
setTransform
setTransformOriginPoint
setTransformations
setVisible
setX
setY
setZValue
show
stackBefore
toGraphicsObject
toolTip
topLevelItem
topLevelWidget
transform
transformOriginPoint
transformations
ungrabKeyboard
ungrabMouse
unsetCursor
update
updateMicroFocus
wheelEvent
window
x
y
zValue
GraphicsItemFlag
GraphicsItemChange
CacheMode
PanelModality
Extension
UserType