Skip to content

Commit 66dfe98

Browse files
committed
Updated PIPE constant vars to enums.
1 parent fc40706 commit 66dfe98

File tree

5 files changed

+108
-67
lines changed

5 files changed

+108
-67
lines changed

NodeGraphQt/base/graph.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import re
77

8-
from Qt import QtCore, QtWidgets, QtGui
8+
from Qt import QtCore, QtWidgets
99

1010
from NodeGraphQt.base.commands import (NodeAddedCmd,
1111
NodeRemovedCmd,
@@ -18,7 +18,7 @@
1818
from NodeGraphQt.base.port import Port
1919
from NodeGraphQt.constants import (
2020
NODE_LAYOUT_DIRECTION, NODE_LAYOUT_HORIZONTAL, NODE_LAYOUT_VERTICAL,
21-
PIPE_LAYOUT_CURVED, PIPE_LAYOUT_STRAIGHT, PIPE_LAYOUT_ANGLE,
21+
PIPE_LAYOUT,
2222
URI_SCHEME, URN_SCHEME,
2323
IN_PORT, OUT_PORT,
2424
VIEWER_GRID_LINES
@@ -752,7 +752,7 @@ def set_pipe_collision(self, mode=True):
752752
self._model.pipe_collision = mode
753753
self._viewer.pipe_collision = mode
754754

755-
def set_pipe_style(self, style=PIPE_LAYOUT_CURVED):
755+
def set_pipe_style(self, style=PIPE_LAYOUT.CURVED.value):
756756
"""
757757
Set node graph pipes to be drawn as straight, curved or angled.
758758
@@ -764,17 +764,17 @@ def set_pipe_style(self, style=PIPE_LAYOUT_CURVED):
764764
765765
Pipe Layout Styles:
766766
767-
* :attr:`NodeGraphQt.constants.PIPE_LAYOUT_CURVED`
768-
* :attr:`NodeGraphQt.constants.PIPE_LAYOUT_STRAIGHT`
769-
* :attr:`NodeGraphQt.constants.PIPE_LAYOUT_ANGLE`
767+
* :attr:`NodeGraphQt.constants.PIPE_LAYOUT.CURVED.value`
768+
* :attr:`NodeGraphQt.constants.PIPE_LAYOUT.STRAIGHT.value`
769+
* :attr:`NodeGraphQt.constants.PIPE_LAYOUT.ANGLE.value`
770770
771771
Args:
772772
style (int): pipe layout style.
773773
"""
774-
pipe_max = max([PIPE_LAYOUT_CURVED,
775-
PIPE_LAYOUT_STRAIGHT,
776-
PIPE_LAYOUT_ANGLE])
777-
style = style if 0 <= style <= pipe_max else PIPE_LAYOUT_CURVED
774+
pipe_max = max([PIPE_LAYOUT.CURVED.value,
775+
PIPE_LAYOUT.STRAIGHT.value,
776+
PIPE_LAYOUT.ANGLE.value])
777+
style = style if 0 <= style <= pipe_max else PIPE_LAYOUT.CURVED.value
778778
self._viewer.set_pipe_layout(style)
779779

780780
def fit_to_selection(self):

NodeGraphQt/constants.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44

55
from Qt import QtWidgets
6+
from enum import Enum
67

78
from .pkg_info import __version__
89

@@ -11,28 +12,59 @@
1112
the whole ``NodeGraphQt`` library.
1213
"""
1314

15+
# =================================== GLOBAL ===================================
16+
1417
#: Current version of the NodeGraphQt framework.
1518
VERSION = __version__
1619

17-
# === PIPE ===
18-
19-
PIPE_WIDTH = 1.2
20-
PIPE_STYLE_DEFAULT = 0
21-
PIPE_STYLE_DASHED = 1
22-
PIPE_STYLE_DOTTED = 2
23-
PIPE_DEFAULT_COLOR = (175, 95, 30, 255)
24-
PIPE_DISABLED_COLOR = (190, 20, 20, 255)
25-
PIPE_ACTIVE_COLOR = (70, 255, 220, 255)
26-
PIPE_HIGHLIGHT_COLOR = (232, 184, 13, 255)
27-
PIPE_SLICER_COLOR = (255, 50, 75)
28-
#: Style to draw the connection pipes as straight lines.
29-
PIPE_LAYOUT_STRAIGHT = 0
30-
#: Style to draw the connection pipes as curved lines.
31-
PIPE_LAYOUT_CURVED = 1
32-
#: Style to draw the connection pipes as angled lines.
33-
PIPE_LAYOUT_ANGLE = 2
34-
35-
# === PORT ===
20+
# ==================================== PIPE ====================================
21+
22+
23+
class PIPE_STYLING(Enum):
24+
"""
25+
Pipe Constant.
26+
"""
27+
#: pipe width.
28+
WIDTH = 1.2
29+
#: default color.
30+
COLOR = (175, 95, 30, 255)
31+
#: pipe color to a node when it's disabled.
32+
DISABLED_COLOR = (190, 20, 20, 255)
33+
#: color when the pipe is selected or mouse hovered.
34+
ACTIVE_COLOR = (70, 255, 220, 255)
35+
#: pipe color to a node when it's selected.
36+
HIGHLIGHT_COLOR = (232, 184, 13, 255)
37+
#: draw connection as a line.
38+
DRAW_TYPE_DEFAULT = 0
39+
#: draw connection as dashed lines.
40+
DRAW_TYPE_DASHED = 1
41+
#: draw connection as a dotted line.
42+
DRAW_TYPE_DOTTED = 2
43+
44+
45+
class PIPE_SLICER_STYLING(Enum):
46+
"""
47+
Slicer Pipe Constant.
48+
"""
49+
#: pipe width.
50+
WIDTH = 1.5
51+
#: default color.
52+
COLOR = (255, 50, 75)
53+
54+
55+
class PIPE_LAYOUT(Enum):
56+
"""
57+
Pipe layout constant.
58+
"""
59+
#: draw the connection pipes as straight lines.
60+
STRAIGHT = 0
61+
#: draw the connection pipes as curved lines.
62+
CURVED = 1
63+
#: draw the connection pipes as angled lines.
64+
ANGLE = 2
65+
66+
67+
# ==================================== PORT ====================================
3668

3769
#: Connection type for input ports.
3870
IN_PORT = 'in'
@@ -48,7 +80,7 @@
4880
PORT_HOVER_BORDER_COLOR = (136, 255, 35, 255)
4981
PORT_FALLOFF = 15.0
5082

51-
# === NODE ===
83+
# ==================================== NODE ====================================
5284

5385
NODE_WIDTH = 160
5486
NODE_HEIGHT = 60
@@ -108,6 +140,8 @@
108140
VIEWER_GRID_COLOR = (45, 45, 45)
109141
VIEWER_GRID_SIZE = 50
110142

143+
# ================================== PRIVATE ===================================
144+
111145
URI_SCHEME = 'nodegraphqt://'
112146
URN_SCHEME = 'nodegraphqt::'
113147

NodeGraphQt/qgraphics/pipe.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@
44
from Qt import QtCore, QtGui, QtWidgets
55

66
from NodeGraphQt.constants import (
7-
PIPE_DEFAULT_COLOR, PIPE_ACTIVE_COLOR,
8-
PIPE_HIGHLIGHT_COLOR, PIPE_DISABLED_COLOR,
9-
PIPE_STYLE_DASHED, PIPE_STYLE_DEFAULT, PIPE_STYLE_DOTTED,
10-
PIPE_LAYOUT_STRAIGHT, PIPE_WIDTH, IN_PORT, OUT_PORT, Z_VAL_PIPE,
7+
PIPE_STYLING, PIPE_LAYOUT, IN_PORT, OUT_PORT, Z_VAL_PIPE,
118
Z_VAL_NODE_WIDGET,
12-
PIPE_LAYOUT_ANGLE, PIPE_LAYOUT_CURVED,
139
ITEM_CACHE_MODE,
1410
NODE_LAYOUT_VERTICAL, NODE_LAYOUT_HORIZONTAL,
15-
NODE_LAYOUT_DIRECTION)
11+
NODE_LAYOUT_DIRECTION
12+
)
1613
from NodeGraphQt.qgraphics.port import PortItem
1714

1815
PIPE_STYLES = {
19-
PIPE_STYLE_DEFAULT: QtCore.Qt.SolidLine,
20-
PIPE_STYLE_DASHED: QtCore.Qt.DashLine,
21-
PIPE_STYLE_DOTTED: QtCore.Qt.DotLine
16+
PIPE_STYLING.DRAW_TYPE_DEFAULT.value: QtCore.Qt.SolidLine,
17+
PIPE_STYLING.DRAW_TYPE_DASHED.value: QtCore.Qt.DashLine,
18+
PIPE_STYLING.DRAW_TYPE_DOTTED.value: QtCore.Qt.DotLine
2219
}
2320

2421

@@ -32,8 +29,8 @@ def __init__(self, input_port=None, output_port=None):
3229
self.setZValue(Z_VAL_PIPE)
3330
self.setAcceptHoverEvents(True)
3431
self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
35-
self._color = PIPE_DEFAULT_COLOR
36-
self._style = PIPE_STYLE_DEFAULT
32+
self._color = PIPE_STYLING.COLOR.value
33+
self._style = PIPE_STYLING.DRAW_TYPE_DEFAULT.value
3734
self._active = False
3835
self._highlight = False
3936
self._input_port = input_port
@@ -76,22 +73,22 @@ def paint(self, painter, option, widget):
7673
"""
7774
color = QtGui.QColor(*self._color)
7875
pen_style = PIPE_STYLES.get(self.style)
79-
pen_width = PIPE_WIDTH
76+
pen_width = PIPE_STYLING.WIDTH.value
8077
if self._active:
81-
color = QtGui.QColor(*PIPE_ACTIVE_COLOR)
78+
color = QtGui.QColor(*PIPE_STYLING.ACTIVE_COLOR.value)
8279
if pen_style == QtCore.Qt.DashDotDotLine:
8380
pen_width += 1
8481
else:
8582
pen_width += 0.35
8683
elif self._highlight:
87-
color = QtGui.QColor(*PIPE_HIGHLIGHT_COLOR)
88-
pen_style = PIPE_STYLES.get(PIPE_STYLE_DEFAULT)
84+
color = QtGui.QColor(*PIPE_STYLING.HIGHLIGHT_COLOR.value)
85+
pen_style = PIPE_STYLES.get(PIPE_STYLING.DRAW_TYPE_DEFAULT.value)
8986

9087
if self.disabled():
9188
if not self._active:
92-
color = QtGui.QColor(*PIPE_DISABLED_COLOR)
89+
color = QtGui.QColor(*PIPE_STYLING.DISABLED_COLOR.value)
9390
pen_width += 0.2
94-
pen_style = PIPE_STYLES.get(PIPE_STYLE_DOTTED)
91+
pen_style = PIPE_STYLES.get(PIPE_STYLING.DRAW_TYPE_DOTTED.value)
9592

9693
pen = QtGui.QPen(color, pen_width, pen_style)
9794
pen.setCapStyle(QtCore.Qt.RoundCap)
@@ -154,7 +151,7 @@ def __draw_path_vertical(self, start_port, pos1, pos2, path):
154151
pos2 (QPointF): end port position.
155152
path (QPainterPath): path to draw.
156153
"""
157-
if self.viewer_pipe_layout() == PIPE_LAYOUT_CURVED:
154+
if self.viewer_pipe_layout() == PIPE_LAYOUT.CURVED.value:
158155
ctr_offset_y1, ctr_offset_y2 = pos1.y(), pos2.y()
159156
tangent = abs(ctr_offset_y1 - ctr_offset_y2)
160157

@@ -171,7 +168,7 @@ def __draw_path_vertical(self, start_port, pos1, pos2, path):
171168
ctr_point2 = QtCore.QPointF(pos2.x(), ctr_offset_y2)
172169
path.cubicTo(ctr_point1, ctr_point2, pos2)
173170
self.setPath(path)
174-
elif self.viewer_pipe_layout() == PIPE_LAYOUT_ANGLE:
171+
elif self.viewer_pipe_layout() == PIPE_LAYOUT.ANGLE.value:
175172
ctr_offset_y1, ctr_offset_y2 = pos1.y(), pos2.y()
176173
distance = abs(ctr_offset_y1 - ctr_offset_y2)/2
177174
if start_port.port_type == IN_PORT:
@@ -198,7 +195,7 @@ def __draw_path_horizontal(self, start_port, pos1, pos2, path):
198195
pos2 (QPointF): end port position.
199196
path (QPainterPath): path to draw.
200197
"""
201-
if self.viewer_pipe_layout() == PIPE_LAYOUT_CURVED:
198+
if self.viewer_pipe_layout() == PIPE_LAYOUT.CURVED.value:
202199
ctr_offset_x1, ctr_offset_x2 = pos1.x(), pos2.x()
203200
tangent = abs(ctr_offset_x1 - ctr_offset_x2)
204201

@@ -215,7 +212,7 @@ def __draw_path_horizontal(self, start_port, pos1, pos2, path):
215212
ctr_point2 = QtCore.QPointF(ctr_offset_x2, pos2.y())
216213
path.cubicTo(ctr_point1, ctr_point2, pos2)
217214
self.setPath(path)
218-
elif self.viewer_pipe_layout() == PIPE_LAYOUT_ANGLE:
215+
elif self.viewer_pipe_layout() == PIPE_LAYOUT.ANGLE.value:
219216
ctr_offset_x1, ctr_offset_x2 = pos1.x(), pos2.x()
220217
distance = abs(ctr_offset_x1 - ctr_offset_x2) / 2
221218
if start_port.port_type == IN_PORT:
@@ -260,7 +257,7 @@ def draw_path(self, start_port, end_port=None, cursor_pos=None):
260257
path = QtGui.QPainterPath()
261258
path.moveTo(line.x1(), line.y1())
262259

263-
if self.viewer_pipe_layout() == PIPE_LAYOUT_STRAIGHT:
260+
if self.viewer_pipe_layout() == PIPE_LAYOUT.STRAIGHT.value:
264261
path.lineTo(pos2)
265262
self.setPath(path)
266263
return
@@ -298,17 +295,21 @@ def viewer_pipe_layout(self):
298295

299296
def activate(self):
300297
self._active = True
301-
color = QtGui.QColor(*PIPE_ACTIVE_COLOR)
302-
pen = QtGui.QPen(color, 2.5, PIPE_STYLES.get(PIPE_STYLE_DEFAULT))
298+
color = QtGui.QColor(*PIPE_STYLING.ACTIVE_COLOR.value)
299+
pen = QtGui.QPen(
300+
color, 2.5, PIPE_STYLES.get(PIPE_STYLING.DRAW_TYPE_DEFAULT.value)
301+
)
303302
self.setPen(pen)
304303

305304
def active(self):
306305
return self._active
307306

308307
def highlight(self):
309308
self._highlight = True
310-
color = QtGui.QColor(*PIPE_HIGHLIGHT_COLOR)
311-
pen = QtGui.QPen(color, 2, PIPE_STYLES.get(PIPE_STYLE_DEFAULT))
309+
color = QtGui.QColor(*PIPE_STYLING.HIGHLIGHT_COLOR.value)
310+
pen = QtGui.QPen(
311+
color, 2, PIPE_STYLES.get(PIPE_STYLING.DRAW_TYPE_DEFAULT.value)
312+
)
312313
self.setPen(pen)
313314

314315
def highlighted(self):
@@ -409,9 +410,9 @@ def paint(self, painter, option, widget):
409410
used to describe the parameters needed to draw.
410411
widget (QtWidgets.QWidget): not used.
411412
"""
412-
color = QtGui.QColor(*PIPE_ACTIVE_COLOR)
413-
pen_style = PIPE_STYLES.get(PIPE_STYLE_DASHED)
414-
pen_width = PIPE_WIDTH + 0.35
413+
color = QtGui.QColor(*PIPE_STYLING.ACTIVE_COLOR.value)
414+
pen_style = PIPE_STYLES.get(PIPE_STYLING.DRAW_TYPE_DASHED.value)
415+
pen_width = PIPE_STYLING.WIDTH.value + 0.35
415416

416417
pen = QtGui.QPen(color, pen_width)
417418
pen.setStyle(pen_style)

NodeGraphQt/qgraphics/slicer.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from Qt import QtCore, QtGui, QtWidgets
55

6-
from NodeGraphQt.constants import Z_VAL_NODE_WIDGET, PIPE_SLICER_COLOR
6+
from NodeGraphQt.constants import Z_VAL_NODE_WIDGET, PIPE_SLICER_STYLING
77

88

99
class SlicerPipeItem(QtWidgets.QGraphicsPathItem):
@@ -25,7 +25,7 @@ def paint(self, painter, option, widget):
2525
used to describe the parameters needed to draw.
2626
widget (QtWidgets.QWidget): not used.
2727
"""
28-
color = QtGui.QColor(*PIPE_SLICER_COLOR)
28+
color = QtGui.QColor(*PIPE_SLICER_STYLING.COLOR.value)
2929
p1 = self.path().pointAtPercent(0)
3030
p2 = self.path().pointAtPercent(1)
3131
size = 6.0
@@ -42,15 +42,21 @@ def paint(self, painter, option, widget):
4242
text_x = painter.fontMetrics().width(text) / 2
4343
text_y = painter.fontMetrics().height() / 1.5
4444
text_pos = QtCore.QPointF(p1.x() - text_x, p1.y() - text_y)
45-
text_color = QtGui.QColor(*PIPE_SLICER_COLOR)
45+
text_color = QtGui.QColor(*PIPE_SLICER_STYLING.COLOR.value)
4646
text_color.setAlpha(80)
47-
painter.setPen(QtGui.QPen(text_color, 1.5, QtCore.Qt.SolidLine))
47+
painter.setPen(QtGui.QPen(
48+
text_color, PIPE_SLICER_STYLING.WIDTH.value, QtCore.Qt.SolidLine
49+
))
4850
painter.drawText(text_pos, text)
4951

50-
painter.setPen(QtGui.QPen(color, 1.5, QtCore.Qt.DashDotLine))
52+
painter.setPen(QtGui.QPen(
53+
color, PIPE_SLICER_STYLING.WIDTH.value, QtCore.Qt.DashDotLine
54+
))
5155
painter.drawPath(self.path())
5256

53-
pen = QtGui.QPen(color, 1.5, QtCore.Qt.SolidLine)
57+
pen = QtGui.QPen(
58+
color, PIPE_SLICER_STYLING.WIDTH.value, QtCore.Qt.SolidLine
59+
)
5460
pen.setCapStyle(QtCore.Qt.RoundCap)
5561
pen.setJoinStyle(QtCore.Qt.MiterJoin)
5662
painter.setPen(pen)

NodeGraphQt/widgets/viewer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from Qt import QtGui, QtCore, QtWidgets
77

88
from NodeGraphQt.base.menu import BaseMenu
9-
from NodeGraphQt.constants import IN_PORT, OUT_PORT, PIPE_LAYOUT_CURVED
9+
from NodeGraphQt.constants import IN_PORT, OUT_PORT, PIPE_LAYOUT
1010
from NodeGraphQt.qgraphics.node_abstract import AbstractNodeItem
1111
from NodeGraphQt.qgraphics.node_backdrop import BackdropNodeItem
1212
from NodeGraphQt.qgraphics.pipe import PipeItem, LivePipeItem
@@ -71,7 +71,7 @@ def __init__(self, parent=None, undo_stack=None):
7171
self._update_scene()
7272
self._last_size = self.size()
7373

74-
self._pipe_layout = PIPE_LAYOUT_CURVED
74+
self._pipe_layout = PIPE_LAYOUT.CURVED.value
7575
self._detached_port = None
7676
self._start_port = None
7777
self._origin_pos = None

0 commit comments

Comments
 (0)