-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstructions.py
More file actions
224 lines (198 loc) · 8.96 KB
/
Copy pathinstructions.py
File metadata and controls
224 lines (198 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Instruction dialog components for RapNote."""
from __future__ import annotations
from typing import List, Optional
from PyQt6.QtWidgets import (
QCheckBox,
QDialog,
QDialogButtonBox,
QFormLayout,
QListWidget,
QListWidgetItem,
QLabel,
QLineEdit,
QMessageBox,
QPushButton,
QVBoxLayout,
QWidget,
)
from PyQt6.QtCore import Qt
from i18n import I18N
from model import InstructionTemplate, list_instruction_templates, remove_instruction_template
__all__ = ["InstructionDialog"]
class InstructionDialog(QDialog):
"""Dialog widget for editing block instructions."""
def __init__(
self,
section_label: str,
section_key: Optional[str],
initial_command: str,
initial_description: str,
i18n: I18N,
parent: Optional[QWidget] = None,
) -> None:
super().__init__(parent)
self.section_label = section_label
normalized_section = section_key.strip() if isinstance(section_key, str) else None
if normalized_section and normalized_section.lower() == "none":
normalized_section = None
self.section_key = normalized_section or None
self.i18n = i18n
self.command_value: str = initial_command
self.description_value: str = initial_description
self.save_to_library: bool = False
self.clear_requested: bool = False
self.template_list: Optional[QListWidget] = None
self.delete_template_button: Optional[QPushButton] = None
self.templates: List[InstructionTemplate] = []
self.setWindowTitle(self.i18n.t("INSTRUCTIONS_DIALOG_TITLE"))
self.resize(420, 220)
layout = QVBoxLayout(self)
info_label = QLabel(
self.i18n.t("INSTRUCTIONS_DIALOG_INFO").format(
section=section_label or self.i18n.t("SECTION_NONE")
),
self,
)
info_label.setWordWrap(True)
layout.addWidget(info_label)
form_layout = QFormLayout()
self.command_edit = QLineEdit(self)
self.command_edit.setPlaceholderText(self.i18n.t("INSTRUCTIONS_COMMAND_PLACEHOLDER"))
self.command_edit.setToolTip(self.i18n.t("INSTRUCTIONS_COMMAND_TIP"))
self.command_edit.setText(initial_command)
self.description_edit = QLineEdit(self)
self.description_edit.setPlaceholderText(self.i18n.t("INSTRUCTIONS_DESCRIPTION_PLACEHOLDER"))
self.description_edit.setToolTip(self.i18n.t("INSTRUCTIONS_DESCRIPTION_TIP"))
self.description_edit.setText(initial_description)
form_layout.addRow(self.i18n.t("INSTRUCTIONS_COMMAND_LABEL"), self.command_edit)
form_layout.addRow(self.i18n.t("INSTRUCTIONS_DESCRIPTION_LABEL"), self.description_edit)
layout.addLayout(form_layout)
self._build_template_list(layout)
self.save_checkbox = QCheckBox(self.i18n.t("INSTRUCTIONS_SAVE_CHECKBOX"), self)
self.save_checkbox.setToolTip(self.i18n.t("INSTRUCTIONS_SAVE_TIP"))
layout.addWidget(self.save_checkbox)
self.button_box = QDialogButtonBox(self)
self.apply_button = self.button_box.addButton(
self.i18n.t("INSTRUCTIONS_APPLY_BUTTON"),
QDialogButtonBox.ButtonRole.AcceptRole,
)
self.clear_button = self.button_box.addButton(
self.i18n.t("INSTRUCTIONS_CLEAR_BUTTON"),
QDialogButtonBox.ButtonRole.ResetRole,
)
self.cancel_button = self.button_box.addButton(
self.i18n.t("INSTRUCTIONS_CANCEL_BUTTON"),
QDialogButtonBox.ButtonRole.RejectRole,
)
self.apply_button.setDefault(True)
self.apply_button.setToolTip(self.i18n.t("INSTRUCTIONS_APPLY_TIP"))
self.clear_button.setToolTip(self.i18n.t("INSTRUCTIONS_CLEAR_TIP"))
self.cancel_button.setToolTip(self.i18n.t("INSTRUCTIONS_CANCEL_TIP"))
layout.addWidget(self.button_box)
self.apply_button.clicked.connect(self._on_apply)
self.clear_button.clicked.connect(self._on_clear)
self.cancel_button.clicked.connect(self.reject)
self.command_edit.textChanged.connect(self._update_apply_state)
self._update_apply_state()
def _build_template_list(self, layout: QVBoxLayout) -> None:
self.templates = list_instruction_templates(section=self.section_key)
if not self.templates:
empty_label = QLabel(self.i18n.t("INSTRUCTIONS_TEMPLATES_EMPTY"), self)
empty_label.setObjectName("SmallMuted")
layout.addWidget(empty_label)
return
caption = QLabel(self.i18n.t("INSTRUCTIONS_TEMPLATES_LABEL"), self)
caption.setWordWrap(True)
layout.addWidget(caption)
self.template_list = QListWidget(self)
self.template_list.setToolTip(self.i18n.t("INSTRUCTIONS_TEMPLATES_TIP"))
self.template_list.setSelectionMode(QListWidget.SelectionMode.SingleSelection)
for template in self.templates:
item_text = template.command
if template.description:
item_text = f"{item_text} ({template.description})"
if template.section and template.section != self.section_key:
item_text = f"{item_text} [{template.section}]"
item = QListWidgetItem(item_text, self.template_list)
item.setData(Qt.ItemDataRole.UserRole, template)
if (
template.command == self.command_value
and template.description == self.description_value
and (template.section or None) == self.section_key
):
item.setSelected(True)
self.template_list.itemSelectionChanged.connect(self._on_template_selected)
layout.addWidget(self.template_list)
self.delete_template_button = QPushButton(self.i18n.t("INSTRUCTIONS_DELETE_TEMPLATE_BUTTON"), self)
self.delete_template_button.setToolTip(self.i18n.t("INSTRUCTIONS_DELETE_TEMPLATE_TIP"))
self.delete_template_button.setEnabled(bool(self.template_list.selectedItems()))
self.delete_template_button.clicked.connect(self._on_delete_template)
layout.addWidget(self.delete_template_button)
def _on_template_selected(self) -> None:
if self.template_list is None:
return
items = self.template_list.selectedItems()
if not items:
if self.delete_template_button is not None:
self.delete_template_button.setEnabled(False)
return
template_data = items[0].data(Qt.ItemDataRole.UserRole)
if not isinstance(template_data, InstructionTemplate):
return
self.command_edit.setText(template_data.command)
self.description_edit.setText(template_data.description)
self.command_value = template_data.command
self.description_value = template_data.description
if self.delete_template_button is not None:
self.delete_template_button.setEnabled(True)
self._update_apply_state()
def _on_delete_template(self) -> None:
if self.template_list is None:
return
items = self.template_list.selectedItems()
if not items:
if self.delete_template_button is not None:
self.delete_template_button.setEnabled(False)
return
item = items[0]
template_data = item.data(Qt.ItemDataRole.UserRole)
if not isinstance(template_data, InstructionTemplate):
return
answer = QMessageBox.question(
self,
self.i18n.t("INSTRUCTIONS_DELETE_TEMPLATE_TITLE"),
self.i18n.t("INSTRUCTIONS_DELETE_TEMPLATE_Q").format(command=template_data.command),
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)
if answer != QMessageBox.StandardButton.Yes:
return
if remove_instruction_template(
command=template_data.command,
description=template_data.description,
section=template_data.section,
):
row = self.template_list.row(item)
self.template_list.takeItem(row)
if self.delete_template_button is not None:
self.delete_template_button.setEnabled(False)
def _update_apply_state(self) -> None:
self.apply_button.setEnabled(bool(self.command_edit.text().strip()))
def _on_apply(self) -> None:
command = self.command_edit.text().strip()
if not command:
self.command_edit.setFocus()
return
self.command_value = command
self.description_value = self.description_edit.text().strip()
self.save_to_library = self.save_checkbox.isChecked()
self.clear_requested = False
self.accept()
def _on_clear(self) -> None:
self.command_value = ""
self.description_value = ""
self.save_to_library = False
self.clear_requested = True
if self.template_list is not None:
self.template_list.clearSelection()
self.accept()