Skip to content

Commit 7809dd0

Browse files
committed
custom port colors #118
1 parent 4786f5a commit 7809dd0

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

NodeGraphQt/base/node.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,16 @@ def add_checkbox(self, name='', label='', text='', state=False, tab=None):
457457
widget.value_changed.connect(lambda k, v: self.set_property(k, v))
458458
self.view.add_widget(widget)
459459

460-
def add_input(self, name='input', multi_input=False, display_name=True):
460+
def add_input(self, name='input', multi_input=False, display_name=True,
461+
color=None):
461462
"""
462463
Add input :class:`Port` to node.
463464
464465
Args:
465466
name (str): name for the input port.
466467
multi_input (bool): allow port to have more than one connection.
467468
display_name (bool): display the port name on the node.
469+
color (tuple): initial port color (r, g, b) 0-255.
468470
469471
Returns:
470472
NodeGraphQt.Port: the created port object.
@@ -473,6 +475,9 @@ def add_input(self, name='input', multi_input=False, display_name=True):
473475
raise PortRegistrationError(
474476
'port name "{}" already registered.'.format(name))
475477
view = self.view.add_input(name, multi_input, display_name)
478+
if color:
479+
view.color = color
480+
view.border_color = [min([255, max([0, i + 80])]) for i in color]
476481
port = Port(self, view)
477482
port.model.type_ = IN_PORT
478483
port.model.name = name
@@ -482,14 +487,16 @@ def add_input(self, name='input', multi_input=False, display_name=True):
482487
self.model.inputs[port.name()] = port.model
483488
return port
484489

485-
def add_output(self, name='output', multi_output=True, display_name=True):
490+
def add_output(self, name='output', multi_output=True, display_name=True,
491+
color=None):
486492
"""
487493
Add output :class:`Port` to node.
488494
489495
Args:
490496
name (str): name for the output port.
491497
multi_output (bool): allow port to have more than one connection.
492498
display_name (bool): display the port name on the node.
499+
color (tuple): initial port color (r, g, b) 0-255.
493500
494501
Returns:
495502
NodeGraphQt.Port: the created port object.
@@ -498,6 +505,9 @@ def add_output(self, name='output', multi_output=True, display_name=True):
498505
raise PortRegistrationError(
499506
'port name "{}" already registered.'.format(name))
500507
view = self.view.add_output(name, multi_output, display_name)
508+
if color:
509+
view.color = color
510+
view.border_color = [min([255, max([0, i + 80])]) for i in color]
501511
port = Port(self, view)
502512
port.model.type_ = OUT_PORT
503513
port.model.name = name

NodeGraphQt/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
OUT_PORT = 'out'
2828

2929
PORT_DEFAULT_SIZE = 22.0
30+
PORT_DEFAULT_COLOR = (49, 115, 100, 255)
31+
PORT_DEFAULT_BORDER_COLOR = (29, 202, 151, 255)
3032
PORT_ACTIVE_COLOR = (14, 45, 59, 255)
3133
PORT_ACTIVE_BORDER_COLOR = (107, 166, 193, 255)
3234
PORT_HOVER_COLOR = (17, 43, 82, 255)

NodeGraphQt/qgraphics/port.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from NodeGraphQt.constants import (
55
IN_PORT, OUT_PORT,
6+
PORT_DEFAULT_COLOR,
7+
PORT_DEFAULT_BORDER_COLOR,
68
PORT_DEFAULT_SIZE,
79
PORT_FALLOFF,
810
PORT_HOVER_COLOR,
@@ -29,8 +31,8 @@ def __init__(self, parent=None):
2931
self._hovered = False
3032
self._name = 'port'
3133
self._display_name = True
32-
self._color = (49, 115, 100, 255)
33-
self._border_color = (29, 202, 151, 255)
34+
self._color = PORT_DEFAULT_COLOR
35+
self._border_color = PORT_DEFAULT_BORDER_COLOR
3436
self._border_size = 1
3537
self._port_type = None
3638
self._multi_connection = False
@@ -78,9 +80,9 @@ def paint(self, painter, option, widget):
7880
color = QtGui.QColor(*self.color)
7981
border_color = QtGui.QColor(*self.border_color)
8082

81-
painter.setBrush(color)
8283
pen = QtGui.QPen(border_color, 1.8)
8384
painter.setPen(pen)
85+
painter.setBrush(color)
8486
painter.drawEllipse(port_rect)
8587

8688
if self.connected_pipes and not self._hovered:
@@ -90,6 +92,10 @@ def paint(self, painter, option, widget):
9092
rect = QtCore.QRectF(port_rect.center().x() - w / 2,
9193
port_rect.center().y() - h / 2,
9294
w, h)
95+
border_color = QtGui.QColor(*self.border_color)
96+
pen = QtGui.QPen(border_color, 1.6)
97+
painter.setPen(pen)
98+
painter.setBrush(border_color)
9399
painter.drawEllipse(rect)
94100
elif self._hovered:
95101
if self.multi_connection:

example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self):
2929
self.set_color(25, 58, 51)
3030

3131
# create input and output port.
32-
self.add_input('in port')
32+
self.add_input('in port', color=(200, 10, 0))
3333
self.add_output('out port')
3434

3535

@@ -97,7 +97,7 @@ def show_nodes_list(node):
9797
# create example "TextInputNode".
9898
checkbox_node = graph.create_node('com.chantasticvfx.CheckboxNode',
9999
name='checkbox node',
100-
pos=[-480, -60])
100+
pos=[-480, -20])
101101

102102
# create node with a combo box menu.
103103
menu_node = graph.create_node('com.chantasticvfx.DropdownMenuNode',

example_nodes/widget_nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ def __init__(self):
6666
self.add_checkbox('cb_world', '', 'World', False)
6767

6868
# create input and output port.
69-
self.add_input('in')
70-
self.add_output('out')
69+
self.add_input('in', color=(200, 100, 0))
70+
self.add_output('out', color=(0, 100, 200))

0 commit comments

Comments
 (0)