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,578 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from threading import Event
|
|
5
|
+
|
|
6
|
+
from qtpy.QtCore import (
|
|
7
|
+
QByteArray, QMimeData, QObject, QPointF, QSettings, QThread, Qt, Signal, Slot,
|
|
8
|
+
)
|
|
9
|
+
from qtpy.QtGui import QAction, QActionGroup, QDrag, QKeySequence, QShortcut
|
|
10
|
+
from qtpy.QtWidgets import (
|
|
11
|
+
QAbstractItemView, QDockWidget, QFileDialog, QMainWindow, QMessageBox,
|
|
12
|
+
QPlainTextEdit, QTreeWidget, QTreeWidgetItem,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from taskgraph.core.executor import (
|
|
16
|
+
VALID_WORKER_COUNTS,
|
|
17
|
+
GraphExecutionCancelled,
|
|
18
|
+
GraphExecutionError,
|
|
19
|
+
execute_graph,
|
|
20
|
+
)
|
|
21
|
+
from taskgraph.core.model import Graph
|
|
22
|
+
from taskgraph.core.registry import (
|
|
23
|
+
create_node,
|
|
24
|
+
load_custom_node_directory,
|
|
25
|
+
nodes_by_category,
|
|
26
|
+
)
|
|
27
|
+
from taskgraph.core.serialization import load_graph, save_graph
|
|
28
|
+
from taskgraph.ui.plugins import load_gui_plugin_directory
|
|
29
|
+
from taskgraph.ui.properties import PropertyEditor
|
|
30
|
+
from taskgraph.ui.scene import GraphScene, GraphView
|
|
31
|
+
from taskgraph.ui.theme import APP_STYLESHEET
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class GraphExecutionWorker(QObject):
|
|
35
|
+
event = Signal(str)
|
|
36
|
+
node_state = Signal(str, str)
|
|
37
|
+
completed = Signal(object)
|
|
38
|
+
failed = Signal(str)
|
|
39
|
+
cancelled = Signal(str)
|
|
40
|
+
|
|
41
|
+
def __init__(self, graph, worker_count):
|
|
42
|
+
super().__init__()
|
|
43
|
+
self.graph = graph
|
|
44
|
+
self.worker_count = worker_count
|
|
45
|
+
self.cancel_event = Event()
|
|
46
|
+
|
|
47
|
+
def request_cancel(self) -> None:
|
|
48
|
+
self.cancel_event.set()
|
|
49
|
+
|
|
50
|
+
@Slot()
|
|
51
|
+
def run(self) -> None:
|
|
52
|
+
try:
|
|
53
|
+
result = execute_graph(
|
|
54
|
+
self.graph,
|
|
55
|
+
self.event.emit,
|
|
56
|
+
max_workers=self.worker_count,
|
|
57
|
+
on_node_state=self.node_state.emit,
|
|
58
|
+
cancel_event=self.cancel_event,
|
|
59
|
+
)
|
|
60
|
+
except GraphExecutionCancelled as exc:
|
|
61
|
+
self.cancelled.emit(str(exc))
|
|
62
|
+
except GraphExecutionError as exc:
|
|
63
|
+
self.failed.emit(str(exc))
|
|
64
|
+
except Exception as exc:
|
|
65
|
+
self.failed.emit(f"Unexpected execution error: {exc}")
|
|
66
|
+
else:
|
|
67
|
+
self.completed.emit(result)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class NodePalette(QTreeWidget):
|
|
71
|
+
def __init__(self, parent=None):
|
|
72
|
+
super().__init__(parent)
|
|
73
|
+
self.setHeaderHidden(True)
|
|
74
|
+
self.setDragEnabled(True)
|
|
75
|
+
self.setDragDropMode(QAbstractItemView.DragOnly)
|
|
76
|
+
self.reload()
|
|
77
|
+
|
|
78
|
+
def reload(self) -> None:
|
|
79
|
+
self.clear()
|
|
80
|
+
for category, classes in nodes_by_category().items():
|
|
81
|
+
parent = QTreeWidgetItem([category])
|
|
82
|
+
parent.setFlags(parent.flags() & ~Qt.ItemIsDragEnabled)
|
|
83
|
+
self.addTopLevelItem(parent)
|
|
84
|
+
for cls in classes:
|
|
85
|
+
child = QTreeWidgetItem([cls.title])
|
|
86
|
+
child.setData(0, Qt.UserRole, cls.type_id)
|
|
87
|
+
child.setToolTip(0, cls.type_id)
|
|
88
|
+
parent.addChild(child)
|
|
89
|
+
parent.setExpanded(True)
|
|
90
|
+
|
|
91
|
+
def startDrag(self, supported_actions) -> None:
|
|
92
|
+
item = self.currentItem()
|
|
93
|
+
type_id = item.data(0, Qt.UserRole) if item else None
|
|
94
|
+
if not type_id:
|
|
95
|
+
return
|
|
96
|
+
mime = QMimeData()
|
|
97
|
+
mime.setData("application/x-taskgraph-node", QByteArray(type_id.encode()))
|
|
98
|
+
drag = QDrag(self)
|
|
99
|
+
drag.setMimeData(mime)
|
|
100
|
+
drag.exec(Qt.CopyAction)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class MainWindow(QMainWindow):
|
|
104
|
+
def __init__(self):
|
|
105
|
+
super().__init__()
|
|
106
|
+
self.setWindowTitle("TaskGraph — Untitled")
|
|
107
|
+
self.resize(1400, 850)
|
|
108
|
+
self.setStyleSheet(APP_STYLESHEET)
|
|
109
|
+
self.current_path: Path | None = None
|
|
110
|
+
self.dirty = False
|
|
111
|
+
self.execution_thread: QThread | None = None
|
|
112
|
+
self.execution_worker: GraphExecutionWorker | None = None
|
|
113
|
+
self.plugin_actions: list[QAction] = []
|
|
114
|
+
self.plugin_docks: list[QDockWidget] = []
|
|
115
|
+
saved_workers = QSettings().value("execution/max_workers", 4, type=int)
|
|
116
|
+
self.worker_count = min(
|
|
117
|
+
VALID_WORKER_COUNTS,
|
|
118
|
+
key=lambda count: abs(count - saved_workers),
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
self.scene = GraphScene()
|
|
122
|
+
self.view = GraphView(self.scene)
|
|
123
|
+
self.setCentralWidget(self.view)
|
|
124
|
+
self.palette = NodePalette()
|
|
125
|
+
self.properties = PropertyEditor()
|
|
126
|
+
self.console = QPlainTextEdit()
|
|
127
|
+
self.console.setReadOnly(True)
|
|
128
|
+
self.console.setMaximumBlockCount(1000)
|
|
129
|
+
self.console.setContextMenuPolicy(Qt.CustomContextMenu)
|
|
130
|
+
|
|
131
|
+
self.docks = {
|
|
132
|
+
"Nodes": self._add_dock("Nodes", self.palette, Qt.LeftDockWidgetArea),
|
|
133
|
+
"Properties": self._add_dock("Properties", self.properties, Qt.RightDockWidgetArea),
|
|
134
|
+
"Execution": self._add_dock("Execution", self.console, Qt.BottomDockWidgetArea),
|
|
135
|
+
}
|
|
136
|
+
self._create_actions()
|
|
137
|
+
|
|
138
|
+
self.palette.itemDoubleClicked.connect(self._palette_double_clicked)
|
|
139
|
+
self.view.node_type_dropped.connect(self.add_node)
|
|
140
|
+
self.scene.node_selected.connect(self.properties.set_node)
|
|
141
|
+
self.scene.graph_changed.connect(self._mark_dirty)
|
|
142
|
+
self.properties.property_changed.connect(self._mark_dirty)
|
|
143
|
+
self.properties.property_changed.connect(self.scene.refresh_nodes)
|
|
144
|
+
self.statusBar().showMessage("Drag nodes onto the canvas. Connect output ports to input ports.")
|
|
145
|
+
|
|
146
|
+
def _add_dock(self, title, widget, area) -> QDockWidget:
|
|
147
|
+
dock = QDockWidget(title, self)
|
|
148
|
+
dock.setObjectName(f"{title.lower()}Dock")
|
|
149
|
+
dock.setWidget(widget)
|
|
150
|
+
self.addDockWidget(area, dock)
|
|
151
|
+
return dock
|
|
152
|
+
|
|
153
|
+
def _create_actions(self) -> None:
|
|
154
|
+
self.actions: dict[str, QAction] = {}
|
|
155
|
+
|
|
156
|
+
def make_action(key, text, shortcut, callback):
|
|
157
|
+
action = QAction(text, self)
|
|
158
|
+
if shortcut:
|
|
159
|
+
action.setShortcut(shortcut)
|
|
160
|
+
action.triggered.connect(callback)
|
|
161
|
+
self.addAction(action)
|
|
162
|
+
self.actions[key] = action
|
|
163
|
+
return action
|
|
164
|
+
|
|
165
|
+
make_action("new", "&New", QKeySequence.New, self.new_graph)
|
|
166
|
+
make_action("open", "&Open…", QKeySequence.Open, self.open_graph)
|
|
167
|
+
make_action("save", "&Save", QKeySequence.Save, self.save_graph)
|
|
168
|
+
make_action("save_as", "Save &As…", QKeySequence.SaveAs, self.save_graph_as)
|
|
169
|
+
make_action("quit", "&Quit TaskGraph", QKeySequence.Quit, self.close)
|
|
170
|
+
make_action("run", "&Run Graph", "Ctrl+R", self.run_graph)
|
|
171
|
+
cancel = make_action(
|
|
172
|
+
"cancel", "&Cancel Execution", QKeySequence(Qt.Key_Escape),
|
|
173
|
+
self.cancel_execution,
|
|
174
|
+
)
|
|
175
|
+
cancel.setEnabled(False)
|
|
176
|
+
make_action(
|
|
177
|
+
"clear_log", "Clear Execution &Log", None, self.console.clear,
|
|
178
|
+
)
|
|
179
|
+
make_action(
|
|
180
|
+
"add_node_location", "Add Custom Node &Location…", None,
|
|
181
|
+
self.add_custom_node_location,
|
|
182
|
+
)
|
|
183
|
+
make_action(
|
|
184
|
+
"add_gui_plugin_location", "Add GUI Plugin &Location…", None,
|
|
185
|
+
self.add_gui_plugin_location,
|
|
186
|
+
)
|
|
187
|
+
make_action("frame", "&Frame All", "F", self.frame_all)
|
|
188
|
+
auto_arrange = make_action(
|
|
189
|
+
"auto_arrange", "&Auto Arrange Nodes", "Shift+A",
|
|
190
|
+
self.auto_arrange_nodes,
|
|
191
|
+
)
|
|
192
|
+
# A single-letter shortcut belongs to the canvas, not property text
|
|
193
|
+
# fields where Shift+A should continue to type an uppercase letter.
|
|
194
|
+
self.removeAction(auto_arrange)
|
|
195
|
+
auto_arrange.setShortcutContext(Qt.WidgetWithChildrenShortcut)
|
|
196
|
+
self.view.addAction(auto_arrange)
|
|
197
|
+
make_action(
|
|
198
|
+
"add_backdrop", "Add &Backdrop", None, self.add_backdrop,
|
|
199
|
+
)
|
|
200
|
+
make_action(
|
|
201
|
+
"toggle_disabled", "Toggle &Disabled", None,
|
|
202
|
+
self.scene.toggle_selected_disabled,
|
|
203
|
+
)
|
|
204
|
+
copy_nodes = QAction("&Copy Nodes", self.view)
|
|
205
|
+
copy_nodes.setShortcut(QKeySequence.Copy)
|
|
206
|
+
copy_nodes.setShortcutContext(Qt.WidgetWithChildrenShortcut)
|
|
207
|
+
copy_nodes.triggered.connect(self.scene.copy_selected)
|
|
208
|
+
self.view.addAction(copy_nodes)
|
|
209
|
+
self.actions["copy"] = copy_nodes
|
|
210
|
+
paste_nodes = QAction("&Paste Nodes", self.view)
|
|
211
|
+
paste_nodes.setShortcut(QKeySequence.Paste)
|
|
212
|
+
paste_nodes.setShortcutContext(Qt.WidgetWithChildrenShortcut)
|
|
213
|
+
paste_nodes.triggered.connect(self.scene.paste_clipboard)
|
|
214
|
+
self.view.addAction(paste_nodes)
|
|
215
|
+
self.actions["paste"] = paste_nodes
|
|
216
|
+
|
|
217
|
+
delete = make_action("delete", "Delete Selected", None, self.scene.delete_selected)
|
|
218
|
+
# macOS keyboards normally expose Backspace as the key labelled
|
|
219
|
+
# "delete", while Windows/Linux commonly use forward Delete.
|
|
220
|
+
delete.setShortcuts([
|
|
221
|
+
QKeySequence(Qt.Key_Backspace),
|
|
222
|
+
QKeySequence(Qt.Key_Delete),
|
|
223
|
+
])
|
|
224
|
+
delete.setShortcutContext(Qt.ApplicationShortcut)
|
|
225
|
+
|
|
226
|
+
self.menus = {}
|
|
227
|
+
file_menu = self.menuBar().addMenu("&File")
|
|
228
|
+
self.menus["file"] = file_menu
|
|
229
|
+
file_menu.addActions([
|
|
230
|
+
self.actions["new"], self.actions["open"],
|
|
231
|
+
])
|
|
232
|
+
file_menu.addSeparator()
|
|
233
|
+
file_menu.addActions([
|
|
234
|
+
self.actions["save"], self.actions["save_as"],
|
|
235
|
+
])
|
|
236
|
+
file_menu.addSeparator()
|
|
237
|
+
file_menu.addAction(self.actions["quit"])
|
|
238
|
+
|
|
239
|
+
edit_menu = self.menuBar().addMenu("&Edit")
|
|
240
|
+
self.menus["edit"] = edit_menu
|
|
241
|
+
edit_menu.addActions([self.actions["copy"], self.actions["paste"]])
|
|
242
|
+
edit_menu.addSeparator()
|
|
243
|
+
edit_menu.addAction(self.actions["delete"])
|
|
244
|
+
edit_menu.addAction(self.actions["toggle_disabled"])
|
|
245
|
+
|
|
246
|
+
# Scope the single-letter shortcut to the canvas so typing in the
|
|
247
|
+
# property editor can never disable a node accidentally.
|
|
248
|
+
self.disable_shortcut = QShortcut(QKeySequence("D"), self.view)
|
|
249
|
+
self.disable_shortcut.setContext(Qt.WidgetWithChildrenShortcut)
|
|
250
|
+
self.disable_shortcut.activated.connect(self.scene.toggle_selected_disabled)
|
|
251
|
+
|
|
252
|
+
nodes_menu = self.menuBar().addMenu("&Nodes")
|
|
253
|
+
self.menus["nodes"] = nodes_menu
|
|
254
|
+
nodes_menu.addAction(self.actions["add_node_location"])
|
|
255
|
+
|
|
256
|
+
plugins_menu = self.menuBar().addMenu("&Plugins")
|
|
257
|
+
self.menus["plugins"] = plugins_menu
|
|
258
|
+
plugins_menu.addAction(self.actions["add_gui_plugin_location"])
|
|
259
|
+
|
|
260
|
+
graph_menu = self.menuBar().addMenu("&Graph")
|
|
261
|
+
self.menus["graph"] = graph_menu
|
|
262
|
+
graph_menu.addAction(self.actions["run"])
|
|
263
|
+
graph_menu.addAction(self.actions["cancel"])
|
|
264
|
+
graph_menu.addAction(self.actions["clear_log"])
|
|
265
|
+
workers_menu = graph_menu.addMenu("&Worker Count")
|
|
266
|
+
self.menus["workers"] = workers_menu
|
|
267
|
+
self.worker_actions = QActionGroup(self)
|
|
268
|
+
self.worker_actions.setExclusive(True)
|
|
269
|
+
for count in VALID_WORKER_COUNTS:
|
|
270
|
+
label = f"{count} Worker" if count == 1 else f"{count} Workers"
|
|
271
|
+
action = QAction(label, self, checkable=True)
|
|
272
|
+
action.setChecked(count == self.worker_count)
|
|
273
|
+
action.triggered.connect(
|
|
274
|
+
lambda checked=False, value=count: self.set_worker_count(value)
|
|
275
|
+
)
|
|
276
|
+
self.worker_actions.addAction(action)
|
|
277
|
+
workers_menu.addAction(action)
|
|
278
|
+
graph_menu.addSeparator()
|
|
279
|
+
graph_menu.addAction(self.actions["add_backdrop"])
|
|
280
|
+
graph_menu.addAction(self.actions["auto_arrange"])
|
|
281
|
+
graph_menu.addAction(self.actions["frame"])
|
|
282
|
+
|
|
283
|
+
view_menu = self.menuBar().addMenu("&View")
|
|
284
|
+
self.menus["view"] = view_menu
|
|
285
|
+
for dock in self.docks.values():
|
|
286
|
+
# Qt keeps this check state synchronized when a dock is closed,
|
|
287
|
+
# hidden, floated, or restored.
|
|
288
|
+
view_menu.addAction(dock.toggleViewAction())
|
|
289
|
+
view_menu.addSeparator()
|
|
290
|
+
show_all = QAction("Show All Panels", self)
|
|
291
|
+
self.actions["show_all_panels"] = show_all
|
|
292
|
+
show_all.triggered.connect(self.show_all_panels)
|
|
293
|
+
view_menu.addAction(show_all)
|
|
294
|
+
self.console.customContextMenuRequested.connect(
|
|
295
|
+
self._show_console_context_menu
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
def menu_for_plugin(self, name: str):
|
|
299
|
+
key = self._plugin_menu_key(name)
|
|
300
|
+
if key in self.menus:
|
|
301
|
+
return self.menus[key]
|
|
302
|
+
menu = self.menuBar().addMenu(name)
|
|
303
|
+
self.menus[key] = menu
|
|
304
|
+
return menu
|
|
305
|
+
|
|
306
|
+
def add_plugin_dock(
|
|
307
|
+
self,
|
|
308
|
+
title: str,
|
|
309
|
+
widget,
|
|
310
|
+
area: Qt.DockWidgetArea = Qt.RightDockWidgetArea,
|
|
311
|
+
) -> QDockWidget:
|
|
312
|
+
dock = self._add_dock(title, widget, area)
|
|
313
|
+
self.plugin_docks.append(dock)
|
|
314
|
+
self.menus["view"].insertAction(
|
|
315
|
+
self.actions["show_all_panels"],
|
|
316
|
+
dock.toggleViewAction(),
|
|
317
|
+
)
|
|
318
|
+
return dock
|
|
319
|
+
|
|
320
|
+
@staticmethod
|
|
321
|
+
def _plugin_menu_key(name: str) -> str:
|
|
322
|
+
return name.replace("&", "").strip().lower()
|
|
323
|
+
|
|
324
|
+
def _show_console_context_menu(self, position) -> None:
|
|
325
|
+
menu = self.console.createStandardContextMenu()
|
|
326
|
+
menu.addSeparator()
|
|
327
|
+
menu.addAction(self.actions["clear_log"])
|
|
328
|
+
menu.exec(self.console.viewport().mapToGlobal(position))
|
|
329
|
+
menu.deleteLater()
|
|
330
|
+
|
|
331
|
+
def show_all_panels(self) -> None:
|
|
332
|
+
for dock in self.docks.values():
|
|
333
|
+
dock.show()
|
|
334
|
+
dock.raise_()
|
|
335
|
+
|
|
336
|
+
def add_custom_node_location(self) -> None:
|
|
337
|
+
directory = QFileDialog.getExistingDirectory(
|
|
338
|
+
self, "Add Custom Node Location"
|
|
339
|
+
)
|
|
340
|
+
if not directory:
|
|
341
|
+
return
|
|
342
|
+
try:
|
|
343
|
+
loaded = load_custom_node_directory(directory)
|
|
344
|
+
except Exception as exc:
|
|
345
|
+
QMessageBox.critical(
|
|
346
|
+
self, "Custom node loading failed", f"{directory}\n\n{exc}"
|
|
347
|
+
)
|
|
348
|
+
return
|
|
349
|
+
settings = QSettings()
|
|
350
|
+
locations = settings.value("nodes/custom_locations", [])
|
|
351
|
+
if isinstance(locations, str):
|
|
352
|
+
locations = [locations]
|
|
353
|
+
if directory not in locations:
|
|
354
|
+
locations.append(directory)
|
|
355
|
+
settings.setValue("nodes/custom_locations", locations)
|
|
356
|
+
self.palette.reload()
|
|
357
|
+
self.statusBar().showMessage(
|
|
358
|
+
f"Loaded {len(loaded)} custom node module(s) from {directory}",
|
|
359
|
+
6000,
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
def add_gui_plugin_location(self) -> None:
|
|
363
|
+
directory = QFileDialog.getExistingDirectory(
|
|
364
|
+
self, "Add GUI Plugin Location"
|
|
365
|
+
)
|
|
366
|
+
if not directory:
|
|
367
|
+
return
|
|
368
|
+
try:
|
|
369
|
+
loaded = load_gui_plugin_directory(directory, self)
|
|
370
|
+
except Exception as exc:
|
|
371
|
+
QMessageBox.critical(
|
|
372
|
+
self, "GUI plugin loading failed", f"{directory}\n\n{exc}"
|
|
373
|
+
)
|
|
374
|
+
return
|
|
375
|
+
settings = QSettings()
|
|
376
|
+
locations = settings.value("plugins/custom_locations", [])
|
|
377
|
+
if isinstance(locations, str):
|
|
378
|
+
locations = [locations]
|
|
379
|
+
if directory not in locations:
|
|
380
|
+
locations.append(directory)
|
|
381
|
+
settings.setValue("plugins/custom_locations", locations)
|
|
382
|
+
self.palette.reload()
|
|
383
|
+
self.statusBar().showMessage(
|
|
384
|
+
f"Loaded {len(loaded)} GUI plugin module(s) from {directory}",
|
|
385
|
+
6000,
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
def set_worker_count(self, count: int) -> None:
|
|
389
|
+
self.worker_count = min(
|
|
390
|
+
VALID_WORKER_COUNTS,
|
|
391
|
+
key=lambda valid: abs(valid - count),
|
|
392
|
+
)
|
|
393
|
+
QSettings().setValue("execution/max_workers", self.worker_count)
|
|
394
|
+
self.statusBar().showMessage(
|
|
395
|
+
f"Graph execution will use {self.worker_count} worker(s)", 4000
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
def _palette_double_clicked(self, item, column) -> None:
|
|
399
|
+
type_id = item.data(0, Qt.UserRole)
|
|
400
|
+
if type_id:
|
|
401
|
+
center = self.view.mapToScene(self.view.viewport().rect().center())
|
|
402
|
+
self.add_node(type_id, center)
|
|
403
|
+
|
|
404
|
+
def add_node(self, type_id: str, position: QPointF) -> None:
|
|
405
|
+
try:
|
|
406
|
+
self.scene.add_process_node(create_node(type_id), position)
|
|
407
|
+
except Exception as exc:
|
|
408
|
+
QMessageBox.warning(self, "Could not add node", str(exc))
|
|
409
|
+
|
|
410
|
+
def add_backdrop(self) -> None:
|
|
411
|
+
position = self.view.mapToScene(self.view.viewport().rect().center())
|
|
412
|
+
self.scene.add_backdrop(position - QPointF(240, 150))
|
|
413
|
+
|
|
414
|
+
def _mark_dirty(self) -> None:
|
|
415
|
+
self.dirty = True
|
|
416
|
+
self._update_title()
|
|
417
|
+
|
|
418
|
+
def _update_title(self) -> None:
|
|
419
|
+
name = self.current_path.name if self.current_path else "Untitled"
|
|
420
|
+
self.setWindowTitle(f"TaskGraph — {name}{' *' if self.dirty else ''}")
|
|
421
|
+
|
|
422
|
+
def _confirm_discard(self) -> bool:
|
|
423
|
+
if not self.dirty:
|
|
424
|
+
return True
|
|
425
|
+
answer = QMessageBox.question(
|
|
426
|
+
self, "Unsaved graph", "Discard unsaved changes?",
|
|
427
|
+
QMessageBox.Yes | QMessageBox.No, QMessageBox.No,
|
|
428
|
+
)
|
|
429
|
+
return answer == QMessageBox.Yes
|
|
430
|
+
|
|
431
|
+
def new_graph(self) -> None:
|
|
432
|
+
if not self._confirm_discard():
|
|
433
|
+
return
|
|
434
|
+
self.scene.set_graph(Graph())
|
|
435
|
+
self.current_path = None
|
|
436
|
+
self.dirty = False
|
|
437
|
+
self.console.clear()
|
|
438
|
+
self._update_title()
|
|
439
|
+
|
|
440
|
+
def open_graph(self) -> None:
|
|
441
|
+
if not self._confirm_discard():
|
|
442
|
+
return
|
|
443
|
+
filename, _ = QFileDialog.getOpenFileName(self, "Open graph", "", "TaskGraph (*.taskgraph *.json)")
|
|
444
|
+
if not filename:
|
|
445
|
+
return
|
|
446
|
+
try:
|
|
447
|
+
self.scene.set_graph(load_graph(filename))
|
|
448
|
+
except Exception as exc:
|
|
449
|
+
QMessageBox.critical(self, "Open failed", str(exc))
|
|
450
|
+
return
|
|
451
|
+
self.current_path = Path(filename)
|
|
452
|
+
self.dirty = False
|
|
453
|
+
self._update_title()
|
|
454
|
+
self.frame_all()
|
|
455
|
+
|
|
456
|
+
def save_graph(self) -> bool:
|
|
457
|
+
if not self.current_path:
|
|
458
|
+
return self.save_graph_as()
|
|
459
|
+
try:
|
|
460
|
+
save_graph(self.scene.graph, self.current_path)
|
|
461
|
+
except Exception as exc:
|
|
462
|
+
QMessageBox.critical(self, "Save failed", str(exc))
|
|
463
|
+
return False
|
|
464
|
+
self.dirty = False
|
|
465
|
+
self._update_title()
|
|
466
|
+
self.statusBar().showMessage(f"Saved {self.current_path}", 4000)
|
|
467
|
+
return True
|
|
468
|
+
|
|
469
|
+
def save_graph_as(self) -> bool:
|
|
470
|
+
filename, _ = QFileDialog.getSaveFileName(self, "Save graph", "", "TaskGraph (*.taskgraph)")
|
|
471
|
+
if not filename:
|
|
472
|
+
return False
|
|
473
|
+
path = Path(filename)
|
|
474
|
+
if not path.suffix:
|
|
475
|
+
path = path.with_suffix(".taskgraph")
|
|
476
|
+
self.current_path = path
|
|
477
|
+
return self.save_graph()
|
|
478
|
+
|
|
479
|
+
def run_graph(self) -> None:
|
|
480
|
+
if self.execution_thread and self.execution_thread.isRunning():
|
|
481
|
+
return
|
|
482
|
+
self.console.clear()
|
|
483
|
+
self.console.appendPlainText("Running graph…")
|
|
484
|
+
self.scene.reset_execution_states()
|
|
485
|
+
self.actions["run"].setEnabled(False)
|
|
486
|
+
self.actions["cancel"].setEnabled(True)
|
|
487
|
+
self.view.setInteractive(False)
|
|
488
|
+
self.palette.setEnabled(False)
|
|
489
|
+
self.properties.setEnabled(False)
|
|
490
|
+
for key in (
|
|
491
|
+
"new", "open", "paste", "delete", "toggle_disabled",
|
|
492
|
+
"auto_arrange", "add_backdrop",
|
|
493
|
+
):
|
|
494
|
+
self.actions[key].setEnabled(False)
|
|
495
|
+
|
|
496
|
+
thread = QThread(self)
|
|
497
|
+
worker = GraphExecutionWorker(self.scene.graph, self.worker_count)
|
|
498
|
+
worker.moveToThread(thread)
|
|
499
|
+
thread.started.connect(worker.run)
|
|
500
|
+
worker.event.connect(self.console.appendPlainText)
|
|
501
|
+
worker.node_state.connect(self._set_node_execution_state)
|
|
502
|
+
worker.completed.connect(self._execution_completed)
|
|
503
|
+
worker.failed.connect(self._execution_failed)
|
|
504
|
+
worker.cancelled.connect(self._execution_cancelled)
|
|
505
|
+
worker.completed.connect(thread.quit)
|
|
506
|
+
worker.failed.connect(thread.quit)
|
|
507
|
+
worker.cancelled.connect(thread.quit)
|
|
508
|
+
worker.completed.connect(worker.deleteLater)
|
|
509
|
+
worker.failed.connect(worker.deleteLater)
|
|
510
|
+
worker.cancelled.connect(worker.deleteLater)
|
|
511
|
+
thread.finished.connect(self._execution_thread_finished)
|
|
512
|
+
self.execution_thread = thread
|
|
513
|
+
self.execution_worker = worker
|
|
514
|
+
self.statusBar().showMessage(
|
|
515
|
+
f"Running graph with {self.worker_count} worker(s)…"
|
|
516
|
+
)
|
|
517
|
+
thread.start()
|
|
518
|
+
|
|
519
|
+
def _execution_completed(self, result) -> None:
|
|
520
|
+
self.console.appendPlainText(f"\nCompleted {len(result.order)} node(s).")
|
|
521
|
+
self.statusBar().showMessage("Graph execution completed", 5000)
|
|
522
|
+
|
|
523
|
+
def _execution_failed(self, message: str) -> None:
|
|
524
|
+
self.console.appendPlainText(f"\nERROR: {message}")
|
|
525
|
+
self.statusBar().showMessage("Graph execution failed", 5000)
|
|
526
|
+
|
|
527
|
+
def _execution_cancelled(self, message: str) -> None:
|
|
528
|
+
self.console.appendPlainText(f"\nCANCELLED: {message}")
|
|
529
|
+
self.statusBar().showMessage("Graph execution cancelled", 5000)
|
|
530
|
+
|
|
531
|
+
def cancel_execution(self) -> None:
|
|
532
|
+
if not self.execution_worker:
|
|
533
|
+
return
|
|
534
|
+
self.actions["cancel"].setEnabled(False)
|
|
535
|
+
self.execution_worker.request_cancel()
|
|
536
|
+
self.statusBar().showMessage("Cancelling all running processes…")
|
|
537
|
+
|
|
538
|
+
def _execution_thread_finished(self) -> None:
|
|
539
|
+
if self.execution_thread:
|
|
540
|
+
self.execution_thread.deleteLater()
|
|
541
|
+
self.execution_thread = None
|
|
542
|
+
self.execution_worker = None
|
|
543
|
+
self.actions["run"].setEnabled(True)
|
|
544
|
+
self.actions["cancel"].setEnabled(False)
|
|
545
|
+
self.view.setInteractive(True)
|
|
546
|
+
self.palette.setEnabled(True)
|
|
547
|
+
self.properties.setEnabled(True)
|
|
548
|
+
for key in (
|
|
549
|
+
"new", "open", "paste", "delete", "toggle_disabled",
|
|
550
|
+
"auto_arrange", "add_backdrop",
|
|
551
|
+
):
|
|
552
|
+
self.actions[key].setEnabled(True)
|
|
553
|
+
|
|
554
|
+
def _set_node_execution_state(self, node_id: str, state: str) -> None:
|
|
555
|
+
self.scene.set_execution_state(node_id, state)
|
|
556
|
+
if state == "running":
|
|
557
|
+
# Render immediately so even the final node's running state reaches
|
|
558
|
+
# the viewport before a very fast completion signal is handled.
|
|
559
|
+
self.view.viewport().repaint()
|
|
560
|
+
|
|
561
|
+
def frame_all(self) -> None:
|
|
562
|
+
rect = self.scene.itemsBoundingRect()
|
|
563
|
+
if rect.isValid() and not rect.isEmpty():
|
|
564
|
+
self.view.fitInView(rect.adjusted(-80, -80, 80, 80), Qt.KeepAspectRatio)
|
|
565
|
+
|
|
566
|
+
def auto_arrange_nodes(self) -> None:
|
|
567
|
+
self.scene.auto_arrange()
|
|
568
|
+
self.frame_all()
|
|
569
|
+
self.statusBar().showMessage("Nodes arranged by dependency order", 4000)
|
|
570
|
+
|
|
571
|
+
def closeEvent(self, event) -> None:
|
|
572
|
+
if self.execution_thread and self.execution_thread.isRunning():
|
|
573
|
+
self.statusBar().showMessage(
|
|
574
|
+
"Wait for graph execution to finish before closing", 5000
|
|
575
|
+
)
|
|
576
|
+
event.ignore()
|
|
577
|
+
return
|
|
578
|
+
event.accept() if self._confirm_discard() else event.ignore()
|