koode-taskgraph 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- koode_taskgraph-0.1.0.dist-info/METADATA +7 -0
- koode_taskgraph-0.1.0.dist-info/RECORD +23 -0
- koode_taskgraph-0.1.0.dist-info/WHEEL +5 -0
- koode_taskgraph-0.1.0.dist-info/entry_points.txt +3 -0
- koode_taskgraph-0.1.0.dist-info/top_level.txt +1 -0
- taskgraph/__init__.py +4 -0
- taskgraph/__main__.py +4 -0
- taskgraph/app.py +53 -0
- taskgraph/cli.py +123 -0
- taskgraph/core/__init__.py +2 -0
- taskgraph/core/executor.py +232 -0
- taskgraph/core/model.py +190 -0
- taskgraph/core/registry.py +90 -0
- taskgraph/core/serialization.py +35 -0
- taskgraph/nodes/__init__.py +14 -0
- taskgraph/nodes/builtin.py +152 -0
- taskgraph/ui/__init__.py +1 -0
- taskgraph/ui/items.py +403 -0
- taskgraph/ui/main_window.py +578 -0
- taskgraph/ui/plugins.py +208 -0
- taskgraph/ui/properties.py +165 -0
- taskgraph/ui/scene.py +426 -0
- taskgraph/ui/theme.py +30 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from hashlib import sha256
|
|
5
|
+
from importlib.util import module_from_spec, spec_from_file_location
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import sys
|
|
8
|
+
from typing import TypeVar
|
|
9
|
+
|
|
10
|
+
from taskgraph.core.model import ProcessNode
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T", bound=type[ProcessNode])
|
|
13
|
+
_NODE_TYPES: dict[str, type[ProcessNode]] = {}
|
|
14
|
+
_LOADED_CUSTOM_MODULES: set[Path] = set()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def register_node(node_class: T) -> T:
|
|
18
|
+
if not node_class.type_id or node_class.type_id == ProcessNode.type_id:
|
|
19
|
+
raise ValueError(f"{node_class.__name__} must define a unique type_id")
|
|
20
|
+
if node_class.type_id in _NODE_TYPES:
|
|
21
|
+
raise ValueError(f"Duplicate node type: {node_class.type_id}")
|
|
22
|
+
_NODE_TYPES[node_class.type_id] = node_class
|
|
23
|
+
return node_class
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def node_class(type_id: str) -> type[ProcessNode]:
|
|
27
|
+
try:
|
|
28
|
+
return _NODE_TYPES[type_id]
|
|
29
|
+
except KeyError as exc:
|
|
30
|
+
raise ValueError(f"Unknown node type: {type_id}") from exc
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def create_node(
|
|
34
|
+
type_id: str,
|
|
35
|
+
node_id: str | None = None,
|
|
36
|
+
values: dict | None = None,
|
|
37
|
+
disabled: bool = False,
|
|
38
|
+
name: str | None = None,
|
|
39
|
+
) -> ProcessNode:
|
|
40
|
+
return node_class(type_id)(
|
|
41
|
+
node_id=node_id, values=values, disabled=disabled, name=name
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def nodes_by_category() -> dict[str, list[type[ProcessNode]]]:
|
|
46
|
+
result: dict[str, list[type[ProcessNode]]] = defaultdict(list)
|
|
47
|
+
for cls in _NODE_TYPES.values():
|
|
48
|
+
result[cls.category].append(cls)
|
|
49
|
+
return {
|
|
50
|
+
category: sorted(classes, key=lambda cls: cls.title.lower())
|
|
51
|
+
for category, classes in sorted(result.items())
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def load_custom_node_directory(directory: str | Path) -> list[Path]:
|
|
56
|
+
"""Import standalone custom-node modules from an arbitrary directory."""
|
|
57
|
+
location = Path(directory).expanduser().resolve()
|
|
58
|
+
if not location.is_dir():
|
|
59
|
+
raise ValueError(f"Custom node location does not exist: {location}")
|
|
60
|
+
|
|
61
|
+
loaded = []
|
|
62
|
+
for path in sorted(location.glob("*.py")):
|
|
63
|
+
if path.name.startswith("_") or path in _LOADED_CUSTOM_MODULES:
|
|
64
|
+
continue
|
|
65
|
+
module_name = (
|
|
66
|
+
"_taskgraph_custom_"
|
|
67
|
+
+ sha256(str(path).encode("utf-8")).hexdigest()[:20]
|
|
68
|
+
)
|
|
69
|
+
spec = spec_from_file_location(module_name, path)
|
|
70
|
+
if not spec or not spec.loader:
|
|
71
|
+
raise ImportError(f"Could not load custom node module: {path}")
|
|
72
|
+
module = module_from_spec(spec)
|
|
73
|
+
registered_before = set(_NODE_TYPES)
|
|
74
|
+
sys.modules[module_name] = module
|
|
75
|
+
sys.path.insert(0, str(location))
|
|
76
|
+
try:
|
|
77
|
+
spec.loader.exec_module(module)
|
|
78
|
+
except Exception:
|
|
79
|
+
for type_id in set(_NODE_TYPES) - registered_before:
|
|
80
|
+
_NODE_TYPES.pop(type_id, None)
|
|
81
|
+
sys.modules.pop(module_name, None)
|
|
82
|
+
raise
|
|
83
|
+
finally:
|
|
84
|
+
try:
|
|
85
|
+
sys.path.remove(str(location))
|
|
86
|
+
except ValueError:
|
|
87
|
+
pass
|
|
88
|
+
_LOADED_CUSTOM_MODULES.add(path)
|
|
89
|
+
loaded.append(path)
|
|
90
|
+
return loaded
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from taskgraph.core.model import Backdrop, Connection, Graph
|
|
5
|
+
from taskgraph.core.registry import create_node
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def save_graph(graph: Graph, path: str | Path) -> None:
|
|
9
|
+
Path(path).write_text(json.dumps(graph.to_dict(), indent=2), encoding="utf-8")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def load_graph(path: str | Path) -> Graph:
|
|
13
|
+
data = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
14
|
+
if data.get("version") != 1:
|
|
15
|
+
raise ValueError(f"Unsupported graph version: {data.get('version')}")
|
|
16
|
+
graph = Graph()
|
|
17
|
+
for item in data.get("nodes", []):
|
|
18
|
+
node = create_node(
|
|
19
|
+
item["type"], item["id"], item.get("values"),
|
|
20
|
+
disabled=item.get("disabled", False),
|
|
21
|
+
name=item.get("name"),
|
|
22
|
+
)
|
|
23
|
+
graph.add_node(node, tuple(item.get("position", (0, 0))))
|
|
24
|
+
for item in data.get("connections", []):
|
|
25
|
+
graph.connect(Connection(**item))
|
|
26
|
+
for item in data.get("backdrops", []):
|
|
27
|
+
graph.add_backdrop(Backdrop(
|
|
28
|
+
id=item["id"],
|
|
29
|
+
title=item.get("title", "Notes"),
|
|
30
|
+
note=item.get("note", ""),
|
|
31
|
+
color=item.get("color", "#168c9c"),
|
|
32
|
+
position=tuple(item.get("position", (0, 0))),
|
|
33
|
+
size=tuple(item.get("size", (480, 300))),
|
|
34
|
+
))
|
|
35
|
+
return graph
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Node discovery.
|
|
2
|
+
|
|
3
|
+
Every Python module placed in this package is imported at startup, so extending
|
|
4
|
+
the application only requires adding a module containing registered nodes.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from importlib import import_module
|
|
8
|
+
from pkgutil import iter_modules
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def load_builtin_nodes() -> None:
|
|
12
|
+
for module in iter_modules(__path__, f"{__name__}."):
|
|
13
|
+
if not module.name.rsplit(".", 1)[-1].startswith("_"):
|
|
14
|
+
import_module(module.name)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import shlex
|
|
3
|
+
import signal
|
|
4
|
+
import subprocess
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
from taskgraph.core.model import NodeCancelled, NodeProperty, PortSpec, ProcessNode
|
|
8
|
+
from taskgraph.core.registry import register_node
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@register_node
|
|
12
|
+
class TextValue(ProcessNode):
|
|
13
|
+
type_id = "input.text"
|
|
14
|
+
title = "Text"
|
|
15
|
+
category = "Inputs"
|
|
16
|
+
color = "#087f8c"
|
|
17
|
+
outputs = (PortSpec("text", "text"),)
|
|
18
|
+
properties = (NodeProperty("text", "Text", "text", "Hello graph"),)
|
|
19
|
+
|
|
20
|
+
def process(self, inputs):
|
|
21
|
+
return {"text": self.text}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@register_node
|
|
25
|
+
class NumberValue(ProcessNode):
|
|
26
|
+
type_id = "input.number"
|
|
27
|
+
title = "Number"
|
|
28
|
+
category = "Inputs"
|
|
29
|
+
color = "#087f8c"
|
|
30
|
+
outputs = (PortSpec("number", "number"),)
|
|
31
|
+
properties = (NodeProperty("number", "Number", "float", 1.0),)
|
|
32
|
+
|
|
33
|
+
def process(self, inputs):
|
|
34
|
+
return {"number": self.number}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@register_node
|
|
38
|
+
class FormatText(ProcessNode):
|
|
39
|
+
type_id = "text.format"
|
|
40
|
+
title = "Format Text"
|
|
41
|
+
category = "Utils"
|
|
42
|
+
color = "#6f4a8e"
|
|
43
|
+
inputs = (PortSpec("value", "any", required=True),)
|
|
44
|
+
outputs = (PortSpec("text", "text"),)
|
|
45
|
+
properties = (
|
|
46
|
+
NodeProperty("template", "Template", "text", "Result: {value}"),
|
|
47
|
+
NodeProperty("uppercase", "Uppercase", "bool", False),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def process(self, inputs):
|
|
51
|
+
text = self.template.format(value=inputs["value"])
|
|
52
|
+
return {"text": text.upper() if self.uppercase else text}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@register_node
|
|
56
|
+
class PrintValue(ProcessNode):
|
|
57
|
+
type_id = "output.print"
|
|
58
|
+
title = "Print"
|
|
59
|
+
category = "System"
|
|
60
|
+
color = "#a33a4b"
|
|
61
|
+
inputs = (PortSpec("value", "any", required=True),)
|
|
62
|
+
properties = (NodeProperty("prefix", "Prefix", "text", ""),)
|
|
63
|
+
|
|
64
|
+
def process(self, inputs):
|
|
65
|
+
value = f"{self.prefix}{inputs['value']}"
|
|
66
|
+
print(value)
|
|
67
|
+
return {}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@register_node
|
|
71
|
+
class RunCommand(ProcessNode):
|
|
72
|
+
type_id = "system.command"
|
|
73
|
+
title = "Run Command"
|
|
74
|
+
category = "System"
|
|
75
|
+
color = "#566b32"
|
|
76
|
+
inputs = (PortSpec("command", "text"),)
|
|
77
|
+
outputs = (
|
|
78
|
+
PortSpec("stdout", "text"),
|
|
79
|
+
PortSpec("stderr", "text"),
|
|
80
|
+
PortSpec("return_code", "number"),
|
|
81
|
+
)
|
|
82
|
+
properties = (
|
|
83
|
+
NodeProperty("command", "Command", "text", "echo Hello from TaskGraph"),
|
|
84
|
+
NodeProperty("working_directory", "Working Directory", "text", ""),
|
|
85
|
+
NodeProperty("shell", "Use Shell", "bool", True),
|
|
86
|
+
NodeProperty("timeout", "Timeout (seconds)", "int", 60, minimum=1, maximum=86400),
|
|
87
|
+
NodeProperty("fail_on_error", "Fail on Non-zero Exit", "bool", True),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
def process(self, inputs):
|
|
91
|
+
command = str(inputs.get("command", self.command)).strip()
|
|
92
|
+
if not command:
|
|
93
|
+
raise ValueError("Command cannot be empty")
|
|
94
|
+
args = command if self.shell else shlex.split(command)
|
|
95
|
+
popen_options = {}
|
|
96
|
+
if os.name == "posix":
|
|
97
|
+
popen_options["start_new_session"] = True
|
|
98
|
+
elif os.name == "nt":
|
|
99
|
+
popen_options["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
|
|
100
|
+
process = subprocess.Popen(
|
|
101
|
+
args,
|
|
102
|
+
cwd=self.working_directory.strip() or None,
|
|
103
|
+
shell=self.shell,
|
|
104
|
+
stdout=subprocess.PIPE,
|
|
105
|
+
stderr=subprocess.PIPE,
|
|
106
|
+
text=True,
|
|
107
|
+
**popen_options,
|
|
108
|
+
)
|
|
109
|
+
deadline = time.monotonic() + self.timeout
|
|
110
|
+
while True:
|
|
111
|
+
if self.cancellation_requested:
|
|
112
|
+
self._terminate_process(process)
|
|
113
|
+
process.communicate()
|
|
114
|
+
raise NodeCancelled("Command cancelled")
|
|
115
|
+
remaining = deadline - time.monotonic()
|
|
116
|
+
if remaining <= 0:
|
|
117
|
+
self._terminate_process(process)
|
|
118
|
+
process.communicate()
|
|
119
|
+
raise RuntimeError(f"command timed out after {self.timeout} seconds")
|
|
120
|
+
try:
|
|
121
|
+
stdout, stderr = process.communicate(timeout=min(0.1, remaining))
|
|
122
|
+
break
|
|
123
|
+
except subprocess.TimeoutExpired:
|
|
124
|
+
continue
|
|
125
|
+
if self.fail_on_error and process.returncode:
|
|
126
|
+
detail = stderr.strip() or stdout.strip()
|
|
127
|
+
raise RuntimeError(
|
|
128
|
+
f"command exited with code {process.returncode}"
|
|
129
|
+
+ (f": {detail}" if detail else "")
|
|
130
|
+
)
|
|
131
|
+
return {
|
|
132
|
+
"stdout": stdout,
|
|
133
|
+
"stderr": stderr,
|
|
134
|
+
"return_code": process.returncode,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@staticmethod
|
|
138
|
+
def _terminate_process(process):
|
|
139
|
+
if process.poll() is not None:
|
|
140
|
+
return
|
|
141
|
+
try:
|
|
142
|
+
if os.name == "posix":
|
|
143
|
+
os.killpg(process.pid, signal.SIGTERM)
|
|
144
|
+
else:
|
|
145
|
+
process.terminate()
|
|
146
|
+
process.wait(timeout=0.5)
|
|
147
|
+
except (ProcessLookupError, subprocess.TimeoutExpired):
|
|
148
|
+
if process.poll() is None:
|
|
149
|
+
if os.name == "posix":
|
|
150
|
+
os.killpg(process.pid, signal.SIGKILL)
|
|
151
|
+
else:
|
|
152
|
+
process.kill()
|
taskgraph/ui/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Qt user interface for TaskGraph."""
|
taskgraph/ui/items.py
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
5
|
+
|
|
6
|
+
from qtpy.QtCore import QPointF, QRectF, Qt
|
|
7
|
+
from qtpy.QtGui import (
|
|
8
|
+
QBrush, QColor, QFont, QPainter, QPainterPath, QPen, QPolygonF,
|
|
9
|
+
)
|
|
10
|
+
from qtpy.QtWidgets import QGraphicsItem, QGraphicsObject, QStyle
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from taskgraph.core.model import Backdrop, Connection, PortSpec, ProcessNode
|
|
14
|
+
from taskgraph.ui.scene import GraphScene
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
TYPE_COLORS = {
|
|
18
|
+
"any": "#56c7d9", "text": "#61b7ff", "number": "#f0b35b",
|
|
19
|
+
"bool": "#db78e8", "list": "#75d98b", "dependency": "#f2b04f",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PortItem(QGraphicsObject):
|
|
24
|
+
RADIUS = 6.0
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self, node_item: "NodeItem", spec: "PortSpec", is_output: bool,
|
|
28
|
+
connection_kind: str = "attribute",
|
|
29
|
+
):
|
|
30
|
+
super().__init__(node_item)
|
|
31
|
+
self.node_item = node_item
|
|
32
|
+
self.spec = spec
|
|
33
|
+
self.is_output = is_output
|
|
34
|
+
self.connection_kind = connection_kind
|
|
35
|
+
self.setAcceptHoverEvents(True)
|
|
36
|
+
self.setCursor(Qt.CrossCursor)
|
|
37
|
+
|
|
38
|
+
def boundingRect(self) -> QRectF:
|
|
39
|
+
r = self.RADIUS + 3
|
|
40
|
+
return QRectF(-r, -r, r * 2, r * 2)
|
|
41
|
+
|
|
42
|
+
def paint(self, painter: QPainter, option, widget=None) -> None:
|
|
43
|
+
color = QColor(TYPE_COLORS.get(self.spec.data_type, TYPE_COLORS["any"]))
|
|
44
|
+
if option.state & QStyle.State_MouseOver:
|
|
45
|
+
color = color.lighter(145)
|
|
46
|
+
painter.setPen(QPen(color.lighter(135), 1.5))
|
|
47
|
+
painter.setBrush(QBrush(color.darker(125)))
|
|
48
|
+
painter.drawEllipse(QPointF(0, 0), self.RADIUS, self.RADIUS)
|
|
49
|
+
|
|
50
|
+
def mousePressEvent(self, event) -> None:
|
|
51
|
+
if event.button() == Qt.LeftButton:
|
|
52
|
+
self.node_item.graph_scene.begin_connection(self)
|
|
53
|
+
event.accept()
|
|
54
|
+
return
|
|
55
|
+
super().mousePressEvent(event)
|
|
56
|
+
|
|
57
|
+
def mouseMoveEvent(self, event) -> None:
|
|
58
|
+
self.node_item.graph_scene.update_connection(event.scenePos())
|
|
59
|
+
event.accept()
|
|
60
|
+
|
|
61
|
+
def mouseReleaseEvent(self, event) -> None:
|
|
62
|
+
self.node_item.graph_scene.finish_connection(event.scenePos())
|
|
63
|
+
event.accept()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class BackdropResizeHandle(QGraphicsObject):
|
|
67
|
+
SIZE = 18.0
|
|
68
|
+
|
|
69
|
+
def __init__(self, backdrop_item: "BackdropItem"):
|
|
70
|
+
super().__init__(backdrop_item)
|
|
71
|
+
self.backdrop_item = backdrop_item
|
|
72
|
+
self._start_position = QPointF()
|
|
73
|
+
self._start_size = (0.0, 0.0)
|
|
74
|
+
self.setCursor(Qt.SizeFDiagCursor)
|
|
75
|
+
self.setZValue(1)
|
|
76
|
+
|
|
77
|
+
def boundingRect(self) -> QRectF:
|
|
78
|
+
return QRectF(-self.SIZE, -self.SIZE, self.SIZE, self.SIZE)
|
|
79
|
+
|
|
80
|
+
def paint(self, painter: QPainter, option, widget=None) -> None:
|
|
81
|
+
color = QColor(self.backdrop_item.backdrop.color).lighter(150)
|
|
82
|
+
painter.setPen(QPen(color, 2, Qt.SolidLine, Qt.RoundCap))
|
|
83
|
+
painter.drawLine(QPointF(-12, -3), QPointF(-3, -12))
|
|
84
|
+
painter.drawLine(QPointF(-8, -3), QPointF(-3, -8))
|
|
85
|
+
|
|
86
|
+
def mousePressEvent(self, event) -> None:
|
|
87
|
+
if event.button() == Qt.LeftButton:
|
|
88
|
+
self.backdrop_item.setSelected(True)
|
|
89
|
+
self._start_position = event.scenePos()
|
|
90
|
+
self._start_size = self.backdrop_item.backdrop.size
|
|
91
|
+
event.accept()
|
|
92
|
+
return
|
|
93
|
+
super().mousePressEvent(event)
|
|
94
|
+
|
|
95
|
+
def mouseMoveEvent(self, event) -> None:
|
|
96
|
+
delta = event.scenePos() - self._start_position
|
|
97
|
+
self.backdrop_item.set_interactive_size(
|
|
98
|
+
self._start_size[0] + delta.x(),
|
|
99
|
+
self._start_size[1] + delta.y(),
|
|
100
|
+
)
|
|
101
|
+
event.accept()
|
|
102
|
+
|
|
103
|
+
def mouseReleaseEvent(self, event) -> None:
|
|
104
|
+
if event.button() == Qt.LeftButton:
|
|
105
|
+
self.backdrop_item.graph_scene.node_selected.emit(
|
|
106
|
+
self.backdrop_item.backdrop
|
|
107
|
+
)
|
|
108
|
+
event.accept()
|
|
109
|
+
return
|
|
110
|
+
super().mouseReleaseEvent(event)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class BackdropItem(QGraphicsObject):
|
|
114
|
+
HEADER = 34.0
|
|
115
|
+
|
|
116
|
+
def __init__(self, graph_scene: "GraphScene", backdrop: "Backdrop"):
|
|
117
|
+
super().__init__()
|
|
118
|
+
self.graph_scene = graph_scene
|
|
119
|
+
self.backdrop = backdrop
|
|
120
|
+
self._size = backdrop.size
|
|
121
|
+
self.setFlags(
|
|
122
|
+
QGraphicsItem.ItemIsMovable
|
|
123
|
+
| QGraphicsItem.ItemIsSelectable
|
|
124
|
+
| QGraphicsItem.ItemSendsGeometryChanges
|
|
125
|
+
)
|
|
126
|
+
self.setZValue(0)
|
|
127
|
+
self.resize_handle = BackdropResizeHandle(self)
|
|
128
|
+
self.resize_handle.setPos(*self._size)
|
|
129
|
+
|
|
130
|
+
def boundingRect(self) -> QRectF:
|
|
131
|
+
width, height = self._size
|
|
132
|
+
return QRectF(0, 0, width, height)
|
|
133
|
+
|
|
134
|
+
def paint(self, painter: QPainter, option, widget=None) -> None:
|
|
135
|
+
rect = self.boundingRect()
|
|
136
|
+
color = QColor(self.backdrop.color)
|
|
137
|
+
fill = QColor(color)
|
|
138
|
+
fill.setAlpha(42)
|
|
139
|
+
header = QColor(color)
|
|
140
|
+
header.setAlpha(185)
|
|
141
|
+
painter.setPen(QPen(
|
|
142
|
+
color.lighter(145) if self.isSelected() else color,
|
|
143
|
+
2 if self.isSelected() else 1,
|
|
144
|
+
))
|
|
145
|
+
painter.setBrush(fill)
|
|
146
|
+
painter.drawRoundedRect(rect, 5, 5)
|
|
147
|
+
painter.fillRect(QRectF(0, 0, rect.width(), self.HEADER), header)
|
|
148
|
+
painter.setPen(QColor("#f2f5f7"))
|
|
149
|
+
font = QFont(painter.font())
|
|
150
|
+
font.setBold(True)
|
|
151
|
+
painter.setFont(font)
|
|
152
|
+
painter.drawText(
|
|
153
|
+
QRectF(12, 0, rect.width() - 24, self.HEADER),
|
|
154
|
+
Qt.AlignVCenter,
|
|
155
|
+
self.backdrop.title or "Notes",
|
|
156
|
+
)
|
|
157
|
+
painter.setFont(QFont())
|
|
158
|
+
painter.setPen(QColor("#d5dbe3"))
|
|
159
|
+
painter.drawText(
|
|
160
|
+
rect.adjusted(14, self.HEADER + 12, -14, -12),
|
|
161
|
+
Qt.AlignLeft | Qt.AlignTop | Qt.TextWordWrap,
|
|
162
|
+
self.backdrop.note,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
def itemChange(self, change, value):
|
|
166
|
+
if change == QGraphicsItem.ItemPositionHasChanged and self.scene():
|
|
167
|
+
position = self.pos()
|
|
168
|
+
self.backdrop.position = (position.x(), position.y())
|
|
169
|
+
self.graph_scene.graph_changed.emit()
|
|
170
|
+
return super().itemChange(change, value)
|
|
171
|
+
|
|
172
|
+
def refresh_geometry(self) -> None:
|
|
173
|
+
if self._size != self.backdrop.size:
|
|
174
|
+
self.prepareGeometryChange()
|
|
175
|
+
self._size = self.backdrop.size
|
|
176
|
+
self.resize_handle.setPos(*self._size)
|
|
177
|
+
self.update()
|
|
178
|
+
|
|
179
|
+
def set_interactive_size(self, width: float, height: float) -> None:
|
|
180
|
+
new_size = (max(200.0, width), max(120.0, height))
|
|
181
|
+
if new_size == self._size:
|
|
182
|
+
return
|
|
183
|
+
self.prepareGeometryChange()
|
|
184
|
+
self._size = new_size
|
|
185
|
+
self.backdrop.size = new_size
|
|
186
|
+
self.resize_handle.setPos(*new_size)
|
|
187
|
+
self.update()
|
|
188
|
+
self.graph_scene.graph_changed.emit()
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class NodeItem(QGraphicsObject):
|
|
192
|
+
WIDTH = 250.0
|
|
193
|
+
HEADER = 30.0
|
|
194
|
+
ROW = 25.0
|
|
195
|
+
|
|
196
|
+
def __init__(self, graph_scene: "GraphScene", node: "ProcessNode"):
|
|
197
|
+
super().__init__()
|
|
198
|
+
self.graph_scene = graph_scene
|
|
199
|
+
self.node = node
|
|
200
|
+
self.input_ports: dict[str, PortItem] = {}
|
|
201
|
+
self.output_ports: dict[str, PortItem] = {}
|
|
202
|
+
row_count = max(len(node.inputs), len(node.outputs), 1)
|
|
203
|
+
self._height = max(104.0, self.HEADER + (row_count + 1) * self.ROW + 18)
|
|
204
|
+
self.setFlags(
|
|
205
|
+
QGraphicsItem.ItemIsMovable
|
|
206
|
+
| QGraphicsItem.ItemIsSelectable
|
|
207
|
+
| QGraphicsItem.ItemSendsGeometryChanges
|
|
208
|
+
)
|
|
209
|
+
self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
|
|
210
|
+
self.setZValue(2)
|
|
211
|
+
self.dependency_input = PortItem(
|
|
212
|
+
self, node.dependency_input, False, "dependency"
|
|
213
|
+
)
|
|
214
|
+
self.dependency_input.setPos(0, self.HEADER + 17)
|
|
215
|
+
self.dependency_output = PortItem(
|
|
216
|
+
self, node.dependency_output, True, "dependency"
|
|
217
|
+
)
|
|
218
|
+
self.dependency_output.setPos(self.WIDTH, self.HEADER + 17)
|
|
219
|
+
for index, spec in enumerate(node.inputs):
|
|
220
|
+
port = PortItem(self, spec, False)
|
|
221
|
+
port.setPos(0, self.HEADER + 17 + (index + 1) * self.ROW)
|
|
222
|
+
self.input_ports[spec.name] = port
|
|
223
|
+
for index, spec in enumerate(node.outputs):
|
|
224
|
+
port = PortItem(self, spec, True)
|
|
225
|
+
port.setPos(self.WIDTH, self.HEADER + 17 + (index + 1) * self.ROW)
|
|
226
|
+
self.output_ports[spec.name] = port
|
|
227
|
+
|
|
228
|
+
def boundingRect(self) -> QRectF:
|
|
229
|
+
return QRectF(-8, -2, self.WIDTH + 16, self._height + 10)
|
|
230
|
+
|
|
231
|
+
def shape(self) -> QPainterPath:
|
|
232
|
+
path = QPainterPath()
|
|
233
|
+
path.addRoundedRect(QRectF(0, 0, self.WIDTH, self._height), 6, 6)
|
|
234
|
+
return path
|
|
235
|
+
|
|
236
|
+
def paint(self, painter: QPainter, option, widget=None) -> None:
|
|
237
|
+
body = QRectF(0, 0, self.WIDTH, self._height)
|
|
238
|
+
state = self.graph_scene.execution_states.get(self.node.id, "idle")
|
|
239
|
+
state_colors = {
|
|
240
|
+
"running": QColor("#ffbe3d"),
|
|
241
|
+
"finished": QColor("#45d483"),
|
|
242
|
+
"failed": QColor("#ff5261"),
|
|
243
|
+
"skipped": QColor("#88939d"),
|
|
244
|
+
"blocked": QColor("#b77986"),
|
|
245
|
+
"cancelled": QColor("#9b8eb8"),
|
|
246
|
+
}
|
|
247
|
+
border = state_colors.get(
|
|
248
|
+
state,
|
|
249
|
+
QColor("#64cddd") if self.isSelected() else QColor("#40505d"),
|
|
250
|
+
)
|
|
251
|
+
painter.setPen(QPen(border, 3 if state == "running" else (2 if self.isSelected() else 1)))
|
|
252
|
+
painter.setBrush(QColor("#131b21"))
|
|
253
|
+
painter.drawRoundedRect(body, 6, 6)
|
|
254
|
+
header = QPainterPath()
|
|
255
|
+
header.addRoundedRect(QRectF(0, 0, self.WIDTH, self.HEADER + 5), 6, 6)
|
|
256
|
+
header.addRect(QRectF(0, self.HEADER - 5, self.WIDTH, 10))
|
|
257
|
+
painter.fillPath(header, QColor(self.node.color))
|
|
258
|
+
painter.setPen(QColor("#f2f5f7"))
|
|
259
|
+
font = QFont(painter.font())
|
|
260
|
+
font.setBold(True)
|
|
261
|
+
painter.setFont(font)
|
|
262
|
+
painter.drawText(
|
|
263
|
+
QRectF(12, 0, self.WIDTH - 58, self.HEADER),
|
|
264
|
+
Qt.AlignVCenter,
|
|
265
|
+
self.node.display_name,
|
|
266
|
+
)
|
|
267
|
+
if state != "idle":
|
|
268
|
+
painter.setPen(Qt.NoPen)
|
|
269
|
+
painter.setBrush(state_colors[state])
|
|
270
|
+
painter.drawEllipse(QPointF(self.WIDTH - 16, self.HEADER / 2), 5, 5)
|
|
271
|
+
painter.setPen(QColor("#ffffff"))
|
|
272
|
+
painter.setFont(QFont(painter.font().family(), 7))
|
|
273
|
+
painter.drawText(
|
|
274
|
+
QRectF(self.WIDTH - 58, 0, 36, self.HEADER),
|
|
275
|
+
Qt.AlignVCenter | Qt.AlignRight,
|
|
276
|
+
state.upper(),
|
|
277
|
+
)
|
|
278
|
+
painter.setFont(QFont())
|
|
279
|
+
painter.setPen(QColor("#b9c3cc"))
|
|
280
|
+
painter.drawText(
|
|
281
|
+
QRectF(12, self.HEADER + 5, self.WIDTH - 24, self.ROW),
|
|
282
|
+
Qt.AlignVCenter,
|
|
283
|
+
"dependency",
|
|
284
|
+
)
|
|
285
|
+
painter.drawText(
|
|
286
|
+
QRectF(12, self.HEADER + 5, self.WIDTH - 24, self.ROW),
|
|
287
|
+
Qt.AlignVCenter | Qt.AlignRight,
|
|
288
|
+
"dependency",
|
|
289
|
+
)
|
|
290
|
+
for index, spec in enumerate(self.node.inputs):
|
|
291
|
+
painter.drawText(
|
|
292
|
+
QRectF(
|
|
293
|
+
12,
|
|
294
|
+
self.HEADER + 5 + (index + 1) * self.ROW,
|
|
295
|
+
self.WIDTH / 2 - 24,
|
|
296
|
+
self.ROW,
|
|
297
|
+
),
|
|
298
|
+
Qt.AlignVCenter,
|
|
299
|
+
spec.name,
|
|
300
|
+
)
|
|
301
|
+
for index, spec in enumerate(self.node.outputs):
|
|
302
|
+
painter.drawText(
|
|
303
|
+
QRectF(
|
|
304
|
+
self.WIDTH / 2,
|
|
305
|
+
self.HEADER + 5 + (index + 1) * self.ROW,
|
|
306
|
+
self.WIDTH / 2 - 12,
|
|
307
|
+
self.ROW,
|
|
308
|
+
),
|
|
309
|
+
Qt.AlignVCenter | Qt.AlignRight,
|
|
310
|
+
spec.name,
|
|
311
|
+
)
|
|
312
|
+
if self.node.disabled:
|
|
313
|
+
painter.fillRect(body.adjusted(1, 1, -1, -1), QColor(8, 10, 12, 175))
|
|
314
|
+
painter.setPen(QPen(QColor("#ef4d5a"), 2))
|
|
315
|
+
painter.drawLine(body.topLeft() + QPointF(5, 5), body.bottomRight() - QPointF(5, 5))
|
|
316
|
+
painter.drawLine(body.topRight() + QPointF(-5, 5), body.bottomLeft() + QPointF(5, -5))
|
|
317
|
+
painter.setPen(QColor("#ff6673"))
|
|
318
|
+
painter.drawText(body, Qt.AlignCenter, "DISABLED")
|
|
319
|
+
|
|
320
|
+
def itemChange(self, change, value):
|
|
321
|
+
if change == QGraphicsItem.ItemPositionHasChanged and self.scene():
|
|
322
|
+
self.graph_scene.node_moved(self)
|
|
323
|
+
return super().itemChange(change, value)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
class ConnectionItem(QGraphicsItem):
|
|
327
|
+
def __init__(self, graph_scene: "GraphScene", connection: "Connection" | None = None):
|
|
328
|
+
super().__init__()
|
|
329
|
+
self.graph_scene = graph_scene
|
|
330
|
+
self.connection = connection
|
|
331
|
+
self.start = QPointF()
|
|
332
|
+
self.end = QPointF()
|
|
333
|
+
self.path = QPainterPath()
|
|
334
|
+
self.setFlags(QGraphicsItem.ItemIsSelectable)
|
|
335
|
+
self.setZValue(1)
|
|
336
|
+
self.refresh()
|
|
337
|
+
|
|
338
|
+
def boundingRect(self) -> QRectF:
|
|
339
|
+
return self.path.boundingRect().adjusted(-8, -8, 8, 8)
|
|
340
|
+
|
|
341
|
+
def shape(self) -> QPainterPath:
|
|
342
|
+
from qtpy.QtGui import QPainterPathStroker
|
|
343
|
+
stroker = QPainterPathStroker()
|
|
344
|
+
stroker.setWidth(12)
|
|
345
|
+
return stroker.createStroke(self.path)
|
|
346
|
+
|
|
347
|
+
def set_points(self, start: QPointF, end: QPointF) -> None:
|
|
348
|
+
self.prepareGeometryChange()
|
|
349
|
+
self.start, self.end = start, end
|
|
350
|
+
self._update_path()
|
|
351
|
+
|
|
352
|
+
def refresh(self) -> None:
|
|
353
|
+
if self.connection and self.connection.source_node in self.graph_scene.node_items:
|
|
354
|
+
source = self.graph_scene.node_items[self.connection.source_node]
|
|
355
|
+
target = self.graph_scene.node_items[self.connection.target_node]
|
|
356
|
+
if self.connection.kind == "dependency":
|
|
357
|
+
source_port = source.dependency_output
|
|
358
|
+
target_port = target.dependency_input
|
|
359
|
+
else:
|
|
360
|
+
source_port = source.output_ports[self.connection.source_port]
|
|
361
|
+
target_port = target.input_ports[self.connection.target_port]
|
|
362
|
+
self.set_points(
|
|
363
|
+
source_port.scenePos(),
|
|
364
|
+
target_port.scenePos(),
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
def _update_path(self) -> None:
|
|
368
|
+
self.path = QPainterPath(self.start)
|
|
369
|
+
distance = max(70.0, abs(self.end.x() - self.start.x()) * 0.55)
|
|
370
|
+
direction = 1 if self.end.x() >= self.start.x() else -1
|
|
371
|
+
self.path.cubicTo(
|
|
372
|
+
self.start + QPointF(distance * direction, 0),
|
|
373
|
+
self.end - QPointF(distance * direction, 0),
|
|
374
|
+
self.end,
|
|
375
|
+
)
|
|
376
|
+
self.update()
|
|
377
|
+
|
|
378
|
+
def paint(self, painter: QPainter, option, widget=None) -> None:
|
|
379
|
+
if self.connection and self.connection.kind == "dependency":
|
|
380
|
+
color = QColor("#ffd166" if self.isSelected() else "#d4922f")
|
|
381
|
+
else:
|
|
382
|
+
color = QColor("#f2b04f" if self.isSelected() else "#4db8c6")
|
|
383
|
+
painter.setPen(QPen(color, 3 if self.isSelected() else 2, Qt.SolidLine, Qt.RoundCap))
|
|
384
|
+
painter.drawPath(self.path)
|
|
385
|
+
if self.connection and self.connection.kind == "dependency":
|
|
386
|
+
# Place the arrow inside the curve rather than at the port, where
|
|
387
|
+
# it remains readable even when several connections share a node.
|
|
388
|
+
before = self.path.pointAtPercent(0.54)
|
|
389
|
+
tip = self.path.pointAtPercent(0.58)
|
|
390
|
+
angle = math.atan2(tip.y() - before.y(), tip.x() - before.x())
|
|
391
|
+
direction = QPointF(math.cos(angle), math.sin(angle))
|
|
392
|
+
normal = QPointF(-direction.y(), direction.x())
|
|
393
|
+
arrow_length = 11.0
|
|
394
|
+
arrow_width = 6.0
|
|
395
|
+
base = tip - direction * arrow_length
|
|
396
|
+
arrow = QPolygonF([
|
|
397
|
+
tip,
|
|
398
|
+
base + normal * arrow_width,
|
|
399
|
+
base - normal * arrow_width,
|
|
400
|
+
])
|
|
401
|
+
painter.setPen(Qt.NoPen)
|
|
402
|
+
painter.setBrush(color)
|
|
403
|
+
painter.drawPolygon(arrow)
|