This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
forked from zhipeng-jia/project-lemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resultviewer.cpp
264 lines (243 loc) · 10.6 KB
/
resultviewer.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
/***************************************************************************
This file is part of Project Lemon
Copyright (C) 2011 Zhipeng Jia
Copyright (C) 2016 Menci
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 "resultviewer.h"
#include "judgingdialog.h"
#include "contestant.h"
#include "settings.h"
#include "contest.h"
#include "task.h"
#include "detaildialog.h"
ResultViewer::ResultViewer(QWidget *parent) :
QTableWidget(parent)
{
curContest = 0;
deleteContestantAction = new QAction(tr("Delete"), this);
detailInformationAction = new QAction(tr("Details"), this);
judgeSelectedAction = new QAction(tr("Judge"), this);
deleteContestantKeyAction = new QAction(this);
deleteContestantKeyAction->setShortcut(QKeySequence::Delete);
deleteContestantKeyAction->setShortcutContext(Qt::WidgetShortcut);
addAction(deleteContestantKeyAction);
connect(deleteContestantAction, SIGNAL(triggered()),
this, SLOT(deleteContestant()));
connect(detailInformationAction, SIGNAL(triggered()),
this, SLOT(detailInformation()));
connect(judgeSelectedAction, SIGNAL(triggered()),
this, SLOT(judgeSelected()));
connect(deleteContestantKeyAction, SIGNAL(triggered()),
this, SLOT(deleteContestant()));
connect(this, SIGNAL(cellDoubleClicked(int, int)),
this, SLOT(detailInformation()));
}
void ResultViewer::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
deleteContestantAction->setText(QApplication::translate("ResultViewer", "Delete",
0));
detailInformationAction->setText(QApplication::translate("ResultViewer", "Details",
0));
judgeSelectedAction->setText(QApplication::translate("ResultViewer", "Judge",
0));
}
}
void ResultViewer::contextMenuEvent(QContextMenuEvent *event)
{
QList<QTableWidgetSelectionRange> selectionRange = selectedRanges();
if (selectionRange.size() == 0) return;
QMenu *contextMenu = new QMenu(this);
if (selectionRange.size() == 1 && selectionRange[0].rowCount() == 1) {
contextMenu->addAction(detailInformationAction);
contextMenu->setDefaultAction(detailInformationAction);
}
if (selectionRange.size() > 0) {
contextMenu->addAction(judgeSelectedAction);
}
contextMenu->addAction(deleteContestantAction);
contextMenu->exec(QCursor::pos());
delete contextMenu;
}
void ResultViewer::setContest(Contest *contest)
{
if (curContest) {
disconnect(curContest, SIGNAL(taskAddedForViewer()),
this, SLOT(refreshViewer()));
disconnect(curContest, SIGNAL(taskDeletedForViewer(int)),
this, SLOT(refreshViewer()));
disconnect(curContest, SIGNAL(problemTitleChanged()),
this, SLOT(refreshViewer()));
disconnect(curContest, SIGNAL(taskJudgingFinished()),
this, SLOT(refreshViewer()));
}
curContest = contest;
if (! curContest) return;
connect(curContest, SIGNAL(taskAddedForViewer()),
this, SLOT(refreshViewer()));
connect(curContest, SIGNAL(taskDeletedForViewer(int)),
this, SLOT(refreshViewer()));
connect(curContest, SIGNAL(problemTitleChanged()),
this, SLOT(refreshViewer()));
connect(curContest, SIGNAL(taskJudgingFinished()),
this, SLOT(refreshViewer()));
}
void ResultViewer::refreshViewer()
{
clear();
setRowCount(0);
setColumnCount(0);
if (! curContest) return;
QStringList headerList;
headerList << tr("Name") << tr("Rank");
QList<Task*> taskList = curContest->getTaskList();
for (int i = 0; i < taskList.size(); i ++) {
headerList << taskList[i]->getProblemTile();
}
headerList << tr("Total Score") << tr("Total Used Time (s)") << tr("Judging Time");
setColumnCount(taskList.size() + 5);
setHorizontalHeaderLabels(headerList);
horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
QList<Contestant*> contestantList = curContest->getContestantList();
QList< QPair<int, QString> > sortList;
setRowCount(contestantList.size());
for (int i = 0; i < contestantList.size(); i ++) {
setItem(i, 0, new QTableWidgetItem(contestantList[i]->getContestantName()));
setItem(i, 1, new QTableWidgetItem());
for (int j = 0; j < taskList.size(); j ++) {
setItem(i, j + 2, new QTableWidgetItem());
int score = contestantList[i]->getTaskScore(j);
if (score != -1) {
item(i, j + 2)->setData(Qt::DisplayRole, score);
} else {
item(i, j + 2)->setText(tr("Invalid"));
}
}
setItem(i, taskList.size() + 2, new QTableWidgetItem());
setItem(i, taskList.size() + 3, new QTableWidgetItem());
setItem(i, taskList.size() + 4, new QTableWidgetItem());
int totalScore = contestantList[i]->getTotalScore();
int totalUsedTime = contestantList[i]->getTotalUsedTime();
QDateTime judgingTime = contestantList[i]->getJudingTime();
if (totalScore != -1) {
item(i, taskList.size() + 2)->setData(Qt::DisplayRole, totalScore);
item(i, taskList.size() + 3)->setData(Qt::DisplayRole, double(totalUsedTime) / 1000);
item(i, taskList.size() + 4)->setData(Qt::DisplayRole, judgingTime);
sortList.append(qMakePair(-totalScore, contestantList[i]->getContestantName()));
} else {
item(i, taskList.size() + 2)->setText(tr("Invalid"));
item(i, taskList.size() + 3)->setText(tr("Invalid"));
item(i, taskList.size() + 4)->setText(tr("Invalid"));
}
}
qSort(sortList);
QMap<QString, int> rankList;
for (int i = 0; i < sortList.size(); i ++) {
if (i > 0 && sortList[i].first == sortList[i-1].first) {
rankList.insert(sortList[i].second, rankList[sortList[i-1].second]);
} else {
rankList.insert(sortList[i].second, i);
}
}
for (int i = 0; i < rowCount(); i ++) {
if (rankList.contains(contestantList[i]->getContestantName())) {
item(i, 1)->setData(Qt::DisplayRole, rankList[contestantList[i]->getContestantName()] + 1);
} else {
item(i, 1)->setText(tr("Invalid"));
}
}
for (int i = 0; i < rowCount(); i ++) {
for (int j = 0; j < columnCount(); j ++) {
item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
}
}
void ResultViewer::judgeSelected()
{
QStringList nameList;
QList<QTableWidgetSelectionRange> selectionRange = selectedRanges();
for (int i = 0; i < selectionRange.size(); i ++) {
for (int j = selectionRange[i].topRow(); j <= selectionRange[i].bottomRow(); j ++) {
nameList.append(item(j, 0)->text());
}
}
JudgingDialog *dialog = new JudgingDialog(this);
dialog->setModal(true);
dialog->setContest(curContest);
dialog->show();
dialog->judge(nameList);
delete dialog;
refreshViewer();
}
void ResultViewer::judgeAll()
{
JudgingDialog *dialog = new JudgingDialog(this);
dialog->setModal(true);
dialog->setContest(curContest);
dialog->show();
dialog->judgeAll();
delete dialog;
refreshViewer();
}
void ResultViewer::clearPath(const QString &curDir)
{
QDir dir(curDir);
QStringList fileList = dir.entryList(QDir::Files);
for (int i = 0; i < fileList.size(); i ++) {
dir.remove(fileList[i]);
}
QStringList dirList = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
for (int i = 0; i < dirList.size(); i ++) {
clearPath(curDir + dirList[i] + QDir::separator());
dir.rmdir(dirList[i]);
}
}
void ResultViewer::deleteContestant()
{
QMessageBox *messageBox = new QMessageBox(QMessageBox::Warning, tr("Lemon"),
QString("<span style=\"font-size:large\">")
+ tr("Are you sure to delete selected contestant(s)?") + "</span>",
QMessageBox::Ok | QMessageBox::Cancel, this);
QHBoxLayout *layout = new QHBoxLayout;
QCheckBox *checkBox = new QCheckBox(tr("Delete directories in the hard disk as well"));
layout->addWidget(checkBox);
layout->setAlignment(checkBox, Qt::AlignHCenter);
dynamic_cast<QGridLayout*>(messageBox->layout())->addLayout(layout, 1, 1);
dynamic_cast<QGridLayout*>(messageBox->layout())->setVerticalSpacing(10);
if (messageBox->exec() != QMessageBox::Ok) return;
QList<QTableWidgetSelectionRange> selectionRange = selectedRanges();
for (int i = 0; i < selectionRange.size(); i ++) {
for (int j = selectionRange[i].topRow(); j <= selectionRange[i].bottomRow(); j ++) {
curContest->deleteContestant(item(j, 0)->text());
if (checkBox->isChecked()) {
clearPath(Settings::sourcePath() + item(j, 0)->text() + QDir::separator());
QDir(Settings::sourcePath()).rmdir(item(j, 0)->text());
}
}
}
delete checkBox;
delete messageBox;
refreshViewer();
emit contestantDeleted();
}
void ResultViewer::detailInformation()
{
QList<QTableWidgetSelectionRange> selectionRange = selectedRanges();
int index = selectionRange[0].topRow();
DetailDialog *dialog = new DetailDialog(this);
dialog->setModal(true);
dialog->refreshViewer(curContest, curContest->getContestant(item(index, 0)->text()));
connect(dialog, SIGNAL(rejudgeSignal()), this, SLOT(refreshViewer()));
dialog->showDialog();
delete dialog;
}