-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
105 lines (84 loc) · 3.76 KB
/
node.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
from PyQt5.QtCore import Qt, QMimeData, QMarginsF
from PyQt5.QtGui import QDrag, QTransform
from PyQt5.QtWidgets import QGraphicsEllipseItem, QGraphicsTextItem, QApplication, QGraphicsItem
from edge import GraphicsSemiEdge, GraphicsEdge
class GraphicsTextItem(QGraphicsTextItem):
def __init__(self, *args, **kwargs):
QGraphicsTextItem.__init__(self, *args, **kwargs)
def keyPressEvent(self, e):
QGraphicsTextItem.keyPressEvent(self, e)
self.parentItem().update()
def mousePressEvent(self, e):
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ControlModifier:
e.ignore()
else:
for item in self.scene().selectedItems():
item.setSelected(False)
QGraphicsTextItem.mousePressEvent(self, e)
def mouseMoveEvent(self, e):
parent = self.parentItem()
if parent.edgeInConstruction == None:
parent.buildEdge(parent.scenePos())
parent.mouseMoveEvent(e)
def mouseReleaseEvent(self, e):
self.parentItem().mouseReleaseEvent(e)
class GraphicsNode(QGraphicsEllipseItem):
def __init__(self, label = ""):
QGraphicsEllipseItem.__init__(self)
self.text = label
self.textItem = GraphicsTextItem(self.text, parent = self)
self.textItem.setTextInteractionFlags(Qt.TextEditorInteraction)
self.setRect(self.textItem.boundingRect().marginsAdded(QMarginsF(10,10,10,10)))
self.setFlags(QGraphicsItem.ItemIsSelectable)
self.observers = set()
self.edgeInConstruction = None
def mouseMoveEvent(self, e):
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ControlModifier:
mimeData = QMimeData()
drag = QDrag(e.widget())
mimeData.setText("node")
drag.setMimeData(mimeData)
drag.exec_(Qt.MoveAction)
self.ungrabMouse()
if self.edgeInConstruction != None:
self.edgeInConstruction.obsUpdate(e.scenePos())
def buildEdge(self, pos):
self.edgeInConstruction = GraphicsSemiEdge(self, pos)
self.scene().addItem(self.edgeInConstruction)
def mousePressEvent(self, e):
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.NoModifier:
for item in self.scene().selectedItems():
item.setSelected(False)
self.setSelected(True)
self.buildEdge(e.scenePos())
def mouseReleaseEvent(self, e):
if self.edgeInConstruction != None:
self.scene().removeItem(self.edgeInConstruction)
self.observers.remove(self.edgeInConstruction)
self.edgeInConstruction = None
items = [it for it in self.scene().items(e.scenePos()) if isinstance(it, GraphicsNode)]
if len(items) != 0:
self.scene().addItem(GraphicsEdge(self, items[0]))
def mouseDoubleClickEvent(self, e):
self.scene().removeItem(self.edgeInConstruction)
self.edgeInConstruction = None
def update(self, *args, **kwargs):
self.setRect(self.textItem.boundingRect().marginsAdded(QMarginsF(10,10,10,10)))
QGraphicsEllipseItem.update(self, *args, **kwargs)
def paint(self, *args, **kwargs):
QGraphicsEllipseItem.paint(self, *args, **kwargs)
for obs in self.observers:
obs.obsUpdate()
def addObserver(self, obs):
if obs not in self.observers:
self.observers.add(obs)
def delObserver(self, obs):
if obs in self.observers:
self.obervers.remove(obs)
def removeEdges(self):
for obs in self.observers:
self.scene().removeItem(obs)
self.observers = set()