Qt Utilities  5.6.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 <QKeyEvent>
9 #include <QMessageBox>
10 #include <QGuiApplication>
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 
36  QDialog(parent),
37  m_ui(new Ui::EnterPasswordDialog)
38 {
39  // setup ui
40  m_ui->setupUi(this);
41  makeHeading(m_ui->instructionLabel);
42  setStyleSheet(dialogStyle());
44  setPromptForUserName(false);
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);
51  // capslock key detection
52 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
53  m_capslockPressed = isCapslockPressed();
54 #else
55  m_capslockPressed = false;
56 #endif
57  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
58  // draw icon to capslock warning graphics view
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));
62  scene->addItem(item);
63  m_ui->capslockWarningGraphicsView->setScene(scene);
64  // connect signals and slots
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);
69  // grab the keyboard
70  grabKeyboard();
71 }
72 
77 {}
78 
84 {
85  return m_ui->descLabel->text();
86 }
87 
93 {
94  m_ui->descLabel->setText(description);
95  m_ui->descLabel->setHidden(description.isEmpty());
96  adjustSize();
97 }
98 
107 {
108  return !m_ui->userNameLineEdit->isHidden();
109 }
110 
116 {
117  m_ui->userNameLineEdit->setHidden(!prompt);
118  adjustSize();
119 }
120 
127 {
128  return !m_ui->password2LineEdit->isHidden();
129 }
130 
140 {
141  return m_ui->noPwCheckBox->isHidden();
142 }
143 
153 {
154  m_ui->noPwCheckBox->setHidden(value);
155  m_ui->noPwCheckBox->setChecked(false);
156  adjustSize();
157 }
158 
164 void EnterPasswordDialog::updateShowPassword()
165 {
166  m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked()
167  ? QLineEdit::Normal
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()));
171 }
172 
179 {
180  if(m_instruction.isEmpty()) {
181  m_ui->instructionLabel->setText(value ? tr("Enter the new password") : tr("Enter the password"));
182  }
183  m_ui->password2LineEdit->setHidden(!value);
184  adjustSize();
185 }
186 
192 void EnterPasswordDialog::setInstruction(const QString &value)
193 {
194  m_instruction = value;
195  if(m_instruction.isEmpty()) {
196  m_ui->instructionLabel->setText(isVerificationRequired() ? tr("Enter the new password") : tr("Enter the password"));
197  } else {
198  m_ui->instructionLabel->setText(value);
199  }
200  adjustSize();
201 }
202 
204 {
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;
210  }
211  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
212  break;
213  }
214  default:
215  ;
216  }
217  return QDialog::event(event);
218 }
219 
225 bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
226 {
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;
232  } else {
233  QString text = keyEvent->text();
234  if(text.length()) {
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;
241  }
242  }
243  }
244  m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
245  }
246  break;
247  case QEvent::FocusIn:
248  if(sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
249  releaseKeyboard();
250  qobject_cast<QWidget *>(sender)->grabKeyboard();
251  }
252  break;
253  case QEvent::FocusOut:
254  if(sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
255  qobject_cast<QWidget *>(sender)->releaseKeyboard();
256  grabKeyboard();
257  }
258  break;
259  default:
260  ;
261  }
262  return false;
263 }
264 
271 void EnterPasswordDialog::confirm()
272 {
273  if(!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
274  m_password.clear();
275  done(QDialog::Accepted);
276  } else {
277  QString userName = m_ui->userNameLineEdit->text();
278  QString password = m_ui->password1LineEdit->text();
279  QString repeatedPassword = m_ui->password2LineEdit->text();
280  if(promtForUserName() && userName.isEmpty()) {
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."));
284  } else {
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."));
288  } else {
289  QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
290  }
291  } else {
292  m_userName = userName;
293  m_password = password;
294  done(QDialog::Accepted);
295  }
296  }
297  }
298 }
299 
311 {
312 #ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
313  // platform dependent method of determining if CAPS LOCK is pressed
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;
319  if (d) {
320  unsigned n;
321  XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
322  caps_state = (n & 0x01) == 1;
323  }
324  return caps_state;
325 # else
326  return false;
327 # endif
328  return false;
329 #endif
330 }
331 
332 }
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
const QString QT_UTILITIES_EXPORT & dialogStyle()
Returns the stylesheet for dialogs and other windows used in my applications.
Definition: dialogutils.cpp:75