Qt Utilities 6.14.4
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.
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#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
16#include <windows.h>
17#elif defined(X_AVAILABLE)
18#include <X11/XKBlib.h>
19#undef KeyPress
20#undef KeyRelease
21#undef FocusIn
22#undef FocusOut
23#endif
24
25namespace QtUtilities {
26
38 : QDialog(parent)
39 , m_ui(new Ui::EnterPasswordDialog)
40{
41 // setup ui
42 m_ui->setupUi(this);
43 makeHeading(m_ui->instructionLabel);
44 setStyleSheet(dialogStyleForPalette(palette()));
48 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
49 installEventFilter(this);
50 m_ui->userNameLineEdit->installEventFilter(this);
51 m_ui->password1LineEdit->installEventFilter(this);
52 m_ui->password2LineEdit->installEventFilter(this);
53 m_capslockPressed = isCapslockPressed();
54 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
55 // draw icon to capslock warning graphics view
56 QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, this);
57 QGraphicsScene *scene = new QGraphicsScene();
58 QGraphicsPixmapItem *item = new QGraphicsPixmapItem(icon.pixmap(16, 16));
59 scene->addItem(item);
60 m_ui->capslockWarningGraphicsView->setScene(scene);
61 // connect signals and slots
62 connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked, this, &EnterPasswordDialog::updateShowPassword);
63 connect(m_ui->noPwCheckBox, &QCheckBox::clicked, this, &EnterPasswordDialog::updateShowPassword);
64 connect(m_ui->confirmPushButton, &QPushButton::clicked, this, &EnterPasswordDialog::confirm);
65 connect(m_ui->abortPushButton, &QPushButton::clicked, this, &EnterPasswordDialog::abort);
66 // grab the keyboard
67 grabKeyboard();
68}
69
76
83{
84 return m_ui->descLabel->text();
85}
86
91void EnterPasswordDialog::setDescription(const QString &description)
92{
93 m_ui->descLabel->setText(description);
94 m_ui->descLabel->setHidden(description.isEmpty());
95 adjustSize();
96}
97
106{
107 return !m_ui->userNameLineEdit->isHidden();
108}
109
115{
116 m_ui->userNameLineEdit->setHidden(!prompt);
117 adjustSize();
118}
119
127{
128 return !m_ui->password2LineEdit->isHidden();
129}
130
141{
142 return m_ui->noPwCheckBox->isHidden();
143}
144
155{
156 m_ui->noPwCheckBox->setHidden(value);
157 m_ui->noPwCheckBox->setChecked(false);
158 adjustSize();
159}
160
167void EnterPasswordDialog::updateShowPassword()
168{
169 m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked() ? QLineEdit::Normal : QLineEdit::Password);
170 m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
171 m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
172}
173
181{
182 if (m_instruction.isEmpty()) {
183 m_ui->instructionLabel->setText(value ? tr("Enter the new password") : tr("Enter the password"));
184 }
185 m_ui->password2LineEdit->setHidden(!value);
186 adjustSize();
187}
188
194void EnterPasswordDialog::setInstruction(const QString &value)
195{
196 m_instruction = value;
197 if (m_instruction.isEmpty()) {
198 m_ui->instructionLabel->setText(isVerificationRequired() ? tr("Enter the new password") : tr("Enter the password"));
199 } else {
200 m_ui->instructionLabel->setText(value);
201 }
202 adjustSize();
203}
204
205bool EnterPasswordDialog::event(QEvent *event)
206{
207 switch (event->type()) {
208 case QEvent::PaletteChange:
209 setStyleSheet(dialogStyleForPalette(palette()));
210 break;
211 case QEvent::KeyPress: {
212 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
213 if (keyEvent->key() == Qt::Key_CapsLock) {
214 m_capslockPressed = !m_capslockPressed;
215 }
216 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
217 break;
218 }
219 case QEvent::LanguageChange:
220 m_ui->retranslateUi(this);
221 break;
222 default:;
223 }
224 return QDialog::event(event);
225}
226
234bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
235{
236 switch (event->type()) {
237 case QEvent::KeyPress: {
238 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
239 if (keyEvent->key() == Qt::Key_CapsLock) {
240 m_capslockPressed = !m_capslockPressed;
241 } else {
242 QString text = keyEvent->text();
243 if (text.length()) {
244 QChar firstChar = text.at(0);
245 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
246 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
247 m_capslockPressed = true;
248 } else if (firstChar.isLetter()) {
249 m_capslockPressed = false;
250 }
251 }
252 }
253 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
254 } break;
255 case QEvent::FocusIn:
256 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
257 releaseKeyboard();
258 qobject_cast<QWidget *>(sender)->grabKeyboard();
259 }
260 break;
261 case QEvent::FocusOut:
262 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
263 qobject_cast<QWidget *>(sender)->releaseKeyboard();
264 grabKeyboard();
265 }
266 break;
267 default:;
268 }
269 return false;
270}
271
279void EnterPasswordDialog::confirm()
280{
281 if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
282 m_password.clear();
283 done(QDialog::Accepted);
284 } else {
285 QString userName = m_ui->userNameLineEdit->text();
286 QString password = m_ui->password1LineEdit->text();
287 QString repeatedPassword = m_ui->password2LineEdit->text();
288 if (promtForUserName() && userName.isEmpty()) {
289 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a user name."));
290 } else if (password.isEmpty()) {
291 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a password."));
292 } else {
293 if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
294 if (repeatedPassword.isEmpty()) {
295 QMessageBox::warning(this, windowTitle(),
296 tr("You have to enter the new password twice to "
297 "ensure you enterd it correct."));
298 } else {
299 QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
300 }
301 } else {
302 m_userName = userName;
303 m_password = password;
304 done(QDialog::Accepted);
305 }
306 }
307 }
308}
309
325{
326#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
327 return GetKeyState(VK_CAPITAL) == 1;
328#elif defined(X_AVAILABLE)
329 auto *const d = XOpenDisplay(nullptr);
330 auto capsState = false;
331 if (d) {
332 unsigned n;
333 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
334 capsState = (n & 0x01) == 1;
335 }
336 return capsState;
337#else
338 return false;
339#endif
340}
341} // 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