Warning, file /frameworks/kwidgetsaddons/tests/knewpasswordwidget_test.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "knewpasswordwidget_test.h"
0008 
0009 #include <QApplication>
0010 #include <QCheckBox>
0011 #include <QDialogButtonBox>
0012 #include <QPushButton>
0013 #include <QVBoxLayout>
0014 
0015 #include <KMessageBox>
0016 #include <KNewPasswordWidget>
0017 
0018 // Doxygen will generate code snippets from this file.
0019 // We can't use i18n() here, but we want it to show up in the apidox.
0020 #define i18n QStringLiteral
0021 
0022 MyPasswordDialog::MyPasswordDialog(QWidget *parent)
0023     : QDialog(parent)
0024 {
0025     m_passwordWidget = new KNewPasswordWidget(this);
0026     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0027 
0028     m_passwordWidget->setMinimumPasswordLength(6);
0029 
0030     auto layout = new QVBoxLayout(this);
0031     layout->addWidget(m_passwordWidget);
0032     layout->addWidget(new QCheckBox(QStringLiteral("A checkbox"), this));
0033     layout->addWidget(m_buttonBox);
0034 
0035     connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0036     connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0037 }
0038 
0039 //! [accept_custom_dialog]
0040 void MyPasswordDialog::accept()
0041 {
0042     switch (m_passwordWidget->passwordStatus()) {
0043     case KNewPasswordWidget::WeakPassword:
0044     case KNewPasswordWidget::StrongPassword:
0045         QDialog::accept();
0046         break;
0047     case KNewPasswordWidget::PasswordNotVerified:
0048         KMessageBox::error(nullptr, i18n("The chosen password does not match the given verification password."));
0049         break;
0050     case KNewPasswordWidget::EmptyPasswordNotAllowed:
0051         KMessageBox::error(nullptr, i18n("The chosen password cannot be empty."));
0052         break;
0053     case KNewPasswordWidget::PasswordTooShort:
0054         KMessageBox::error(nullptr, i18n("The chosen password is too short."));
0055         break;
0056     }
0057 }
0058 //! [accept_custom_dialog]
0059 
0060 //! [update_custom_dialog]
0061 void MyPasswordDialog::slotPasswordStatusChanged()
0062 {
0063     // You may want to extend this switch with more cases,
0064     // in order to warn the user about all the possible password issues.
0065     switch (m_passwordWidget->passwordStatus()) {
0066     case KNewPasswordWidget::WeakPassword:
0067     case KNewPasswordWidget::StrongPassword:
0068         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0069         break;
0070     default:
0071         m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0072         break;
0073     }
0074 }
0075 //! [update_custom_dialog]
0076 
0077 int main(int argc, char *argv[])
0078 {
0079     QApplication app(argc, argv);
0080     app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0081 
0082     MyPasswordDialog dialog;
0083     QObject::connect(&dialog, &QDialog::finished, &app, &QCoreApplication::quit);
0084     dialog.show();
0085 
0086     return app.exec();
0087 }
0088 
0089 #include "moc_knewpasswordwidget_test.cpp"