-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkgi.py
executable file
·116 lines (88 loc) · 3.14 KB
/
linkgi.py
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
GraphicsItem to model a link
author: Rinse Wester
"""
import sys
from PyQt5.QtWidgets import QWidget, QGraphicsItem, QGraphicsPathItem, QMenu, QAction
from PyQt5.QtCore import QRectF, QRect, QPointF, QPoint, Qt, QVariant
from PyQt5.QtGui import QColor, QPainter, QPen, QBrush, QPainterPath, QFont
from collections import Counter
import schemastyle
class LinkGI(QGraphicsPathItem):
def __init__(self, name, srcSocket, dstSocket, thickness=2):
super().__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setAcceptHoverEvents(True)
self.hovering = False
self.linkName = name
self.srcSocket = srcSocket
self.dstSocket = dstSocket
self.srcSocket.link = self
self.dstSocket.link = self
self.updateShape()
# set the pen style
self.linkPen = QPen()
self.linkPen.setWidth(thickness)
self.linkPen.setCapStyle(Qt.RoundCap);
self.linkPen.setColor(schemastyle.LINK_COLOR)
self.setPen(self.linkPen)
def updateShape(self):
# Create the bezier curve path
srcX, srcY = self.srcSocket.linkConnectionPos()
dstX, dstY = self.dstSocket.linkConnectionPos()
linkPath = QPainterPath()
linkPath.setFillRule(Qt.WindingFill)
linkPath.moveTo(srcX, srcY)
linkPath.cubicTo(srcX + 100, srcY, dstX - 100, dstY, dstX, dstY)
self.setPath(linkPath)
def hoverEnterEvent(self, event):
self.linkPen.setColor(QColor(Qt.white))
self.setPen(self.linkPen)
super().hoverEnterEvent(event)
def hoverLeaveEvent(self, event):
self.linkPen.setColor(QColor(Qt.gray))
self.setPen(self.linkPen)
super().hoverLeaveEvent(event)
@property
def name(self):
return self.linkName
@name.setter
def name(self, name):
self.linkName = name
self.update()
@property
def thickness(self):
return self.linkPen.width()
@thickness.setter
def thickness(self, thickness):
self.linkPen.setWidth(thickness)
self.setPen(self.linkPen)
self.update()
class PartialLinkGI(QGraphicsPathItem):
def __init__(self, dstTempPos=(0, 0)):
super().__init__()
self.dstTempPos = dstTempPos
# set the pen style
self.linkPen = QPen()
self.linkPen.setWidth(2)
self.linkPen.setCapStyle(Qt.RoundCap);
self.linkPen.setColor(schemastyle.LINK_COLOR)
self.setPen(self.linkPen)
def setSourceSocket(self, srcSock):
self.srcSocket = srcSock
self.srcSocket.link = self
self.updateShape()
def setDestTempPos(self, xpos, ypos):
self.dstTempPos = xpos, ypos
self.updateShape()
def updateShape(self):
# Create the bezier curve path
srcX, srcY = self.srcSocket.linkConnectionPos()
dstX, dstY = self.dstTempPos
linkPath = QPainterPath()
linkPath.setFillRule(Qt.WindingFill)
linkPath.moveTo(srcX, srcY)
linkPath.cubicTo(srcX + 100, srcY, dstX - 100, dstY, dstX, dstY)
self.setPath(linkPath)