This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqnodeseditor.py
192 lines (148 loc) · 7.55 KB
/
qnodeseditor.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
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
# Copyright (c) 2014, ALDO HOEBEN
# Copyright (c) 2012, STANISLAW ADASZEWSKI
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of STANISLAW ADASZEWSKI nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#DISCLAIMED. IN NO EVENT SHALL STANISLAW ADASZEWSKI BE LIABLE FOR ANY
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from PySide.QtCore import (Qt, QObject, QEvent, QSizeF, QRectF, QPointF)
from PySide.QtGui import (QBrush, QPen, QPainter, QPainterPath, QPixmap)
from PySide.QtGui import (QApplication, QGraphicsView, QGraphicsItem,
QGraphicsPathItem, QGraphicsSceneMouseEvent)
from qneblock import QNEBlock
from qneport import QNEPort
from qneconnection import QNEConnection
class QNodesEditor(QObject):
def __init__(self, parent, scene, view):
super(QNodesEditor, self).__init__(parent)
self.scene = scene
self.scene.installEventFilter(self)
gridSize = 25
gridMap = QPixmap(gridSize,gridSize)
gridPainter = QPainter(gridMap)
gridPainter.fillRect(0,0,gridSize,gridSize, QApplication.palette().window().color().darker(103))
gridPainter.fillRect(1,1,gridSize-2,gridSize-2, QApplication.palette().window())
gridPainter.end()
self.scene.setBackgroundBrush( QBrush(gridMap) )
originSize = 50
originItem = QGraphicsPathItem()
path = QPainterPath()
path.moveTo(0,-originSize)
path.lineTo(0,originSize)
path.moveTo(-originSize,0)
path.lineTo(originSize,0)
originItem.setPath(path)
originItem.setPen(QPen(QApplication.palette().window().color().darker(110),2))
originItem.setZValue(-2)
self.scene.addItem(originItem)
self.view = view
self.view.setDragMode(QGraphicsView.RubberBandDrag)
self.view.setRenderHint(QPainter.Antialiasing)
self.connection = None
def selectNone(self):
for item in self.scene.items():
if item.type() == QNEBlock.Type or item.type() == QNEConnection.Type:
item.setSelected(False)
def selectAll(self):
for item in self.scene.items():
if item.type() == QNEBlock.Type or item.type() == QNEConnection.Type:
item.setSelected(True)
def selectInverse(self):
for item in self.scene.items():
if item.type() == QNEBlock.Type or item.type() == QNEConnection.Type:
item.setSelected(not item.isSelected())
def deleteSelected(self):
for item in self.scene.items():
if (item.isSelected() and item.type() == QNEConnection.Type):
port1 = item.port1()
port2 = item.port2()
if port1.isOutput():
self.onRemoveConnection(item, port1, port2)
else:
self.onRemoveConnection(item, port2, port1)
item.delete()
def itemAt(self, position):
items = self.scene.items(QRectF( position - QPointF(2,2) , QSizeF(4,4) ))
for item in items:
if item.type() > QGraphicsItem.UserType:
return item
return None
def eventFilter(self, object, event):
if event.type() == QEvent.GraphicsSceneMousePress:
self.mousePressOnBlock = False
self.mouseDragged = False
if event.button() == Qt.LeftButton:
item = self.itemAt(event.scenePos())
if item and item.type() == QNEPort.Type:
self.view.setDragMode(QGraphicsView.NoDrag)
self.connection = QNEConnection(None)
self.scene.addItem(self.connection)
self.connection.setPort1(item)
self.connection.setPos1(item.scenePos()+QPointF(item.radius(),0))
self.connection.setPos2(event.scenePos())
self.connection.updatePath()
self.selectNone()
return True
elif item and item.type() == QNEBlock.Type:
self.mousePressOnBlock = True
return False
elif event.type() == QEvent.GraphicsSceneMouseMove:
if self.connection:
self.connection.setPos2(event.scenePos())
self.connection.updatePath()
return True
else:
self.mouseDragged = True
return False
elif event.type() == QEvent.GraphicsSceneMouseRelease:
if self.connection and event.button() == Qt.LeftButton:
self.view.setDragMode(QGraphicsView.RubberBandDrag)
item = self.itemAt(event.scenePos())
if item and item.type() == QNEPort.Type:
port1 = self.connection.port1()
port2 = item
if port1.block() != port2.block() and port1.isOutput() != port2.isOutput() and not port1.isConnected(port2):
self.connection.setPos2(port2.scenePos())
self.connection.setPort2(port2)
self.connection.updatePath()
if port1.isOutput():
self.onAddConnection(self.connection, port1, port2)
else:
self.onAddConnection(self.connection, port2, port1)
self.connection = None
return True
self.connection.delete()
self.connection = None
return True
elif event.button() == Qt.LeftButton:
if self.mousePressOnBlock and self.mouseDragged:
for item in self.scene.selectedItems():
if item.type() == QNEBlock.Type:
self.onBlockMoved(item)
return super(QNodesEditor, self).eventFilter(object, event)
def onAddConnection(self, connection, fromPort, toPort):
print ("Added connection from %s on %s to %s on %s" %
(fromPort.portName(), fromPort.block().name(), toPort.portName(), toPort.block().name()))
def onRemoveConnection(self, connection, fromPort, toPort):
print ("Removed connection from %s on %s to %s on %s" %
(fromPort.portName(), fromPort.block().name(), toPort.portName(), toPort.block().name()))
def onBlockMoved(self, block):
print ("Block %s moved" % block.name())