4#include "ui_enterpassworddialog.h"
8#include <QGraphicsPixmapItem>
9#include <QGraphicsScene>
15#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
17#elif defined(X_AVAILABLE)
18#include <X11/XKBlib.h>
43 makeHeading(m_ui->instructionLabel);
44 setStyleSheet(dialogStyleForPalette(palette()));
48 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
49 installEventFilter(
this);
50 m_ui->userNameLineEdit->installEventFilter(
this);
51 m_ui->password1LineEdit->installEventFilter(
this);
52 m_ui->password2LineEdit->installEventFilter(
this);
54 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
56 QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr,
this);
57 QGraphicsScene *scene =
new QGraphicsScene();
58 QGraphicsPixmapItem *item =
new QGraphicsPixmapItem(icon.pixmap(16, 16));
60 m_ui->capslockWarningGraphicsView->setScene(scene);
62 connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
63 connect(m_ui->noPwCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
64 connect(m_ui->confirmPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::confirm);
65 connect(m_ui->abortPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::abort);
84 return m_ui->descLabel->text();
107 return !m_ui->userNameLineEdit->isHidden();
116 m_ui->userNameLineEdit->setHidden(!prompt);
128 return !m_ui->password2LineEdit->isHidden();
142 return m_ui->noPwCheckBox->isHidden();
156 m_ui->noPwCheckBox->setHidden(value);
157 m_ui->noPwCheckBox->setChecked(
false);
167void EnterPasswordDialog::updateShowPassword()
169 m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked() ? QLineEdit::Normal : QLineEdit::Password);
170 m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
171 m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
182 if (m_instruction.isEmpty()) {
183 m_ui->instructionLabel->setText(value ? tr(
"Enter the new password") : tr(
"Enter the password"));
185 m_ui->password2LineEdit->setHidden(!value);
196 m_instruction = value;
197 if (m_instruction.isEmpty()) {
198 m_ui->instructionLabel->setText(
isVerificationRequired() ? tr(
"Enter the new password") : tr(
"Enter the password"));
200 m_ui->instructionLabel->setText(value);
207 switch (
event->type()) {
208 case QEvent::PaletteChange:
209 setStyleSheet(dialogStyleForPalette(palette()));
211 case QEvent::KeyPress: {
212 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
213 if (keyEvent->key() == Qt::Key_CapsLock) {
214 m_capslockPressed = !m_capslockPressed;
216 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
219 case QEvent::LanguageChange:
220 m_ui->retranslateUi(
this);
224 return QDialog::event(
event);
236 switch (
event->type()) {
237 case QEvent::KeyPress: {
238 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
239 if (keyEvent->key() == Qt::Key_CapsLock) {
240 m_capslockPressed = !m_capslockPressed;
242 QString
text = keyEvent->text();
244 QChar firstChar =
text.at(0);
245 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
246 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
247 m_capslockPressed =
true;
248 }
else if (firstChar.isLetter()) {
249 m_capslockPressed =
false;
253 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
255 case QEvent::FocusIn:
256 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
258 qobject_cast<QWidget *>(sender)->grabKeyboard();
261 case QEvent::FocusOut:
262 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
263 qobject_cast<QWidget *>(sender)->releaseKeyboard();
279void EnterPasswordDialog::confirm()
283 done(QDialog::Accepted);
285 QString
userName = m_ui->userNameLineEdit->text();
286 QString
password = m_ui->password1LineEdit->text();
287 QString repeatedPassword = m_ui->password2LineEdit->text();
289 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a user name."));
291 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a password."));
294 if (repeatedPassword.isEmpty()) {
295 QMessageBox::warning(
this, windowTitle(),
296 tr(
"You have to enter the new password twice to "
297 "ensure you enterd it correct."));
299 QMessageBox::warning(
this, windowTitle(), tr(
"You mistyped the password."));
304 done(QDialog::Accepted);
326#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
327 return GetKeyState(VK_CAPITAL) == 1;
328#elif defined(X_AVAILABLE)
329 auto *
const d = XOpenDisplay(
nullptr);
330 auto capsState =
false;
333 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
334 capsState = (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