forked from JLuebben/Floppy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeLib.py
More file actions
294 lines (253 loc) · 9.94 KB
/
nodeLib.py
File metadata and controls
294 lines (253 loc) · 9.94 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QPoint, QModelIndex
from PyQt5.QtGui import *
from floppy.node import NODECLASSES
import os
from importlib.machinery import SourceFileLoader
customNodesPath = os.path.join(os.path.realpath(__file__)[:-10], 'CustomNodes')
# try:
# [SourceFileLoader(str(i), os.path.join(customNodesPath, path)).load_module() for i, path in enumerate(os.listdir(customNodesPath)) if path.endswith('py')]
# except Exception as e:
# print('Warning: error in custom node:\n{}'.format(str(e)))
for i, path in enumerate(os.listdir(customNodesPath)):
if path.endswith('py'):
try:
SourceFileLoader(str(i), os.path.join(customNodesPath, path)).load_module()
except Exception as e:
print('Warning: error in custom node:\n{}'.format(str(e)))
class NodeFilter(QLineEdit):
"""
Widget for filtering a list of available nodes by user specified characteristics.
"""
def __init__(self, parent=None):
super(NodeFilter, self).__init__(parent)
self.textEdited.connect(self.check)
self.setStyleSheet("NodeFilter {background-color:rgb(75,75,75) ;border:1px solid rgb(0, 0, 0); "
"border-color:black; color: white }")
def focusInEvent(self, event):
"""
This is supposed to selected the whole text whenever the widget is focused. Apparently, things don't work that
way here.
:param event: QFocusEvent.
:return: None
"""
super(NodeFilter, self).focusInEvent(event)
self.selectAll()
def check(self, text):
"""
Interpret the text in the LineEdit and send the filtered node list to the registered NodeList widget.
:param text: string that is used for filtering the node list.
:return: None
"""
text = text.lower()
# nodes = [str(node) for node in nodeList if text in str(node).lower()]
if not text.startswith('$'):
nodes = [node for node in NODECLASSES.keys() if text in node.lower()]
else:
# nodes = set(self.nodeScanner.getHints(text[1:]) + self.nodeScanner.getHints(text[1:], False))
text = text[1:]
nodes = set([nodeName for nodeName, node in NODECLASSES.items() if node.matchHint(text)])
model = QStandardItemModel()
for node in sorted(nodes):
item = QStandardItem()
item.setText(node)
model.appendRow(item)
self.listView.setModel(model)
def registerNodeListLayout(self, layout, widget):
"""
Do I still need this? It doesn't look like it.
:param layout:
:param widget:
:return:
"""
self.check('')
# print layout.children()
def registerListView(self, view):
"""
Establishes a reference to the NodeList instance used for displaying the filtering results.
:param view: Reference to a NodeList instance.
:return: None
"""
self.listView = view
self.check('')
def getSelectedNode(self):
"""
Do I still need this? It doesn't look like it.
"""
pass
def keyPressEvent(self, event):
"""
Handling navigation through the nodeLib widgets by key presses.
:param event: QKeyEvent.
:return: None
"""
super(NodeFilter, self).keyPressEvent(event)
self.parent().keyPressEvent(event)
if event.key() == Qt.Key_Down:
self.listView.setFocus()
self.listView.setCurrentIndex(self.listView.model().index(0, 0))
class NodeList(QListView):
"""
Widget for displaying the available (and filtered) list of nodes and handling drag&drop spawning of nodes.
"""
def __init__(self, parent=None):
super(NodeList, self).__init__(parent)
self.filter = None
self.graph = None
self.down = False
self.selectedClass = None
self.setStyleSheet('''
NodeList {background-color:rgb(75,75,75) ;border:1px solid rgb(0, 0, 0);
border-color:black}
NodeList::item {color: white}
''')
def setup(self, nodeFilter, graph):
self.filter = nodeFilter
self.graph = graph
self.filter.registerListView(self)
def mousePressEvent(self, event):
"""
Handling drag&drop of nodes in the list into the diagram.
:param event: QMouseEvent
:return: None
"""
if not self.down:
super(NodeList, self).mousePressEvent(event)
self.down = True
name = self.filter.listView.selectedIndexes()[0].data()
self.selectedClass = NODECLASSES[name]
# self.blockSignals(True)
# self.selectionChanged()
def mouseReleaseEvent(self, event):
"""
Handling drag&drop of nodes in the list into the diagram.
:param event: QMouseEvent
:return: None
"""
super(NodeList, self).mouseReleaseEvent(event)
self.down = False
if event.pos().x() < 0:
# transform = self.graph.painter.transform
pos = QCursor.pos()
topLeft = self.graph.painter.mapToGlobal(self.graph.painter.pos())
pos -= topLeft
pos -= self.graph.painter.center
# pos -= self.graph.painter.center
# print(pos, self.graph.painter.center, pos*transform)
pos /= self.graph.painter.scale
# print(transform)
self.graph.spawnNode(self.selectedClass, position=(pos.x(), pos.y()))
self.graph.update()
def mouseMoveEvent(self, event):
"""
Handling drag&drop of nodes in the list into the diagram.
:param event: QMouseEvent
:return: None
"""
if not self.down:
super(NodeList, self).mouseMoveEvent(event)
class ContextNodeList(NodeList):
"""
A NodeList widget adapted to work in the context menu created when dragging a connection into open space in the
diagram.
"""
def registerDialog(self, dialog):
"""
Establish a reference to the context menu holding the widget.
:param dialog: Reference to a NodeDialog instance
:return: None
"""
self.dialog = dialog
def registerGraph(self, graph):
self.graph = graph
def registerPainter(self, painter):
self.painter = painter
self.down = False
# def mousePressEvent(self, event):
# """
# Handling drag&drop of nodes in the list into the diagram.
# :param event: QMouseEvent
# :return: None
# """
# # super(NodeList, self).mousePressEvent(event)
# self.down = True
# name = self.filter.listView.selectedIndexes()[0].data()
# self.selectedClass = NODECLASSES[name]
def mouseReleaseEvent(self, event):
"""
Handling the selection and spawning of nodes in the list.
:param event: QMouseEvent.
:return: None
"""
super(NodeList, self).mouseReleaseEvent(event)
# pos = QCursor.pos()
pos = self.parent().mapToGlobal(self.parent().pos())
topLeft = self.graph.painter.mapToGlobal(self.graph.painter.pos())
pos -= topLeft
pos -= self.graph.painter.center
pos /= self.graph.painter.scale
self.graph.spawnNode(self.selectedClass, position=(pos.x(), pos.y()))
self.down = False
self.graph.requestUpdate()
self.dialog.close(True)
def keyPressEvent(self, event):
"""
Handling the selection and spawning of nodes in the list.
:param event: QMouseEvent.
:return: None
"""
super(ContextNodeList, self).keyPressEvent(event)
if event.key() == Qt.Key_Return:
name = self.filter.listView.selectedIndexes()[0].data()
self.selectedClass = NODECLASSES[name]
pos = self.parent().mapToGlobal(self.parent().pos())
topLeft = self.graph.painter.mapToGlobal(self.graph.painter.pos())
pos -= topLeft
pos -= self.graph.painter.center
pos /= self.graph.painter.scale
self.graph.spawnNode(self.selectedClass, position=(pos.x()-5, pos.y()-40))
self.down = False
self.graph.requestUpdate()
self.dialog.close(True)
class ContextNodeFilter(NodeFilter):
"""
A NodeFilter widget adapted to work in the context menu created when dragging a connection into open space in the
diagram.
"""
def registerDialog(self, dialog, back=False):
"""
Establish a reference to the context menu holding the widget and specifying whether a connection to an InputPin
or an OutputPin is about to be established.
:param dialog: Reference to a NodeDialog instance
:param back: boolean. True if the newly created node will be connected to the nodes input. False if otherwise.
:return: None
"""
self.dialog = dialog
self.back = back
def check(self, text):
"""
Adapted method from base class to do hinting by default.
:param text: string interpreted for filtering the node list.
:return: None
"""
try:
text = text.lower()
except AttributeError:
text = ''
# nodes = [str(node) for node in nodeList if text in str(node).lower()]
if self.dialog.cB.checkState():
if text:
text = '$' + text
else:
text = '$' + self.dialog.getTypeHint()
if not text.startswith('$'):
nodes = [node for node in NODECLASSES.keys() if text in node.lower()]
else:
text = text[1:]
nodes = set([nodeName for nodeName, node in NODECLASSES.items() if node.matchHint(text)])
model = QStandardItemModel()
for node in sorted(nodes):
item = QStandardItem()
item.setText(node)
model.appendRow(item)
self.listView.setModel(model)