Skip to content

Commit bcfba66

Browse files
committed
update sub graph
1 parent 59de5c1 commit bcfba66

File tree

11 files changed

+162
-80
lines changed

11 files changed

+162
-80
lines changed

NodeGraphQt/base/graph.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ def _insert_node(self, pipe, node_id, prev_node_pos):
171171
disconnected = [(pipe.input_port, pipe.output_port)]
172172
connected = []
173173

174-
if node.inputs():
174+
if node.input_ports():
175175
connected.append(
176-
(pipe.output_port, list(node.inputs().values())[0].view)
176+
(pipe.output_port, node.input_ports()[0].view)
177177
)
178-
if node.outputs():
178+
if node.output_ports():
179179
connected.append(
180-
(list(node.outputs().values())[0].view, pipe.input_port)
180+
(node.output_ports()[0].view, pipe.input_port)
181181
)
182182

183183
self._undo_stack.beginMacro('inserted node')
@@ -224,8 +224,8 @@ def _on_node_double_clicked(self, node_id):
224224
"""
225225
node = self.get_node_by_id(node_id)
226226
self.node_double_clicked.emit(node)
227-
if isinstance(node,SubGraph):
228-
node.enter()
227+
if isinstance(node, SubGraph):
228+
self.set_node_space(node)
229229

230230
def _on_node_selected(self, node_id):
231231
"""
@@ -420,6 +420,7 @@ def set_background_color(self, r, g, b):
420420
b (int): blue value.
421421
"""
422422
self.scene().background_color = (r, g, b)
423+
self._viewer.force_update()
423424

424425
def grid_color(self):
425426
"""
@@ -440,6 +441,7 @@ def set_grid_color(self, r, g, b):
440441
b (int): blue value.
441442
"""
442443
self.scene().grid_color = (r, g, b)
444+
self._viewer.force_update()
443445

444446
def display_grid(self, display=True):
445447
"""
@@ -449,6 +451,7 @@ def display_grid(self, display=True):
449451
display: False to not draw the background grid.
450452
"""
451453
self.scene().grid = display
454+
self._viewer.force_update()
452455

453456
def add_properties_bin(self, prop_bin):
454457
"""
@@ -795,7 +798,14 @@ def set_node_space(self, node):
795798
Args:
796799
node (NodeGraphQt.SubGraph): node object.
797800
"""
801+
if self._current_node_space is not None:
802+
[n.hide() for n in self._current_node_space.children()]
803+
self._current_node_space.exit()
804+
798805
self._current_node_space = node
806+
if node is not None:
807+
[n.show() for n in node.children()]
808+
node.enter()
799809

800810
def get_node_space(self):
801811
"""
@@ -1097,7 +1107,8 @@ def save_session(self, file_path):
10971107
node_space = self.get_node_space()
10981108
if node_space is not None:
10991109
node_space = node_space.id
1100-
serliazed_data['graph'] = {'node_space':node_space,'pipe_layout':self._viewer.get_pipe_layout()}
1110+
serliazed_data['graph'] = {'node_space': node_space, 'pipe_layout': self._viewer.get_pipe_layout()}
1111+
serliazed_data['graph']['graph_rect'] = self._viewer.scene_rect()
11011112
file_path = file_path.strip()
11021113
with open(file_path, 'w') as file_out:
11031114
json.dump(serliazed_data, file_out, indent=2, separators=(',', ':'))
@@ -1144,6 +1155,8 @@ def import_session(self, file_path):
11441155
self.set_node_space(self.get_node_by_id(node_space_id))
11451156
self._viewer.set_pipe_layout(layout_data['graph']['pipe_layout'])
11461157

1158+
self._viewer.set_scene_rect(layout_data['graph']['graph_rect'])
1159+
11471160
self._undo_stack.clear()
11481161
self._model.session = file_path
11491162
self.session_changed.emit(file_path)
@@ -1314,6 +1327,24 @@ def save_dialog(self, current_dir=None, ext=None):
13141327

13151328
def use_opengl(self):
13161329
"""
1317-
use opengl to draw the graph
1330+
Use OpenGL to draw the graph.
13181331
"""
13191332
self._viewer.use_opengl()
1333+
1334+
def graph_rect(self):
1335+
"""
1336+
Get the graph viewer range.
1337+
1338+
Returns:
1339+
list: [x, y, width, height].
1340+
"""
1341+
return self._viewer.scene_rect()
1342+
1343+
def set_graph_rect(self, rect):
1344+
"""
1345+
Set the graph viewer range.
1346+
1347+
Args:
1348+
rect (list): [x, y, width, height].
1349+
"""
1350+
self._viewer.set_scene_rect(rect)

NodeGraphQt/base/node.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, f):
3030
def __get__(self, instance, owner):
3131
return self.f(owner)
3232

33+
3334
class NodeObject(object):
3435
"""
3536
The ``NodeGraphQt.NodeObject`` class is the main base class that all
@@ -437,7 +438,7 @@ def set_parent(self, parent_node):
437438
parent = self.parent()
438439
if parent is not None:
439440
parent.remove_child(self)
440-
441+
441442
if parent_node is not None:
442443
self.model.parent_id = parent_node.id
443444
parent_node.add_child(self)
@@ -466,30 +467,33 @@ def path(self):
466467
str: current node path.
467468
"""
468469
if self.parent_id is None:
469-
return "/" + self.name()
470+
return "/"
470471

471-
return self.parent().path() + "/" + self.name()
472+
return self.parent().path() + self.name() + "/"
472473

473474
def delete(self):
474475
"""
475476
Delete node view.
476477
"""
477478
self._view.delete()
478-
if self.parent_id is not None:
479-
self.parent().remove_child(self)
479+
parent = self.parent()
480+
if parent is not None:
481+
parent.remove_child(self)
480482
# self.set_parent_id(None)
481483

482484
def hide(self):
483485
"""
484486
Hide node.
485487
"""
486-
self.set_property('visible', False)
488+
self.view.visible = False
489+
self.model.visible = False
487490

488491
def show(self):
489492
"""
490493
Show node.
491494
"""
492-
self.set_property('visible', True)
495+
self.view.visible = True
496+
self.model.visible = True
493497

494498

495499
class BaseNode(NodeObject):
@@ -538,20 +542,15 @@ def hide(self):
538542
"""
539543
Hide node.
540544
"""
541-
self.set_property('visible', False)
542-
[pipe.setVisible(False) for port in self._inputs+self._outputs for pipe in port.view.connected_pipes]
543-
544-
for port in self._inputs+self._outputs:
545-
for pipe in port.view.connected_pipes:
546-
print(pipe)
547-
pipe.setVisible(False)
545+
super(BaseNode, self).hide()
546+
[pipe.hide() for port in self._inputs + self._outputs for pipe in port.view.connected_pipes]
548547

549548
def show(self):
550549
"""
551550
Show node.
552551
"""
553-
self.set_property('visible', True)
554-
[pipe.setVisible(True) for port in self._inputs+self._outputs for pipe in port.view.connected_pipes]
552+
super(BaseNode, self).show()
553+
[pipe.show() for port in self._inputs + self._outputs for pipe in port.view.connected_pipes]
555554

556555
def update_model(self):
557556
"""
@@ -771,6 +770,7 @@ def add_input(self, name='input', multi_input=False, display_name=True,
771770
raise PortRegistrationError(
772771
'port name "{}" already registered.'.format(name))
773772
view = self.view.add_input(name, multi_input, display_name)
773+
774774
if color:
775775
view.color = color
776776
view.border_color = [min([255, max([0, i + 80])]) for i in color]

NodeGraphQt/base/utils.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
PIPE_LAYOUT_STRAIGHT,
88
PIPE_LAYOUT_ANGLE)
99

10+
1011
def setup_context_menu(graph):
1112
"""
1213
populate the specified graph's context menu with essential menus commands.
@@ -87,13 +88,14 @@ def setup_context_menu(graph):
8788

8889
pipe_menu = edit_menu.add_menu('&Pipe')
8990
pipe_menu.add_command('Curved Pipe', _curved_pipe)
90-
pipe_menu.add_command('Straght Pipe', _straght_pipe)
91+
pipe_menu.add_command('Straght Pipe', _straight_pipe)
9192
pipe_menu.add_command('Angle Pipe', _angle_pipe)
9293

9394
edit_menu.add_command('Toggle Disable Grid', _toggle_grid)
9495

9596
edit_menu.add_separator()
9697

98+
9799
# --- menu command functions. ---
98100

99101

@@ -210,9 +212,11 @@ def _clear_undo(graph):
210212
def _copy_nodes(graph):
211213
graph.copy_nodes()
212214

215+
213216
def _cut_nodes(graph):
214217
graph.cut_nodes()
215218

219+
216220
def _paste_nodes(graph):
217221
graph.paste_nodes()
218222

@@ -241,26 +245,33 @@ def _duplicate_nodes(graph):
241245
def _fit_to_selection(graph):
242246
graph.fit_to_selection()
243247

248+
244249
def _jump_in(graph):
245250
nodes = graph.selected_nodes()
246251
if nodes:
247252
node = nodes[0]
248253
if isinstance(node, SubGraph):
249-
node.enter()
254+
graph.set_node_space(node)
255+
250256

251257
def _jump_out(graph):
252258
node = graph.get_node_space()
253259
if node:
254-
node.exit()
260+
if node.parent() is not None:
261+
graph.set_node_space(node.parent())
262+
255263

256264
def _curved_pipe(graph):
257265
graph.set_pipe_style(PIPE_LAYOUT_CURVED)
258266

259-
def _straght_pipe(graph):
267+
268+
def _straight_pipe(graph):
260269
graph.set_pipe_style(PIPE_LAYOUT_STRAIGHT)
261270

271+
262272
def _angle_pipe(graph):
263273
graph.set_pipe_style(PIPE_LAYOUT_ANGLE)
264274

275+
265276
def _toggle_grid(graph):
266-
graph.display_grid(not graph.scene().grid)
277+
graph.display_grid(not graph.scene().grid)

NodeGraphQt/qgraphics/node_abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def selected(self, selected=False):
144144

145145
@property
146146
def visible(self):
147-
return self._properties['visible']
147+
return self._properties['visible']
148148

149149
@visible.setter
150150
def visible(self, visible=False):

NodeGraphQt/qgraphics/node_base.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,10 @@ def paint(self, painter, option, widget):
170170
painter.setBrush(QtGui.QColor(*NODE_SEL_COLOR))
171171
painter.drawRoundedRect(rect, radius, radius)
172172

173-
label_rect = QtCore.QRectF(rect.left() + (radius / 2),
174-
rect.top() + (radius / 2),
175-
self._width - (radius / 1.25),
176-
28)
173+
label_rect = QtCore.QRectF(rect.left(), rect.top(), self._width, 28)
177174
path = QtGui.QPainterPath()
178-
path.addRoundedRect(label_rect, radius / 1.5, radius / 1.5)
179-
painter.setBrush(QtGui.QColor(0, 0, 0, 50))
175+
path.addRoundedRect(label_rect, radius, radius)
176+
painter.setBrush(QtGui.QColor(30, 30, 30, 200))
180177
painter.fillPath(path, painter.brush())
181178

182179
border_width = 0.8
@@ -456,7 +453,6 @@ def draw_node(self):
456453
Draw the node item in the scene.
457454
"""
458455
height = self._text_item.boundingRect().height()
459-
460456
# setup initial base size.
461457
self._set_base_size(add_w=0.0, add_h=height)
462458
# set text color when node is initialized.

NodeGraphQt/qgraphics/pipe.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ def highlight(self):
248248
def highlighted(self):
249249
return self._highlight
250250

251-
252251
def reset(self):
253252
self._active = False
254253
self._highlight = False
@@ -400,4 +399,4 @@ def paint(self, painter, option, widget):
400399
scale = dist / 20.0
401400
transform.scale(scale, scale)
402401
painter.drawPolygon(transform.map(self._arrow))
403-
painter.restore()
402+
painter.restore()

NodeGraphQt/widgets/viewer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ def establish_connection(self, start_port, end_port):
669669
pipe.draw_path(pipe.input_port, pipe.output_port)
670670
if start_port.node.selected or end_port.node.selected:
671671
pipe.highlight()
672+
if not start_port.node.visible or not end_port.node.visible:
673+
pipe.hide()
672674

673675
def acyclic_check(self, start_port, end_port):
674676
"""
@@ -884,6 +886,16 @@ def zoom_to_nodes(self, nodes):
884886
if self.get_zoom() > 0.1:
885887
self.reset_zoom(self._scene_range.center())
886888

889+
def force_update(self):
890+
self._update_scene()
891+
892+
def scene_rect(self):
893+
return [self._scene_range.x(), self._scene_range.y(), self._scene_range.width(), self._scene_range.height()]
894+
895+
def set_scene_rect(self, rect):
896+
self._scene_range = QtCore.QRectF(*rect)
897+
self._update_scene()
898+
887899
def use_opengl(self):
888900
format = QtOpenGL.QGLFormat(QtOpenGL.QGL.SampleBuffers)
889901
format.setSamples(4)

0 commit comments

Comments
 (0)