#include "examquestionpage.h" #include "appevent.h" #include "qjsonobject.h" #include "qpainter.h" #include "tmodel.h" #include "widgets/examsmodel.h" #include "widgets/pagination.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "cwf/sqlquery.h" #include #include #include #include #include "teachertr.h" static CWF::SqlDatabaseStorage storage("QSQLITE", "localhost", "data.db", "", ""); class CheckDelegate : public QStyledItemDelegate { public: CheckDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QStyledItemDelegate::paint(painter, option, index); // 获取数据 QString tagText = index.data(Qt::DisplayRole).toString(); QColor color = Qt::lightGray; if (tagText == "1" || tagText == "true") { tagText = TeacherServer::Tr::tr("Select"); color = Qt::green; } else { tagText = TeacherServer::Tr::tr("Not Selected"); } // 设置背景色和边框 painter->save(); painter->setRenderHint(QPainter::Antialiasing); painter->setPen(Qt::NoPen); painter->setBrush(color); // 背景色 painter->drawRoundedRect(option.rect.adjusted(2, 2, -2, -2), 5, 5); // 圆角矩形 // 设置文本颜色和对齐方式 painter->setPen(QColor("#0078D4")); // 文本颜色 painter->setFont(QFont("Arial", 10)); painter->drawText(option.rect.adjusted(5, 2, -5, -2), Qt::AlignCenter, tagText); painter->restore(); } QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 返回标签的合适大小 QString tagText = index.data(Qt::DisplayRole).toString(); QFontMetrics fm(option.font); QSize size = fm.size(0, tagText); return size + QSize(10, 4); // 调整大小,以适应内容 } }; ExamsQuestionPage::ExamsQuestionPage(QWidget *parent) : QWidget{parent} { examsLineEdit = new QLineEdit; examsView = new QTableView; pagination = new Pagination; examsNameLineEdit = new QLineEdit; fileDirLineEdit = new QLineEdit; selectCheckBox = new QCheckBox; selectCheckBox->setDisabled(true); pageSize = new QComboBox; pageSize->addItem("10", 10); pageSize->addItem("20", 20); pageSize->addItem("30", 30); pageSize->addItem("50", 50); pageSize->addItem("100", 100); using namespace Layouting; auto examsEditor = Group{title(tr("Test question information")), Form{ tr("exams name"), examsNameLineEdit, br, tr("file dir"), fileDirLineEdit, PushButton{text(tr("open dir")), onClicked([this]() { getDir(); })}, PushButton{text(tr("open file")), onClicked([this]() { getFile(); })}, br, tr("select"), selectCheckBox, }, normalMargin} .emerge(); auto op = Column{PushButton{text(tr("Add")), onClicked([this]() { addExamsQuestion(); })}, PushButton{text(tr("Remove")), onClicked([this]() { removeExamsQuestion(); })}, PushButton{text(tr("Set as Default")), onClicked([this]() { setDefault(); })}, st, PushButton{text(tr("Save")), onClicked([this]() { save(); })}} .emerge(); // examsEditor->setMinimumWidth(300); op->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); // Form{"examsPath:", examsLineEdit, PushButton{text(tr("path"))}},} auto pageWidget = Row{pagination, st, tr("pageSize"), pageSize, st}; Row{Column{examsView, pageWidget, examsEditor}, op}.attachTo(this); // 考试系统 // 创建数据模型 ExamsTableModel *examsTableModel = new ExamsTableModel; // 创建表格视图 examsView->setModel(examsTableModel); examsView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); examsView->setSelectionBehavior(QAbstractItemView::SelectRows); examsView->setSelectionMode(QAbstractItemView::SingleSelection); examsView->setItemDelegateForColumn(2, new CheckDelegate(this)); // 设置第0列应用委托 // 连接信号和槽 QObject::connect(pageSize, &QComboBox::currentTextChanged, [this](const QString &) { updateExamsView(); }); QObject::connect(pagination, &Pagination::valueChanged, [this](int) { updateExamsView(); }); connect(examsView, &QTableView::clicked, this, [=](const QModelIndex &index) { ExamsTableModel *model = qobject_cast(examsView->model()); ExamsTableModel::Exams exam = model->getCurrentExam(index); examsNameLineEdit->setText(exam.name); fileDirLineEdit->setText(exam.fileDir); fileDirLineEdit->setToolTip(exam.fileDir); selectCheckBox->setChecked(exam.select); }); updateExamsView(); } void ExamsQuestionPage::updateExamsView() { int size = pageSize->currentData().toInt(); pagination->setPageSizes(size); ExamsQuestionModel examsModel(storage); ExamsTableModel *model = qobject_cast(examsView->model()); CWF::SqlQueryManager queryManager(storage); queryManager.select("COUNT(*) AS total", examsModel.getTableName()); queryManager.exec(queryManager.getQueryText()); QJsonArray array = queryManager.toJson(); if (!array.isEmpty()) { QJsonObject object = array[0].toObject(); qint64 total = object["total"].toVariant().toLongLong(); pagination->setTotal(total); } int pageNumber = pagination->currentPage(); int pageSize = pagination->pageSizes(); QString queryStr = QString("SELECT * FROM %1 LIMIT %2 OFFSET %3") .arg(examsModel.getTableName()) .arg(pageSize) .arg((pageNumber - 1) * pageSize); queryManager.exec(queryStr); model->loadJsonData(queryManager.toJson()); } void ExamsQuestionPage::addExamsQuestion() { ExamsQuestionModel examsModel(storage); CWF::SqlQueryManager queryManager(storage); queryManager.insert(examsModel.getTableName(), "name,fileDir,isSelect"); queryManager.prepare(); queryManager.addBindValue(""); queryManager.addBindValue(""); queryManager.addBindValue(false); QJsonObject status = queryManager.exec(); if (status["success"].toBool()) { updateExamsView(); } } void ExamsQuestionPage::setDefault() { ExamsTableModel *model = qobject_cast(examsView->model()); const QModelIndex index = examsView->currentIndex(); ExamsTableModel::Exams exams = model->getCurrentExam(index); if (exams.id == 0) { return; } ExamsQuestionModel examsModel(storage); CWF::SqlQueryManager queryManager(storage); //queryManager.select("*", examsModel.getTableName()).where("isSelect = ?"); queryManager.update(examsModel.getTableName(), "isSelect =(CASE WHEN id = ? THEN 1 ELSE 0 END );"); queryManager.prepare(); queryManager.addBindValue(exams.id); QJsonObject status = queryManager.exec(); if (status["success"].toBool()) { updateExamsView(); examsView->setCurrentIndex(index); AppEvent::instance()->examQuestionChenge(); } } void ExamsQuestionPage::removeExamsQuestion() { ExamsTableModel *model = qobject_cast(examsView->model()); const QModelIndex index = examsView->currentIndex(); ExamsTableModel::Exams exams = model->getCurrentExam(index); if (exams.id == 0) { return; } ExamsQuestionModel examsModel(storage); CWF::SqlQueryManager queryManager(storage); //queryManager.select("*", examsModel.getTableName()).where("isSelect = ?"); queryManager.remove(examsModel.getTableName(), "id = ?"); queryManager.prepare(); queryManager.addBindValue(exams.id); QJsonObject status = queryManager.exec(); if (status["success"].toBool()) { updateExamsView(); examsView->setCurrentIndex(index); } } void ExamsQuestionPage::save() { ExamsTableModel *model = qobject_cast(examsView->model()); const QModelIndex index = examsView->currentIndex(); ExamsTableModel::Exams exams = model->getCurrentExam(index); if (exams.id == 0) { return; } ExamsQuestionModel examsModel(storage); CWF::SqlQueryManager queryManager(storage); //queryManager.select("*", examsModel.getTableName()).where("isSelect = ?"); queryManager.update(examsModel.getTableName(), "name=?,fileDir=?,isSelect=?").where("id = ?"); queryManager.prepare(); queryManager.addBindValue(examsNameLineEdit->text()); queryManager.addBindValue(fileDirLineEdit->text()); queryManager.addBindValue(selectCheckBox->isChecked()); queryManager.addBindValue(exams.id); QJsonObject status = queryManager.exec(); if (status["success"].toBool()) { updateExamsView(); examsView->setCurrentIndex(index); } } void ExamsQuestionPage::getDir() { const QString dir = QFileDialog::getExistingDirectory(this, QObject::tr("选择目录"), "./", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); fileDirLineEdit->setText(dir); } void ExamsQuestionPage::getFile() { const QString file = QFileDialog::getOpenFileName(this, QObject::tr("选择文件"), "./", QObject::tr("所有文件 (*)")); fileDirLineEdit->setText(file); }