Qt Utilities 6.12.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
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
27namespace 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(dialogStyleForPalette(palette()));
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
98void 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
174void 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
201void 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
212bool EnterPasswordDialog::event(QEvent *event)
213{
214 switch (event->type()) {
215 case QEvent::PaletteChange:
216 setStyleSheet(dialogStyleForPalette(palette()));
217 break;
218 case QEvent::KeyPress: {
219 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
220 if (keyEvent->key() == Qt::Key_CapsLock) {
221 m_capslockPressed = !m_capslockPressed;
222 }
223 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
224 break;
225 }
226 default:;
227 }
228 return QDialog::event(event);
229}
230
238bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
239{
240 switch (event->type()) {
241 case QEvent::KeyPress: {
242 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
243 if (keyEvent->key() == Qt::Key_CapsLock) {
244 m_capslockPressed = !m_capslockPressed;
245 } else {
246 QString text = keyEvent->text();
247 if (text.length()) {
248 QChar firstChar = text.at(0);
249 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
250 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
251 m_capslockPressed = true;
252 } else if (firstChar.isLetter()) {
253 m_capslockPressed = false;
254 }
255 }
256 }
257 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
258 } break;
259 case QEvent::FocusIn:
260 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
261 releaseKeyboard();
262 qobject_cast<QWidget *>(sender)->grabKeyboard();
263 }
264 break;
265 case QEvent::FocusOut:
266 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
267 qobject_cast<QWidget *>(sender)->releaseKeyboard();
268 grabKeyboard();
269 }
270 break;
271 default:;
272 }
273 return false;
274}
275
283void EnterPasswordDialog::confirm()
284{
285 if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
286 m_password.clear();
287 done(QDialog::Accepted);
288 } else {
289 QString userName = m_ui->userNameLineEdit->text();
290 QString password = m_ui->password1LineEdit->text();
291 QString repeatedPassword = m_ui->password2LineEdit->text();
292 if (promtForUserName() && userName.isEmpty()) {
293 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a user name."));
294 } else if (password.isEmpty()) {
295 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a password."));
296 } else {
297 if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
298 if (repeatedPassword.isEmpty()) {
299 QMessageBox::warning(this, windowTitle(),
300 tr("You have to enter the new password twice to "
301 "ensure you enterd it correct."));
302 } else {
303 QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
304 }
305 } else {
306 m_userName = userName;
307 m_password = password;
308 done(QDialog::Accepted);
309 }
310 }
311 }
312}
313
329{
330#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
331// platform dependent method of determining if CAPS LOCK is pressed
332#if defined(Q_OS_WIN32)
333 return GetKeyState(VK_CAPITAL) == 1;
334#elif defined(X_AVAILABLE)
335 Display *d = XOpenDisplay((char *)0);
336 bool caps_state = false;
337 if (d) {
338 unsigned n;
339 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
340 caps_state = (n & 0x01) == 1;
341 }
342 return caps_state;
343#else
344 return false;
345#endif
346#else
347 return false;
348#endif
349}
350} // 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