@@ -123,40 +123,95 @@ def _align_ports_horizontal(self, v_offset):
123123 text .setPos (txt_x , port .y () - 1.5 )
124124
125125 def _align_ports_vertical (self , v_offset ):
126+ height = self ._height
127+ node_center_x = self .boundingRect ().center ().x () + v_offset
128+
126129 # adjust input position
127130 inputs = [p for p in self .inputs if p .isVisible ()]
128131 if inputs :
129132 port_width = inputs [0 ].boundingRect ().width ()
130133 port_height = inputs [0 ].boundingRect ().height ()
131- half_width = port_width / 2
132- delta = self ._width / (len (inputs ) + 1 )
133- port_x = delta
134- port_y = (port_height / 2 ) * - 1
135- for port in inputs :
136- port .setPos (port_x - half_width , port_y )
137- port_x += delta
134+
135+ count = len (inputs )
136+ if count > 2 :
137+ is_odd = bool (count % 2 )
138+ middle_idx = int (count / 2 )
139+
140+ delta = (self ._width / (len (inputs ) + 1 )) / 2
141+
142+ # left half
143+ port_x = node_center_x - (port_width / 2 )
144+ port_y = (port_height / 2 ) * - 1
145+ for idx , port in enumerate (reversed (inputs [:middle_idx ])):
146+ if idx == 0 :
147+ if is_odd :
148+ port_x -= (port_width / 2 ) + delta
149+ port_y += (port_height / 2 )
150+ else :
151+ port_x -= delta
152+ port .setPos (port_x , port_y )
153+ port_x -= (port_width / 2 ) + delta
154+ port_y += (port_height / 2 )
155+
156+ # right half
157+ port_x = node_center_x - (port_width / 2 )
158+ port_y = (port_height / 2 ) * - 1
159+ for idx , port in enumerate (inputs [middle_idx :]):
160+ if idx == 0 :
161+ if not is_odd :
162+ port_x += delta
163+ port .setPos (port_x , port_y )
164+ port_x += (port_width / 2 ) + delta
165+ port_y += (port_height / 2 )
138166
139167 # adjust output position
140168 outputs = [p for p in self .outputs if p .isVisible ()]
141169 if outputs :
142170 port_width = outputs [0 ].boundingRect ().width ()
143171 port_height = outputs [0 ].boundingRect ().height ()
144- half_width = port_width / 2
145- delta = self ._width / (len (outputs ) + 1 )
146- port_x = delta
147- port_y = self ._height - (port_height / 2 )
148- for port in outputs :
149- port .setPos (port_x - half_width , port_y )
150- port_x += delta
172+
173+ count = len (outputs )
174+ if count > 2 :
175+ is_odd = bool (count % 2 )
176+ middle_idx = int (count / 2 )
177+
178+ delta = (self ._width / (len (outputs ) + 1 )) / 2
179+
180+ # left half
181+ port_x = node_center_x - (port_width / 2 )
182+ port_y = height - (port_height / 2 )
183+ for idx , port in enumerate (reversed (outputs [:middle_idx ])):
184+ if idx == 0 :
185+ if is_odd :
186+ port_x -= (port_width / 2 ) + delta
187+ port_y -= (port_height / 2 )
188+ else :
189+ port_x -= delta
190+ port .setPos (port_x , port_y )
191+ port_x -= (port_width / 2 ) + delta
192+ port_y -= (port_height / 2 )
193+
194+ # right half
195+ port_x = node_center_x - (port_width / 2 )
196+ port_y = height - (port_height / 2 )
197+ for idx , port in enumerate (outputs [middle_idx :]):
198+ if idx == 0 :
199+ if not is_odd :
200+ port_x += delta
201+ port .setPos (port_x , port_y )
202+ port_x += (port_width / 2 ) + delta
203+ port_y -= (port_height / 2 )
151204
152205 def _paint_horizontal (self , painter , option , widget ):
153206 painter .save ()
154207
155- # debugging show click area.
156- # painter.setPen(QtCore.Qt.NoPen)
157- # painter.setBrush(QtGui.QColor(255, 255, 255, 80))
208+ # display node area for debugging
209+ # ----------------------------------------------------------------------
210+ # pen = QtGui.QPen(QtGui.QColor(255, 255, 255, 80), 0.8)
211+ # pen.setStyle(QtCore.Qt.DotLine)
212+ # painter.setPen(pen)
158213 # painter.drawRect(self.boundingRect())
159-
214+ # ----------------------------------------------------------------------
160215
161216 text_rect = self ._text_item .boundingRect ()
162217 text_width = text_rect .width ()
@@ -263,10 +318,13 @@ def _paint_horizontal(self, painter, option, widget):
263318 def _paint_vertical (self , painter , option , widget ):
264319 painter .save ()
265320
266- # debugging show click area.
267- # painter.setPen(QtCore.Qt.NoPen)
268- # painter.setBrush(QtGui.QColor(255, 255, 255, 80))
321+ # display node area for debugging
322+ # ----------------------------------------------------------------------
323+ # pen = QtGui.QPen(QtGui.QColor(255, 255, 255, 80), 0.8)
324+ # pen.setStyle(QtCore.Qt.DotLine)
325+ # painter.setPen(pen)
269326 # painter.drawRect(self.boundingRect())
327+ # ----------------------------------------------------------------------
270328
271329 rect = self .boundingRect ()
272330 width = min (rect .width (), rect .height ()) / 1.8
@@ -395,19 +453,30 @@ def _align_label_horizontal(self, h_offset, v_offset):
395453 def _align_label_vertical (self , h_offset , v_offset ):
396454 rect = self .boundingRect ()
397455 text_rect = self ._text_item .boundingRect ()
398- x = rect .right () - (rect .width () / 3 )
456+ x = rect .right () - (rect .width () / 4 )
399457 y = rect .center ().y () - (text_rect .height () / 2 )
400458 self ._text_item .setPos (x + h_offset , y + v_offset )
401459
402460 def _draw_node_horizontal (self ):
403- add_width = self .text_item .boundingRect ().width () * 1.5
404- add_height = self .text_item .boundingRect ().width () / 2
405-
406461 # update port text items in visibility.
462+ text_width = 0
463+ port_widths = 0
407464 for port , text in self ._input_items .items ():
408465 text .setVisible (port .display_name )
466+ if port .display_name :
467+ if text .boundingRect ().width () > text_width :
468+ text_width = text .boundingRect ().width ()
469+ port_widths += port .boundingRect ().width () / len (self ._input_items )
470+
409471 for port , text in self ._output_items .items ():
410472 text .setVisible (port .display_name )
473+ if port .display_name :
474+ if text .boundingRect ().width () > text_width :
475+ text_width = text .boundingRect ().width ()
476+ port_widths += port .boundingRect ().width () / len (self ._output_items )
477+
478+ add_width = (text_width * 2 ) + port_widths
479+ add_height = self .text_item .boundingRect ().width () / 2
411480
412481 # setup initial base size.
413482 self ._set_base_size (add_w = add_width , add_h = add_height )
@@ -424,22 +493,28 @@ def _draw_node_horizontal(self):
424493 # arrange icon
425494 self .align_icon ()
426495 # arrange input and output ports.
427- # self.align_ports(v_offset=add_height / 2)
428496 self .align_ports ()
429497 # arrange node widgets
430498 self .align_widgets (v_offset = 0.0 )
431499
432500 self .update ()
433501
434502 def _draw_node_vertical (self ):
503+ add_height = 0
504+
435505 # hide the port text items in vertical layout.
436506 for port , text in self ._input_items .items ():
437507 text .setVisible (False )
508+ add_height += port .boundingRect ().height () / 2
438509 for port , text in self ._output_items .items ():
439510 text .setVisible (False )
511+ add_height += port .boundingRect ().height () / 2
512+
513+ if add_height < 50 :
514+ add_height = 50
440515
441516 # setup initial base size.
442- self ._set_base_size (add_w = 50 , add_h = 50 )
517+ self ._set_base_size (add_w = 50 , add_h = add_height )
443518 # set text color when node is initialized.
444519 self ._set_text_color (self .text_color )
445520 # set the tooltip
0 commit comments