1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
#include "./enterpassworddialog.h"
#include "../misc/dialogutils.h"
#include "ui_enterpassworddialog.h"
#include <QApplication>
#include <QEvent>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QIcon>
#include <QKeyEvent>
#include <QMessageBox>
#include <QStyle>
#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
#include <windows.h>
#elif defined(X_AVAILABLE)
#include <X11/XKBlib.h>
#undef KeyPress
#undef KeyRelease
#undef FocusIn
#undef FocusOut
#endif
namespace QtUtilities {
/*!
* \class EnterPasswordDialog
* \brief The EnterPasswordDialog class provides a simple dialog to ask the user
* for a password.
*/
/*!
* \brief Constructs a password dialog.
* \param parent Specifies the parent widget.
*/
EnterPasswordDialog::EnterPasswordDialog(QWidget *parent)
: QDialog(parent)
, m_ui(new Ui::EnterPasswordDialog)
{
// setup ui
m_ui->setupUi(this);
makeHeading(m_ui->instructionLabel);
setStyleSheet(dialogStyleForPalette(palette()));
setDescription();
setPromptForUserName(false);
setVerificationRequired(false);
setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
installEventFilter(this);
m_ui->userNameLineEdit->installEventFilter(this);
m_ui->password1LineEdit->installEventFilter(this);
m_ui->password2LineEdit->installEventFilter(this);
m_capslockPressed = isCapslockPressed();
m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
// draw icon to capslock warning graphics view
QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, this);
QGraphicsScene *scene = new QGraphicsScene();
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(icon.pixmap(16, 16));
scene->addItem(item);
m_ui->capslockWarningGraphicsView->setScene(scene);
// connect signals and slots
connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked, this, &EnterPasswordDialog::updateShowPassword);
connect(m_ui->noPwCheckBox, &QCheckBox::clicked, this, &EnterPasswordDialog::updateShowPassword);
connect(m_ui->confirmPushButton, &QPushButton::clicked, this, &EnterPasswordDialog::confirm);
connect(m_ui->abortPushButton, &QPushButton::clicked, this, &EnterPasswordDialog::abort);
// grab the keyboard
grabKeyboard();
}
/*!
* \brief Destroys the password dialog.
*/
EnterPasswordDialog::~EnterPasswordDialog()
{
}
/*!
* \brief Returns the description. The description is shown under the
* instruction text.
* \sa setDescription()
*/
QString EnterPasswordDialog::description() const
{
return m_ui->descLabel->text();
}
/*!
* \brief Sets the description.
* \sa description()
*/
void EnterPasswordDialog::setDescription(const QString &description)
{
m_ui->descLabel->setText(description);
m_ui->descLabel->setHidden(description.isEmpty());
adjustSize();
}
/*!
* \brief Returns whether the dialogs prompts for a user name as well.
*
* The dialog does not prompt for a user name by default.
*
* \sa setPromptForUserName()
*/
bool EnterPasswordDialog::promtForUserName() const
{
return !m_ui->userNameLineEdit->isHidden();
}
/*!
* \brief Sets whethere the dialog prompts for a user name as well.
* \sa promptForUserName()
*/
void EnterPasswordDialog::setPromptForUserName(bool prompt)
{
m_ui->userNameLineEdit->setHidden(!prompt);
adjustSize();
}
/*!
* \brief Returns an indication whether a verification (password has to be
* entered twice) is required.
*
* \sa EnterPasswordDialog::setVerificationRequired()
*/
bool EnterPasswordDialog::isVerificationRequired() const
{
return !m_ui->password2LineEdit->isHidden();
}
/*!
* \brief Returns an indication whether the user is force to enter a password.
*
* If no password is required, the user is allowed to skip the dialog without
* entering
* a password.
*
* \sa EnterPasswordDialog::setPasswordRequired()
*/
bool EnterPasswordDialog::isPasswordRequired() const
{
return m_ui->noPwCheckBox->isHidden();
}
/*!
* \brief Sets whether the user is force to enter a password.
*
* If no password is required, the user is allowed to skip the dialog without
* entering
* a password.
*
* \sa EnterPasswordDialog::isPasswordRequired()
*/
void EnterPasswordDialog::setPasswordRequired(bool value)
{
m_ui->noPwCheckBox->setHidden(value);
m_ui->noPwCheckBox->setChecked(false);
adjustSize();
}
/*!
* \brief Updates the relevant controls to show entered characters or to mask
* them them.
*
* This private slot is called when m_ui->showPasswordCheckBox is clicked.
*/
void EnterPasswordDialog::updateShowPassword()
{
m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked() ? QLineEdit::Normal : QLineEdit::Password);
m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
}
/*!
* \brief Sets whether a verification (password has to be entered twice) is
* required.
*
* \sa EnterPasswordDialog::isVerificationRequired()
*/
void EnterPasswordDialog::setVerificationRequired(bool value)
{
if (m_instruction.isEmpty()) {
m_ui->instructionLabel->setText(value ? tr("Enter the new password") : tr("Enter the password"));
}
m_ui->password2LineEdit->setHidden(!value);
adjustSize();
}
/*!
* \brief Sets the instruction text.
*
* \sa EnterPasswordDialog::instruction()
*/
void EnterPasswordDialog::setInstruction(const QString &value)
{
m_instruction = value;
if (m_instruction.isEmpty()) {
m_ui->instructionLabel->setText(isVerificationRequired() ? tr("Enter the new password") : tr("Enter the password"));
} else {
m_ui->instructionLabel->setText(value);
}
adjustSize();
}
bool EnterPasswordDialog::event(QEvent *event)
{
switch (event->type()) {
case QEvent::PaletteChange:
setStyleSheet(dialogStyleForPalette(palette()));
break;
case QEvent::KeyPress: {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_CapsLock) {
m_capslockPressed = !m_capslockPressed;
}
m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
break;
}
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:;
}
return QDialog::event(event);
}
/*!
* \brief Internal method to notice when the capslock key is pressed by the
* user.
*
* Invocation of this method is done by installing the event filter in the
* constructor.
*/
bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
{
switch (event->type()) {
case QEvent::KeyPress: {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_CapsLock) {
m_capslockPressed = !m_capslockPressed;
} else {
QString text = keyEvent->text();
if (text.length()) {
QChar firstChar = text.at(0);
bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
m_capslockPressed = true;
} else if (firstChar.isLetter()) {
m_capslockPressed = false;
}
}
}
m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
} break;
case QEvent::FocusIn:
if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
releaseKeyboard();
qobject_cast<QWidget *>(sender)->grabKeyboard();
}
break;
case QEvent::FocusOut:
if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
qobject_cast<QWidget *>(sender)->releaseKeyboard();
grabKeyboard();
}
break;
default:;
}
return false;
}
/*!
* \brief Sets the dialog status to QDialog::Accepted if a valid password has
* been enterd.
* Displays an error message otherwise.
*
* This private slot is called when m_ui->confirmPushButton is clicked.
*/
void EnterPasswordDialog::confirm()
{
if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
m_password.clear();
done(QDialog::Accepted);
} else {
QString userName = m_ui->userNameLineEdit->text();
QString password = m_ui->password1LineEdit->text();
QString repeatedPassword = m_ui->password2LineEdit->text();
if (promtForUserName() && userName.isEmpty()) {
QMessageBox::warning(this, windowTitle(), tr("You didn't enter a user name."));
} else if (password.isEmpty()) {
QMessageBox::warning(this, windowTitle(), tr("You didn't enter a password."));
} else {
if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
if (repeatedPassword.isEmpty()) {
QMessageBox::warning(this, windowTitle(),
tr("You have to enter the new password twice to "
"ensure you enterd it correct."));
} else {
QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
}
} else {
m_userName = userName;
m_password = password;
done(QDialog::Accepted);
}
}
}
}
/*!
* \brief Returns an indication whether the capslock key is pressed using
* platform specific functions.
*
* \remarks
* - Returns always false for unsupported platforms.
* - This method always returns false when the detection is not
* supported. It is supported under X11
* and Windows.
* - The function requires the application to be linked against X11 on
* Linux/Unix.
* - This static function will be used internally to detect whether the
* capslock key is pressed when initializing the dialog.
*/
bool EnterPasswordDialog::isCapslockPressed()
{
#if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
return GetKeyState(VK_CAPITAL) == 1;
#elif defined(X_AVAILABLE)
auto *const d = XOpenDisplay(nullptr);
auto capsState = false;
if (d) {
unsigned n;
XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
capsState = (n & 0x01) == 1;
}
return capsState;
#else
return false;
#endif
}
} // namespace QtUtilities
|