Skip to content

Commit

Permalink
Merge pull request #105 from jchanvfx/property_bin_signal_cleanup
Browse files Browse the repository at this point in the history
Property bin signal cleanup
  • Loading branch information
jchanvfx authored May 21, 2019
2 parents b456253 + e29b428 commit 172024f
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 81 deletions.
10 changes: 7 additions & 3 deletions NodeGraphQt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
__url__ = 'https://github.com/jchanvfx/NodeGraphQt'

__all__ = [
'NodeGraph', 'NodeObject', 'BaseNode', 'BackdropNode', 'Port',
'Menu', 'MenuCommand', 'setup_context_menu'
'BackdropNode', 'BaseNode', 'Menu', 'MenuCommand', 'NodeGraph',
'NodeObject', 'NodeTreeWidget', 'Port', 'PropertiesBinWidget',
'constants', 'setup_context_menu'
]


try:
from Qt import QtWidgets, QtGui, QtCore, QtCompat
except ImportError as ie:
Expand All @@ -61,3 +61,7 @@

# functions
from .base.actions import setup_context_menu

# widgets
from .widgets.node_tree import NodeTreeWidget
from .widgets.properties_bin import PropertiesBinWidget
24 changes: 8 additions & 16 deletions NodeGraphQt/base/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,21 @@ def set_node_prop(self, name, value):
name = 'xy_pos'
setattr(view, name, value)

def update_prop_bin(self, name, value):
"""
updates the property bin widget.
"""
graph = self.node.graph
prop_bin = graph.properties_bin()
properties_wgt = prop_bin.prop_widget(self.node)
if properties_wgt:
prop_wgt = properties_wgt.get_widget(name)
# check if previous value is identical to current value,
# prevent signals from causing a infinite loop.
if prop_wgt and prop_wgt.get_value() != value:
prop_wgt.set_value(value)

def undo(self):
if self.old_val != self.new_val:
self.set_node_prop(self.name, self.old_val)
self.update_prop_bin(self.name, self.old_val)

# emit property changed signal.
graph = self.node.graph
graph.property_changed.emit(self.node, self.name, self.old_val)

def redo(self):
if self.old_val != self.new_val:
self.set_node_prop(self.name, self.new_val)
self.update_prop_bin(self.name, self.new_val)

# emit property changed signal.
graph = self.node.graph
graph.property_changed.emit(self.node, self.name, self.new_val)


class NodeMovedCmd(QtWidgets.QUndoCommand):
Expand Down
92 changes: 57 additions & 35 deletions NodeGraphQt/base/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
from NodeGraphQt.base.node import NodeObject
from NodeGraphQt.base.port import Port
from NodeGraphQt.constants import DRAG_DROP_ID
from NodeGraphQt.widgets.node_tree import NodeTreeWidget
from NodeGraphQt.widgets.properties_bin import PropertiesBinWidget
from NodeGraphQt.widgets.viewer import NodeViewer


class NodeGraph(QtCore.QObject):
"""
base node graph controller.
Args:
tab_search_key(str): hotkey for the tab search widget (default: "tab").
"""

#: signal for when a node has been created in the node graph.
Expand All @@ -49,9 +44,6 @@ def __init__(self, parent=None):
self._node_factory = NodeFactory()
self._undo_stack = QtWidgets.QUndoStack(self)

self._properties_bin = None
self._nodes_tree = None

tab = QtWidgets.QAction('Search Nodes', self)
tab.setShortcut('tab')
tab.triggered.connect(self._toggle_tab_search)
Expand Down Expand Up @@ -80,9 +72,9 @@ def _toggle_tab_search(self):
self._viewer.tab_search_set_nodes(self._node_factory.names)
self._viewer.tab_search_toggle()

def _on_property_changed(self, node_id, prop_name, prop_value):
def _on_property_bin_changed(self, node_id, prop_name, prop_value):
"""
called when a property widget has changed in the properties bin.
called when a property widget has changed in a properties bin.
(emits the node object, property name, property value)
Args:
Expand All @@ -91,8 +83,10 @@ def _on_property_changed(self, node_id, prop_name, prop_value):
prop_value (object): python object.
"""
node = self.get_node_by_id(node_id)
node.set_property(prop_name, prop_value)
self.property_changed.emit(node, prop_name, prop_value)

# prevent signals from causing a infinite loop.
if node.get_property(prop_name) != prop_value:
node.set_property(prop_name, prop_value)

def _on_node_double_clicked(self, node_id):
"""
Expand All @@ -103,9 +97,6 @@ def _on_node_double_clicked(self, node_id):
node_id (str): node id emitted by the viewer.
"""
node = self.get_node_by_id(node_id)
if self._properties_bin:
self._properties_bin.add_node(node)

self.node_double_clicked.emit(node)

def _on_node_selected(self, node_id):
Expand Down Expand Up @@ -237,32 +228,63 @@ def scene(self):
"""
return self._viewer.scene()

def properties_bin(self):
def background_color(self):
"""
Initializes the node properties bin widget when first called.
Return the node graph background color.
Returns:
PropBinWidget: the initialized widget.
tuple: r, g ,b
"""
return self.scene().background_color

def set_background_color(self, r, g, b):
"""
if self._properties_bin is None:
self._properties_bin = PropertiesBinWidget()
# wire up widget.
self._properties_bin.property_changed.connect(
self._on_property_changed
)
return self._properties_bin
Set node graph background color.
def nodes_tree(self):
Args:
r (int): red value.
g (int): green value.
b (int): blue value.
"""
self.scene().background_color = (r, g, b)

def grid_color(self):
"""
Initializes the nodes list widget when first called.
Return the node graph grid color.
Returns:
NodeTreeWidget: the initialized widget.
tuple: r, g ,b
"""
return self.scene().grid_color

def set_grid_color(self, r, g, b):
"""
Set node graph grid color.
Args:
r (int): red value.
g (int): green value.
b (int): blue value.
"""
self.scene().grid_color = (r, g, b)

def display_grid(self, display=True):
"""
Display node graph background grid.
Args:
display: False to not draw the background grid.
"""
self.scene().grid = display

def add_properties_bin(self, prop_bin):
"""
Wire up a properties bin widget to the node graph.
Args:
prop_bin (NodeGraphQt.PropertiesBinWidget): properties widget.
"""
if self._nodes_tree is None:
self._nodes_tree = NodeTreeWidget()
self._nodes_tree.set_node_factory(self._node_factory)
return self._nodes_tree
prop_bin.property_changed.connect(self._on_property_bin_changed)

def undo_stack(self):
"""
Expand Down Expand Up @@ -315,12 +337,12 @@ def acyclic(self):
"""
return self._model.acyclic

def set_acyclic(self, mode=True):
def set_acyclic(self, mode=False):
"""
Set the node graph to be acyclic or not. (default=True)
Enable the node graph to be a acyclic graph. (default=False)
Args:
mode (bool): false to disable acyclic.
mode (bool): true to enable acyclic.
"""
self._model.acyclic = mode
self._viewer.acyclic = mode
Expand Down
18 changes: 13 additions & 5 deletions NodeGraphQt/base/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ def view(self):
Returns the :class:`QtWidgets.QGraphicsItem` used in the scene.
Returns:
AbstractNodeItem: node item.
NodeGraphQt.qgraphics.node_abstract.AbstractNodeItem: node item.
"""
return self._view

def set_view(self, item):
"""
Sets the graphic item to use for the scene.
Sets the qgraphics item to use for the scene.
Args:
item (AbstractNodeItem): node view item.
item (NodeGraphQt.qgraphics.node_abstract.AbstractNodeItem): node item.
"""
self._view = item
self._view.id = self.model.id
Expand All @@ -109,14 +109,20 @@ def set_view(self, item):
@property
def model(self):
"""
Returns the node model.
Return the node model.
Returns:
NodeModel: node model object.
NodeGraphQt.base.model.NodeModel: node model object.
"""
return self._model

def set_model(self, model):
"""
Set the node model.
Args:
model (NodeGraphQt.base.model.NodeModel): node model object.
"""
self._model = model
self._model.type_ = self.type_
self._model.id = self.view.id
Expand Down Expand Up @@ -264,6 +270,8 @@ def set_property(self, name, value):
name (str): name of the property.
value (object): property data.
"""

# prevent signals from causing a infinite loop.
if self.get_property(name) == value:
return

Expand Down
4 changes: 2 additions & 2 deletions NodeGraphQt/base/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def view(self):
returns the :class:`QtWidgets.QGraphicsItem` used in the scene.
Returns:
PortItem: port item.
NodeGraphQt.qgraphics.port.PortItem: port item.
"""
return self.__view

Expand All @@ -38,7 +38,7 @@ def model(self):
returns the port model.
Returns:
PortModel: port model.
NodeGraphQt.base.model.PortModel: port model.
"""
return self.__model

Expand Down
1 change: 0 additions & 1 deletion NodeGraphQt/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@

# === NODE VIEWER ===

#: Node graph background color.
VIEWER_BG_COLOR = (35, 35, 35)
VIEWER_GRID_COLOR = (45, 45, 45)
VIEWER_GRID_OVERLAY = True
Expand Down
14 changes: 11 additions & 3 deletions NodeGraphQt/widgets/node_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ def __eq__(self, other):


class NodeTreeWidget(QtWidgets.QTreeWidget):
"""
Node tree for displaying node types.
def __init__(self, parent=None):
Args:
parent (QtWidgets.QWidget): parent of the new widget.
node_graph (NodeGraphQt.NodeGraph): node graph.
"""

def __init__(self, parent=None, node_graph=None):
super(NodeTreeWidget, self).__init__(parent)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
self.setHeaderHidden(True)
self._factory = None
self._custom_labels = {}
self._set_node_factory(node_graph._node_factory)

def __repr__(self):
return '<{} object at {}>'.format(self.__class__.__name__, hex(id(self)))
Expand Down Expand Up @@ -66,7 +74,7 @@ def _build_tree(self):

category_item.addChild(item)

def set_node_factory(self, factory):
def _set_node_factory(self, factory):
"""
Set current node factory.
Expand All @@ -87,6 +95,6 @@ def set_category_label(self, category, label):

def update(self):
"""
Update and refresh the node list widget.
Update and refresh the node tree widget.
"""
self._build_tree()
2 changes: 1 addition & 1 deletion NodeGraphQt/widgets/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class NodePropWidget(QtWidgets.QWidget):
Node properties widget for display a Node object.
Args:
parent:
parent (QtWidgets.QWidget): parent object.
node (NodeGraphQt.BaseNode): node.
"""

Expand Down
Loading

0 comments on commit 172024f

Please sign in to comment.