Skip to content

Commit eebe6dc

Browse files
committed
updated node ports #115
1 parent b9fc231 commit eebe6dc

File tree

5 files changed

+67
-41
lines changed

5 files changed

+67
-41
lines changed

NodeGraphQt/constants.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525
IN_PORT = 'in'
2626
#: Connection type for output ports.
2727
OUT_PORT = 'out'
28-
PORT_ACTIVE_COLOR = (29, 80, 84, 255)
29-
PORT_ACTIVE_BORDER_COLOR = (45, 215, 255, 255)
30-
PORT_HOVER_COLOR = (17, 96, 20, 255)
28+
29+
PORT_DEFAULT_SIZE = 22.0
30+
PORT_ACTIVE_COLOR = (14, 45, 59, 255)
31+
PORT_ACTIVE_BORDER_COLOR = (107, 166, 193, 255)
32+
PORT_HOVER_COLOR = (17, 43, 82, 255)
3133
PORT_HOVER_BORDER_COLOR = (136, 255, 35, 255)
34+
PORT_FALLOFF = 15.0
3235

3336
# === NODE ===
3437

35-
NODE_WIDTH = 100
38+
NODE_WIDTH = 170
3639
NODE_HEIGHT = 80
3740
NODE_ICON_SIZE = 24
3841
NODE_SEL_COLOR = (255, 255, 255, 30)

NodeGraphQt/qgraphics/node_base.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
NODE_WIDTH, NODE_HEIGHT,
66
NODE_ICON_SIZE, ICON_NODE_BASE,
77
NODE_SEL_COLOR, NODE_SEL_BORDER_COLOR,
8-
Z_VAL_NODE, Z_VAL_NODE_WIDGET)
8+
PORT_FALLOFF, Z_VAL_NODE, Z_VAL_NODE_WIDGET)
99
from NodeGraphQt.errors import NodeWidgetError
1010
from NodeGraphQt.qgraphics.node_abstract import AbstractNodeItem
1111
from NodeGraphQt.qgraphics.port import PortItem
@@ -194,7 +194,7 @@ def paint(self, painter, option, widget):
194194

195195
def mousePressEvent(self, event):
196196
if event.button() == QtCore.Qt.LeftButton:
197-
start = PortItem().boundingRect().width()
197+
start = PortItem().boundingRect().width() - PORT_FALLOFF
198198
end = self.boundingRect().width() - start
199199
x_pos = event.pos().x()
200200
if not start <= x_pos <= end:
@@ -300,17 +300,19 @@ def calc_size(self, add_w=0.0, add_h=0.0):
300300
height = self._text_item.boundingRect().height()
301301

302302
if self._widgets:
303-
width = max([
303+
wid_width = max([
304304
w.boundingRect().width() for w in self._widgets.values()
305305
])
306+
if width < wid_width:
307+
width = wid_width
306308

307309
port_height = 0.0
308310
if self._input_items:
309311
input_widths = []
310312
for port, text in self._input_items.items():
311-
input_width = port.boundingRect().width()
313+
input_width = port.boundingRect().width() - PORT_FALLOFF
312314
if text.isVisible():
313-
input_width += text.boundingRect().width()
315+
input_width += text.boundingRect().width() / 1.5
314316
input_widths.append(input_width)
315317
width += max(input_widths)
316318
port_height = port.boundingRect().height()
@@ -320,7 +322,7 @@ def calc_size(self, add_w=0.0, add_h=0.0):
320322
for port, text in self._output_items.items():
321323
output_width = port.boundingRect().width()
322324
if text.isVisible():
323-
output_width += text.boundingRect().width()
325+
output_width += text.boundingRect().width() / 1.5
324326
output_widths.append(output_width)
325327
width += max(output_widths)
326328
port_height = port.boundingRect().height()
@@ -395,6 +397,8 @@ def arrange_ports(self, v_offset=0.0):
395397
v_offset (float): port vertical offset.
396398
"""
397399
width = self._width
400+
txt_offset = PORT_FALLOFF - 2
401+
spacing = 1
398402

399403
# adjust input position
400404
inputs = [p for p in self.inputs if p.isVisible()]
@@ -405,11 +409,12 @@ def arrange_ports(self, v_offset=0.0):
405409
port_y = v_offset
406410
for port in inputs:
407411
port.setPos(port_x, port_y)
408-
port_y += port_height
412+
port_y += port_height + spacing
409413
# adjust input text position
410414
for port, text in self._input_items.items():
411415
if port.isVisible():
412-
text.setPos(port.boundingRect().width() / 2, port.y() - 1.5)
416+
txt_x = port.boundingRect().width() / 2 - txt_offset
417+
text.setPos(txt_x, port.y() - 1.5)
413418

414419
# adjust output position
415420
outputs = [p for p in self.outputs if p.isVisible()]
@@ -420,14 +425,13 @@ def arrange_ports(self, v_offset=0.0):
420425
port_y = v_offset
421426
for port in outputs:
422427
port.setPos(port_x, port_y)
423-
port_y += port_height
428+
port_y += port_height + spacing
424429
# adjust output text position
425430
for port, text in self._output_items.items():
426-
if not port.isVisible():
427-
continue
428-
txt_width = text.boundingRect().width()
429-
txt_x = port.x() - txt_width
430-
text.setPos(txt_x, port.y() - 1.5)
431+
if port.isVisible():
432+
txt_width = text.boundingRect().width() - txt_offset
433+
txt_x = port.x() - txt_width
434+
text.setPos(txt_x, port.y() - 1.5)
431435

432436
def offset_label(self, x=0.0, y=0.0):
433437
"""
@@ -441,14 +445,9 @@ def offset_label(self, x=0.0, y=0.0):
441445
icon_y = self._text_item.pos().y() + y
442446
self._text_item.setPos(icon_x, icon_y)
443447

444-
def post_init(self, viewer=None, pos=None):
448+
def draw_node(self):
445449
"""
446-
Called after node has been added into the scene.
447-
Adjust the node layout and form after the node has been added.
448-
449-
Args:
450-
viewer (NodeGraphQt.widgets.viewer.NodeViewer): not used
451-
pos (tuple): cursor position.
450+
Draw the node item in the scene.
452451
"""
453452
height = self._text_item.boundingRect().height()
454453

@@ -470,6 +469,17 @@ def post_init(self, viewer=None, pos=None):
470469
# arrange node widgets
471470
self.arrange_widgets(v_offset=height / 2)
472471

472+
def post_init(self, viewer=None, pos=None):
473+
"""
474+
Called after node has been added into the scene.
475+
Adjust the node layout and form after the node has been added.
476+
477+
Args:
478+
viewer (NodeGraphQt.widgets.viewer.NodeViewer): not used
479+
pos (tuple): cursor position.
480+
"""
481+
self.draw_node()
482+
473483
# set initial node position.
474484
if pos:
475485
self.xy_pos = pos
@@ -522,7 +532,7 @@ def name(self, name=''):
522532
AbstractNodeItem.name.fset(self, name)
523533
self._text_item.setPlainText(name)
524534
if self.scene():
525-
self.post_init()
535+
self.draw_node()
526536

527537
@AbstractNodeItem.color.setter
528538
def color(self, color=(100, 100, 100, 255)):

NodeGraphQt/qgraphics/pipe.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
PIPE_STYLES = {
1414
PIPE_STYLE_DEFAULT: QtCore.Qt.SolidLine,
15-
PIPE_STYLE_DASHED: QtCore.Qt.DashDotDotLine,
15+
PIPE_STYLE_DASHED: QtCore.Qt.DashLine,
1616
PIPE_STYLE_DOTTED: QtCore.Qt.DotLine
1717
}
1818

@@ -142,17 +142,15 @@ def draw_path(self, start_port, end_port, cursor_pos=None):
142142
"""
143143
if not start_port:
144144
return
145-
offset = (start_port.boundingRect().width() / 2)
146145
pos1 = start_port.scenePos()
147-
pos1.setX(pos1.x() + offset)
148-
pos1.setY(pos1.y() + offset)
146+
pos1.setX(pos1.x() + (start_port.boundingRect().width() / 2))
147+
pos1.setY(pos1.y() + (start_port.boundingRect().height() / 2))
149148
if cursor_pos:
150149
pos2 = cursor_pos
151150
elif end_port:
152-
offset = start_port.boundingRect().width() / 2
153151
pos2 = end_port.scenePos()
154-
pos2.setX(pos2.x() + offset)
155-
pos2.setY(pos2.y() + offset)
152+
pos2.setX(pos2.x() + (start_port.boundingRect().width() / 2))
153+
pos2.setY(pos2.y() + (start_port.boundingRect().height() / 2))
156154
else:
157155
return
158156

NodeGraphQt/qgraphics/port.py

Lines changed: 15 additions & 7 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_SIZE,
7+
PORT_FALLOFF,
68
PORT_HOVER_COLOR,
79
PORT_HOVER_BORDER_COLOR,
810
PORT_ACTIVE_COLOR,
@@ -22,8 +24,8 @@ def __init__(self, parent=None):
2224
self.setFlag(self.ItemSendsScenePositionChanges, True)
2325
self.setZValue(Z_VAL_PORT)
2426
self._pipes = []
25-
self._width = 18.5
26-
self._height = 18.5
27+
self._width = PORT_DEFAULT_SIZE
28+
self._height = PORT_DEFAULT_SIZE
2729
self._hovered = False
2830
self._name = 'port'
2931
self._display_name = True
@@ -40,7 +42,7 @@ def __repr__(self):
4042
return '{}.PortItem("{}")'.format(self.__module__, self.name)
4143

4244
def boundingRect(self):
43-
return QtCore.QRectF(0.0, 0.0, self._width, self._height)
45+
return QtCore.QRectF(0.0, 0.0, self._width + PORT_FALLOFF, self._height)
4446

4547
def paint(self, painter, option, widget):
4648
"""
@@ -54,10 +56,16 @@ def paint(self, painter, option, widget):
5456
"""
5557
painter.save()
5658

57-
rect_w = self._width / 1.5
58-
rect_h = self._height / 1.5
59-
rect_x = self._width - (rect_w * 1.25)
60-
rect_y = self._height - (rect_h * 1.25)
59+
### display the falloff colision ###
60+
# pen = QtGui.QPen(QtGui.QColor(255, 255, 255, 80), 0.8)
61+
# pen.setStyle(QtCore.Qt.DotLine)
62+
# painter.setPen(pen)
63+
# painter.drawRect(self.boundingRect())
64+
65+
rect_w = self._width / 1.8
66+
rect_h = self._height / 1.8
67+
rect_x = self.boundingRect().center().x() - (rect_w / 2)
68+
rect_y = self.boundingRect().center().y() - (rect_h / 2)
6169
port_rect = QtCore.QRectF(rect_x, rect_y, rect_w, rect_h)
6270

6371
if self._hovered:

NodeGraphQt/widgets/viewer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
PIPE_LAYOUT_CURVED,
88
PIPE_LAYOUT_STRAIGHT,
99
PIPE_STYLE_DASHED,
10-
SCENE_AREA)
10+
SCENE_AREA,
11+
Z_VAL_NODE_WIDGET)
1112
from NodeGraphQt.qgraphics.node_abstract import AbstractNodeItem
1213
from NodeGraphQt.qgraphics.node_backdrop import BackdropNodeItem
1314
from NodeGraphQt.qgraphics.pipe import Pipe
@@ -325,8 +326,10 @@ def sceneMouseMoveEvent(self, event):
325326
pos = event.scenePos()
326327
items = self.scene().items(pos)
327328
if items and isinstance(items[0], PortItem):
329+
x = items[0].boundingRect().width() / 2
328330
y = items[0].boundingRect().height() / 2
329331
pos = items[0].scenePos()
332+
pos.setX(pos.x() + x)
330333
pos.setY(pos.y() + y)
331334

332335
self._live_pipe.draw_path(self._start_port, None, pos)
@@ -381,6 +384,7 @@ def sceneMousePressEvent(self, event):
381384
pipe = pipe_items[0]
382385
attr = {IN_PORT: 'output_port', OUT_PORT: 'input_port'}
383386
from_port = pipe.port_from_pos(pos, True)
387+
from_port._hovered = True
384388

385389
self._detached_port = getattr(pipe, attr[from_port.port_type])
386390
self.start_live_connection(from_port)
@@ -399,6 +403,8 @@ def sceneMouseReleaseEvent(self, event):
399403
if not self._live_pipe:
400404
return
401405

406+
self._start_port._hovered = False
407+
402408
# find the end port.
403409
end_port = None
404410
for item in self.scene().items(event.scenePos()):
@@ -475,6 +481,7 @@ def start_live_connection(self, selected_port):
475481
return
476482
self._start_port = selected_port
477483
self._live_pipe = Pipe()
484+
self._live_pipe.setZValue(Z_VAL_NODE_WIDGET)
478485
self._live_pipe.activate()
479486
self._live_pipe.style = PIPE_STYLE_DASHED
480487
if self._start_port.type == IN_PORT:

0 commit comments

Comments
 (0)