2#include "../misc/dialogutils.h"
4#include "ui_enterpassworddialog.h"
8#include <QGraphicsPixmapItem>
9#include <QGraphicsScene>
15#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
16#if defined(Q_OS_WIN32)
18#elif defined(X_AVAILABLE)
19#include <X11/XKBlib.h>
45 makeHeading(m_ui->instructionLabel);
46 setStyleSheet(dialogStyleForPalette(palette()));
50 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
51 installEventFilter(
this);
52 m_ui->userNameLineEdit->installEventFilter(
this);
53 m_ui->password1LineEdit->installEventFilter(
this);
54 m_ui->password2LineEdit->installEventFilter(
this);
56#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
59 m_capslockPressed =
false;
61 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
63 QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr,
this);
64 QGraphicsScene *scene =
new QGraphicsScene();
65 QGraphicsPixmapItem *item =
new QGraphicsPixmapItem(icon.pixmap(16, 16));
67 m_ui->capslockWarningGraphicsView->setScene(scene);
69 connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
70 connect(m_ui->noPwCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
71 connect(m_ui->confirmPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::confirm);
72 connect(m_ui->abortPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::abort);
91 return m_ui->descLabel->text();
114 return !m_ui->userNameLineEdit->isHidden();
123 m_ui->userNameLineEdit->setHidden(!prompt);
135 return !m_ui->password2LineEdit->isHidden();
149 return m_ui->noPwCheckBox->isHidden();
163 m_ui->noPwCheckBox->setHidden(value);
164 m_ui->noPwCheckBox->setChecked(
false);
174void EnterPasswordDialog::updateShowPassword()
176 m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked() ? QLineEdit::Normal : QLineEdit::Password);
177 m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
178 m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
189 if (m_instruction.isEmpty()) {
190 m_ui->instructionLabel->setText(value ? tr(
"Enter the new password") : tr(
"Enter the password"));
192 m_ui->password2LineEdit->setHidden(!value);
203 m_instruction = value;
204 if (m_instruction.isEmpty()) {
205 m_ui->instructionLabel->setText(
isVerificationRequired() ? tr(
"Enter the new password") : tr(
"Enter the password"));
207 m_ui->instructionLabel->setText(value);
214 switch (
event->type()) {
215 case QEvent::PaletteChange:
216 setStyleSheet(dialogStyleForPalette(palette()));
218 case QEvent::KeyPress: {
219 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
220 if (keyEvent->key() == Qt::Key_CapsLock) {
221 m_capslockPressed = !m_capslockPressed;
223 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
228 return QDialog::event(
event);
240 switch (
event->type()) {
241 case QEvent::KeyPress: {
242 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
243 if (keyEvent->key() == Qt::Key_CapsLock) {
244 m_capslockPressed = !m_capslockPressed;
246 QString
text = keyEvent->text();
248 QChar firstChar =
text.at(0);
249 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
250 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
251 m_capslockPressed =
true;
252 }
else if (firstChar.isLetter()) {
253 m_capslockPressed =
false;
257 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
259 case QEvent::FocusIn:
260 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
262 qobject_cast<QWidget *>(sender)->grabKeyboard();
265 case QEvent::FocusOut:
266 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
267 qobject_cast<QWidget *>(sender)->releaseKeyboard();
283void EnterPasswordDialog::confirm()
287 done(QDialog::Accepted);
289 QString
userName = m_ui->userNameLineEdit->text();
290 QString
password = m_ui->password1LineEdit->text();
291 QString repeatedPassword = m_ui->password2LineEdit->text();
293 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a user name."));
295 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a password."));
298 if (repeatedPassword.isEmpty()) {
299 QMessageBox::warning(
this, windowTitle(),
300 tr(
"You have to enter the new password twice to "
301 "ensure you enterd it correct."));
303 QMessageBox::warning(
this, windowTitle(), tr(
"You mistyped the password."));
308 done(QDialog::Accepted);
330#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
332#if defined(Q_OS_WIN32)
333 return GetKeyState(VK_CAPITAL) == 1;
334#elif defined(X_AVAILABLE)
335 Display *d = XOpenDisplay((
char *)0);
336 bool caps_state =
false;
339 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
340 caps_state = (n & 0x01) == 1;
The EnterPasswordDialog class provides a simple dialog to ask the user for a password.
EnterPasswordDialog(QWidget *parent=nullptr)
Constructs a password dialog.
bool isCapslockPressed
Returns an indication whether the capslock key is pressed using platform specific functions.
bool isVerificationRequired
bool eventFilter(QObject *sender, QEvent *event) override
Internal method to notice when the capslock key is pressed by the user.
void setPromptForUserName(bool prompt)
Sets whethere the dialog prompts for a user name as well.
void setDescription(const QString &description=QString())
Sets the description.
void setVerificationRequired(bool value)
Sets whether a verification (password has to be entered twice) is required.
void setInstruction(const QString &value)
Sets the instruction text.
void setPasswordRequired(bool value)
Sets whether the user is force to enter a password.
~EnterPasswordDialog() override
Destroys the password dialog.
bool event(QEvent *event) override