Skip to content

Commit a12ec29

Browse files
authored
Merge pull request #466 from sionex-code/main
Update viewer.py
2 parents 6dd58df + 0e9bdf9 commit a12ec29

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

NodeGraphQt/nodes/base_node.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from NodeGraphQt.widgets.node_widgets import (
1515
NodeBaseWidget,
1616
NodeCheckBox,
17+
NodeButton,
1718
NodeComboBox,
1819
NodeLineEdit
1920
)
@@ -273,7 +274,31 @@ def add_text_input(self, name, label='', text='', placeholder_text='',
273274
self.view.add_widget(widget)
274275
#: redraw node to address calls outside the "__init__" func.
275276
self.view.draw_node()
277+
def add_button(self, name, label='', text='', tooltip=None, tab=None):
278+
"""
279+
Creates and embeds a QPushButton widget into the node.
280+
281+
This function is used to add an interactive button to a node's properties
282+
panel. You can then connect your own functions to its `clicked` signal.
283+
284+
Args:
285+
name (str): A unique name for the widget property.
286+
label (str): The label displayed next to the widget (optional).
287+
text (str): The text displayed on the button itself.
288+
tooltip (str): A tooltip that appears on hover.
289+
tab (str): The name of the tab to display the widget in.
290+
"""
291+
# This would create the button widget internally.
292+
# Unlike a checkbox, a button doesn't store a value, so we don't
293+
# call self.create_property().
294+
widget = NodeButton(self.view, name, label, text)
295+
widget.setToolTip(tooltip or '')
296+
297+
# The widget is added to the node's layout.
298+
self.view.add_widget(widget)
276299

300+
# The node is redrawn to ensure the new widget is visible.
301+
self.view.draw_node()
277302
def add_checkbox(self, name, label='', text='', state=False, tooltip=None,
278303
tab=None):
279304
"""

NodeGraphQt/widgets/node_widgets.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,87 @@ def set_value(self, text=''):
383383
if text != self.get_value():
384384
self.get_custom_widget().setText(text)
385385
self.on_value_changed()
386+
class NodeButton(NodeBaseWidget):
387+
"""
388+
Displays as a ``QPushButton`` in a node.
389+
390+
.. inheritance-diagram:: NodeGraphQt.widgets.node_widgets.NodeButton
391+
:parts: 1
392+
393+
.. note::
394+
`To embed a` ``QPushButton`` `in a node, you would typically use a
395+
helper function like` :meth:`NodeGraphQt.BaseNode.add_button`
396+
"""
397+
398+
def __init__(self, parent=None, name='', label='', text=''):
399+
super(NodeButton, self).__init__(parent, name, label)
400+
401+
# Create the core QPushButton widget.
402+
self._button = QtWidgets.QPushButton(text)
403+
404+
# --- Styling ---
405+
# Calculate text color based on the viewer's background for good contrast.
406+
text_color = tuple(map(lambda i, j: i - j, (255, 255, 255),
407+
ViewerEnum.BACKGROUND_COLOR.value))
408+
409+
# Define a clean, modern stylesheet for the button.
410+
style_dict = {
411+
'QPushButton': {
412+
'background-color': 'rgba(40, 40, 40, 200)',
413+
'border': '1px solid rgba(100, 100, 100, 255)',
414+
'border-radius': '3px',
415+
'color': 'rgba({0},{1},{2},150)'.format(*text_color),
416+
'font-size': '10pt',
417+
'padding': '2px 15px',
418+
'max-height': '20px',
419+
},
420+
'QPushButton:hover': {
421+
'background-color': 'rgba(50, 50, 50, 220)',
422+
'border': '1px solid rgba(150, 150, 150, 255)',
423+
},
424+
'QPushButton:pressed': {
425+
'background-color': 'rgba(25, 25, 25, 200)',
426+
'border': '1px solid rgba(80, 80, 80, 255)',
427+
}
428+
}
386429

430+
# Apply the stylesheet.
431+
stylesheet = ''
432+
for css_class, css in style_dict.items():
433+
style = '{} {{\n'.format(css_class)
434+
for elm_name, elm_val in css.items():
435+
style += ' {}:{};\n'.format(elm_name, elm_val)
436+
style += '}\n'
437+
stylesheet += style
438+
self._button.setStyleSheet(stylesheet)
439+
440+
# --- Signal Connection ---
441+
# When the button is clicked, it will trigger the base widget's
442+
# 'on_value_changed' method, which in turn emits the 'value_changed'
443+
# signal that the node can listen to.
444+
self._button.clicked.connect(self.on_value_changed)
445+
446+
# Embed the styled button into the node widget layout.
447+
self.set_custom_widget(self._button)
448+
449+
@property
450+
def type_(self):
451+
"""
452+
Returns the unique type identifier for this widget.
453+
"""
454+
return 'ButtonNodeWidget'
455+
456+
def get_value(self):
457+
"""
458+
Returns the current text of the button.
459+
"""
460+
return self._button.text()
461+
462+
def set_value(self, text):
463+
"""
464+
Sets the text displayed on the button.
465+
"""
466+
self._button.setText(text)
387467

388468
class NodeCheckBox(NodeBaseWidget):
389469
"""

NodeGraphQt/widgets/viewer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def dropEvent(self, event):
675675
pos = self.mapToScene(event.pos())
676676
event.setDropAction(QtCore.Qt.CopyAction)
677677
self.data_dropped.emit(
678-
event.mimeData(), QtCore.QPoint(pos.x(), pos.y())
678+
event.mimeData(), QtCore.QPoint(int(pos.x()), int(pos.y()))
679679
)
680680

681681
def dragEnterEvent(self, event):

0 commit comments

Comments
 (0)