From 24751f81dd00e87c44b0248311b94849d808dbe2 Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 11 Nov 2018 12:30:53 +0100 Subject: [PATCH] Improved PasswordPromptDialog - can now also set caption - can set initial state of save option - improved size and spacing --- pglab/MasterController.cpp | 9 ++++-- pglab/PasswordPromptDialog.cpp | 55 ++++++++++++++++++++++++---------- pglab/PasswordPromptDialog.h | 14 +++++++-- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/pglab/MasterController.cpp b/pglab/MasterController.cpp index 4dc03a2..6b16feb 100644 --- a/pglab/MasterController.cpp +++ b/pglab/MasterController.cpp @@ -138,6 +138,7 @@ bool MasterController::retrieveConnectionPassword(ConnectionConfig &cc) // ook aan de gebruiker vragen zoals hier gebeurd. QString str = ConnectionListModel::makeLongDescription(cc); auto dlg = std::make_unique(PasswordPromptDialog::SaveOption, nullptr); + dlg->setCaption(tr("Connection password prompt")); dlg->setDescription(QString(tr("Please provide password for connection %1")).arg(str)); int exec_result = dlg->exec(); @@ -181,7 +182,8 @@ bool MasterController::UnlockPasswordManagerIfNeeded() while (true) { // ask user for passphrase auto dlg = std::make_unique(nullptr, nullptr); - dlg->setDescription(tr("Enter passphrase for password manager")); + dlg->setCaption(tr("Unlock password manager")); + dlg->setDescription(tr("Enter password for password manager")); int exec_result = dlg->exec(); bool ok = (exec_result == QDialog::Accepted); @@ -200,7 +202,10 @@ bool MasterController::UnlockPasswordManagerIfNeeded() // Ask user for passphrase + confirmation, clearly instruct this is first setup // create auto dlg = std::make_unique(PasswordPromptDialog::ConfirmPassword, nullptr); - dlg->setDescription(tr("Enter passphrase for password manager initialization")); + dlg->setCaption(tr("Password manager setup")); + dlg->setDescription(tr("Enter a strong password for password manager initialization. A strong key will be " + "derived from your password and it will be impossible to recover anything from the " + "password manager without the password you enter here.")); int exec_result = dlg->exec(); if (exec_result == QDialog::Accepted) { QString passphrase = dlg->password(); diff --git a/pglab/PasswordPromptDialog.cpp b/pglab/PasswordPromptDialog.cpp index e089a6c..a7cd121 100644 --- a/pglab/PasswordPromptDialog.cpp +++ b/pglab/PasswordPromptDialog.cpp @@ -9,18 +9,24 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(PasswordPromptDialog::Flags) +const char* PasswordPromptDialog::translateContext = "PasswordPromptDialog"; + PasswordPromptDialog::PasswordPromptDialog(Flags flags, QWidget *parent) : QDialog(parent) , m_Flags(flags) { - m_connectionLabel = new QLabel(this); + m_descriptionLabel = new QLabel(this); m_DialogButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); const size_t inputFieldCount = flags.testFlag(ConfirmPassword) ? 2 : 1; + setMinimumWidth(400); auto mainLayout = new QGridLayout; + mainLayout->setHorizontalSpacing(12); + mainLayout->setVerticalSpacing(18); + int row = 0; - mainLayout->addWidget(m_connectionLabel, row, 0, 1, 2); + mainLayout->addWidget(m_descriptionLabel, row, 0, 1, 2); ++row; for (size_t idx = 0; idx < inputFieldCount; ++idx) { auto lbl = new QLabel(this); @@ -51,18 +57,43 @@ PasswordPromptDialog::PasswordPromptDialog(Flags flags, QWidget *parent) connect(m_passwordInput[1], &QLineEdit::textChanged, this, &PasswordPromptDialog::passwordChanged); } +PasswordPromptDialog& PasswordPromptDialog::setDescription(const QString &description) +{ + m_descriptionLabel->setText(QString("%1").arg(description)); + return *this; +} + +PasswordPromptDialog& PasswordPromptDialog::setCaption(const std::optional &caption) +{ + m_customCaption = caption; + UpdateCaption(); + return *this; +} + +PasswordPromptDialog& PasswordPromptDialog::setSaveChecked(bool save) +{ + if (m_saveCheck) + m_saveCheck->setCheckState(save ? Qt::CheckState::Checked : Qt::CheckState::Unchecked); + return *this; +} + +void PasswordPromptDialog::UpdateCaption() +{ + QString cap = m_customCaption.value_or(QApplication::translate(translateContext, "Password dialog", nullptr)); + setWindowTitle(cap); +} + void PasswordPromptDialog::retranslateUi() { - const char * context = "PasswordPromptDialog"; - setWindowTitle(QApplication::translate(context, "Connection password", nullptr)); - m_passwordLabel[0]->setText(QApplication::translate(context, "Password", nullptr)); - m_passwordInput[0]->setPlaceholderText(QApplication::translate(context, "Enter password", nullptr)); + UpdateCaption(); + m_passwordLabel[0]->setText(QApplication::translate(translateContext, "Password", nullptr)); + m_passwordInput[0]->setPlaceholderText(QApplication::translate(translateContext, "Enter password", nullptr)); if (m_passwordLabel[1]) - m_passwordLabel[1]->setText(QApplication::translate(context, "Confirm password", nullptr)); + m_passwordLabel[1]->setText(QApplication::translate(translateContext, "Confirm password", nullptr)); if (m_passwordInput[1]) - m_passwordInput[1]->setPlaceholderText(QApplication::translate(context, "Reenter same password for confirmation", nullptr)); + m_passwordInput[1]->setPlaceholderText(QApplication::translate(translateContext, "Reenter same password for confirmation", nullptr)); if (m_saveCheck) - m_saveCheck->setText(QApplication::translate(context, "Save password", nullptr)); + m_saveCheck->setText(QApplication::translate(translateContext, "Save password", nullptr)); } void PasswordPromptDialog::updateOkEnabled() @@ -80,12 +111,6 @@ void PasswordPromptDialog::passwordChanged(const QString &) updateOkEnabled(); } -void PasswordPromptDialog::setDescription(const QString &description) -{ - m_connectionLabel->setText(QString("%1").arg(description)); - -} - QString PasswordPromptDialog::password() const { return m_passwordInput[0]->text(); diff --git a/pglab/PasswordPromptDialog.h b/pglab/PasswordPromptDialog.h index a1c31f5..695eecf 100644 --- a/pglab/PasswordPromptDialog.h +++ b/pglab/PasswordPromptDialog.h @@ -2,6 +2,7 @@ #define PASSWORDPROMPTDIALOG_H #include +#include class QCheckBox; class QDialogButtonBox; @@ -22,20 +23,29 @@ public: explicit PasswordPromptDialog(Flags flags, QWidget *parent = nullptr); - void setDescription(const QString &description); + + PasswordPromptDialog& setCaption(const std::optional &caption); + PasswordPromptDialog& setDescription(const QString &description); + /// Checks the save checkbox when save is true, this call is ignored when the checkbox does not exist + PasswordPromptDialog& setSaveChecked(bool save); QString password() const; bool saveChecked() const; private: + static const char* translateContext; + Flags m_Flags; - QLabel *m_connectionLabel = nullptr; + QLabel *m_descriptionLabel = nullptr; QLabel *m_passwordLabel[2] = { nullptr, nullptr }; QLineEdit *m_passwordInput[2] = { nullptr, nullptr }; QCheckBox *m_saveCheck = nullptr; QDialogButtonBox *m_DialogButtons = nullptr; + std::optional m_customCaption; + void retranslateUi(); void updateOkEnabled(); + void UpdateCaption(); private slots: void passwordChanged(const QString &text);