2 #include "../misc/dialogutils.h" 4 #include "ui_enterpassworddialog.h" 7 #include <QGraphicsPixmapItem> 10 #include <QGuiApplication> 12 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION 13 # if defined(Q_OS_WIN32) 15 # elif defined(X_AVAILABLE) 16 # include <X11/XKBlib.h> 41 makeHeading(m_ui->instructionLabel);
46 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
47 installEventFilter(
this);
48 m_ui->userNameLineEdit->installEventFilter(
this);
49 m_ui->password1LineEdit->installEventFilter(
this);
50 m_ui->password2LineEdit->installEventFilter(
this);
52 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION 55 m_capslockPressed =
false;
57 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
59 QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr,
this);
60 QGraphicsScene* scene =
new QGraphicsScene();
61 QGraphicsPixmapItem* item =
new QGraphicsPixmapItem(icon.pixmap(16, 16));
63 m_ui->capslockWarningGraphicsView->setScene(scene);
65 connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
66 connect(m_ui->noPwCheckBox, &QCheckBox::clicked,
this, &EnterPasswordDialog::updateShowPassword);
67 connect(m_ui->confirmPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::confirm);
68 connect(m_ui->abortPushButton, &QPushButton::clicked,
this, &EnterPasswordDialog::abort);
85 return m_ui->descLabel->text();
94 m_ui->descLabel->setText(description);
95 m_ui->descLabel->setHidden(description.isEmpty());
108 return !m_ui->userNameLineEdit->isHidden();
117 m_ui->userNameLineEdit->setHidden(!prompt);
128 return !m_ui->password2LineEdit->isHidden();
141 return m_ui->noPwCheckBox->isHidden();
154 m_ui->noPwCheckBox->setHidden(value);
155 m_ui->noPwCheckBox->setChecked(
false);
164 void EnterPasswordDialog::updateShowPassword()
166 m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked()
168 : QLineEdit::Password);
169 m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
170 m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
180 if(m_instruction.isEmpty()) {
181 m_ui->instructionLabel->setText(value ? tr(
"Enter the new password") : tr(
"Enter the password"));
183 m_ui->password2LineEdit->setHidden(!value);
194 m_instruction = value;
195 if(m_instruction.isEmpty()) {
196 m_ui->instructionLabel->setText(
isVerificationRequired() ? tr(
"Enter the new password") : tr(
"Enter the password"));
198 m_ui->instructionLabel->setText(value);
205 switch(event->type()) {
206 case QEvent::KeyPress: {
207 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
208 if(keyEvent->key() == Qt::Key_CapsLock) {
209 m_capslockPressed = !m_capslockPressed;
211 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
217 return QDialog::event(event);
227 switch(event->type()) {
228 case QEvent::KeyPress: {
229 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>(
event);
230 if(keyEvent->key() == Qt::Key_CapsLock) {
231 m_capslockPressed = !m_capslockPressed;
233 QString
text = keyEvent->text();
235 QChar firstChar = text.at(0);
236 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
237 if((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
238 m_capslockPressed =
true;
239 }
else if(firstChar.isLetter()) {
240 m_capslockPressed =
false;
244 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
247 case QEvent::FocusIn:
248 if(sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
250 qobject_cast<QWidget *>(sender)->grabKeyboard();
253 case QEvent::FocusOut:
254 if(sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
255 qobject_cast<QWidget *>(sender)->releaseKeyboard();
271 void EnterPasswordDialog::confirm()
275 done(QDialog::Accepted);
277 QString
userName = m_ui->userNameLineEdit->text();
278 QString
password = m_ui->password1LineEdit->text();
279 QString repeatedPassword = m_ui->password2LineEdit->text();
281 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a user name."));
282 }
else if(password.isEmpty()) {
283 QMessageBox::warning(
this, windowTitle(), tr(
"You didn't enter a password."));
285 if(
isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
286 if(repeatedPassword.isEmpty()) {
287 QMessageBox::warning(
this, windowTitle(), tr(
"You have to enter the new password twice to ensure you enterd it correct."));
289 QMessageBox::warning(
this, windowTitle(), tr(
"You mistyped the password."));
294 done(QDialog::Accepted);
312 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION 314 # if defined(Q_OS_WIN32) 315 return GetKeyState(VK_CAPITAL) == 1;
316 # elif defined(X_AVAILABLE) 317 Display *d = XOpenDisplay((
char*)0);
318 bool caps_state =
false;
321 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
322 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
const QString QT_UTILITIES_EXPORT & dialogStyle()
Returns the stylesheet for dialogs and other windows used in my applications.