Qt Utilities 6.13.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 case QEvent::LanguageChange:
227 m_ui->retranslateUi(this);
228 break;
229 default:;
230 }
231 return QDialog::event(event);
232}
233
241bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
242{
243 switch (event->type()) {
244 case QEvent::KeyPress: {
245 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
246 if (keyEvent->key() == Qt::Key_CapsLock) {
247 m_capslockPressed = !m_capslockPressed;
248 } else {
249 QString text = keyEvent->text();
250 if (text.length()) {
251 QChar firstChar = text.at(0);
252 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
253 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
254 m_capslockPressed = true;
255 } else if (firstChar.isLetter()) {
256 m_capslockPressed = false;
257 }
258 }
259 }
260 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
261 } break;
262 case QEvent::FocusIn:
263 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
264 releaseKeyboard();
265 qobject_cast<QWidget *>(sender)->grabKeyboard();
266 }
267 break;
268 case QEvent::FocusOut:
269 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
270 qobject_cast<QWidget *>(sender)->releaseKeyboard();
271 grabKeyboard();
272 }
273 break;
274 default:;
275 }
276 return false;
277}
278
286void EnterPasswordDialog::confirm()
287{
288 if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
289 m_password.clear();
290 done(QDialog::Accepted);
291 } else {
292 QString userName = m_ui->userNameLineEdit->text();
293 QString password = m_ui->password1LineEdit->text();
294 QString repeatedPassword = m_ui->password2LineEdit->text();
295 if (promtForUserName() && userName.isEmpty()) {
296 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a user name."));
297 } else if (password.isEmpty()) {
298 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a password."));
299 } else {
300 if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
301 if (repeatedPassword.isEmpty()) {
302 QMessageBox::warning(this, windowTitle(),
303 tr("You have to enter the new password twice to "
304 "ensure you enterd it correct."));
305 } else {
306 QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
307 }
308 } else {
309 m_userName = userName;
310 m_password = password;
311 done(QDialog::Accepted);
312 }
313 }
314 }
315}
316
332{
333#ifdef QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION
334// platform dependent method of determining if CAPS LOCK is pressed
335#if defined(Q_OS_WIN32)
336 return GetKeyState(VK_CAPITAL) == 1;
337#elif defined(X_AVAILABLE)
338 Display *d = XOpenDisplay((char *)0);
339 bool caps_state = false;
340 if (d) {
341 unsigned n;
342 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
343 caps_state = (n & 0x01) == 1;
344 }
345 return caps_state;
346#else
347 return false;
348#endif
349#else
350 return false;
351#endif
352}
353} // 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