2018-11-04 11:26:20 +01:00
|
|
|
|
#include "PasswordPromptDialog.h"
|
|
|
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
|
#include <QCheckBox>
|
|
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
#include <QLayout>
|
|
|
|
|
|
#include <QLineEdit>
|
2018-11-08 21:50:49 +01:00
|
|
|
|
#include <QPushButton>
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(PasswordPromptDialog::Flags)
|
|
|
|
|
|
|
2018-11-11 12:30:53 +01:00
|
|
|
|
const char* PasswordPromptDialog::translateContext = "PasswordPromptDialog";
|
|
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
PasswordPromptDialog::PasswordPromptDialog(Flags flags, QWidget *parent)
|
2018-11-04 11:26:20 +01:00
|
|
|
|
: QDialog(parent)
|
2018-11-08 21:50:49 +01:00
|
|
|
|
, m_Flags(flags)
|
2018-11-04 11:26:20 +01:00
|
|
|
|
{
|
2018-11-11 12:30:53 +01:00
|
|
|
|
m_descriptionLabel = new QLabel(this);
|
2018-11-08 21:50:49 +01:00
|
|
|
|
m_DialogButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
const size_t inputFieldCount = flags.testFlag(ConfirmPassword) ? 2 : 1;
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
2018-11-11 12:30:53 +01:00
|
|
|
|
setMinimumWidth(400);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
auto mainLayout = new QGridLayout;
|
2018-11-11 12:30:53 +01:00
|
|
|
|
mainLayout->setHorizontalSpacing(12);
|
|
|
|
|
|
mainLayout->setVerticalSpacing(18);
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
int row = 0;
|
2018-11-11 12:30:53 +01:00
|
|
|
|
mainLayout->addWidget(m_descriptionLabel, row, 0, 1, 2);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
++row;
|
2018-11-08 21:50:49 +01:00
|
|
|
|
for (size_t idx = 0; idx < inputFieldCount; ++idx) {
|
|
|
|
|
|
auto lbl = new QLabel(this);
|
|
|
|
|
|
auto input = new QLineEdit(this);
|
|
|
|
|
|
input->setEchoMode(QLineEdit::Password);
|
|
|
|
|
|
mainLayout->addWidget(lbl, row, 0);
|
|
|
|
|
|
mainLayout->addWidget(input, row, 1);
|
|
|
|
|
|
m_passwordLabel[idx] = lbl;
|
|
|
|
|
|
m_passwordInput[idx] = input;
|
|
|
|
|
|
++row;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (m_Flags.testFlag(SaveOption)) {
|
|
|
|
|
|
m_saveCheck = new QCheckBox(this);
|
|
|
|
|
|
mainLayout->addWidget(m_saveCheck, row, 1);
|
|
|
|
|
|
++row;
|
|
|
|
|
|
}
|
|
|
|
|
|
mainLayout->addWidget(m_DialogButtons, row, 0, 1 ,2);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
m_passwordInput[0]->setFocus();
|
2018-11-04 11:26:20 +01:00
|
|
|
|
retranslateUi();
|
|
|
|
|
|
|
|
|
|
|
|
// QMetaObject::connectSlotsByName(BackupDialog);
|
2018-11-08 21:50:49 +01:00
|
|
|
|
connect(m_DialogButtons, &QDialogButtonBox::accepted, this, &PasswordPromptDialog::accept);
|
|
|
|
|
|
connect(m_DialogButtons, &QDialogButtonBox::rejected, this, &PasswordPromptDialog::reject);
|
|
|
|
|
|
connect(m_passwordInput[0], &QLineEdit::textChanged, this, &PasswordPromptDialog::passwordChanged);
|
|
|
|
|
|
if (m_passwordInput[1])
|
|
|
|
|
|
connect(m_passwordInput[1], &QLineEdit::textChanged, this, &PasswordPromptDialog::passwordChanged);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-11 12:30:53 +01:00
|
|
|
|
PasswordPromptDialog& PasswordPromptDialog::setDescription(const QString &description)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_descriptionLabel->setText(QString("%1").arg(description));
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PasswordPromptDialog& PasswordPromptDialog::setCaption(const std::optional<QString> &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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
void PasswordPromptDialog::retranslateUi()
|
|
|
|
|
|
{
|
2018-11-11 12:30:53 +01:00
|
|
|
|
UpdateCaption();
|
|
|
|
|
|
m_passwordLabel[0]->setText(QApplication::translate(translateContext, "Password", nullptr));
|
|
|
|
|
|
m_passwordInput[0]->setPlaceholderText(QApplication::translate(translateContext, "Enter password", nullptr));
|
2018-11-08 21:50:49 +01:00
|
|
|
|
if (m_passwordLabel[1])
|
2018-11-11 12:30:53 +01:00
|
|
|
|
m_passwordLabel[1]->setText(QApplication::translate(translateContext, "Confirm password", nullptr));
|
2018-11-08 21:50:49 +01:00
|
|
|
|
if (m_passwordInput[1])
|
2018-11-11 12:30:53 +01:00
|
|
|
|
m_passwordInput[1]->setPlaceholderText(QApplication::translate(translateContext, "Reenter same password for confirmation", nullptr));
|
2018-11-08 21:50:49 +01:00
|
|
|
|
if (m_saveCheck)
|
2018-11-11 12:30:53 +01:00
|
|
|
|
m_saveCheck->setText(QApplication::translate(translateContext, "Save password", nullptr));
|
2018-11-04 11:26:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
void PasswordPromptDialog::updateOkEnabled()
|
2018-11-04 11:26:20 +01:00
|
|
|
|
{
|
2018-11-08 21:50:49 +01:00
|
|
|
|
bool enabled = true;
|
|
|
|
|
|
if (m_passwordInput[1])
|
|
|
|
|
|
enabled = m_passwordInput[0]->text() == m_passwordInput[1]->text();
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
auto btn = m_DialogButtons->button(QDialogButtonBox::Ok);
|
|
|
|
|
|
btn->setEnabled(enabled);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
void PasswordPromptDialog::passwordChanged(const QString &)
|
2018-11-04 11:26:20 +01:00
|
|
|
|
{
|
2018-11-08 21:50:49 +01:00
|
|
|
|
updateOkEnabled();
|
2018-11-04 11:26:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
QString PasswordPromptDialog::password() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_passwordInput[0]->text();
|
|
|
|
|
|
}
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
bool PasswordPromptDialog::saveChecked() const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_saveCheck)
|
|
|
|
|
|
return m_saveCheck->checkState() == Qt::Checked;
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|