116 lines
3.2 KiB
C++
116 lines
3.2 KiB
C++
#include "passwordmgmtlogin.h"
|
|
#include "numpaddialog.h"
|
|
#include <QKeyEvent>
|
|
#include <QVBoxLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QLineEdit>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QTimer>
|
|
#include "global.h"
|
|
|
|
PasswordMgmtLoginPage::PasswordMgmtLoginPage(AppContext* ctx, QWidget* parent)
|
|
: BasePage(ctx, parent)
|
|
{
|
|
QVBoxLayout* root = new QVBoxLayout(this);
|
|
root->addStretch(1);
|
|
|
|
QWidget* col = new QWidget(this);
|
|
col->setFixedWidth(700);
|
|
QVBoxLayout* lay = new QVBoxLayout(col);
|
|
lay->setSpacing(24);
|
|
lay->setAlignment(Qt::AlignHCenter);
|
|
|
|
QLabel* title = label("密码管理登录界面", 28, C::TEXT, true, col);
|
|
title->setAlignment(Qt::AlignCenter);
|
|
lay->addWidget(title);
|
|
|
|
QLabel* sub = label("请输入管理员密码", 18, C::TEXT_SUB, false, col);
|
|
sub->setAlignment(Qt::AlignCenter);
|
|
lay->addWidget(sub);
|
|
|
|
m_input = new QLineEdit(col);
|
|
m_input->setReadOnly(true);
|
|
m_input->setEchoMode(QLineEdit::Password);
|
|
m_input->setAlignment(Qt::AlignCenter);
|
|
m_input->setFixedSize(400, 88);
|
|
m_input->setFocusPolicy(Qt::StrongFocus);
|
|
m_input->installEventFilter(this);
|
|
lay->addWidget(m_input, 0, Qt::AlignHCenter);
|
|
|
|
m_error = label("管理员密码错误", 18, C::DANGER, true, col);
|
|
m_error->setAlignment(Qt::AlignCenter);
|
|
m_error->hide();
|
|
lay->addWidget(m_error);
|
|
|
|
QHBoxLayout* btnRow = new QHBoxLayout();
|
|
btnRow->addStretch(1);
|
|
QPushButton* ok = button("确认", "", col);
|
|
ok->setFixedSize(180, 72);
|
|
connect(ok, &QPushButton::clicked, this, &PasswordMgmtLoginPage::onConfirm);
|
|
QPushButton* cancel = button("取消", "", col);
|
|
cancel->setFixedSize(180, 72);
|
|
connect(cancel, &QPushButton::clicked, this, &PasswordMgmtLoginPage::onCancel);
|
|
btnRow->addWidget(ok);
|
|
btnRow->addWidget(cancel);
|
|
btnRow->addStretch(1);
|
|
lay->addLayout(btnRow);
|
|
|
|
root->addWidget(col, 0, Qt::AlignHCenter);
|
|
root->addStretch(1);
|
|
}
|
|
|
|
bool PasswordMgmtLoginPage::eventFilter(QObject* obj, QEvent* ev)
|
|
{
|
|
if (obj == m_input) {
|
|
if (ev->type() == QEvent::MouseButtonPress) {
|
|
onInputClicked();
|
|
return true;
|
|
}
|
|
if (ev->type() == QEvent::KeyPress) {
|
|
QKeyEvent* ke = static_cast<QKeyEvent*>(ev);
|
|
if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter) {
|
|
onInputClicked();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return BasePage::eventFilter(obj, ev);
|
|
}
|
|
|
|
void PasswordMgmtLoginPage::onInputClicked()
|
|
{
|
|
NumPadDialog dlg(this);
|
|
dlg.setMaxLen(4);
|
|
dlg.setValue(m_value);
|
|
if (dlg.exec() == QDialog::Accepted) {
|
|
m_value = dlg.value();
|
|
m_input->setText(m_value);
|
|
m_error->hide();
|
|
}
|
|
}
|
|
|
|
void PasswordMgmtLoginPage::onConfirm()
|
|
{
|
|
if (m_value == ctx()->adminPassword()) {
|
|
clearInput();
|
|
emit navigate(PageMgmt);
|
|
} else {
|
|
m_error->show();
|
|
QTimer::singleShot(3000, this, [this]() { m_error->hide(); });
|
|
}
|
|
}
|
|
|
|
void PasswordMgmtLoginPage::onCancel()
|
|
{
|
|
clearInput();
|
|
emit navigate(PageBoot);
|
|
}
|
|
|
|
void PasswordMgmtLoginPage::clearInput()
|
|
{
|
|
m_value.clear();
|
|
m_input->setText(m_value);
|
|
m_error->hide();
|
|
}
|