#include "bootpassword.h" #include "numpaddialog.h" #include #include #include #include #include #include #include "global.h" BootPasswordPage::BootPasswordPage(AppContext* ctx, QWidget* parent) : BasePage(ctx, parent) { QVBoxLayout* root = new QVBoxLayout(this); root->setContentsMargins(0, 0, 0, 0); root->addStretch(1); QWidget* col = new QWidget(this); col->setFixedWidth(600); col->setProperty("card", true); // 登录主体框体背景(面板色 + 圆角边框) QVBoxLayout* lay = new QVBoxLayout(col); lay->setContentsMargins(40, 40, 40, 40); // 框体内边距 lay->setSpacing(30); lay->setAlignment(Qt::AlignHCenter); // 标题 QLabel* title = label("请输入开机密码", 60, C::TEXT, true, col); title->setAlignment(Qt::AlignCenter); lay->addWidget(title); lay->addSpacing(30); // 密码输入框(只读回显,点击弹出数字键盘) m_input = new QLineEdit(col); m_input->setReadOnly(true); m_input->setEchoMode(QLineEdit::Password); m_input->setAlignment(Qt::AlignCenter); m_input->setFixedSize(450, 120); m_input->setStyleSheet("font-size:50px"); m_input->setProperty("clickable", true); lay->addWidget(m_input, 0, Qt::AlignHCenter); //lay->addSpacing(20); m_input->installEventFilter(this); m_input->setFocusPolicy(Qt::StrongFocus); // 错误提示(预留固定高度,出现/消失不影响下方按钮位置) m_error = new QLabel(col); m_error->setAlignment(Qt::AlignCenter); //m_error->setFixedHeight(30); m_error->setStyleSheet("color:#E5484D;font-size:40px;font-weight:700;"); lay->addWidget(m_error); // 确认 / 取消 QHBoxLayout* btnRow = new QHBoxLayout(); btnRow->setSpacing(80); btnRow->addStretch(1); QPushButton* ok = button("确认", "", col); ok->setFixedSize(160, 100); ok->setStyleSheet("font-size:60px;"); connect(ok, &QPushButton::clicked, this, &BootPasswordPage::onConfirm); QPushButton* cancel = button("清空", "", col); cancel->setFixedSize(160, 100); cancel->setStyleSheet(cancelButtonStyle(60)); // 选中时红底白字 connect(cancel, &QPushButton::clicked, this, &BootPasswordPage::onCancel); btnRow->addWidget(ok); btnRow->addWidget(cancel); btnRow->addStretch(1); lay->addLayout(btnRow); // 密码管理入口(平时红字透明底,按下红底白字) const QString linkBtnStyle = "QPushButton{color:#E5484D;background:transparent;border:1px solid transparent;border-radius:6px;font-size:40px;}" "QPushButton:hover,QPushButton:pressed,QPushButton:focus{background-color:#E5484D;color:#FFFFFF;border:1px solid #FFA6A9;}"; QPushButton* mgmt = button("密码管理", "", col); mgmt->setFixedSize(170, 60); mgmt->setStyleSheet(linkBtnStyle); connect(mgmt, &QPushButton::clicked, this, &BootPasswordPage::onMgmt); lay->addWidget(mgmt, 0, Qt::AlignHCenter); root->addWidget(col, 0, Qt::AlignHCenter); root->addStretch(1); refreshInput(); } // 点击密码输入框 → 弹出数字键盘(拦截鼠标点击) bool BootPasswordPage::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 BootPasswordPage::onInputClicked() { NumPadDialog dlg(this); dlg.setMaxLen(4); dlg.setValue(m_value); if (dlg.exec() == QDialog::Accepted) { m_value = dlg.value(); m_error->setText(""); refreshInput(); } } void BootPasswordPage::onConfirm() { if (m_value == ctx()->password()) { clearInput(); emit navigate(PageSelfCheck); } else { clearInput(); m_error->setText("密码错误!"); } } void BootPasswordPage::onCancel() { clearInput(); } void BootPasswordPage::onMgmt() { clearInput(); // 密码管理:进入管理员登录界面 emit navigate(PageMgmtLogin); } void BootPasswordPage::clearInput() { m_value.clear(); m_error->setText(""); refreshInput(); } void BootPasswordPage::focusInput() { onInputClicked(); } void BootPasswordPage::refreshInput() { const bool empty = m_value.isEmpty(); // 空态:无论是否被选中都显示提示文字(灰字);有内容:显示密码圆点 m_input->setEchoMode(empty ? QLineEdit::Normal : QLineEdit::Password); m_input->setText(empty ? "点击输入密码" : m_value); m_input->setStyleSheet(empty ? "font-size:50px;color:#9AA7B4;" : "font-size:50px;"); }