-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfirmable_dialog.py
More file actions
185 lines (158 loc) · 6.7 KB
/
Copy pathconfirmable_dialog.py
File metadata and controls
185 lines (158 loc) · 6.7 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
"""Directory Operations Dialog - Modal dialog with click-to-confirm pattern."""
from dataclasses import dataclass
from typing import Callable
from PySide6.QtWidgets import (
QWidget,
QVBoxLayout,
QPushButton,
QLabel
)
from PySide6.QtCore import QTimer, Qt
from buttons_dialog import ButtonsDialog, ButtonSpec
@dataclass
class ConfirmableButtonSpec(ButtonSpec):
"""Specification for a confirmable dialog button."""
confirm: bool = False
class ConfirmableDialog(ButtonsDialog):
"""
Modal dialog for directory operations with click-to-confirm pattern.
Displays a list of operation buttons. For buttons with confirm=True:
- First click highlights the button in red
- Second click on the same button confirms and executes the operation
- Clicking a different button switches the highlight
For buttons with confirm=False: executes immediately like base class.
Cancel and Escape close immediately without confirmation.
"""
def __init__(
self,
parent: QWidget,
window_title: str,
processing_message: str,
button_specs: list[ConfirmableButtonSpec],
with_hotkeys: bool = True,
on_complete: Callable | None = None
):
"""
Args:
parent: The parent window (typically App instance)
window_title: Title for the dialog window
processing_message: Message to show while processing
button_specs: List of ConfirmableButtonSpec objects
on_complete: Callback invoked after a 'change' operation completes
"""
self._pending_button: QPushButton | None = None
# Store button specs as ConfirmableButtonSpec list
self._confirmable_button_specs = button_specs
# Initialize base class - cast to list[ButtonSpec] for type compatibility
super().__init__(
parent,
window_title,
list(button_specs), # type: ignore
processing_message,
with_hotkeys=with_hotkeys,
on_complete=on_complete
)
def _create_widgets(self) -> None:
"""Create all dialog widgets with confirmation support."""
# Main layout
layout = QVBoxLayout(self)
layout.setContentsMargins(10, 10, 10, 10)
layout.setSpacing(5)
# Operation buttons with confirm support
for index, spec in enumerate(self._confirmable_button_specs):
# Prepend hotkey label if enabled
if self._with_hotkeys and index < 9:
display_text = f"[{index + 1}] {spec.label}"
else:
display_text = spec.label
btn = QPushButton(display_text)
btn.setStyleSheet("text-align: left; padding: 0 20px;")
# Disable button if spec says so
if not spec.enabled:
btn.setEnabled(False)
else:
btn.clicked.connect(
lambda checked, b=btn, s=spec:
self._on_operation_click(b, s, s.confirm)
)
self._buttons.append((btn, spec))
layout.addWidget(btn)
# Status label (hidden initially, shown when needed)
self._status_label = QLabel("")
self._status_label.setStyleSheet("color: gray;")
self._status_label.hide()
layout.addWidget(self._status_label)
# Cancel button
self._add_cancel_button(layout)
def _on_operation_click(
self, button: QPushButton, spec: ConfirmableButtonSpec, must_confirm: bool = False
) -> None:
"""Handle operation button click with optional confirmation pattern.
If must_confirm is False: execute immediately.
If must_confirm is True:
First click: highlight button red, show confirmation message.
Second click on same button: execute callback and close dialog.
Click different button: switch highlight to new button.
"""
if not must_confirm:
# Execute immediately
self._status_label.setText(self._processing_message)
self._status_label.show()
QTimer.singleShot(100, lambda: self._execute_callback(spec.callable))
return
# Confirmation required - check if same button clicked again
if self._pending_button == button:
self._status_label.setText(self._processing_message)
self._status_label.show()
QTimer.singleShot(100, lambda: self._execute_callback(spec.callable))
return
# Reset previous pending button to default colors
if self._pending_button:
self._pending_button.setStyleSheet("")
# Set new pending button
self._pending_button = button
button.setStyleSheet("background-color: red;")
self._status_label.setText("Click the button again to confirm")
self._status_label.show()
def keyPressEvent(self, event):
"""Handle Escape key to close dialog and digit keys for button hotkeys.
For confirmable buttons: first hotkey press highlights, second executes.
For non-confirmable buttons: executes immediately.
Disabled buttons are skipped.
"""
if event.key() == Qt.Key.Key_Escape:
self._close()
elif self._with_hotkeys:
# Handle digit keys 1-9 for button hotkeys
digit = None
if event.key() == Qt.Key.Key_1:
digit = 1
elif event.key() == Qt.Key.Key_2:
digit = 2
elif event.key() == Qt.Key.Key_3:
digit = 3
elif event.key() == Qt.Key.Key_4:
digit = 4
elif event.key() == Qt.Key.Key_5:
digit = 5
elif event.key() == Qt.Key.Key_6:
digit = 6
elif event.key() == Qt.Key.Key_7:
digit = 7
elif event.key() == Qt.Key.Key_8:
digit = 8
elif event.key() == Qt.Key.Key_9:
digit = 9
if digit is not None and digit <= len(self._buttons):
# Get button and spec from the confirmable specs list
button, _ = self._buttons[digit - 1]
spec = self._confirmable_button_specs[digit - 1]
# Only trigger if button is enabled
if spec.enabled:
self._on_operation_click(button, spec, spec.confirm)
else:
super().keyPressEvent(event)
else:
super().keyPressEvent(event)
else:
super().keyPressEvent(event)