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);
226 case QEvent::LanguageChange:
227 m_ui->retranslateUi(
this);
231 return QDialog::event(
event);
243 switch (
event->type()) {
244 case QEvent::KeyPress: {
245 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
246 if (keyEvent->key() == Qt::Key_CapsLock) {
247 m_capslockPressed = !m_capslockPressed;
249 QString
text = keyEvent->text();
251 QChar firstChar =
text.at(0);
252 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
253 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
254 m_capslockPressed =
true;
255 }
else if (firstChar.isLetter()) {
256 m_capslockPressed =
false;
260 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
262 case QEvent::FocusIn:
263 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
265 qobject_cast<QWidget *>(sender)->grabKeyboard();
268 case QEvent::FocusOut:
269 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
270 qobject_cast<QWidget *>(sender)->releaseKeyboard();
286void EnterPasswordDialog::confirm()
290 done(QDialog::Accepted);
292 QString
userName = m_ui->userNameLineEdit->text();
293 QString
password = m_ui->password1LineEdit->text();
294 QString repeatedPassword = m_ui->password2LineEdit->text();
296 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a user name."));
298 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a password."));
301 if (repeatedPassword.isEmpty()) {
302 QMessageBox::warning(
this, windowTitle(),
303 tr(
"You have to enter the new password twice to "
304 "ensure you enterd it correct."));
306 QMessageBox::warning(
this, windowTitle(), tr(
"You mistyped the password."));
311 done(QDialog::Accepted);
333#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
335#if defined(Q_OS_WIN32)
336 return GetKeyState(VK_CAPITAL) == 1;
337#elif defined(X_AVAILABLE)
338 Display *d = XOpenDisplay((
char *)0);
339 bool caps_state =
false;
342 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
343 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