2 #include "../misc/dialogutils.h" 4 #include "ui_enterpassworddialog.h" 7 #include <QGraphicsPixmapItem> 8 #include <QGuiApplication> 10 #include <QMessageBox> 12 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION 13 #if defined(Q_OS_WIN32) 15 #elif defined(X_AVAILABLE) 16 #include <X11/XKBlib.h> 42 makeHeading(m_ui->instructionLabel);
43 setStyleSheet(dialogStyle());
47 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
48 installEventFilter(
this);
49 m_ui->userNameLineEdit->installEventFilter(
this);
50 m_ui->password1LineEdit->installEventFilter(
this);
51 m_ui->password2LineEdit->installEventFilter(
this);
53 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION 56 m_capslockPressed =
false;
58 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
60 QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr,
this);
61 QGraphicsScene *scene =
new QGraphicsScene();
62 QGraphicsPixmapItem *item =
new QGraphicsPixmapItem(icon.pixmap(16, 16));
64 m_ui->capslockWarningGraphicsView->setScene(scene);
66 connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
67 connect(m_ui->noPwCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
68 connect(m_ui->confirmPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::confirm);
69 connect(m_ui->abortPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::abort);
88 return m_ui->descLabel->text();
97 m_ui->descLabel->setText(description);
98 m_ui->descLabel->setHidden(description.isEmpty());
111 return !m_ui->userNameLineEdit->isHidden();
120 m_ui->userNameLineEdit->setHidden(!prompt);
132 return !m_ui->password2LineEdit->isHidden();
146 return m_ui->noPwCheckBox->isHidden();
160 m_ui->noPwCheckBox->setHidden(value);
161 m_ui->noPwCheckBox->setChecked(
false);
171 void EnterPasswordDialog::updateShowPassword()
173 m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked() ? QLineEdit::Normal : QLineEdit::Password);
174 m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
175 m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
186 if (m_instruction.isEmpty()) {
187 m_ui->instructionLabel->setText(value ? tr(
"Enter the new password") : tr(
"Enter the password"));
189 m_ui->password2LineEdit->setHidden(!value);
200 m_instruction = value;
201 if (m_instruction.isEmpty()) {
202 m_ui->instructionLabel->setText(
isVerificationRequired() ? tr(
"Enter the new password") : tr(
"Enter the password"));
204 m_ui->instructionLabel->setText(value);
211 switch (event->type()) {
212 case QEvent::KeyPress: {
213 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
214 if (keyEvent->key() == Qt::Key_CapsLock) {
215 m_capslockPressed = !m_capslockPressed;
217 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
222 return QDialog::event(event);
234 switch (event->type()) {
235 case QEvent::KeyPress: {
236 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
237 if (keyEvent->key() == Qt::Key_CapsLock) {
238 m_capslockPressed = !m_capslockPressed;
240 QString
text = keyEvent->text();
242 QChar firstChar = text.at(0);
243 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
244 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
245 m_capslockPressed =
true;
246 }
else if (firstChar.isLetter()) {
247 m_capslockPressed =
false;
251 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
253 case QEvent::FocusIn:
254 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
256 qobject_cast<QWidget *>(sender)->grabKeyboard();
259 case QEvent::FocusOut:
260 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
261 qobject_cast<QWidget *>(sender)->releaseKeyboard();
277 void EnterPasswordDialog::confirm()
281 done(QDialog::Accepted);
283 QString
userName = m_ui->userNameLineEdit->text();
284 QString
password = m_ui->password1LineEdit->text();
285 QString repeatedPassword = m_ui->password2LineEdit->text();
287 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a user name."));
288 }
else if (password.isEmpty()) {
289 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a password."));
291 if (
isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
292 if (repeatedPassword.isEmpty()) {
293 QMessageBox::warning(
this, windowTitle(), tr(
"You have to enter the new password twice to " 294 "ensure you enterd it correct."));
296 QMessageBox::warning(
this, windowTitle(), tr(
"You mistyped the password."));
301 done(QDialog::Accepted);
323 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION 325 #if defined(Q_OS_WIN32) 326 return GetKeyState(VK_CAPITAL) == 1;
327 #elif defined(X_AVAILABLE) 328 Display *d = XOpenDisplay((
char *)0);
329 bool caps_state =
false;
332 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
333 caps_state = (n & 0x01) == 1;
bool isPasswordRequired() const
The EnterPasswordDialog class provides a simple dialog to ask the user for a password.
void setPasswordRequired(bool value)
Sets whether the user is force to enter a password.
void setPromptForUserName(bool prompt)
Sets whethere the dialog prompts for a user name as well.
void setDescription(const QString &description=QString())
Sets the description.
~EnterPasswordDialog()
Destroys the password dialog.
void setInstruction(const QString &value)
Sets the instruction text.
bool event(QEvent *event)
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
const QString & password() const
void setVerificationRequired(bool value)
Sets whether a verification (password has to be entered twice) is required.
EnterPasswordDialog(QWidget *parent=nullptr)
Constructs a password dialog.
QString description() const
bool promtForUserName() const
bool eventFilter(QObject *sender, QEvent *event)
Internal method to notice when the capslock key is pressed by the user.
static bool isCapslockPressed()
const QString & userName() const
bool isVerificationRequired() const