Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion NodeGraphQt/nodes/base_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
NodeCheckBox,
NodeButton,
NodeComboBox,
NodeLineEdit
NodeLineEdit,
NodeSpinBox
)


Expand Down Expand Up @@ -274,6 +275,46 @@ def add_text_input(self, name, label='', text='', placeholder_text='',
self.view.add_widget(widget)
#: redraw node to address calls outside the "__init__" func.
self.view.draw_node()

def add_spinbox(self, name, label='', value=0, min_value=0, max_value=100,
tooltip=None, tab=None,double=False):
"""
Creates a custom property with the :meth:`NodeObject.create_property`
function and embeds a :class:`PySide2.QtWidgets.QLineEdit` widget
into the node.

Note:
The ``value_changed`` signal from the added node widget is wired
up to the :meth:`NodeObject.set_property` function.

Args:
name (str): name for the custom property.
label (str): label to be displayed.
value (double): pre-filled value.
min_value (double): minimum value.
max_value (double): maximum value.
tooltip (str): widget tooltip.
tab (str): name of the widget tab to display in.
double (bool): double or integer.
"""
if not double:
value = int(value)
min_value = int(min_value)
max_value = int(max_value)
self.create_property(
name,
value=value,
widget_type=NodePropWidgetEnum.QLINE_EDIT.value,
widget_tooltip=tooltip,
tab=tab
)
widget = NodeSpinBox(self.view, name, label, 0,min_value, max_value ,double)
widget.setToolTip(tooltip or '')
widget.value_changed.connect(lambda k, v: self.set_property(k, v))
self.view.add_widget(widget)
#: redraw node to address calls outside the "__init__" func.
self.view.draw_node()

def add_button(self, name, label='', text='', tooltip=None, tab=None):
"""
Creates and embeds a QPushButton widget into the node.
Expand Down
72 changes: 72 additions & 0 deletions NodeGraphQt/widgets/node_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,78 @@ def set_value(self, text=''):
if text != self.get_value():
self.get_custom_widget().setText(text)
self.on_value_changed()
class NodeSpinBox(NodeBaseWidget):
"""
Displays as a ``QSpinBox`` in a node.

.. inheritance-diagram:: NodeGraphQt.widgets.node_widgets.NodeSpinBox
:parts: 1

.. note::
`To embed a` ``QSpinBox`` `in a node see func:`
:meth:`NodeGraphQt.BaseNode.add_spinbox`
"""

def __init__(self, parent=None, name='', label='', value=0, min_value=0, max_value=100, double=False):
super(NodeSpinBox, self).__init__(parent, name, label)
bg_color = ViewerEnum.BACKGROUND_COLOR.value
text_color = tuple(map(lambda i, j: i - j, (255, 255, 255),
bg_color))
text_sel_color = text_color
style_dict = {
'QSpinBox': {
'background': 'rgba({0},{1},{2},20)'.format(*bg_color),
'border': '1px solid rgb({0},{1},{2})'
.format(*ViewerEnum.GRID_COLOR.value),
'border-radius': '3px',
'color': 'rgba({0},{1},{2},150)'.format(*text_color),
'selection-background-color': 'rgba({0},{1},{2},100)'
.format(*text_sel_color),
}
}
stylesheet = ''
for css_class, css in style_dict.items():
style = '{} {{\n'.format(css_class)
for elm_name, elm_val in css.items():
style += ' {}:{};\n'.format(elm_name, elm_val)
style += '}\n'
stylesheet += style
if double:
spin_box = QtWidgets.QDoubleSpinBox()
else:
spin_box = QtWidgets.QSpinBox()
spin_box.setRange(min_value, max_value)
spin_box.setValue(value)
spin_box.setStyleSheet(stylesheet)
spin_box.setAlignment(QtCore.Qt.AlignCenter)
spin_box.editingFinished.connect(self.on_value_changed)
spin_box.clearFocus()
self.set_custom_widget(spin_box)
self.widget().setMaximumWidth(140)

@property
def type_(self):
return 'SpinBoxNodeWidget'

def get_value(self):
"""
Returns the widgets current value.

Returns:
str: current text.
"""
return str(self.get_custom_widget().value())

def set_value(self, value=0):
"""
Sets the widgets current text.

Args:
text (int): new value.
"""
if value != self.get_value():
self.get_custom_widget().setValue(value)
self.on_value_changed()
class NodeButton(NodeBaseWidget):
"""
Displays as a ``QPushButton`` in a node.
Expand Down
5 changes: 5 additions & 0 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def main():
group_node.MyGroupNode,
widget_nodes.DropdownMenuNode,
widget_nodes.TextInputNode,
widget_nodes.SpinBoxNode,
widget_nodes.CheckboxNode
])

Expand Down Expand Up @@ -76,6 +77,10 @@ def main():
n_text_input = graph.create_node(
'nodes.widget.TextInputNode', name='text node', color='#0a1e20')

# create node with the embedded QSpinBox widget.
n_spinbox_input = graph.create_node(
'nodes.widget.SpinBoxNode', name='spinbox node', color='#0a1e20')

# create node with the embedded QCheckBox widgets.
n_checkbox = graph.create_node(
'nodes.widget.CheckboxNode', name='checkbox node')
Expand Down
24 changes: 24 additions & 0 deletions examples/nodes/widget_nodes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from sympy import false

from NodeGraphQt import BaseNode


Expand Down Expand Up @@ -69,3 +71,25 @@ def __init__(self):
# create input and output port.
self.add_input('in', color=(200, 100, 0))
self.add_output('out', color=(0, 100, 200))

class SpinBoxNode(BaseNode):
"""
An example of a node with a embedded QSpinBox.
"""

# unique node identifier.
__identifier__ = 'nodes.widget'

# initial default node name.
NODE_NAME = 'spinbox'

def __init__(self):
super(SpinBoxNode, self).__init__()

# create input & output ports
self.add_input('in')
self.add_output('out')

# create QSpinBox widget.
self.add_spinbox("value", "value", 0, 0, 255, double=True)
self.add_spinbox("value2", "value", 0, 0, 255, double=False)