-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_graphics_node.py
More file actions
351 lines (290 loc) · 13.5 KB
/
Copy pathnode_graphics_node.py
File metadata and controls
351 lines (290 loc) · 13.5 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# -*- coding: utf-8 -*-
"""
A module containing Graphics representation of :class:`~nodeeditor.node_node.Node`
"""
from qtpy.QtWidgets import QGraphicsItem, QWidget, QGraphicsTextItem
from qtpy.QtGui import QFont, QColor, QPen, QBrush, QPainterPath
from qtpy.QtCore import Qt, QRectF
class QDMGraphicsNode(QGraphicsItem):
"""Class describing Graphics representation of :class:`~nodeeditor.node_node.Node`"""
def __init__(self, node:'Node', parent:QWidget=None):
"""
:param node: reference to :class:`~nodeeditor.node_node.Node`
:type node: :class:`~nodeeditor.node_node.Node`
:param parent: parent widget
:type parent: QWidget
:Instance Attributes:
- **node** - reference to :class:`~nodeeditor.node_node.Node`
"""
super().__init__(parent)
self.node = node
# init our flags
self.hovered = False
self._was_moved = False
self._last_selected_state = False
# Resize state
self._resizing = False
self._resize_start_pos = None
self._resize_start_size = None
self._resize_handle_size = 12
self._min_width = 170
self._min_height = 90
#Misc
self.enable_lod = True
self.initSizes()
self.initAssets()
self.initUI()
@property
def content(self):
"""Reference to `Node Content`"""
return self.node.content if self.node else None
@property
def title(self):
"""title of this `Node`
:getter: current Graphics Node title
:setter: stores and make visible the new title
:type: str
"""
return self._title
@title.setter
def title(self, value):
self._title = value
self.title_item.setPlainText(self._title)
def initUI(self):
"""Set up this ``QGraphicsItem``"""
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setAcceptHoverEvents(True)
# init title
self.initTitle()
self.title = self.node.title
self.initContent()
def initSizes(self):
"""Set up internal attributes like `width`, `height`, etc."""
self.width = 180
self.height = 240
self.edge_roundness = 0.0 # Removed roundness for performance
self.edge_padding = 10
self.title_height = 24
self.title_horizontal_padding = 16
self.title_vertical_padding = 0
def initAssets(self):
"""Initialize ``QObjects`` like ``QColor``, ``QPen`` and ``QBrush``"""
self._title_color = Qt.white
self._title_font = QFont("Ubuntu", 10)
self._color = QColor("#FF000000")
self._color_selected = QColor("#FFFFA637")
self._color_hovered = QColor("#FF37A6FF")
self._pen_default = QPen(self._color)
self._pen_default.setWidthF(2.0)
self._pen_selected = QPen(self._color_selected)
self._pen_selected.setWidthF(2.0)
self._pen_hovered = QPen(self._color_hovered)
self._pen_hovered.setWidthF(3.0)
self._brush_title = QBrush(QColor("#FF313131"))
self._brush_background = QBrush(QColor("#FF212121"))
def onSelected(self):
"""Our event handling when the node was selected"""
self.node.scene.grScene.itemSelected.emit()
def doSelect(self, new_state=True):
"""Safe version of selecting the `Graphics Node`. Takes care about the selection state flag used internally
:param new_state: ``True`` to select, ``False`` to deselect
:type new_state: ``bool``
"""
self.setSelected(new_state)
self._last_selected_state = new_state
if new_state: self.onSelected()
def mouseMoveEvent(self, event):
"""Overridden event to detect that we moved with this `Node` or resized it"""
if self._resizing:
# Calculate new size
delta = event.pos() - self._resize_start_pos
new_width = max(self._resize_start_size[0] + delta.x(), self._min_width)
new_height = max(self._resize_start_size[1] + delta.y(), self._min_height)
self.width = new_width
self.height = new_height
self.prepareGeometryChange()
self.initTitle()
self.initContent()
# Update socket positions
for socket in self.node.inputs + self.node.outputs:
socket.setSocketPosition()
# Update all connected edges for this node
self.node.updateConnectedEdges()
self.update()
for node in self.scene().scene.nodes:
if node.grNode.isSelected():
node.updateConnectedEdges()
return
super().mouseMoveEvent(event)
# Optimized: batch edge updates and only update when necessary
if self._was_moved:
# Only update connected edges for selected nodes
selected_nodes = [node for node in self.scene().scene.nodes if node.grNode.isSelected()]
for node in selected_nodes:
node.updateConnectedEdges()
self._was_moved = True
def mouseReleaseEvent(self, event):
"""Overriden event to handle when we moved, selected or deselected this `Node` or finished resizing"""
if self._resizing:
self._resizing = False
self.node.scene.history.storeHistory("Node resized", setModified=True)
self.update()
return
super().mouseReleaseEvent(event)
# handle when grNode moved
if self._was_moved:
self._was_moved = False
self.node.scene.history.storeHistory("Node moved", setModified=True)
self.node.scene.resetLastSelectedStates()
self.doSelect() # also trigger itemSelected when node was moved
# we need to store the last selected state, because moving does also select the nodes
self.node.scene._last_selected_items = self.node.scene.getSelectedItems()
# now we want to skip storing selection
return
# handle when grNode was clicked on
if self._last_selected_state != self.isSelected() or self.node.scene._last_selected_items != self.node.scene.getSelectedItems():
self.node.scene.resetLastSelectedStates()
self._last_selected_state = self.isSelected()
self.onSelected()
def mouseDoubleClickEvent(self, event):
"""Overriden event for doubleclick. Resend to `Node::onDoubleClicked`"""
self.node.onDoubleClicked(event)
def mousePressEvent(self, event):
"""Overridden event to handle resize handle press"""
# Check if mouse is on resize handle
handle_rect = QRectF(self.width - self._resize_handle_size, self.height - self._resize_handle_size, self._resize_handle_size, self._resize_handle_size)
if handle_rect.contains(event.pos()):
self._resizing = True
self._resize_start_pos = event.pos()
self._resize_start_size = (self.width, self.height)
return
super().mousePressEvent(event)
def hoverEnterEvent(self, event: 'QGraphicsSceneHoverEvent') -> None:
"""Handle hover effect"""
self.hovered = True
self.update()
def hoverLeaveEvent(self, event: 'QGraphicsSceneHoverEvent') -> None:
"""Handle hover effect"""
self.hovered = False
self.update()
def boundingRect(self) -> QRectF:
"""Defining Qt' bounding rectangle"""
return QRectF(
0,
0,
self.width,
self.height
).normalized()
def initTitle(self):
"""Set up the title Graphics representation: font, color, position, etc."""
# Remove existing title item if it exists
if hasattr(self, 'title_item') and self.title_item is not None:
self.title_item.setParentItem(None)
self.title_item = None
# Create new title item with proper positioning
self.title_item = QGraphicsTextItem(self)
self.title_item.node = self.node
self.title_item.setDefaultTextColor(self._title_color)
self.title_item.setFont(self._title_font)
# Set the title text
if hasattr(self.node, 'title'):
self.title_item.setPlainText(self.node.title)
elif hasattr(self.node, 'op_title'):
self.title_item.setPlainText(self.node.op_title)
# Position the title in the header area with proper alignment
self.title_item.setPos(self.title_horizontal_padding, self.title_vertical_padding)
self.title_item.setTextWidth(
self.width - 2 * self.title_horizontal_padding
)
# Set visibility based on LOD
lod = self.getLOD()
self.title_item.setVisible(lod >= 1) # Hide title at lowest detail level
self.title_item.setZValue(1)
def initContent(self):
"""Set up the `grContent` - ``QGraphicsProxyWidget`` to have a container for `Graphics Content`"""
if self.content is not None:
self.content.setGeometry(self.edge_padding, self.title_height + self.edge_padding,
self.width - 2 * self.edge_padding, self.height - 2 * self.edge_padding - self.title_height)
# Only create the proxy widget once
if not hasattr(self, 'grContent') or self.grContent is None:
self.grContent = self.node.scene.grScene.addWidget(self.content)
self.grContent.node = self.node
self.grContent.setParentItem(self)
else:
# Just update geometry on resize
self.grContent.setGeometry(self.edge_padding, self.title_height + self.edge_padding,
self.width - 2 * self.edge_padding, self.height - 2 * self.edge_padding - self.title_height)
# Set content visibility based on LOD
if hasattr(self, 'grContent') and self.grContent is not None:
lod = self.getLOD()
self.grContent.setVisible(lod >= 2) # Hide content at low detail levels
def getLOD(self):
"""Get Level of Detail based on current zoom level"""
if self.enable_lod:
if hasattr(self.scene(), 'views') and self.scene().views():
view = self.scene().views()[0]
if hasattr(view, 'zoom'):
zoom = view.zoom
if zoom <= 3:
return 0 # Lowest detail - just rectangles
elif zoom <= 6:
return 1 # Medium detail - basic shapes
else:
return 2 # High detail - full rendering
else:
return 2
else:
return 2 # Default to high detail
def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
"""Painting the Node with LOD optimization"""
lod = self.getLOD()
if lod == 0:
# Lowest detail - just simple rectangles
painter.setPen(Qt.NoPen)
painter.setBrush(self._brush_title)
painter.drawRect(0, 0, self.width, self.title_height)
painter.setBrush(self._brush_background)
painter.drawRect(0, self.title_height, self.width, self.height - self.title_height)
# Simple outline
painter.setBrush(Qt.NoBrush)
painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected)
painter.drawRect(0, 0, self.width, self.height)
elif lod == 1:
# Medium detail - basic rectangles with better outline
painter.setPen(Qt.NoPen)
painter.setBrush(self._brush_title)
painter.drawRect(0, 0, self.width, self.title_height)
painter.setBrush(self._brush_background)
painter.drawRect(0, self.title_height, self.width, self.height - self.title_height)
# Better outline with hover effect
painter.setBrush(Qt.NoBrush)
if self.hovered:
painter.setPen(self._pen_hovered)
painter.drawRect(-1, -1, self.width+2, self.height+2)
painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected)
painter.drawRect(0, 0, self.width, self.height)
else:
# High detail - full rendering (rectangular instead of rounded for performance)
# title
painter.setPen(Qt.NoPen)
painter.setBrush(self._brush_title)
painter.drawRect(0, 0, self.width, self.title_height)
# content
painter.setBrush(self._brush_background)
painter.drawRect(0, self.title_height, self.width, self.height - self.title_height)
# outline
painter.setBrush(Qt.NoBrush)
if self.hovered:
painter.setPen(self._pen_hovered)
painter.drawRect(-1, -1, self.width+2, self.height+2)
painter.setPen(self._pen_default)
painter.drawRect(0, 0, self.width, self.height)
else:
painter.setPen(self._pen_default if not self.isSelected() else self._pen_selected)
painter.drawRect(0, 0, self.width, self.height)
# Draw resize handle (bottom-right corner) only at high detail
handle_size = 12
painter.setBrush(QColor(180, 180, 180))
painter.setPen(Qt.NoPen)
painter.drawRect(self.width-handle_size, self.height-handle_size, handle_size, handle_size)