#include "passwordreset.h" #include "numpaddialog.h" #include #include "confirmdialog.h" #include #include #include #include #include #include #include "global.h" PasswordResetPage::PasswordResetPage(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(50); lay->setAlignment(Qt::AlignHCenter); QLabel* title = label("重置密码界面", 60, C::TEXT, true, col); title->setAlignment(Qt::AlignCenter); lay->addWidget(title); m_input = new QLineEdit(col); m_input->setReadOnly(true); m_input->setEchoMode(QLineEdit::Password); m_input->setAlignment(Qt::AlignCenter); m_input->setFixedSize(400, 90); m_input->setStyleSheet("font-size:60px;"); m_input->setFocusPolicy(Qt::StrongFocus); m_input->installEventFilter(this); lay->addWidget(m_input, 0, Qt::AlignHCenter); m_hint = label("请输入 4 位有效密码", 50, C::TEXT_SUB, false, col); m_hint->setAlignment(Qt::AlignCenter); lay->addWidget(m_hint); QHBoxLayout* btnRow = new QHBoxLayout(); btnRow->setSpacing(80); btnRow->addStretch(1); QPushButton* ok = button("确认", "", col); ok->setFixedSize(160, 120); ok->setStyleSheet("font-size:60px;"); connect(ok, &QPushButton::clicked, this, &PasswordResetPage::onConfirm); QPushButton* cancel = button("取消", "", col); cancel->setFixedSize(160, 120); cancel->setStyleSheet(cancelButtonStyle(60)); // 选中时红底白字 connect(cancel, &QPushButton::clicked, this, &PasswordResetPage::onBack); QPushButton* exit = button("退出", "", col); exit->setFixedSize(160, 120); exit->setStyleSheet(cancelButtonStyle(60)); // 选中时红底白字 connect(exit, &QPushButton::clicked, this, &PasswordResetPage::onExit); btnRow->addWidget(ok); btnRow->addWidget(cancel); btnRow->addWidget(exit); btnRow->addStretch(1); lay->addLayout(btnRow); root->addWidget(col, 0, Qt::AlignHCenter); root->addStretch(1); } bool PasswordResetPage::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(ev); // 空格当“点击”:Tab 聚焦后按空格弹出数字键盘 if (Keys::space(ke->key())) { onInputClicked(); return true; } } } return BasePage::eventFilter(obj, ev); } void PasswordResetPage::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_hint->setText("请输入 4 位有效密码"); m_hint->setStyleSheet(QString("color:%1;font-size:60px;").arg(C::TEXT_SUB)); } } void PasswordResetPage::onConfirm() { // 校验:4 位数字 static const QRegularExpression re("^\\d{4}$"); if (!re.match(m_value).hasMatch()) { m_value.clear(); // 校验失败后清空输入框 m_input->setText(m_value); m_hint->setText("密码须为 4 位数字"); m_hint->setStyleSheet(QString("color:%1;font-size:60px;font-weight:700;").arg(C::DANGER)); return; } ctx()->setPassword(m_value); clearInput(); ConfirmDialog::toast(this, "修改成功"); // 2s 后自动消失 emit navigate(PageBoot); // 修改完成,返回开机密码界面 } void PasswordResetPage::onBack() { clearInput(); emit navigate(PageMgmt); // 取消:返回上一界面(密码管理界面) } void PasswordResetPage::onExit() { clearInput(); emit navigate(PageBoot); // 退出:返回开机密码界面 } void PasswordResetPage::clearInput() { m_value.clear(); m_input->setText(m_value); m_hint->setText("请输入 4 位有效密码"); m_hint->setStyleSheet(QString("color:%1;font-size:60px;").arg(C::TEXT_SUB)); }