forked from zhipeng-jia/project-lemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarytree.cpp
291 lines (268 loc) · 11.8 KB
/
summarytree.cpp
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
/***************************************************************************
This file is part of Project Lemon
Copyright (C) 2011 Zhipeng Jia
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#include "summarytree.h"
#include "addtestcaseswizard.h"
#include "settings.h"
#include "contest.h"
#include "task.h"
#include "testcase.h"
SummaryTree::SummaryTree(QWidget *parent) :
QTreeWidget(parent)
{
curContest = 0;
addCount = 0;
addTaskAction = new QAction(tr("Add a New Task"), this);
addTestCaseAction = new QAction(tr("Add a Test Case"), this);
addTestCasesAction = new QAction(tr("Add Test Cases ..."), this);
deleteTaskAction = new QAction(tr("Delete Current Task"), this);
deleteTestCaseAction = new QAction(tr("Delete Current Test Case"), this);
deleteTaskKeyAction = new QAction(this);
deleteTestCaseKeyAction = new QAction(this);
deleteTaskKeyAction->setShortcutContext(Qt::WidgetShortcut);
deleteTestCaseKeyAction->setShortcutContext(Qt::WidgetShortcut);
deleteTaskKeyAction->setShortcut(QKeySequence::Delete);
deleteTestCaseKeyAction->setShortcut(QKeySequence::Delete);
deleteTaskKeyAction->setEnabled(false);
deleteTestCaseKeyAction->setEnabled(false);
addAction(deleteTaskKeyAction);
addAction(deleteTestCaseKeyAction);
connect(addTaskAction, SIGNAL(triggered()),
this, SLOT(addTask()));
connect(addTestCaseAction, SIGNAL(triggered()),
this, SLOT(addTestCase()));
connect(addTestCasesAction, SIGNAL(triggered()),
this, SLOT(addTestCases()));
connect(deleteTaskAction, SIGNAL(triggered()),
this, SLOT(deleteTask()));
connect(deleteTestCaseAction, SIGNAL(triggered()),
this, SLOT(deleteTestCase()));
connect(deleteTaskKeyAction, SIGNAL(triggered()),
this, SLOT(deleteTask()));
connect(deleteTestCaseKeyAction, SIGNAL(triggered()),
this, SLOT(deleteTestCase()));
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
this, SLOT(selectionChanged()));
connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)),
this, SLOT(itemChanged(QTreeWidgetItem*)));
}
void SummaryTree::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
addTaskAction->setText(QApplication::translate("SummaryTree", "Add a New Task",
0, QApplication::UnicodeUTF8));
addTestCaseAction->setText(QApplication::translate("SummaryTree", "Add a Test Case",
0, QApplication::UnicodeUTF8));
addTestCasesAction->setText(QApplication::translate("SummaryTree", "Add Test Cases ...",
0, QApplication::UnicodeUTF8));
deleteTaskAction->setText(QApplication::translate("SummaryTree", "Delete Current Task",
0, QApplication::UnicodeUTF8));
deleteTestCaseAction->setText(QApplication::translate("SummaryTree", "Delete Current Test Case",
0, QApplication::UnicodeUTF8));
for (int i = 0; i < topLevelItemCount(); i ++) {
QTreeWidgetItem *taskItem = topLevelItem(i);
for (int j = 0; j < taskItem->childCount(); j ++) {
taskItem->child(j)->setText(0, QApplication::translate("SummaryTree", "Test Case #%1",
0, QApplication::UnicodeUTF8).arg(j + 1));
}
}
}
}
void SummaryTree::setContest(Contest *contest)
{
if (curContest) {
QList<Task*> taskList = curContest->getTaskList();
for (int i = 0; i < taskList.size(); i ++)
disconnect(taskList[i], SIGNAL(problemTitleChanged(QString)),
this, SLOT(titleChanged(QString)));
}
curContest = contest;
if (! curContest) return;
setEnabled(false);
clear();
QList<Task*> taskList = curContest->getTaskList();
for (int i = 0; i < taskList.size(); i ++) {
connect(taskList[i], SIGNAL(problemTitleChanged(QString)),
this, SLOT(titleChanged(QString)));
QTreeWidgetItem *newTaskItem = new QTreeWidgetItem(this);
newTaskItem->setText(0, taskList[i]->getProblemTile());
newTaskItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
for (int j = 0; j < taskList[i]->getTestCaseList().size(); j ++) {
QTreeWidgetItem *newTestCaseItem = new QTreeWidgetItem(newTaskItem);
newTestCaseItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
newTestCaseItem->setText(0, tr("Test Case #%1").arg(newTaskItem->childCount()));
}
}
if (taskList.size() > 0) setCurrentItem(topLevelItem(0));
setEnabled(true);
emit currentItemChanged(0, 0);
}
void SummaryTree::setSettings(Settings *_settings)
{
settings = _settings;
}
void SummaryTree::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *contextMenu = new QMenu(this);
QTreeWidgetItem *curItem = currentItem();
if (! curItem) {
contextMenu->addAction(addTaskAction);
contextMenu->exec(QCursor::pos());
delete contextMenu;
return;
}
int index = indexOfTopLevelItem(curItem);
if (index != -1) {
contextMenu->addAction(addTaskAction);
contextMenu->addAction(deleteTaskAction);
contextMenu->addSeparator();
contextMenu->addAction(addTestCaseAction);
contextMenu->addAction(addTestCasesAction);
contextMenu->exec(QCursor::pos());
delete contextMenu;
} else {
contextMenu->addAction(addTaskAction);
contextMenu->addAction(deleteTaskAction);
contextMenu->addSeparator();
contextMenu->addAction(addTestCaseAction);
contextMenu->addAction(addTestCasesAction);
contextMenu->addAction(deleteTestCaseAction);
contextMenu->exec(QCursor::pos());
delete contextMenu;
}
}
void SummaryTree::addTask()
{
Task *newTask = new Task;
newTask->setAnswerFileExtension(settings->getDefaultOutputFileExtension());
curContest->addTask(newTask);
newTask->refreshCompilerConfiguration(settings);
connect(newTask, SIGNAL(problemTitleChanged(QString)),
this, SLOT(titleChanged(QString)));
QTreeWidgetItem *newItem = new QTreeWidgetItem(this);
setCurrentItem(newItem);
newItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
newItem->setText(0, tr("Problem %1").arg(++ addCount));
editItem(newItem);
}
void SummaryTree::addTestCase()
{
QTreeWidgetItem *curItem = currentItem();
if (indexOfTopLevelItem(curItem) == -1)
curItem = curItem->parent();
int index = indexOfTopLevelItem(curItem);
Task *curTask = curContest->getTask(index);
TestCase *newTestCase = new TestCase;
newTestCase->setFullScore(settings->getDefaultFullScore());
newTestCase->setTimeLimit(settings->getDefaultTimeLimit());
newTestCase->setMemoryLimit(settings->getDefaultMemoryLimit());
curTask->addTestCase(newTestCase);
QTreeWidgetItem *newItem = new QTreeWidgetItem(curItem);
newItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
newItem->setText(0, tr("Test Case #%1").arg(curItem->childCount()));
setCurrentItem(newItem);
}
void SummaryTree::addTestCases()
{
QTreeWidgetItem *curItem = currentItem();
if (indexOfTopLevelItem(curItem) == -1)
curItem = curItem->parent();
int index = indexOfTopLevelItem(curItem);
Task *curTask = curContest->getTask(index);
AddTestCasesWizard *wizard = new AddTestCasesWizard(this);
wizard->setSettings(settings, curTask->getTaskType() == Task::Traditional);
if (wizard->exec() == QDialog::Accepted) {
QList<QStringList> inputFiles = wizard->getMatchedInputFiles();
QList<QStringList> outputFiles = wizard->getMatchedOutputFiles();
for (int i = 0; i < inputFiles.size(); i ++) {
addTestCase();
QTreeWidgetItem *curItem = currentItem();
QTreeWidgetItem *parentItem = curItem->parent();
int taskIndex = indexOfTopLevelItem(parentItem);
int testCaseIndex = parentItem->indexOfChild(curItem);
Task *curTask = curContest->getTask(taskIndex);
TestCase *curTestCase = curTask->getTestCase(testCaseIndex);
curTestCase->setFullScore(wizard->getFullScore());
curTestCase->setTimeLimit(wizard->getTimeLimit());
curTestCase->setMemoryLimit(wizard->getMemoryLimit());
for (int j = 0; j < inputFiles[i].size(); j ++)
curTestCase->addSingleCase(inputFiles[i][j], outputFiles[i][j]);
setCurrentItem(parentItem);
setCurrentItem(curItem);
}
}
delete wizard;
}
void SummaryTree::deleteTask()
{
if (QMessageBox::warning(this, tr("Lemon"), tr("Are you sure to delete this task?"),
QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Cancel)
return;
QTreeWidgetItem *curItem = currentItem();
if (indexOfTopLevelItem(curItem) == -1)
curItem = curItem->parent();
int index = indexOfTopLevelItem(curItem);
if (index + 1 < topLevelItemCount())
setCurrentItem(topLevelItem(index + 1));
else
if (index - 1 >= 0)
setCurrentItem(topLevelItem(index - 1));
else
setCurrentItem(0);
delete curItem;
curContest->deleteTask(index);
}
void SummaryTree::deleteTestCase()
{
QTreeWidgetItem *curItem = currentItem();
QTreeWidgetItem *parentItem = curItem->parent();
int taskIndex = indexOfTopLevelItem(parentItem);
int testCaseIndex = parentItem->indexOfChild(curItem);
Task *curTask = curContest->getTask(taskIndex);
delete curItem;
curTask->deleteTestCase(testCaseIndex);
for (int i = 0; i < parentItem->childCount(); i ++)
parentItem->child(i)->setText(0, tr("Test Case #%1").arg(i + 1));
}
void SummaryTree::selectionChanged()
{
if (! isEnabled()) return;
QTreeWidgetItem *curItem = currentItem();
if (! curItem) {
deleteTaskKeyAction->setEnabled(false);
deleteTestCaseKeyAction->setEnabled(false);
return;
}
int index = indexOfTopLevelItem(curItem);
if (index != -1) {
deleteTaskKeyAction->setEnabled(true);
deleteTestCaseKeyAction->setEnabled(false);
} else {
deleteTaskKeyAction->setEnabled(false);
deleteTestCaseKeyAction->setEnabled(true);
}
}
void SummaryTree::itemChanged(QTreeWidgetItem *item)
{
int index = indexOfTopLevelItem(item);
if (index != -1) {
Task *curTask = curContest->getTask(index);
curTask->setProblemTitle(item->text(0));
}
}
void SummaryTree::titleChanged(const QString &title)
{
QTreeWidgetItem *curItem = currentItem();
if (curItem) curItem->setText(0, title);
}