|
1 | 1 | # code_editor_dialog.py |
2 | | -# A dedicated dialog window that now uses the advanced PythonCodeEditor widget. |
| 2 | +# A dedicated dialog window that now uses a QTabWidget to host |
| 3 | +# separate editors for execution code, GUI layout, and GUI logic. |
3 | 4 |
|
4 | | -from PySide6.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox |
| 5 | +from PySide6.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox, QTabWidget |
5 | 6 | from PySide6.QtGui import QFont |
6 | 7 | from python_code_editor import PythonCodeEditor |
7 | 8 |
|
| 9 | + |
8 | 10 | class CodeEditorDialog(QDialog): |
9 | 11 | """ |
10 | | - A dialog that hosts the advanced PythonCodeEditor widget. |
| 12 | + A dialog that hosts a tabbed interface for editing all of a node's code. |
11 | 13 | """ |
12 | | - def __init__(self, code, parent=None): |
| 14 | + |
| 15 | + def __init__(self, code, gui_code, gui_logic_code, parent=None): |
13 | 16 | super().__init__(parent) |
14 | | - self.setWindowTitle("Node Code Editor") |
15 | | - self.setMinimumSize(700, 500) |
| 17 | + self.setWindowTitle("Unified Code Editor") |
| 18 | + self.setMinimumSize(750, 600) |
16 | 19 |
|
17 | 20 | layout = QVBoxLayout(self) |
| 21 | + tab_widget = QTabWidget() |
| 22 | + layout.addWidget(tab_widget) |
18 | 23 |
|
19 | | - # --- Use the new advanced code editor --- |
| 24 | + # --- Execution Code Editor --- |
20 | 25 | self.code_editor = PythonCodeEditor() |
21 | 26 | self.code_editor.setFont(QFont("Monospace", 11)) |
22 | | - |
23 | | - # Set placeholder text if the initial code is empty |
24 | | - if not code: |
25 | | - code = ("from typing import Tuple\n\n" |
26 | | - "def process_data(input_1: str) -> Tuple[str, int]:\n" |
27 | | - " # Your code here\n" |
28 | | - " # Use parameters for inputs and return for outputs.\n" |
29 | | - " # Type hints define the pins.\n" |
30 | | - " return 'processed', len(input_1)") |
31 | | - |
32 | | - self.code_editor.setPlainText(code) |
33 | | - |
34 | | - layout.addWidget(self.code_editor) |
35 | | - |
36 | | - # --- OK and Cancel Buttons --- |
| 27 | + exec_placeholder = "from typing import Tuple\n\n" "@node_entry\n" "def node_function(input_1: str) -> Tuple[str, int]:\n" " return 'hello', len(input_1)" |
| 28 | + # Only add placeholder if the code is None (new node) |
| 29 | + self.code_editor.setPlainText(code if code is not None else exec_placeholder) |
| 30 | + tab_widget.addTab(self.code_editor, "Execution Code") |
| 31 | + |
| 32 | + # --- GUI Layout Code Editor --- |
| 33 | + self.gui_editor = PythonCodeEditor() |
| 34 | + self.gui_editor.setFont(QFont("Monospace", 11)) |
| 35 | + gui_placeholder = ( |
| 36 | + "# This script builds the node's custom GUI.\n" |
| 37 | + "# Use 'parent', 'layout', 'widgets', and 'QtWidgets' variables.\n\n" |
| 38 | + "label = QtWidgets.QLabel('Multiplier:', parent)\n" |
| 39 | + "spinbox = QtWidgets.QSpinBox(parent)\n" |
| 40 | + "spinbox.setValue(2)\n" |
| 41 | + "layout.addWidget(label)\n" |
| 42 | + "layout.addWidget(spinbox)\n" |
| 43 | + "widgets['multiplier'] = spinbox\n" |
| 44 | + ) |
| 45 | + self.gui_editor.setPlainText(gui_code if gui_code is not None else gui_placeholder) |
| 46 | + tab_widget.addTab(self.gui_editor, "GUI Layout") |
| 47 | + |
| 48 | + # --- GUI Logic Code Editor --- |
| 49 | + self.gui_logic_editor = PythonCodeEditor() |
| 50 | + self.gui_logic_editor.setFont(QFont("Monospace", 11)) |
| 51 | + gui_logic_placeholder = ( |
| 52 | + "# This script defines how the GUI interacts with the execution code.\n\n" |
| 53 | + "def get_values(widgets):\n" |
| 54 | + " return {'multiplier': widgets['multiplier'].value()}\n\n" |
| 55 | + "def set_values(widgets, outputs):\n" |
| 56 | + " # result = outputs.get('output_1', 'N/A')\n" |
| 57 | + " # widgets['result_label'].setText(f'Result: {result}')\n\n" |
| 58 | + "def set_initial_state(widgets, state):\n" |
| 59 | + " if 'multiplier' in state:\n" |
| 60 | + " widgets['multiplier'].setValue(state['multiplier'])\n" |
| 61 | + ) |
| 62 | + self.gui_logic_editor.setPlainText(gui_logic_code if gui_logic_code is not None else gui_logic_placeholder) |
| 63 | + tab_widget.addTab(self.gui_logic_editor, "GUI Logic") |
| 64 | + |
37 | 65 | button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) |
38 | 66 | button_box.accepted.connect(self.accept) |
39 | 67 | button_box.rejected.connect(self.reject) |
40 | 68 | layout.addWidget(button_box) |
41 | 69 |
|
42 | | - def get_code(self): |
43 | | - """Returns the code currently in the editor.""" |
44 | | - return self.code_editor.toPlainText() |
| 70 | + def get_results(self): |
| 71 | + """Returns the code from all three editors in a dictionary.""" |
| 72 | + return {"code": self.code_editor.toPlainText(), "gui_code": self.gui_editor.toPlainText(), "gui_logic_code": self.gui_logic_editor.toPlainText()} |
0 commit comments