Qt Utilities  6.4.1
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 <QApplication>
7 #include <QEvent>
8 #include <QGraphicsPixmapItem>
9 #include <QGraphicsScene>
10 #include <QIcon>
11 #include <QKeyEvent>
12 #include <QMessageBox>
13 #include <QStyle>
14 
15 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
16 #if defined(Q_OS_WIN32)
17 #include <windows.h>
18 #elif defined(X_AVAILABLE)
19 #include <X11/XKBlib.h>
20 #undef KeyPress
21 #undef KeyRelease
22 #undef FocusIn
23 #undef FocusOut
24 #endif
25 #endif
26 
27 namespace QtUtilities {
28 
40  : QDialog(parent)
41  , m_ui(new Ui::EnterPasswordDialog)
42 {
43  // setup ui
44  m_ui->setupUi(this);
45  makeHeading(m_ui->instructionLabel);
46  setStyleSheet(dialogStyle());
48  setPromptForUserName(false);
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);
55 // capslock key detection
56 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
57  m_capslockPressed = isCapslockPressed();
58 #else
59  m_capslockPressed = false;
60 #endif
61  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
62  // draw icon to capslock warning graphics view
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));
66  scene->addItem(item);
67  m_ui->capslockWarningGraphicsView->setScene(scene);
68  // connect signals and slots
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);
73  // grab the keyboard
74  grabKeyboard();
75 }
76 
81 {
82 }
83 
90 {
91  return m_ui->descLabel->text();
92 }
93 
98 void EnterPasswordDialog::setDescription(const QString &description)
99 {
100  m_ui->descLabel->setText(description);
101  m_ui->descLabel->setHidden(description.isEmpty());
102  adjustSize();
103 }
104 
113 {
114  return !m_ui->userNameLineEdit->isHidden();
115 }
116 
122 {
123  m_ui->userNameLineEdit->setHidden(!prompt);
124  adjustSize();
125 }
126 
134 {
135  return !m_ui->password2LineEdit->isHidden();
136 }
137 
148 {
149  return m_ui->noPwCheckBox->isHidden();
150 }
151 
162 {
163  m_ui->noPwCheckBox->setHidden(value);
164  m_ui->noPwCheckBox->setChecked(false);
165  adjustSize();
166 }
167 
174 void EnterPasswordDialog::updateShowPassword()
175 {
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()));
179 }
180 
188 {
189  if (m_instruction.isEmpty()) {
190  m_ui->instructionLabel->setText(value ? tr("Enter the new password") : tr("Enter the password"));
191  }
192  m_ui->password2LineEdit->setHidden(!value);
193  adjustSize();
194 }
195 
201 void EnterPasswordDialog::setInstruction(const QString &value)
202 {
203  m_instruction = value;
204  if (m_instruction.isEmpty()) {
205  m_ui->instructionLabel->setText(isVerificationRequired() ? tr("Enter the new password") : tr("Enter the password"));
206  } else {
207  m_ui->instructionLabel->setText(value);
208  }
209  adjustSize();
210 }
211 
212 bool EnterPasswordDialog::event(QEvent *event)
213 {
214  switch (event->type()) {
215  case QEvent::KeyPress: {
216  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
217  if (keyEvent->key() == Qt::Key_CapsLock) {
218  m_capslockPressed = !m_capslockPressed;
219  }
220  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
221  break;
222  }
223  default:;
224  }
225  return QDialog::event(event);
226 }
227 
235 bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
236 {
237  switch (event->type()) {
238  case QEvent::KeyPress: {
239  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
240  if (keyEvent->key() == Qt::Key_CapsLock) {
241  m_capslockPressed = !m_capslockPressed;
242  } else {
243  QString text = keyEvent->text();
244  if (text.length()) {
245  QChar firstChar = text.at(0);
246  bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
247  if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
248  m_capslockPressed = true;
249  } else if (firstChar.isLetter()) {
250  m_capslockPressed = false;
251  }
252  }
253  }
254  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
255  } break;
256  case QEvent::FocusIn:
257  if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
258  releaseKeyboard();
259  qobject_cast<QWidget *>(sender)->grabKeyboard();
260  }
261  break;
262  case QEvent::FocusOut:
263  if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
264  qobject_cast<QWidget *>(sender)->releaseKeyboard();
265  grabKeyboard();
266  }
267  break;
268  default:;
269  }
270  return false;
271 }
272 
280 void EnterPasswordDialog::confirm()
281 {
282  if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
283  m_password.clear();
284  done(QDialog::Accepted);
285  } else {
286  QString userName = m_ui->userNameLineEdit->text();
287  QString password = m_ui->password1LineEdit->text();
288  QString repeatedPassword = m_ui->password2LineEdit->text();
289  if (promtForUserName() && userName.isEmpty()) {
290  QMessageBox::warning(this, windowTitle(), tr("You didn't enter a user name."));
291  } else if (password.isEmpty()) {
292  QMessageBox::warning(this, windowTitle(), tr("You didn't enter a password."));
293  } else {
294  if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
295  if (repeatedPassword.isEmpty()) {
296  QMessageBox::warning(this, windowTitle(),
297  tr("You have to enter the new password twice to "
298  "ensure you enterd it correct."));
299  } else {
300  QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
301  }
302  } else {
303  m_userName = userName;
304  m_password = password;
305  done(QDialog::Accepted);
306  }
307  }
308  }
309 }
310 
326 {
327 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
328 // platform dependent method of determining if CAPS LOCK is pressed
329 #if defined(Q_OS_WIN32)
330  return GetKeyState(VK_CAPITAL) == 1;
331 #elif defined(X_AVAILABLE)
332  Display *d = XOpenDisplay((char *)0);
333  bool caps_state = false;
334  if (d) {
335  unsigned n;
336  XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
337  caps_state = (n & 0x01) == 1;
338  }
339  return caps_state;
340 #else
341  return false;
342 #endif
343 #else
344  return false;
345 #endif
346 }
347 } // namespace QtUtilities
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 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
#define text