Skip to content

Commit c01914c

Browse files
committed
clean up refactor vertical node drawing.
1 parent 548d8a3 commit c01914c

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

NodeGraphQt/qgraphics/node_base.py

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,12 @@ def align_icon(self, h_offset=0.0, v_offset=0.0):
845845
v_offset (float): vertical offset.
846846
h_offset (float): horizontal offset.
847847
"""
848-
y = self._height / 2
849-
y -= self._icon_item.boundingRect().height()
850-
self._icon_item.setPos(self._width + h_offset, y + v_offset)
848+
center_y = self.boundingRect().center().y()
849+
icon_rect = self._icon_item.boundingRect()
850+
text_rect = self._text_item.boundingRect()
851+
x = self.boundingRect().right() + h_offset
852+
y = center_y - text_rect.height() - (icon_rect.height() / 2) + v_offset
853+
self._icon_item.setPos(x, y)
851854

852855
def align_label(self, h_offset=0.0, v_offset=0.0):
853856
"""
@@ -857,9 +860,10 @@ def align_label(self, h_offset=0.0, v_offset=0.0):
857860
v_offset (float): vertical offset.
858861
h_offset (float): horizontal offset.
859862
"""
860-
y = self._height / 2
861-
y -= self.text_item.boundingRect().height() / 2
862-
self.text_item.setPos(self._width + h_offset, y + v_offset)
863+
rect = self._text_item.boundingRect()
864+
x = self.boundingRect().right() + h_offset
865+
y = self.boundingRect().center().y() - (rect.height() / 2) + v_offset
866+
self.text_item.setPos(x, y)
863867

864868
def align_ports(self, v_offset=0.0):
865869
"""
@@ -891,6 +895,30 @@ def align_ports(self, v_offset=0.0):
891895
port.setPos(port_x-half_width, port_y)
892896
port_x += delta
893897

898+
def align_widgets(self, v_offset=0.0):
899+
"""
900+
Align node widgets to the default center of the node.
901+
902+
Args:
903+
v_offset (float): vertical offset.
904+
"""
905+
if not self._widgets:
906+
return
907+
rect = self.boundingRect()
908+
y = rect.center().y() + v_offset
909+
widget_height = 0.0
910+
for widget in self._widgets.values():
911+
widget_rect = widget.boundingRect()
912+
widget_height += widget_rect.height()
913+
y -= widget_height / 2
914+
915+
for widget in self._widgets.values():
916+
widget_rect = widget.boundingRect()
917+
x = rect.center().x() - (widget_rect.width() / 2)
918+
widget.widget().setTitleAlign('center')
919+
widget.setPos(x, y)
920+
y += widget_rect.height()
921+
894922
def draw_node(self):
895923
"""
896924
Re-draw the node item in the scene.
@@ -906,9 +934,9 @@ def draw_node(self):
906934
# (do all the graphic item layout offsets here)
907935

908936
# align label text
909-
self.align_label(h_offset=7, v_offset=6)
937+
self.align_label(h_offset=6)
910938
# align icon
911-
self.align_icon(h_offset=4, v_offset=-2)
939+
self.align_icon(v_offset=4)
912940
# arrange input and output ports.
913941
self.align_ports()
914942
# arrange node widgets
@@ -924,36 +952,30 @@ def calc_size(self, add_w=0.0, add_h=0.0):
924952
add_w (float): additional width.
925953
add_h (float): additional height.
926954
"""
927-
width = 0
928-
height = 0
929-
930-
if self._widgets:
931-
wid_width = max([
932-
w.boundingRect().width() for w in self._widgets.values()
933-
])
934-
width = max(width, wid_width)
955+
p_input_width = 0.0
956+
p_output_width = 0.0
957+
p_input_height = 0.0
958+
p_output_height = 0.0
959+
for port in self._input_items.keys():
960+
if port.isVisible():
961+
p_input_width += port.boundingRect().width()
962+
if not p_input_height:
963+
p_input_height = port.boundingRect().height()
964+
for port in self._output_items.keys():
965+
if port.isVisible():
966+
p_output_width += port.boundingRect().width()
967+
if not p_output_height:
968+
p_output_height = port.boundingRect().height()
935969

936-
port_width = 0.0
937-
if self._input_items:
938-
port = list(self._input_items.keys())[0]
939-
port_width = port.boundingRect().width()
940-
941-
if self._output_items:
942-
port = list(self._output_items.keys())[0]
943-
port_width = port.boundingRect().width()
944-
945-
in_count = len([p for p in self.inputs if p.isVisible()])
946-
out_count = len([p for p in self.outputs if p.isVisible()])
947-
width = max(width, port_width * max(in_count, out_count))
948-
if self._widgets:
949-
wid_height = 0.0
950-
for w in self._widgets.values():
951-
wid_height += w.boundingRect().height()
952-
wid_height += wid_height / len(self._widgets.values())
953-
height = wid_height
970+
widget_width = 0.0
971+
widget_height = 0.0
972+
for widget in self._widgets.values():
973+
if widget.boundingRect().width() > widget_width:
974+
widget_width = widget.boundingRect().width()
975+
widget_height += widget.boundingRect().height()
954976

955-
width += add_w
956-
height += add_h
977+
width = max([p_input_width, p_output_width, widget_width]) + add_w
978+
height = p_input_height + p_output_height + widget_height + add_h
957979
return width, height
958980

959981
def add_input(self, name='input', multi_port=False, display_name=True,

NodeGraphQt/widgets/node_widgets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
subcontrol-origin: margin;
2323
subcontrol-position: <$VALUE>;
2424
margin-left: <$VALUE>;
25-
margin-right: <$VALUE>;
25+
margin-right: <$VALUE>;
2626
color: rgba(255, 255, 255, 85);
2727
padding: 0px;
2828
}
@@ -37,6 +37,7 @@
3737
}
3838
'''
3939

40+
4041
class _NodeGroupBox(QtWidgets.QGroupBox):
4142

4243
def __init__(self, label, parent=None):

0 commit comments

Comments
 (0)