@@ -194,15 +194,17 @@ def _tooltip_disable(self, state):
194194
195195 def _set_base_size (self , add_w = 0.0 , add_h = 0.0 ):
196196 """
197- setup initial base size.
197+ Sets the initial base size for the node .
198198
199199 Args:
200- add_w (float): additional width.
201- add_h (float): additional height.
200+ add_w (float): add additional width.
201+ add_h (float): add additional height.
202202 """
203- width , height = self .calc_size (add_w , add_h )
204- self ._width = width if width > NODE_WIDTH else NODE_WIDTH
205- self ._height = height if height > NODE_HEIGHT else NODE_HEIGHT
203+ self ._width , self ._height = self .calc_size (add_w , add_h )
204+ if self ._width < NODE_WIDTH :
205+ self ._width = NODE_WIDTH
206+ if self ._height < NODE_HEIGHT :
207+ self ._height = NODE_HEIGHT
206208
207209 def _set_text_color (self , color ):
208210 """
@@ -256,52 +258,65 @@ def calc_size(self, add_w=0.0, add_h=0.0):
256258 Returns:
257259 tuple(float, float): width, height.
258260 """
259- width = self ._text_item .boundingRect ().width ()
260- height = self ._text_item .boundingRect ().height ()
261-
262- if self ._widgets :
263- wid_width = max ([
264- w .boundingRect ().width () for w in self ._widgets .values ()
265- ])
266- if width < wid_width :
267- width = wid_width
268-
269- port_height = 0.0
270- if self ._input_items :
271- port = None
272- input_widths = []
273- for port , text in self ._input_items .items ():
274- input_width = port .boundingRect ().width () - PORT_FALLOFF
275- if text .isVisible ():
276- input_width += text .boundingRect ().width () / 1.5
277- input_widths .append (input_width )
278- width += max (input_widths )
279- if port :
280- port_height = port .boundingRect ().height ()
281-
282- if self ._output_items :
283- port = None
284- output_widths = []
285- for port , text in self ._output_items .items ():
286- output_width = port .boundingRect ().width ()
287- if text .isVisible ():
288- output_width += text .boundingRect ().width () / 1.5
289- output_widths .append (output_width )
290- width += max (output_widths )
291- if port :
292- port_height = port .boundingRect ().height ()
293-
294- in_count = len ([p for p in self .inputs if p .isVisible ()])
295- out_count = len ([p for p in self .outputs if p .isVisible ()])
296- height += port_height * max ([in_count , out_count ])
297- if self ._widgets :
298- wid_height = 0.0
299- for w in self ._widgets .values ():
300- wid_height += w .boundingRect ().height ()
301- wid_height += wid_height / len (self ._widgets .values ())
302- if wid_height > height :
303- height = wid_height
261+ # width, height from node name text.
262+ text_w = self ._text_item .boundingRect ().width ()
263+ text_h = self ._text_item .boundingRect ().height ()
304264
265+ # width, height from node ports.
266+ port_width = 0.0
267+ p_input_text_width = 0.0
268+ p_output_text_width = 0.0
269+ p_input_height = 0.0
270+ p_output_height = 0.0
271+ for port , text in self ._input_items .items ():
272+ if not port .isVisible ():
273+ continue
274+ if not port_width :
275+ port_width = port .boundingRect ().width ()
276+ t_width = text .boundingRect ().width ()
277+ if text .isVisible () and t_width > p_input_text_width :
278+ p_input_text_width = text .boundingRect ().width ()
279+ p_input_height += port .boundingRect ().height ()
280+ for port , text in self ._output_items .items ():
281+ if not port .isVisible ():
282+ continue
283+ if not port_width :
284+ port_width = port .boundingRect ().width ()
285+ t_width = text .boundingRect ().width ()
286+ if text .isVisible () and t_width > p_output_text_width :
287+ p_output_text_width = text .boundingRect ().width ()
288+ p_output_height += port .boundingRect ().height ()
289+
290+ port_text_width = p_input_text_width + p_output_text_width
291+
292+ # width, height from node embedded widgets.
293+ widget_width = 0.0
294+ widget_height = 0.0
295+ for widget in self ._widgets .values ():
296+ w_width = widget .boundingRect ().width ()
297+ w_height = widget .boundingRect ().height ()
298+ if w_width > widget_width :
299+ widget_width = w_width
300+ widget_height += w_height
301+
302+ side_padding = 0.0
303+ if all ([widget_width , p_input_text_width , p_output_text_width ]):
304+ port_text_width = max ([p_input_text_width , p_output_text_width ])
305+ port_text_width *= 2
306+ elif widget_width :
307+ side_padding = 10
308+
309+ width = port_width + max ([text_w , port_text_width ]) + side_padding
310+ height = max ([text_h , p_input_height , p_output_height , widget_height ])
311+ if widget_width :
312+ # add additional width for node widget.
313+ width += widget_width
314+ if widget_height :
315+ # add bottom margin for node widget.
316+ height += 4.0
317+ height *= 1.05
318+
319+ # additional width, height.
305320 width += add_w
306321 height += add_h
307322 return width , height
@@ -311,12 +326,12 @@ def align_icon(self, h_offset=0.0, v_offset=0.0):
311326 Align node icon to the default top left of the node.
312327
313328 Args:
314- v_offset (float): vertical offset.
315- h_offset (float): horizontal offset.
329+ v_offset (float): additional vertical offset.
330+ h_offset (float): additional horizontal offset.
316331 """
317- x = 2.0 + h_offset
318- y = 1.0 + v_offset
319- self ._icon_item .setPos (x , y )
332+ top_left = self . boundingRect (). topLeft ()
333+ x , y = top_left . x (), top_left . y ()
334+ self ._icon_item .setPos (x + h_offset , y + v_offset )
320335
321336 def align_label (self , h_offset = 0.0 , v_offset = 0.0 ):
322337 """
@@ -329,8 +344,7 @@ def align_label(self, h_offset=0.0, v_offset=0.0):
329344 rect = self .boundingRect ()
330345 text_rect = self ._text_item .boundingRect ()
331346 x = rect .center ().x () - (text_rect .width () / 2 )
332- y = 0.0
333- self ._text_item .setPos (x + h_offset , y + v_offset )
347+ self ._text_item .setPos (x + h_offset , rect .y () + v_offset )
334348
335349 def align_widgets (self , v_offset = 0.0 ):
336350 """
@@ -341,16 +355,23 @@ def align_widgets(self, v_offset=0.0):
341355 """
342356 if not self ._widgets :
343357 return
344- wid_heights = sum (
345- [w .boundingRect ().height () for w in self ._widgets .values ()])
346- pos_y = self ._height / 2
347- pos_y -= wid_heights / 2
348- pos_y += v_offset
358+ rect = self .boundingRect ()
359+ y = rect .y () + v_offset
360+ inputs = [p for p in self .inputs if p .isVisible ()]
361+ outputs = [p for p in self .outputs if p .isVisible ()]
349362 for widget in self ._widgets .values ():
350- rect = widget .boundingRect ()
351- pos_x = (self ._width / 2 ) - (rect .width () / 2 )
352- widget .setPos (pos_x , pos_y )
353- pos_y += rect .height ()
363+ widget_rect = widget .boundingRect ()
364+ if not inputs :
365+ x = rect .left () + 10
366+ widget .widget ().setTitleAlign ('left' )
367+ elif not outputs :
368+ x = rect .right () - widget_rect .width () - 10
369+ widget .widget ().setTitleAlign ('right' )
370+ else :
371+ x = rect .center ().x () - (widget_rect .width () / 2 )
372+ widget .widget ().setTitleAlign ('center' )
373+ widget .setPos (x , y )
374+ y += widget_rect .height ()
354375
355376 def align_ports (self , v_offset = 0.0 ):
356377 """
@@ -401,10 +422,10 @@ def draw_node(self):
401422 Re-draw the node item in the scene.
402423 (re-implemented for vertical layout design)
403424 """
404- height = self ._text_item .boundingRect ().height ()
425+ height = self ._text_item .boundingRect ().height () + 4.0
405426
406427 # setup initial base size.
407- self ._set_base_size (add_w = 0.0 , add_h = height )
428+ self ._set_base_size (add_h = height )
408429 # set text color when node is initialized.
409430 self ._set_text_color (self .text_color )
410431 # set the tooltip
@@ -416,11 +437,11 @@ def draw_node(self):
416437 # align label text
417438 self .align_label ()
418439 # align icon
419- self .align_icon ()
440+ self .align_icon (h_offset = 2.0 , v_offset = 1.0 )
420441 # arrange input and output ports.
421- self .align_ports (v_offset = height + ( height / 2 ) )
442+ self .align_ports (v_offset = height )
422443 # arrange node widgets
423- self .align_widgets (v_offset = height / 2 )
444+ self .align_widgets (v_offset = height )
424445
425446 self .update ()
426447
0 commit comments