You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ExamSystem/src/frontend/selection_widget.cc

180 lines
5.8 KiB

#include "selection_widget.h"
#include <QButtonGroup>
#include <QGroupBox>
#include <QApplication>
SelectionWidget::SelectionWidget(exam_system::BackendInterface* backend, QWidget* parent)
: BaseWidget(backend, parent) {
setupUI();
}
void SelectionWidget::setupUI()
{
auto* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(20, 10, 20, 20);
mainLayout->setSpacing(15);
// 创建菜单栏
createMenuBar();
mainLayout->setMenuBar(menuBar);
// 欢迎标签
welcomeLabel = new QLabel("欢迎使用数学学习软件");
welcomeLabel->setAlignment(Qt::AlignCenter);
welcomeLabel->setStyleSheet("font-size: 24px; font-weight: bold; margin: 20px;");
mainLayout->addWidget(welcomeLabel);
// 用户信息标签
userInfoLabel = new QLabel();
userInfoLabel->setAlignment(Qt::AlignCenter);
userInfoLabel->setStyleSheet("font-size: 14px; margin-bottom: 30px;");
mainLayout->addWidget(userInfoLabel);
// 难度选择区域
auto* difficultyLabel = new QLabel("选择学习阶段:");
difficultyLabel->setStyleSheet("font-size: 16px;");
mainLayout->addWidget(difficultyLabel);
auto* buttonLayout = new QHBoxLayout();
primaryBtn = new QPushButton("小学");
juniorBtn = new QPushButton("初中");
seniorBtn = new QPushButton("高中");
// 难度选择按钮
QString buttonStyle = "QPushButton { font-size: 14px; padding: 10px 20px; min-width: 100px; }";
primaryBtn->setStyleSheet(buttonStyle);
juniorBtn->setStyleSheet(buttonStyle);
seniorBtn->setStyleSheet(buttonStyle);
buttonLayout->addWidget(primaryBtn);
buttonLayout->addWidget(juniorBtn);
buttonLayout->addWidget(seniorBtn);
mainLayout->addLayout(buttonLayout);
// 题目数量选择
auto* countLayout = new QHBoxLayout();
auto* countLabel = new QLabel("题目数量:");
countLabel->setStyleSheet("font-size: 14px;");
countSpinBox = new QSpinBox;
countSpinBox->setRange(5, 50);
countSpinBox->setValue(10);
countLayout->addWidget(countLabel);
countLayout->addWidget(countSpinBox);
countLayout->addStretch();
mainLayout->addLayout(countLayout);
// 开始测试按钮
startBtn = new QPushButton("开始测试");
startBtn->setEnabled(false);
startBtn->setStyleSheet("QPushButton { font-size: 16px; padding: 10px 20px; background-color: #4CAF50; color: white; }");
auto* startLayout = new QHBoxLayout();
startLayout->addStretch();
startLayout->addWidget(startBtn);
startLayout->addStretch();
mainLayout->addLayout(startLayout);
mainLayout->addStretch();
// 连接信号槽
connect(primaryBtn, &QPushButton::clicked, this, &SelectionWidget::onPrimaryClicked);
connect(juniorBtn, &QPushButton::clicked, this, &SelectionWidget::onJuniorClicked);
connect(seniorBtn, &QPushButton::clicked, this, &SelectionWidget::onSeniorClicked);
connect(startBtn, &QPushButton::clicked, this, &SelectionWidget::onStartClicked);
}
void SelectionWidget::createMenuBar()
{
menuBar = new QMenuBar(this);
// 用户菜单
userMenu = menuBar->addMenu("用户");
userInfoAction = new QAction("个人信息", this);
changePasswordAction = new QAction("修改密码", this);
logoutAction = new QAction("退出登录", this);
userMenu->addAction(userInfoAction);
userMenu->addAction(changePasswordAction);
userMenu->addSeparator();
userMenu->addAction(logoutAction);
// 系统菜单
systemMenu = menuBar->addMenu("系统");
exitAction = new QAction("退出程序", this);
systemMenu->addAction(exitAction);
// 连接菜单动作
connect(userInfoAction, &QAction::triggered, this, &SelectionWidget::showUserInfo);
connect(changePasswordAction, &QAction::triggered, this, &SelectionWidget::onChangePasswordClicked);
connect(logoutAction, &QAction::triggered, this, &SelectionWidget::onLogoutClicked);
connect(exitAction, &QAction::triggered, this, &SelectionWidget::onExitClicked);
}
void SelectionWidget::setCurrentUserName(const QString& userName)
{
currentUserName = userName;
userInfoLabel->setText(QString("当前用户: %1").arg(userName));
}
void SelectionWidget::onPrimaryClicked() {
selectedDifficulty = "primary";
showMessage("您已选择小学难度");
startBtn->setEnabled(true);
}
void SelectionWidget::onJuniorClicked() {
selectedDifficulty = "junior";
showMessage("您已选择初中难度");
startBtn->setEnabled(true);
}
void SelectionWidget::onSeniorClicked() {
selectedDifficulty = "senior";
showMessage("您已选择高中难度");
startBtn->setEnabled(true);
}
void SelectionWidget::onStartClicked() {
if (!selectedDifficulty.isEmpty()) {
emit startRequest(selectedDifficulty, countSpinBox->value());
}
}
void SelectionWidget::onChangePasswordClicked()
{
ChangePasswordDialog dialog(backend, this);
if (dialog.exec() == QDialog::Accepted) {
QMessageBox::information(this, "成功", "密码修改成功!");
}
}
void SelectionWidget::onLogoutClicked()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "确认退出", "确定要退出登录吗?",
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
emit logoutRequested();
}
}
void SelectionWidget::onExitClicked()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "退出程序", "确定要退出程序吗?",
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
QApplication::quit();
}
}
void SelectionWidget::showUserInfo()
{
QMessageBox::information(this, "个人信息",
QString("用户名: %1\n\n欢迎使用数学学习软件!").arg(currentUserName));
}