Skip to content

Commit 81133c2

Browse files
committed
optimise clean up
1 parent 534bb9c commit 81133c2

File tree

2 files changed

+83
-99
lines changed

2 files changed

+83
-99
lines changed

NodeGraphQt/base/utils.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ def _open_session(graph):
148148
graph (NodeGraphQt.NodeGraph): node graph.
149149
"""
150150
current = graph.current_session()
151-
viewer = graph.viewer()
152-
file_path = viewer.load_dialog(current)
151+
file_path = graph.load_dialog(current)
153152
if file_path:
154153
graph.load_session(file_path)
155154

@@ -162,8 +161,7 @@ def _import_session(graph):
162161
graph (NodeGraphQt.NodeGraph): node graph.
163162
"""
164163
current = graph.current_session()
165-
viewer = graph.viewer()
166-
file_path = viewer.load_dialog(current)
164+
file_path = graph.load_dialog(current)
167165
if file_path:
168166
graph.import_session(file_path)
169167

@@ -193,8 +191,7 @@ def _save_session_as(graph):
193191
graph (NodeGraphQt.NodeGraph): node graph.
194192
"""
195193
current = graph.current_session()
196-
viewer = graph.viewer()
197-
file_path = viewer.save_dialog(current)
194+
file_path = graph.save_dialog(current)
198195
if file_path:
199196
graph.save_session(file_path)
200197

@@ -206,8 +203,7 @@ def _new_session(graph):
206203
Args:
207204
graph (NodeGraphQt.NodeGraph): node graph.
208205
"""
209-
viewer = graph.viewer()
210-
if viewer.question_dialog('Clear Current Session?', 'Clear Session'):
206+
if graph.question_dialog('Clear Current Session?', 'Clear Session'):
211207
graph.clear_session()
212208

213209

NodeGraphQt/qgraphics/node_base.py

Lines changed: 79 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,46 @@ def paint(self, painter, option, widget):
5555
self.auto_switch_mode()
5656

5757
painter.save()
58-
bg_border = 1.0
59-
rect = QtCore.QRectF(0.5 - (bg_border / 2),
60-
0.5 - (bg_border / 2),
61-
self._width + bg_border,
62-
self._height + bg_border)
63-
radius = 2
64-
border_color = QtGui.QColor(*self.border_color)
65-
66-
path = QtGui.QPainterPath()
67-
path.addRoundedRect(rect, radius, radius)
58+
painter.setPen(QtCore.Qt.NoPen)
59+
painter.setBrush(QtCore.Qt.NoBrush)
6860

61+
# base background.
62+
margin = 1.0
6963
rect = self.boundingRect()
64+
rect = QtCore.QRectF(rect.left() + margin,
65+
rect.top() + margin,
66+
rect.width() - (margin * 2),
67+
rect.height() - (margin * 2))
7068

71-
bg_color = QtGui.QColor(*self.color)
72-
painter.setBrush(bg_color)
73-
painter.setPen(QtCore.Qt.NoPen)
69+
radius = 4.0
70+
painter.setBrush(QtGui.QColor(*self.color))
7471
painter.drawRoundedRect(rect, radius, radius)
7572

76-
if self.selected and NODE_SEL_COLOR:
73+
# light overlay on background when selected.
74+
if self.selected:
7775
painter.setBrush(QtGui.QColor(*NODE_SEL_COLOR))
7876
painter.drawRoundedRect(rect, radius, radius)
7977

80-
label_rect = QtCore.QRectF(rect.left(), rect.top(), self._width, 28)
81-
path = QtGui.QPainterPath()
82-
path.addRoundedRect(label_rect, radius, radius)
83-
painter.setBrush(QtGui.QColor(30, 30, 30, 200))
84-
painter.fillPath(path, painter.brush())
78+
# node name background.
79+
padding = 3.0, 2.0
80+
text_rect = self._text_item.boundingRect()
81+
text_rect = QtCore.QRectF(text_rect.x() + padding[0],
82+
rect.y() + padding[1],
83+
rect.width() - padding[0] - margin,
84+
text_rect.height() - (padding[1] * 2))
85+
painter.setBrush(QtGui.QColor(0, 0, 0, 50))
86+
if self.selected:
87+
painter.setBrush(QtGui.QColor(*NODE_SEL_COLOR))
88+
painter.drawRoundedRect(text_rect, 3.0, 3.0)
8589

90+
# node border
8691
border_width = 0.8
87-
if self.selected and NODE_SEL_BORDER_COLOR:
92+
border_color = QtGui.QColor(*self.border_color)
93+
if self.selected:
8894
border_width = 1.2
8995
border_color = QtGui.QColor(*NODE_SEL_BORDER_COLOR)
90-
border_rect = QtCore.QRectF(rect.left() - (border_width / 2),
91-
rect.top() - (border_width / 2),
92-
rect.width() + border_width,
93-
rect.height() + border_width)
96+
border_rect = QtCore.QRectF(rect.left(), rect.top(),
97+
rect.width(), rect.height())
9498

9599
pen = QtGui.QPen(border_color, border_width)
96100
pen.setCosmetic(self.viewer().get_zoom() < 0.0)
@@ -193,13 +197,9 @@ def _set_base_size(self, add_w=0.0, add_h=0.0):
193197
add_w (float): additional width.
194198
add_h (float): additional height.
195199
"""
196-
self._width = NODE_WIDTH
197-
self._height = NODE_HEIGHT
198200
width, height = self.calc_size(add_w, add_h)
199-
if width > self._width:
200-
self._width = width
201-
if height > self._height:
202-
self._height = height
201+
self._width = width if width > NODE_WIDTH else NODE_WIDTH
202+
self._height = height if height > NODE_HEIGHT else NODE_HEIGHT
203203

204204
def _set_text_color(self, color):
205205
"""
@@ -253,7 +253,7 @@ def calc_size(self, add_w=0.0, add_h=0.0):
253253
Returns:
254254
tuple(float, float): width, height.
255255
"""
256-
width = 0
256+
width = self._text_item.boundingRect().width()
257257
height = self._text_item.boundingRect().height()
258258

259259
if self._widgets:
@@ -301,7 +301,6 @@ def calc_size(self, add_w=0.0, add_h=0.0):
301301

302302
width += add_w
303303
height += add_h
304-
305304
return width, height
306305

307306
def align_icon(self, h_offset=0.0, v_offset=0.0):
@@ -313,7 +312,7 @@ def align_icon(self, h_offset=0.0, v_offset=0.0):
313312
h_offset (float): horizontal offset.
314313
"""
315314
x = 2.0 + h_offset
316-
y = 2.0 + v_offset
315+
y = 1.0 + v_offset
317316
self._icon_item.setPos(x, y)
318317

319318
def align_label(self, h_offset=0.0, v_offset=0.0):
@@ -324,12 +323,11 @@ def align_label(self, h_offset=0.0, v_offset=0.0):
324323
v_offset (float): vertical offset.
325324
h_offset (float): horizontal offset.
326325
"""
326+
rect = self.boundingRect()
327327
text_rect = self._text_item.boundingRect()
328-
text_x = (self._width / 2) - (text_rect.width() / 2)
329-
text_y = 2.0
330-
text_x += h_offset
331-
text_y += v_offset
332-
self._text_item.setPos(text_x, text_y)
328+
x = rect.center().x() - (text_rect.width() / 2)
329+
y = 0.0
330+
self._text_item.setPos(x + h_offset, y + v_offset)
333331

334332
def align_widgets(self, v_offset=0.0):
335333
"""
@@ -425,9 +423,9 @@ def draw_node(self):
425423
# (do all the graphic item layout offsets here)
426424

427425
# align label text
428-
self.align_label(h_offset=0.0, v_offset=0.0)
426+
self.align_label()
429427
# arrange icon
430-
self.align_icon(h_offset=0.0, v_offset=0.0)
428+
self.align_icon()
431429
# arrange input and output ports.
432430
self.align_ports(v_offset=height + (height / 2))
433431
# arrange node widgets
@@ -776,47 +774,45 @@ def paint(self, painter, option, widget):
776774
self.auto_switch_mode()
777775

778776
painter.save()
779-
bg_border = 1.0
780-
rect = QtCore.QRectF(0.5 - (bg_border / 2),
781-
0.5 - (bg_border / 2),
782-
self._width + bg_border,
783-
self._height + bg_border)
784-
radius = 2
785-
border_color = QtGui.QColor(*self.border_color)
786-
787-
path = QtGui.QPainterPath()
788-
path.addRoundedRect(rect, radius, radius)
777+
painter.setPen(QtCore.Qt.NoPen)
778+
painter.setBrush(QtCore.Qt.NoBrush)
789779

780+
# base background.
781+
margin = 1.0
790782
rect = self.boundingRect()
783+
rect = QtCore.QRectF(rect.left() + margin,
784+
rect.top() + margin,
785+
rect.width() - (margin * 2),
786+
rect.height() - (margin * 2))
791787

792-
bg_color = QtGui.QColor(*self.color)
793-
painter.setBrush(bg_color)
794-
painter.setPen(QtCore.Qt.NoPen)
788+
radius = 4.0
789+
painter.setBrush(QtGui.QColor(*self.color))
795790
painter.drawRoundedRect(rect, radius, radius)
796791

797-
if self.selected and NODE_SEL_COLOR:
792+
# light overlay on background when selected.
793+
if self.selected:
798794
painter.setBrush(QtGui.QColor(*NODE_SEL_COLOR))
799795
painter.drawRoundedRect(rect, radius, radius)
800796

801-
label_rect = QtCore.QRectF(rect.left(), rect.top(), self._width, 15)
802-
path = QtGui.QPainterPath()
803-
path.addRoundedRect(label_rect, radius, radius)
804-
painter.setBrush(QtGui.QColor(30, 30, 30, 200))
805-
painter.fillPath(path, painter.brush())
806-
807-
label_rect = QtCore.QRectF(rect.left(), rect.bottom()-15, self._width, 15)
808-
path = QtGui.QPainterPath()
809-
path.addRoundedRect(label_rect, radius, radius)
810-
painter.fillPath(path, painter.brush())
797+
# top & bottom edge background.
798+
padding = 2.0
799+
height = 10
800+
painter.setBrush(QtGui.QColor(0, 0, 0, 50))
801+
if self.selected:
802+
painter.setBrush(QtGui.QColor(*NODE_SEL_COLOR))
803+
for y in [rect.y() + padding, rect.height() - height - 1]:
804+
top_rect = QtCore.QRectF(rect.x() + padding, y,
805+
rect.width() - (padding * 2), height)
806+
painter.drawRoundedRect(top_rect, 3.0, 3.0)
811807

808+
# node border
812809
border_width = 0.8
813-
if self.selected and NODE_SEL_BORDER_COLOR:
810+
border_color = QtGui.QColor(*self.border_color)
811+
if self.selected:
814812
border_width = 1.2
815813
border_color = QtGui.QColor(*NODE_SEL_BORDER_COLOR)
816-
border_rect = QtCore.QRectF(rect.left() - (border_width / 2),
817-
rect.top() - (border_width / 2),
818-
rect.width() + border_width,
819-
rect.height() + border_width)
814+
border_rect = QtCore.QRectF(rect.left(), rect.top(),
815+
rect.width(), rect.height())
820816

821817
pen = QtGui.QPen(border_color, border_width)
822818
pen.setCosmetic(self.viewer().get_zoom() < 0.0)
@@ -830,31 +826,27 @@ def paint(self, painter, option, widget):
830826

831827
def align_icon(self, h_offset=0.0, v_offset=0.0):
832828
"""
833-
Align node icon to the default top left of the node.
829+
Align node icon to the right side of the node.
834830
835831
Args:
836832
v_offset (float): vertical offset.
837833
h_offset (float): horizontal offset.
838834
"""
839-
# icon_rect = self._icon_item.boundingRect()
840-
# x = self._width / 2 - (icon_rect.width() / 2) + h_offset
841-
# y = self._height / 2 - (icon_rect.height() / 2) + v_offset
842-
x = 2.0 + h_offset
843-
y = 17.0 + v_offset
844-
self._icon_item.setPos(x, y)
835+
y = self._height / 2
836+
y -= self._icon_item.boundingRect().height()
837+
self._icon_item.setPos(self._width + h_offset, y + v_offset)
845838

846839
def align_label(self, h_offset=0.0, v_offset=0.0):
847840
"""
848-
Align node label to the default top center of the node.
841+
Align node label to the right side of the node.
849842
850843
Args:
851844
v_offset (float): vertical offset.
852845
h_offset (float): horizontal offset.
853846
"""
854-
text_rect = self.text_item.boundingRect()
855-
text_x = self._width + 10 + h_offset
856-
text_y = self._height / 2 - (text_rect.height() / 2)
857-
self.text_item.setPos(text_x, text_y)
847+
y = self._height / 2
848+
y -= self.text_item.boundingRect().height() / 2
849+
self.text_item.setPos(self._width + h_offset, y + v_offset)
858850

859851
def align_ports(self, v_offset=0.0):
860852
"""
@@ -901,13 +893,13 @@ def draw_node(self):
901893
# (do all the graphic item layout offsets here)
902894

903895
# arrange label text
904-
self.align_label(h_offset=0.0, v_offset=0.0)
896+
self.align_label(h_offset=8)
905897
# arrange icon
906-
self.align_icon(h_offset=0.0, v_offset=0.0)
898+
self.align_icon(h_offset=6, v_offset=-4)
907899
# arrange input and output ports.
908900
self.align_ports()
909901
# arrange node widgets
910-
self.align_widgets(v_offset=0.0)
902+
self.align_widgets()
911903

912904
self.update()
913905

@@ -931,12 +923,10 @@ def calc_size(self, add_w=0.0, add_h=0.0):
931923
port_width = 0.0
932924
if self._input_items:
933925
port = list(self._input_items.keys())[0]
934-
height += port.boundingRect().height()
935926
port_width = port.boundingRect().width()
936927

937928
if self._output_items:
938929
port = list(self._output_items.keys())[0]
939-
height += port.boundingRect().height()
940930
port_width = port.boundingRect().width()
941931

942932
in_count = len([p for p in self.inputs if p.isVisible()])
@@ -947,12 +937,10 @@ def calc_size(self, add_w=0.0, add_h=0.0):
947937
for w in self._widgets.values():
948938
wid_height += w.boundingRect().height()
949939
wid_height += wid_height / len(self._widgets.values())
950-
if wid_height > height:
951-
height = wid_height
940+
height = wid_height
952941

953942
width += add_w
954-
height += add_h + 15
955-
943+
height += add_h
956944
return width, height
957945

958946
def add_input(self, name='input', multi_port=False, display_name=True,
@@ -975,7 +963,7 @@ def add_input(self, name='input', multi_port=False, display_name=True,
975963
name, multi_port, False, locked, painter_func)
976964

977965
def add_output(self, name='output', multi_port=False, display_name=True,
978-
locked=False, painter_func=None):
966+
locked=False, painter_func=None):
979967
"""
980968
Adds a port qgraphics item into the node with the "port_type" set as
981969
OUT_PORT

0 commit comments

Comments
 (0)