-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_dialog.py
More file actions
305 lines (252 loc) · 11.5 KB
/
Copy pathpassword_dialog.py
File metadata and controls
305 lines (252 loc) · 11.5 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
"""
Password Input Dialog for Archive Manager
"""
from PySide6.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QWidget
from PySide6.QtCore import Qt, Signal
from qfluentwidgets import (
PasswordLineEdit, PrimaryPushButton,
MessageBoxBase, SubtitleLabel, BodyLabel, setCustomStyleSheet, InfoBar, InfoBarPosition
)
from PySide6.QtGui import QFont
from con import CON
class PasswordDialog(MessageBoxBase):
"""Custom password input dialog using MessageBoxBase"""
def __init__(self, parent=None, title="Password Required", content="Please enter the password:", error_message=""):
super().__init__(parent)
self.password = ""
self.error_message = error_message
self.setup_ui(title, content)
#self.apply_custom_style()
def setup_ui(self, title, content):
"""Setup the dialog UI"""
self.setWindowTitle(title)
# Add title
self.titleLabel = SubtitleLabel(title)
self.viewLayout.addWidget(self.titleLabel)
# Add content text
self.contentLabel = BodyLabel(content)
self.viewLayout.addWidget(self.contentLabel)
# Add error message only if it's not empty
self.errorLabel = None
if self.error_message and self.error_message.strip():
self.errorLabel = BodyLabel(self.error_message)
self.errorLabel.setStyleSheet("color: red; font-weight: bold;")
self.viewLayout.addWidget(self.errorLabel)
# Add password input
self.password_line_edit = PasswordLineEdit()
self.password_line_edit.setPlaceholderText("Enter password...")
self.password_line_edit.setClearButtonEnabled(True)
self.viewLayout.addWidget(self.password_line_edit)
# Set the minimum width for the dialog
self.widget.setMinimumWidth(400)
# Connect signals
self.password_line_edit.returnPressed.connect(self.on_ok_clicked)
# Set focus to password input
self.password_line_edit.setFocus()
# Add OK and Cancel buttons to button layout
self.yesButton.setText("OK")
self.cancelButton.setText("Cancel")
def set_error_message(self, message):
"""Set or update the error message"""
self.error_message = message
# Remove existing error label if it exists
for i in reversed(range(self.viewLayout.count())):
item = self.viewLayout.itemAt(i)
if item and item.widget() and isinstance(item.widget(), QLabel) and hasattr(self, 'errorLabel') and item.widget() == self.errorLabel:
self.viewLayout.removeItem(item)
widget = item.widget()
if widget:
widget.deleteLater()
break
# Add new error label if message is not empty
if message:
self.errorLabel = BodyLabel(message)
self.errorLabel.setStyleSheet("color: red; font-weight: bold;")
# Insert after content label
for i in range(self.viewLayout.count()):
item = self.viewLayout.itemAt(i)
if item and item.widget() == self.contentLabel:
self.viewLayout.insertWidget(i + 1, self.errorLabel)
break
def get_password(self):
"""Get the entered password"""
return self.password
def exec(self):
"""Override exec to handle password input"""
result = super().exec()
if result == 1: # Accepted
self.password = self.password_line_edit.text()
if not self.password:
# If password is empty, treat as cancelled
return 0
return result
def on_ok_clicked(self):
"""Handle OK button click"""
self.password = self.password_line_edit.text()
if self.password:
self.accept()
else:
# Shake the dialog to indicate empty password
from PySide6.QtCore import QPropertyAnimation, QEasingCurve, QPoint
animation = QPropertyAnimation(self, b"pos")
animation.setDuration(100)
animation.setLoopCount(4)
animation.setEasingCurve(QEasingCurve.Type.InOutQuad)
original_pos = self.pos()
animation.setStartValue(original_pos)
animation.setEndValue(original_pos + QPoint(10, 0))
animation.start()
# Reset position after animation
animation.finished.connect(lambda: self.move(original_pos))
class SimplePasswordDialog(QDialog):
"""Simple password dialog as fallback if MessageBoxBase is not available"""
def __init__(self, parent=None, title="Password Required", content="Please enter the password:", error_message=""):
super().__init__(parent)
self.password = ""
self.error_message = error_message
self.setWindowTitle(title)
self.setModal(True)
self.setFixedSize(400, 220) # Increased height to accommodate error message
self.setup_ui(content)
#self.apply_custom_style()
def setup_ui(self, content):
"""Setup the dialog UI"""
layout = QVBoxLayout(self)
layout.setContentsMargins(30, 30, 30, 30)
layout.setSpacing(20)
# Add content text
self.content_label = QLabel(content)
self.content_label.setWordWrap(True)
layout.addWidget(self.content_label)
# Add error message only if it's not empty
self.error_label = None
if self.error_message and self.error_message.strip():
self.error_label = QLabel(self.error_message)
self.error_label.setWordWrap(True)
self.error_label.setStyleSheet("color: red; font-weight: bold;")
layout.addWidget(self.error_label)
# Add password input
self.password_line_edit = PasswordLineEdit()
self.password_line_edit.setPlaceholderText("Enter password...")
self.password_line_edit.setClearButtonEnabled(True)
layout.addWidget(self.password_line_edit)
# Add buttons
button_layout = QHBoxLayout()
self.ok_button = PrimaryPushButton("OK")
self.ok_button.clicked.connect(self.on_ok_clicked)
self.ok_button.setFixedWidth(100)
self.cancel_button = PrimaryPushButton("Cancel")
self.cancel_button.clicked.connect(self.reject)
self.cancel_button.setFixedWidth(100)
button_layout.addStretch()
button_layout.addWidget(self.ok_button)
button_layout.addWidget(self.cancel_button)
layout.addLayout(button_layout)
# Connect signals
self.password_line_edit.returnPressed.connect(self.on_ok_clicked)
# Set focus to password input
self.password_line_edit.setFocus()
def set_error_message(self, message):
"""Set or update the error message"""
self.error_message = message
# Remove existing error label if it exists
if hasattr(self, 'error_label') and self.error_label:
layout = self.layout()
if layout:
layout.removeWidget(self.error_label)
self.error_label.deleteLater()
self.error_label = None
# Add new error label if message is not empty
if message:
self.error_label = QLabel(message)
self.error_label.setWordWrap(True)
self.error_label.setStyleSheet("color: red; font-weight: bold;")
# Insert after content label
layout = self.layout()
if layout:
# Simply add the error label to the layout (will appear after content label)
layout.addWidget(self.error_label)
def get_password(self):
"""Get the entered password"""
return self.password
def exec(self):
"""Override exec to handle password input"""
result = super().exec()
if result == 1: # Accepted
self.password = self.password_line_edit.text()
if not self.password:
# If password is empty, treat as cancelled
return 0
return result
def on_ok_clicked(self):
"""Handle OK button click"""
self.password = self.password_line_edit.text()
if self.password:
self.accept()
else:
# Shake the dialog to indicate empty password
from PySide6.QtCore import QPropertyAnimation, QEasingCurve, QPoint
animation = QPropertyAnimation(self, b"pos")
animation.setDuration(100)
animation.setLoopCount(4)
animation.setEasingCurve(QEasingCurve.Type.InOutQuad)
original_pos = self.pos()
animation.setStartValue(original_pos)
animation.setEndValue(original_pos + QPoint(10, 0))
animation.start()
# Reset position after animation
animation.finished.connect(lambda: self.move(original_pos))
def get_password(parent=None, title="Password Required", content="Please enter the password:", error_message="", max_attempts=3):
"""
Function to get password from user with retry functionality
Args:
parent: Parent widget
title: Dialog title - always set to "Enter Password"
content: Dialog content message
error_message: Error message to display (ignored, always shows neutral message)
max_attempts: Maximum number of password attempts
Returns:
Password string if successful, None if cancelled or max attempts reached
"""
attempts = 0
# Always use "Enter Password" as title, regardless of what's passed
title = "Enter Password"
while attempts < max_attempts:
try:
# Always create a new dialog with neutral message, ignoring error_message
dialog = PasswordDialog(parent, title, content, "")
result = dialog.exec()
if result == 1: # Accepted
password = dialog.get_password()
if password: # Only return if password is not empty
return password
else:
# Empty password, treat as retry
attempts += 1
if attempts < max_attempts:
# Use neutral message instead of error message
content = "Please enter the password:"
else:
content = "Maximum password attempts reached."
else: # Cancelled
return None
except:
# Fallback to simple dialog
simple_dialog = SimplePasswordDialog(parent, title, content, "")
result = simple_dialog.exec()
if result == 1: # Accepted
password = simple_dialog.get_password()
if password: # Only return if password is not empty
return password
else:
# Empty password, treat as retry
attempts += 1
if attempts < max_attempts:
# Use neutral message instead of error message
content = "Please enter the password:"
else:
content = "Maximum password attempts reached."
else: # Cancelled
return None
return None