/
Flipper
/
Marginal
Обзор
Документация
Войти
/
Flipper
/
Marginal
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
src/UI/PasswordDialog.py
121 строка
4 KB
Alex
Marginal v1.0.0 RELEASE
11 янв 2026, 18:02
11 янв 2026, 18:02
5e1eff8
Код
Авторство
О чём код?
from PySide6.QtWidgets import ( QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QCheckBox ) from PySide6.QtGui import QIcon class PasswordDialog(QDialog): """ Reusable password input dialog with visibility toggle. Returns entered password on accept, None on cancel. """ def __init__(self, title: str, prompt: str, translateFunc): """ Initialize PasswordDialog with title, prompt, and translation function. Args: title (str): Dialog window title. prompt (str): Prompt text displayed above password field. translateFunc (callable): Translation function for UI text. """ super().__init__() self.setWindowTitle(title) self.prompt = prompt self.translate = translateFunc self.setModal(True) # Block interaction with parent window self.setFixedSize(300, 125) self.setWindowIcon(QIcon(":/resources/sprites/Icon_lock_small.png")) self.password = None # Stores password if dialog accepted self.setupUi() def setupUi(self): """ Set up dialog UI components and layout. Returns: None """ layout = QVBoxLayout(self) layout.setContentsMargins(10, 10, 10, 10) # Prompt label self.label = QLabel(self.prompt) # Password input field self.passwordInput = QLineEdit() self.passwordInput.setEchoMode(QLineEdit.Password) self.passwordInput.returnPressed.connect(self.onAccept) # Enter key submits # Password visibility toggle self.showPasswordCheck = QCheckBox(self.translate("Show password")) self.showPasswordCheck.stateChanged.connect(self.togglePasswordVisibility) # Button layout (Cancel and OK) buttonLayout = QHBoxLayout() self.cancelBtn = QPushButton(self.translate("Cancel")) self.okBtn = QPushButton(self.translate("OK")) self.cancelBtn.clicked.connect(self.onCancel) self.okBtn.clicked.connect(self.onAccept) buttonLayout.addStretch() # Push buttons to right buttonLayout.addWidget(self.cancelBtn) buttonLayout.addWidget(self.okBtn) # Assemble layout layout.addWidget(self.label) layout.addWidget(self.passwordInput) layout.addWidget(self.showPasswordCheck) layout.addLayout(buttonLayout) self.passwordInput.setFocus() # Start with focus in password field def togglePasswordVisibility(self, state: int): """ Toggle password field between visible and hidden text. Args: state (int): Checkbox state (0=unchecked, 2=checked). Returns: None """ if state: self.passwordInput.setEchoMode(QLineEdit.Normal) # Show password else: self.passwordInput.setEchoMode(QLineEdit.Password) # Hide password def onAccept(self): """ Handle dialog acceptance (OK button or Enter key). Stores password and closes dialog with accept status. Returns: None """ self.password = self.passwordInput.text() self.accept() def onCancel(self): """ Handle dialog cancellation (Cancel button). Clears password and closes dialog with reject status. Returns: None """ self.password = None self.close() def getPassword(self): """ Get the entered password after dialog closure. Returns: str | None -> Entered password if dialog accepted, None if cancelled. """ return self.password