Qt Utilities  5.10.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
enterpassworddialog.cpp
Go to the documentation of this file.
2 #include "../misc/dialogutils.h"
3 
4 #include "ui_enterpassworddialog.h"
5 
6 #include <QEvent>
7 #include <QGraphicsPixmapItem>
8 #include <QGuiApplication>
9 #include <QKeyEvent>
10 #include <QMessageBox>
11 
12 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
13 #if defined(Q_OS_WIN32)
14 #include <windows.h>
15 #elif defined(X_AVAILABLE)
16 #include <X11/XKBlib.h>
17 #undef KeyPress
18 #undef KeyRelease
19 #undef FocusIn
20 #undef FocusOut
21 #endif
22 #endif
23 
24 namespace Dialogs {
25 
37  : QDialog(parent)
38  , m_ui(new Ui::EnterPasswordDialog)
39 {
40  // setup ui
41  m_ui->setupUi(this);
42  makeHeading(m_ui->instructionLabel);
43  setStyleSheet(dialogStyle());
45  setPromptForUserName(false);
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);
52 // capslock key detection
53 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
54  m_capslockPressed = isCapslockPressed();
55 #else
56  m_capslockPressed = false;
57 #endif
58  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
59  // draw icon to capslock warning graphics view
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));
63  scene->addItem(item);
64  m_ui->capslockWarningGraphicsView->setScene(scene);
65  // connect signals and slots
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);
70  // grab the keyboard
71  grabKeyboard();
72 }
73 
78 {
79 }
80 
87 {
88  return m_ui->descLabel->text();
89 }
90 
95 void EnterPasswordDialog::setDescription(const QString &description)
96 {
97  m_ui->descLabel->setText(description);
98  m_ui->descLabel->setHidden(description.isEmpty());
99  adjustSize();
100 }
101 
110 {
111  return !m_ui->userNameLineEdit->isHidden();
112 }
113 
119 {
120  m_ui->userNameLineEdit->setHidden(!prompt);
121  adjustSize();
122 }
123 
131 {
132  return !m_ui->password2LineEdit->isHidden();
133 }
134 
145 {
146  return m_ui->noPwCheckBox->isHidden();
147 }
148 
159 {
160  m_ui->noPwCheckBox->setHidden(value);
161  m_ui->noPwCheckBox->setChecked(false);
162  adjustSize();
163 }
164 
171 void EnterPasswordDialog::updateShowPassword()
172 {
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()));
176 }
177 
185 {
186  if (m_instruction.isEmpty()) {
187  m_ui->instructionLabel->setText(value ? tr("Enter the new password") : tr("Enter the password"));
188  }
189  m_ui->password2LineEdit->setHidden(!value);
190  adjustSize();
191 }
192 
198 void EnterPasswordDialog::setInstruction(const QString &value)
199 {
200  m_instruction = value;
201  if (m_instruction.isEmpty()) {
202  m_ui->instructionLabel->setText(isVerificationRequired() ? tr("Enter the new password") : tr("Enter the password"));
203  } else {
204  m_ui->instructionLabel->setText(value);
205  }
206  adjustSize();
207 }
208 
209 bool EnterPasswordDialog::event(QEvent *event)
210 {
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;
216  }
217  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
218  break;
219  }
220  default:;
221  }
222  return QDialog::event(event);
223 }
224 
232 bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
233 {
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;
239  } else {
240  QString text = keyEvent->text();
241  if (text.length()) {
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;
248  }
249  }
250  }
251  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
252  } break;
253  case QEvent::FocusIn:
254  if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
255  releaseKeyboard();
256  qobject_cast<QWidget *>(sender)->grabKeyboard();
257  }
258  break;
259  case QEvent::FocusOut:
260  if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
261  qobject_cast<QWidget *>(sender)->releaseKeyboard();
262  grabKeyboard();
263  }
264  break;
265  default:;
266  }
267  return false;
268 }
269 
277 void EnterPasswordDialog::confirm()
278 {
279  if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
280  m_password.clear();
281  done(QDialog::Accepted);
282  } else {
283  QString userName = m_ui->userNameLineEdit->text();
284  QString password = m_ui->password1LineEdit->text();
285  QString repeatedPassword = m_ui->password2LineEdit->text();
286  if (promtForUserName() && userName.isEmpty()) {
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."));
290  } else {
291  if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
292  if (repeatedPassword.isEmpty()) {
293  QMessageBox::warning(this, windowTitle(),
294  tr("You have to enter the new password twice to "
295  "ensure you enterd it correct."));
296  } else {
297  QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
298  }
299  } else {
300  m_userName = userName;
301  m_password = password;
302  done(QDialog::Accepted);
303  }
304  }
305  }
306 }
307 
323 {
324 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
325 // platform dependent method of determining if CAPS LOCK is pressed
326 #if defined(Q_OS_WIN32)
327  return GetKeyState(VK_CAPITAL) == 1;
328 #elif defined(X_AVAILABLE)
329  Display *d = XOpenDisplay((char *)0);
330  bool caps_state = false;
331  if (d) {
332  unsigned n;
333  XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
334  caps_state = (n & 0x01) == 1;
335  }
336  return caps_state;
337 #else
338  return false;
339 #endif
340 #else
341  return false;
342 #endif
343 }
344 } // namespace Dialogs
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.
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
Definition: dialogutils.h:12
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.
bool eventFilter(QObject *sender, QEvent *event)
Internal method to notice when the capslock key is pressed by the user.
const QString & userName() const
bool isVerificationRequired() const
#define text