Skip to content

Commit

Permalink
Merge pull request #213 from jchanvfx/cleanup
Browse files Browse the repository at this point in the history
clean up
  • Loading branch information
jchanvfx authored May 18, 2021
2 parents 63ea968 + 1f43209 commit a5707a1
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 55 deletions.
2 changes: 1 addition & 1 deletion NodeGraphQt/base/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class NodeRemovedCmd(QtWidgets.QUndoCommand):
Args:
graph (NodeGraphQt.NodeGraph): node graph.
node (NodeGraphQt.NodeObject): node.
node (NodeGraphQt.BaseNode or NodeGraphQt.NodeObject): node.
"""

def __init__(self, graph, node):
Expand Down
3 changes: 1 addition & 2 deletions NodeGraphQt/base/factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/python

from ..errors import NodeRegistrationError


Expand Down Expand Up @@ -45,7 +44,7 @@ def nodes(self):

def create_node_instance(self, node_type=None, alias=None):
"""
create node class by the node type identifier or alias.
create node object by the node type identifier or alias.
Args:
node_type (str): node type.
Expand Down
13 changes: 7 additions & 6 deletions NodeGraphQt/base/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _on_insert_node(self, pipe, node_id, prev_node_pos):
node = self.get_node_by_id(node_id)

# exclude the BackdropNode
if not isinstance(node, BaseNode):
if isinstance(node, BackdropNode):
return

disconnected = [(pipe.input_port, pipe.output_port)]
Expand Down Expand Up @@ -348,7 +348,7 @@ def _on_search_triggered(self, node_type, pos):
Args:
node_type (str): node identifier.
pos (tuple): x,y position for the node.
pos (tuple or list): x, y position for the node.
"""
self.create_node(node_type, pos=pos)

Expand Down Expand Up @@ -578,7 +578,7 @@ def set_grid_mode(self, mode=VIEWER_GRID_LINES):
* :attr:`NodeGraphQt.constants.VIEWER_GRID_LINES`
Args:
mode (int): background styles.
mode (int): background style.
"""
self.scene().grid_mode = mode
self._viewer.force_update()
Expand Down Expand Up @@ -819,7 +819,7 @@ def registered_nodes(self):
"""
Return a list of all node types that have been registered.
Hint:
See Also:
To register a node :meth:`NodeGraph.register_node`
Returns:
Expand All @@ -843,7 +843,7 @@ def register_nodes(self, nodes):
Register the nodes to the :meth:`NodeGraph.node_factory`
Args:
nodes (list[NodeGraphQt.NodeObject]): list of nodes.
nodes (list): list of nodes.
"""
[self._node_factory.register_node(n) for n in nodes]
self._viewer.rebuild_tab_search()
Expand All @@ -865,7 +865,7 @@ def create_node(self, node_type, name=None, selected=True, color=None,
pos (list[int, int]): initial x, y position for the node (default: ``(0, 0)``).
Returns:
NodeGraphQt.NodeObject: the created instance of the node.
BaseNode or NodeObject: the created instance of the node.
"""
if not self._editable:
return
Expand Down Expand Up @@ -1285,6 +1285,7 @@ def _deserialize(self, data, relative_pos=False, pos=None, set_parent=True):
# set custom properties.
for prop, val in n_data.get('custom', {}).items():
node.model.set_property(prop, val)

nodes[n_id] = node

if isinstance(node, SubGraph):
Expand Down
1 change: 1 addition & 0 deletions NodeGraphQt/base/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def add_command(self, name, func=None, node_type=None, node_class=None):
"""
if not node_type and not node_class:
raise NodeMenuError('Node type or Node class not specified!')

if node_class:
node_type = node_class.__name__

Expand Down
12 changes: 10 additions & 2 deletions NodeGraphQt/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@


class PortModel(object):
"""
Data dump for a port object.
"""

def __init__(self, node):
self.node = node
Expand Down Expand Up @@ -51,6 +54,9 @@ def to_dict(self):


class NodeModel(object):
"""
Data dump for a node object.
"""

def __init__(self):
self.type_ = None
Expand Down Expand Up @@ -225,9 +231,11 @@ def to_dict(self):
'disabled': False,
'visible': True,
'inputs': {
<port_name>: {<node_id>: [<port_name>, <port_name>]}},
<port_name>: {<node_id>: [<port_name>, <port_name>]}
},
'outputs': {
<port_name>: {<node_id>: [<port_name>, <port_name>]}},
<port_name>: {<node_id>: [<port_name>, <port_name>]}
},
'input_ports': [<port_name>, <port_name>],
'output_ports': [<port_name>, <port_name>],
'width': 0.0,
Expand Down
9 changes: 5 additions & 4 deletions NodeGraphQt/base/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
NodeFilePath)


class classproperty(object):
class _ClassProperty(object):

def __init__(self, f):
self.f = f
Expand Down Expand Up @@ -72,7 +72,7 @@ def __repr__(self):
return '<{}("{}") object at {}>'.format(
self.__class__.__name__, self.NODE_NAME, hex(id(self)))

@classproperty
@_ClassProperty
def type_(cls):
"""
Node type identifier followed by the class name.
Expand Down Expand Up @@ -642,7 +642,7 @@ def add_custom_widget(self, widget, widget_type=NODE_PROP_QLABEL, tab=None):
up to the :meth:`NodeObject.set_property` function.
Args:
widget_cls (NodeBaseWidget): node widget class object.
widget (NodeBaseWidget): node widget class object.
widget_type: widget flag to display in the
:class:`NodeGraphQt.PropertiesBinWidget` (default: QLabel).
tab (str): name of the widget tab to display in.
Expand Down Expand Up @@ -1221,7 +1221,8 @@ def __init__(self):
# override base default color.
self.model.color = (5, 129, 138, 255)
self.create_property('backdrop_text', '',
widget_type=NODE_PROP_QTEXTEDIT, tab='Backdrop')
widget_type=NODE_PROP_QTEXTEDIT,
tab='Backdrop')

def on_backdrop_updated(self, update_prop, value=None):
"""
Expand Down
4 changes: 1 addition & 3 deletions NodeGraphQt/qgraphics/node_abstract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/python

from Qt import QtCore, QtWidgets

from ..constants import (Z_VAL_NODE, NODE_WIDTH, NODE_HEIGHT, ITEM_CACHE_MODE)
Expand All @@ -13,6 +12,7 @@ class AbstractNodeItem(QtWidgets.QGraphicsItem):
def __init__(self, name='node', parent=None):
super(AbstractNodeItem, self).__init__(parent)
self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
self.setCacheMode(ITEM_CACHE_MODE)
self.setZValue(Z_VAL_NODE)
self._properties = {
'id': None,
Expand All @@ -28,8 +28,6 @@ def __init__(self, name='node', parent=None):
self._width = NODE_WIDTH
self._height = NODE_HEIGHT

self.setCacheMode(ITEM_CACHE_MODE)

def __repr__(self):
return '{}.{}(\'{}\')'.format(
self.__module__, self.__class__.__name__, self.name)
Expand Down
18 changes: 16 additions & 2 deletions NodeGraphQt/qgraphics/node_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def mousePressEvent(self, event):
super(NodeItem, self).mousePressEvent(event)

def mouseReleaseEvent(self, event):
"""
Re-implemented to ignore event if Alt modifier is pressed.
Args:
event (QtWidgets.QGraphicsSceneMouseEvent): mouse event.
"""
if event.modifiers() == QtCore.Qt.AltModifier:
event.ignore()
return
Expand Down Expand Up @@ -149,6 +155,13 @@ def mouseDoubleClickEvent(self, event):
super(NodeItem, self).mouseDoubleClickEvent(event)

def itemChange(self, change, value):
"""
Re-implemented to update pipes on selection changed.
Args:
change:
value:
"""
if change == self.ItemSelectedChange and self.scene():
self.reset_pipes()
if value:
Expand All @@ -161,7 +174,7 @@ def itemChange(self, change, value):

def _tooltip_disable(self, state):
"""
updates the node tooltip when the node is enabled/disabled.
Updates the node tooltip when the node is enabled/disabled.
Args:
state (bool): node disable state.
Expand Down Expand Up @@ -213,7 +226,7 @@ def activate_pipes(self):

def highlight_pipes(self):
"""
highlight pipe color.
Highlight pipe color.
"""
ports = self.inputs + self.outputs
for port in ports:
Expand Down Expand Up @@ -400,6 +413,7 @@ def draw_node(self):
(re-implemented for vertical layout design)
"""
height = self._text_item.boundingRect().height()

# setup initial base size.
self._set_base_size(add_w=0.0, add_h=height)
# set text color when node is initialized.
Expand Down
10 changes: 7 additions & 3 deletions NodeGraphQt/qgraphics/pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def paint(self, painter, option, widget):
pen_width += 0.2
pen_style = PIPE_STYLES.get(PIPE_STYLE_DOTTED)

pen = QtGui.QPen(color, pen_width)
pen.setStyle(pen_style)
pen = QtGui.QPen(color, pen_width, pen_style)
pen.setCapStyle(QtCore.Qt.RoundCap)
pen.setJoinStyle(QtCore.Qt.MiterJoin)

painter.save()
painter.setPen(pen)
Expand Down Expand Up @@ -125,7 +125,11 @@ def paint(self, painter, option, widget):
pen_width = 0.6
if dist < 1.0:
pen_width *= (1.0 + dist)
painter.setPen(QtGui.QPen(color, pen_width))

pen = QtGui.QPen(color, pen_width)
pen.setCapStyle(QtCore.Qt.RoundCap)
pen.setJoinStyle(QtCore.Qt.MiterJoin)
painter.setPen(pen)

transform = QtGui.QTransform()
transform.translate(cen_x, cen_y)
Expand Down
5 changes: 2 additions & 3 deletions NodeGraphQt/qgraphics/port.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
from .. import QtGui, QtCore, QtWidgets
from Qt import QtGui, QtCore, QtWidgets

from ..constants import (
IN_PORT, OUT_PORT,
Expand All @@ -23,6 +23,7 @@ class PortItem(QtWidgets.QGraphicsItem):
def __init__(self, parent=None):
super(PortItem, self).__init__(parent)
self.setAcceptHoverEvents(True)
self.setCacheMode(ITEM_CACHE_MODE)
self.setFlag(self.ItemIsSelectable, False)
self.setFlag(self.ItemSendsScenePositionChanges, True)
self.setZValue(Z_VAL_PORT)
Expand All @@ -39,8 +40,6 @@ def __init__(self, parent=None):
self._multi_connection = False
self._locked = False

self.setCacheMode(ITEM_CACHE_MODE)

def __str__(self):
return '{}.PortItem("{}")'.format(self.__module__, self.name)

Expand Down
28 changes: 23 additions & 5 deletions NodeGraphQt/qgraphics/slicer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/python
from .. import QtCore, QtGui, QtWidgets
import math

from Qt import QtCore, QtGui, QtWidgets

from ..constants import Z_VAL_NODE_WIDGET, PIPE_SLICER_COLOR


Expand Down Expand Up @@ -27,6 +30,7 @@ def paint(self, painter, option, widget):
p2 = self.path().pointAtPercent(1)
size = 6.0
offset = size / 2
arrow_size = 4.0

painter.save()
painter.setRenderHint(painter.Antialiasing, True)
Expand All @@ -43,17 +47,31 @@ def paint(self, painter, option, widget):
painter.setPen(QtGui.QPen(text_color, 1.5, QtCore.Qt.SolidLine))
painter.drawText(text_pos, text)

painter.setPen(QtGui.QPen(color, 1.5, QtCore.Qt.DashLine))
painter.setPen(QtGui.QPen(color, 1.5, QtCore.Qt.DashDotLine))
painter.drawPath(self.path())

painter.setPen(QtGui.QPen(color, 1.5, QtCore.Qt.SolidLine))
pen = QtGui.QPen(color, 1.5, QtCore.Qt.SolidLine)
pen.setCapStyle(QtCore.Qt.RoundCap)
pen.setJoinStyle(QtCore.Qt.MiterJoin)
painter.setPen(pen)
painter.setBrush(color)

rect = QtCore.QRectF(p1.x() - offset, p1.y() - offset, size, size)
painter.drawEllipse(rect)

rect = QtCore.QRectF(p2.x() - offset, p2.y() - offset, size, size)
painter.drawEllipse(rect)
arrow = QtGui.QPolygonF()
arrow.append(QtCore.QPointF(-arrow_size, arrow_size))
arrow.append(QtCore.QPointF(0.0, -arrow_size * 0.9))
arrow.append(QtCore.QPointF(arrow_size, arrow_size))

transform = QtGui.QTransform()
transform.translate(p2.x(), p2.y())
radians = math.atan2(p2.y() - p1.y(),
p2.x() - p1.x())
degrees = math.degrees(radians) - 90
transform.rotate(degrees)

painter.drawPolygon(transform.map(arrow))
painter.restore()

def draw_path(self, p1, p2):
Expand Down
2 changes: 1 addition & 1 deletion NodeGraphQt/widgets/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python
from .. import QtCore, QtWidgets
from Qt import QtCore, QtWidgets
from .stylesheet import STYLE_QMENU


Expand Down
10 changes: 5 additions & 5 deletions NodeGraphQt/widgets/dialogs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from .stylesheet import STYLE_MESSAGEBOX
from .. import QtWidgets
from Qt import QtWidgets

current_dir = os.path.expanduser('~')

Expand All @@ -17,8 +17,8 @@ def set_dir(file):
class FileDialog(object):

@staticmethod
def getSaveFileName(parent=None, title="Save File", file_dir=None,
ext_filter="*"):
def getSaveFileName(parent=None, title='Save File', file_dir=None,
ext_filter='*'):
if not file_dir:
file_dir = current_dir
file_dlg = QtWidgets.QFileDialog.getSaveFileName(
Expand All @@ -29,8 +29,8 @@ def getSaveFileName(parent=None, title="Save File", file_dir=None,
return file_dlg

@staticmethod
def getOpenFileName(parent=None, title="Open File", file_dir=None,
ext_filter="*"):
def getOpenFileName(parent=None, title='Open File', file_dir=None,
ext_filter='*'):
if not file_dir:
file_dir = current_dir

Expand Down
2 changes: 1 addition & 1 deletion NodeGraphQt/widgets/node_publish_widget.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from .properties import PropFileSavePath
from .. import QtWidgets
from Qt import QtWidgets


class _element_widget(QtWidgets.QWidget):
Expand Down
2 changes: 1 addition & 1 deletion NodeGraphQt/widgets/node_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class NodeTreeWidget(QtWidgets.QTreeWidget):
def __init__(self, parent=None, node_graph=None):
super(NodeTreeWidget, self).__init__(parent)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
self.setWindowTitle('Node Tree')
self.setWindowTitle('Nodes Tree')
self.setHeaderHidden(True)
self._factory = None
self._custom_labels = {}
Expand Down
Loading

0 comments on commit a5707a1

Please sign in to comment.