-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
352 lines (320 loc) · 12.2 KB
/
widget.cpp
File metadata and controls
352 lines (320 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "widget.h"
#include "./ui_widget.h"
#include <QDesktopServices>
#include <QJsonDocument>
#include <QInputDialog>
#include <QJsonObject>
#include <QJsonArray>
#include <QListWidget>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include "yaml-cpp/yaml.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget) {
ui->setupUi(this);
this->loadExams();
}
Widget::~Widget() {
delete ui;
}
void Widget::setStatus(const QString &status) {
this->ui->statusLabel->setText("当前状态:" + status);
}
void Widget::updateExamsList() {
this->setStatus("更新试卷列表");
this->ui->examsListWidget->clear();
for (const auto& exam:this->exams) {
this->ui->examsListWidget->addItem(exam.name);
}
this->ui->deleteProblemPushButton->setEnabled(false);
this->ui->addProblemPushButton->setEnabled(false);
this->ui->saveProblemPushButton->setEnabled(false);
this->ui->addChoicePushButton->setEnabled(false);
this->ui->deleteChoicePushButton->setEnabled(false);
this->ui->exportExamPushButton->setEnabled(false);
this->setStatus("就绪");
}
void Widget::updateProblemsList() {
this->setStatus("更新题目列表");
this->ui->problemsListWidget->clear();
const auto& exam = this->getCurrentExam();
for (const auto& problem:exam.problems) {
this->ui->problemsListWidget->addItem(problem.statement.left(12) + "...");
}
this->ui->addProblemPushButton->setEnabled(true);
this->ui->saveProblemPushButton->setEnabled(false);
this->ui->addChoicePushButton->setEnabled(false);
this->ui->deleteProblemPushButton->setEnabled(false);
this->ui->deleteChoicePushButton->setEnabled(false);
this->ui->exportExamPushButton->setEnabled(true);
this->setStatus("就绪");
}
void Widget::updateProblemDetail() {
this->setStatus("更新题目详情");
const auto& problem = this->getCurrentProblem();
this->ui->statementTextEdit->setPlainText(problem.statement);
this->ui->choicesListWidget->clear();
for (const auto& choice:problem.choices) {
this->ui->choicesListWidget->addItem(choice);
}
this->ui->correctAnswerLineEdit->setText(problem.correctChoice);
this->ui->scoreDoubleSpinBox->setValue(problem.score);
this->ui->deleteProblemPushButton->setEnabled(true);
this->ui->saveProblemPushButton->setEnabled(true);
this->ui->addChoicePushButton->setEnabled(true);
this->ui->deleteChoicePushButton->setEnabled(false);
this->setStatus("就绪");
}
void Widget::on_addExamPushButton_clicked() {
this->setStatus("添加试卷");
// 弹出一个输入框,要求用户输入试卷名
const QString name = QInputDialog::getText(this, "添加试卷", "请输入试卷名:");
if (name.isEmpty()) return;
this->exams.append(Exam(name, QList<Problem>()));
this->setStatus("就绪");
this->updateExamsList();
}
void Widget::loadExams() {
this->setStatus("加载试卷列表");
QDir examDir("./exams/");
if (!examDir.exists()) examDir.mkdir(".");
for (const auto& info:examDir.entryInfoList()) {
if (!info.fileName().endsWith("json")) continue;
QFile file(info.absoluteFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) continue;
auto jsonDoc = QJsonDocument::fromJson(file.readAll());
this->exams.append(Exam(jsonDoc.object()));
file.close();
}
this->setStatus("就绪");
this->updateExamsList();
}
Problem::Problem() {
this->statement = "请您在这里填写题目描述,不需要包含选项。";
this->choices = QList<QString>();
this->choices << "选项 A 描述" << "选项 B 描述" << "选项 C 描述" << "选项 D 描述";
this->correctChoice = "A";
this->score = 2.0;
}
Problem::Problem(const QJsonObject &obj) {
this->statement = obj["statement"].toString();
this->choices = QStringList();
for (const auto& choice:obj["choices"].toArray()) {
this->choices.append(choice.toString());
}
this->correctChoice = obj["correctChoice"].toString();
this->score = obj["score"].toDouble();
}
void Problem::editProblem(const QString &_statement, const QList<QString> &_choices, const QString &_correctChoice, double _score) {
this->statement = _statement;
this->choices = _choices;
this->correctChoice = _correctChoice;
this->score = _score;
}
QJsonObject Problem::toJsonObject() const {
QJsonObject obj;
obj["statement"] = this->statement;
QJsonArray _choices;
for (const auto& choice:this->choices) {
_choices.append(choice);
}
obj["choices"] = _choices;
obj["correctChoice"] = this->correctChoice;
obj["score"] = this->score;
return obj;
}
Exam::Exam(const QJsonObject &obj) {
this->name = obj["name"].toString();
for (const auto& problem:obj["problems"].toArray()) {
this->problems.append(Problem(problem.toObject()));
}
}
Exam *Exam::addProblem(const Problem &problem) {
this->problems.append(problem);
this->save();
return this;
}
Exam::Exam(const QString &_name, const QList<Problem> &_problems) {
this->name = _name;
this->problems = _problems;
this->save();
}
void Exam::save() {
QFile file("./exams/" + this->name + ".json");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return;
QJsonObject obj;
obj["name"] = this->name;
QJsonArray array;
for (const auto& problem:this->problems) {
array.append(problem.toJsonObject());
}
obj["problems"] = array;
QJsonDocument doc;
doc.setObject(obj);
file.write(doc.toJson());
file.close();
}
Exam *Exam::removeProblem(int index) {
this->problems.removeAt(index);
return this;
}
Exam& Widget::getCurrentExam() {
if (this->ui->examsListWidget->currentRow() == -1) throw std::runtime_error("no exam selected");
return this->exams[this->ui->examsListWidget->currentRow()];
}
Problem& Widget::getCurrentProblem() {
if (this->ui->problemsListWidget->currentRow() == -1) throw std::runtime_error("no problem selected");
return this->getCurrentExam().problems[this->ui->problemsListWidget->currentRow()];
}
void Widget::on_addProblemPushButton_clicked() {
this->setStatus("添加题目");
this->getCurrentExam().addProblem(Problem())->save();
this->setStatus("就绪");
this->updateProblemsList();
}
void Widget::on_deleteProblemPushButton_clicked() {
if (this->ui->problemsListWidget->currentRow() == -1) return;
this->setStatus("删除题目");
this->getCurrentExam().removeProblem(this->ui->problemsListWidget->currentRow())->save();
this->setStatus("就绪");
this->updateProblemsList();
}
void Widget::on_examsListWidget_itemClicked([[maybe_unused]] QListWidgetItem *item) {
this->updateProblemsList();
}
void Widget::on_problemsListWidget_itemClicked([[maybe_unused]] QListWidgetItem *item) {
this->updateProblemDetail();
}
void Widget::on_saveProblemPushButton_clicked() {
this->setStatus("保存题目");
QList<QString> choices;
for (int i = 0; i < this->ui->choicesListWidget->count(); i++) {
choices.append(this->ui->choicesListWidget->item(i)->text());
}
this->getCurrentProblem().editProblem(this->ui->statementTextEdit->toPlainText(), choices, this->ui->correctAnswerLineEdit->text(), this->ui->scoreDoubleSpinBox->value());
this->getCurrentExam().save();
this->setStatus("就绪");
this->updateProblemDetail();
this->updateProblemsList();
}
void Widget::on_deleteChoicePushButton_clicked() {
if (this->ui->choicesListWidget->currentRow() == -1) return;
this->setStatus("删除选项");
this->ui->choicesListWidget->takeItem(this->ui->choicesListWidget->currentRow());
this->setStatus("就绪");
}
void Widget::on_addChoicePushButton_clicked() {
this->setStatus("添加选项");
// 弹出输入框,要求用户输入选项
QString choice = QInputDialog::getText(this, "添加选项", "请输入选项内容:");
if (choice.isEmpty()) return;
this->ui->choicesListWidget->addItem(choice);
this->setStatus("就绪");
}
void Widget::on_choicesListWidget_itemClicked([[maybe_unused]] QListWidgetItem *item) {
this->ui->deleteChoicePushButton->setEnabled(true);
}
void Widget::on_choicesListWidget_itemDoubleClicked(QListWidgetItem *item) {
// 弹出输入框,要求用户输入新选项
this->setStatus("修改选项");
QString choice = QInputDialog::getText(this, "修改选项", "请输入选项内容:", QLineEdit::Normal, item->text());
if (choice.isEmpty()) return;
item->setText(choice);
this->setStatus("就绪");
}
void Widget::on_exportExamPushButton_clicked() {
const auto checkProblem = [](const Problem &problem) {
if (problem.statement.isEmpty()) return false;
if (problem.choices.empty()) return false;
if (problem.correctChoice.size() > 1) return false;
if (problem.correctChoice[0].toLatin1() - 'A' > problem.choices.size()) return false;
return true;
};
const auto generateProblemYaml = [this]() {
YAML::Node node;
// 打开输入框,要求用户输入 PID
QString pid = QInputDialog::getText(this, "导出试卷", "请输入在 Hydro 中的 PID:");
if (pid.isEmpty()) return node;
node["pid"] = pid.toStdString();
node["owner"] = 1;
node["title"] = this->getCurrentExam().name.toStdString();
return node;
};
const auto generateMarkdown = [this]() {
const auto generateOne = [](const Problem &problem, int idx) {
QString choices = "";
for (const auto &choice:problem.choices) {
choices += QString("- %1\n").arg(choice);
}
return QString("%1\n\n{{ select(%2) }}\n\n%3\n")
.arg(problem.statement)
.arg(idx)
.arg(choices);
};
QString statement;
int idx = 0;
for (const auto &problem:this->getCurrentExam().problems) {
statement += generateOne(problem, ++idx);
}
return statement;
};
const auto generateConfigYaml = [this]() {
YAML::Node node;
node["type"] = "objective";
YAML::Node answer;
int idx = 0;
for (const auto &problem:this->getCurrentExam().problems) {
idx++;
answer[QString("%1").arg(idx).toStdString()].push_back(problem.correctChoice.toStdString());
answer[QString("%1").arg(idx).toStdString()].push_back(problem.score);
}
node["answers"] = answer;
return node;
};
int idx = 0;
for (const auto &problem:this->getCurrentExam().problems) {
if (!checkProblem(problem)) {
QMessageBox::warning(this, "导出失败", QString("题目 %1 不合法。").arg(idx));
return;
}
idx++;
}
// 创建一个临时目录
QString path = QString("./export/%1_%2/testdata")
.arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"),
this->getCurrentExam().name);
QDir tempDir(path);
if (!tempDir.exists()) {
if (!tempDir.mkpath(".")) {
QMessageBox::warning(this, "导出失败", QString("无法创建临时目录 %1。").arg(tempDir.absolutePath()));
return;
}
}
tempDir.cdUp();
QFile problemYaml(tempDir.path() + "/problem.yaml");
if (!problemYaml.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "导出失败", "无法创建临时文件。");
return;
}
problemYaml.write(Dump(generateProblemYaml()).c_str());
problemYaml.close();
QFile statementMd(tempDir.path() + "/problem_zh.md");
if (!statementMd.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "导出失败", "无法创建临时文件。");
return;
}
statementMd.write(generateMarkdown().toUtf8());
statementMd.close();
QFile configYaml(tempDir.path() + "/testdata/config.yaml");
if (!configYaml.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "导出失败", "无法创建临时文件。");
return;
}
configYaml.write(Dump(generateConfigYaml()).c_str());
configYaml.close();
tempDir.cdUp();
QDesktopServices::openUrl(QUrl::fromLocalFile(tempDir.path()));
}