Skip to content

Commit 408cd7e

Browse files
committed
updated docs & BaseNodeCircle updates.
1 parent 2b1e294 commit 408cd7e

File tree

6 files changed

+72
-44
lines changed

6 files changed

+72
-44
lines changed

NodeGraphQt/nodes/base_node_circle.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@
44

55

66
class BaseNodeCircle(BaseNode):
7+
"""
8+
`Implemented in` ``v0.5.2``
9+
10+
The ``NodeGraphQt.BaseNodeCircle`` is pretty much the same class as the
11+
:class:`NodeGraphQt.BaseNode` except with a different design.
12+
13+
**Inherited from:** :class:`NodeGraphQt.BaseNode`
14+
15+
.. image:: ../_images/node_circle.png
16+
:width: 250px
17+
18+
example snippet:
19+
20+
.. code-block:: python
21+
:linenos:
22+
23+
from NodeGraphQt import BaseNodeCircle
24+
25+
class ExampleNode(BaseNodeCircle):
26+
27+
# unique node identifier domain.
28+
__identifier__ = 'io.jchanvfx.github'
29+
30+
# initial default node name.
31+
NODE_NAME = 'My Node'
32+
33+
def __init__(self):
34+
super(ExampleNode, self).__init__()
35+
36+
# create an input port.
37+
self.add_input('in')
38+
39+
# create an output port.
40+
self.add_output('out')
41+
"""
742

843
NODE_NAME = 'Circle Node'
944

NodeGraphQt/qgraphics/node_circle.py

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
from Qt import QtCore, QtGui, QtWidgets
33

4-
from NodeGraphQt.constants import NodeEnum, PortEnum
4+
from NodeGraphQt.constants import NodeEnum
55
from NodeGraphQt.qgraphics.node_base import NodeItem
66

77

@@ -21,8 +21,12 @@ def _paint_horizontal(self, painter, option, widget):
2121
painter.save()
2222

2323
text_rect = self._text_item.boundingRect()
24+
text_width = text_rect.width()
25+
if text_width < 20.0:
26+
text_width = 20.0
27+
2428
text_rect = QtCore.QRectF(
25-
self.boundingRect().center().x() - (text_rect.width() / 2),
29+
self.boundingRect().center().x() - (text_width / 2),
2630
self.boundingRect().center().y() - (text_rect.height() / 2),
2731
text_rect.width(),
2832
text_rect.height()
@@ -49,7 +53,7 @@ def _paint_horizontal(self, painter, option, widget):
4953
if p_text.isVisible():
5054
pt_width = p_text.boundingRect().width() * 1.2
5155
else:
52-
pt_width = p.boundingRect().width() / 2
56+
pt_width = p.boundingRect().width() / 4
5357
pt1 = QtCore.QPointF(
5458
p.pos().x() + (p.boundingRect().width() / 2) + pt_width,
5559
p.pos().y() + (p.boundingRect().height() / 2)
@@ -66,14 +70,14 @@ def _paint_horizontal(self, painter, option, widget):
6670
if p_text.isVisible():
6771
pt_width = p_text.boundingRect().width() * 1.2
6872
else:
69-
pt_width = p.boundingRect().width() / 2
73+
pt_width = p.boundingRect().width() / 4
7074
pt1 = QtCore.QPointF(
7175
p.pos().x() + (p.boundingRect().width() / 2) - pt_width,
7276
p.pos().y() + (p.boundingRect().height() / 2)
7377
)
7478
path = QtGui.QPainterPath()
7579
path.moveTo(pt1)
76-
path.lineTo(QtCore.QPointF(pt1.x() - 4.0, pt1.y()))
80+
path.lineTo(QtCore.QPointF(pt1.x() - 2.0, pt1.y()))
7781
path.lineTo(rect.center())
7882
painter.drawPath(path)
7983

@@ -120,21 +124,12 @@ def _paint_horizontal(self, painter, option, widget):
120124

121125
def _paint_vertical(self, painter, option, widget):
122126
painter.save()
123-
124-
text_rect = self._text_item.boundingRect()
125-
text_rect = QtCore.QRectF(
126-
self.boundingRect().center().x() - (text_rect.width() / 2),
127-
self.boundingRect().center().y() - (text_rect.height() / 2),
128-
text_rect.width(),
129-
text_rect.height()
130-
)
131-
132-
padding = 10.0
127+
rect = self.boundingRect()
128+
width = min(rect.width(), rect.height()) / 1.8
133129
rect = QtCore.QRectF(
134-
text_rect.center().x() - (text_rect.width() / 2) - (padding / 2),
135-
text_rect.center().y() - (text_rect.width() / 2) - (padding / 2),
136-
text_rect.width() + padding,
137-
text_rect.width() + padding
130+
rect.center().x() - (width / 2),
131+
rect.center().y() - (width / 2),
132+
width, width
138133
)
139134

140135
# draw port lines.
@@ -152,7 +147,7 @@ def _paint_vertical(self, painter, option, widget):
152147
)
153148
path = QtGui.QPainterPath()
154149
path.moveTo(pt1)
155-
path.lineTo(QtCore.QPointF(pt1.x(), pt1.y()))
150+
path.moveTo(QtCore.QPointF(pt1.x(), pt1.y()))
156151
path.lineTo(rect.center())
157152
painter.drawPath(path)
158153

@@ -192,21 +187,6 @@ def _paint_vertical(self, painter, option, widget):
192187
painter.setPen(QtGui.QPen(border_color, border_width))
193188
painter.drawEllipse(rect)
194189

195-
# node name background.
196-
text_rect = self._text_item.boundingRect()
197-
text_rect = QtCore.QRectF(
198-
rect.center().x() - (text_rect.width() / 2),
199-
rect.center().y() - (text_rect.height() / 2),
200-
text_rect.width(),
201-
text_rect.height()
202-
)
203-
if self.selected:
204-
painter.setBrush(QtGui.QColor(*NodeEnum.SELECTED_COLOR.value))
205-
else:
206-
painter.setBrush(QtGui.QColor(0, 0, 0, 80))
207-
painter.setPen(QtCore.Qt.NoPen)
208-
painter.drawRoundedRect(text_rect, 8.0, 8.0)
209-
210190
painter.restore()
211191

212192
def _align_icon_horizontal(self, h_offset, v_offset):
@@ -216,7 +196,11 @@ def _align_icon_horizontal(self, h_offset, v_offset):
216196
self._icon_item.setPos(x + h_offset, y + v_offset)
217197

218198
def _align_icon_vertical(self, h_offset, v_offset):
219-
self._align_icon_horizontal(h_offset, v_offset)
199+
rect = self.boundingRect()
200+
icon_rect = self._icon_item.boundingRect()
201+
x = rect.left() - icon_rect.width() + (rect.width() / 4)
202+
y = rect.center().y() - (icon_rect.height() / 2)
203+
self._icon_item.setPos(x + h_offset, y + v_offset)
220204

221205
def _align_label_horizontal(self, h_offset, v_offset):
222206
rect = self.boundingRect()
@@ -226,7 +210,11 @@ def _align_label_horizontal(self, h_offset, v_offset):
226210
self._text_item.setPos(x + h_offset, y + v_offset)
227211

228212
def _align_label_vertical(self, h_offset, v_offset):
229-
self._align_label_horizontal(h_offset, v_offset)
213+
rect = self.boundingRect()
214+
text_rect = self._text_item.boundingRect()
215+
x = rect.right() - (rect.width() / 3)
216+
y = rect.center().y() - (text_rect.height() / 2)
217+
self._text_item.setPos(x + h_offset, y + v_offset)
230218

231219
def _draw_node_horizontal(self):
232220
add_width = self.text_item.boundingRect().width() * 1.5
@@ -260,17 +248,14 @@ def _draw_node_horizontal(self):
260248
self.update()
261249

262250
def _draw_node_vertical(self):
263-
add_width = self.text_item.boundingRect().width() * 1.5
264-
add_height = self.text_item.boundingRect().width() / 2
265-
266251
# hide the port text items in vertical layout.
267252
for port, text in self._input_items.items():
268253
text.setVisible(False)
269254
for port, text in self._output_items.items():
270255
text.setVisible(False)
271256

272257
# setup initial base size.
273-
self._set_base_size(add_w=add_width, add_h=add_height)
258+
self._set_base_size(add_w=50, add_h=50)
274259
# set text color when node is initialized.
275260
self._set_text_color(self.text_color)
276261
# set the tooltip

docs/_images/node_circle.png

44 KB
Loading

docs/nodes/BaseNode.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ BaseNode
44
.. autoclass:: NodeGraphQt.BaseNode
55
:members:
66
:exclude-members: update_model, set_layout_direction
7+
8+
BaseNode (Circle)
9+
#################
10+
11+
.. autoclass:: NodeGraphQt.BaseNodeCircle
12+
:members:
13+
:exclude-members: update_model, set_layout_direction

examples/basic_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
# create node and set a custom icon.
5656
n_basic_b = graph.create_node(
5757
'nodes.basic.BasicNodeB', name='custom icon')
58-
this_path = os.path.dirname(os.path.abspath(__file__))
59-
icon = os.path.join(this_path, 'examples', 'star.png')
60-
n_basic_b.set_icon(icon)
58+
n_basic_b.set_icon(
59+
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'star.png')
60+
)
6161

6262
# create node with the custom port shapes.
6363
n_custom_ports = graph.create_node(

examples/nodes/basic_nodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class CircleNode(BaseNodeCircle):
6464

6565
def __init__(self):
6666
super(CircleNode, self).__init__()
67+
self.set_color(10, 24, 38)
6768

6869
# create node inputs
6970
self.add_input('in 1')

0 commit comments

Comments
 (0)