forked from zhipeng-jia/project-lemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcaseeditwidget.cpp
188 lines (164 loc) · 6.94 KB
/
testcaseeditwidget.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
/***************************************************************************
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 "testcaseeditwidget.h"
#include "ui_testcaseeditwidget.h"
#include "settings.h"
#include "testcase.h"
TestCaseEditWidget::TestCaseEditWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::TestCaseEditWidget)
{
ui->setupUi(this);
editTestCase = 0;
deleteAction = new QAction(this);
deleteAction->setShortcutContext(Qt::WidgetShortcut);
deleteAction->setShortcut(QKeySequence::Delete);
deleteAction->setEnabled(false);
ui->fileList->addAction(deleteAction);
ui->fullScore->setValidator(new QIntValidator(1, Settings::upperBoundForFullScore(), this));
ui->timeLimit->setValidator(new QIntValidator(1, Settings::upperBoundForTimeLimit(), this));
ui->memoryLimit->setValidator(new QIntValidator(1, Settings::upperBoundForMemoryLimit(), this));
ui->fileList->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
ui->inputFileEdit->setFilters(QDir::Files);
ui->outputFileEdit->setFilters(QDir::Files);
connect(this, SIGNAL(dataPathChanged()),
ui->inputFileEdit, SLOT(refreshFileList()));
connect(this, SIGNAL(dataPathChanged()),
ui->outputFileEdit, SLOT(refreshFileList()));
connect(ui->addButton, SIGNAL(clicked()),
this, SLOT(addSingleCase()));
connect(deleteAction, SIGNAL(triggered()),
this, SLOT(deleteSingleCase()));
connect(ui->fullScore, SIGNAL(textChanged(QString)),
this, SLOT(fullScoreChanged(QString)));
connect(ui->timeLimit, SIGNAL(textChanged(QString)),
this, SLOT(timeLimitChanged(QString)));
connect(ui->memoryLimit, SIGNAL(textChanged(QString)),
this, SLOT(memoryLimitChanged(QString)));
connect(ui->fileList, SIGNAL(itemSelectionChanged()),
this, SLOT(fileListSelectionChanged()));
connect(ui->fileList, SIGNAL(itemChanged(QTableWidgetItem*)),
this, SLOT(fileListItemChanged(QTableWidgetItem*)));
}
TestCaseEditWidget::~TestCaseEditWidget()
{
delete ui;
}
void TestCaseEditWidget::changeEvent(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
TestCase *bak = editTestCase;
setEditTestCase(0, true);
ui->retranslateUi(this);
setEditTestCase(bak, true);
}
}
void TestCaseEditWidget::refreshFileList()
{
if (! editTestCase) return;
ui->fileList->setRowCount(0);
QStringList inputFiles = editTestCase->getInputFiles();
QStringList outputFiles = editTestCase->getOutputFiles();
ui->fileList->setRowCount(inputFiles.size());
for (int i = 0; i < ui->fileList->rowCount(); i ++) {
QTableWidgetItem *inputFile = new QTableWidgetItem(inputFiles[i]);
QTableWidgetItem *outputFile = new QTableWidgetItem(outputFiles[i]);
ui->fileList->setItem(i, 0, inputFile);
ui->fileList->setItem(i, 1, outputFile);
}
}
void TestCaseEditWidget::setEditTestCase(TestCase *testCase, bool check)
{
editTestCase = testCase;
if (! editTestCase) return;
ui->fullScore->setText(QString("%1").arg(editTestCase->getFullScore()));
ui->timeLimit->setText(QString("%1").arg(editTestCase->getTimeLimit()));
ui->memoryLimit->setText(QString("%1").arg(editTestCase->getMemoryLimit()));
refreshFileList();
ui->timeLimit->setEnabled(check);
ui->timeLimitLabel->setEnabled(check);
ui->msLabel->setEnabled(check);
ui->memoryLimit->setEnabled(check);
ui->memoryLimitLabel->setEnabled(check);
ui->mbLabel->setEnabled(check);
}
void TestCaseEditWidget::setSettings(Settings *_settings)
{
settings = _settings;
ui->inputFileEdit->setFileExtensions(settings->getInputFileExtensions());
ui->outputFileEdit->setFileExtensions(settings->getOutputFileExtensions());
}
void TestCaseEditWidget::addSingleCase()
{
if (ui->inputFileEdit->text().isEmpty()) {
ui->inputFileEdit->setFocus();
QMessageBox::warning(this, tr("Error"), tr("Empty input file name!"), QMessageBox::Close);
return;
}
if (ui->outputFileEdit->text().isEmpty()) {
ui->outputFileEdit->setFocus();
QMessageBox::warning(this, tr("Error"), tr("Empty output file name!"), QMessageBox::Close);
return;
}
ui->fileList->setRowCount(ui->fileList->rowCount() + 1);
editTestCase->addSingleCase(ui->inputFileEdit->text(), ui->outputFileEdit->text());
ui->fileList->setItem(ui->fileList->rowCount() - 1, 0,
new QTableWidgetItem(ui->inputFileEdit->text()));
ui->fileList->setItem(ui->fileList->rowCount() - 1, 1,
new QTableWidgetItem(ui->outputFileEdit->text()));
ui->inputFileEdit->clear();
ui->outputFileEdit->clear();
}
void TestCaseEditWidget::deleteSingleCase()
{
QTableWidgetSelectionRange range = ui->fileList->selectedRanges().at(0);
for (int i = 0; i <= range.rowCount(); i ++)
editTestCase->deleteSingleCase(range.topRow());
refreshFileList();
}
void TestCaseEditWidget::fileListSelectionChanged()
{
if (! editTestCase) return;
QList<QTableWidgetSelectionRange> range = ui->fileList->selectedRanges();
if (range.size() == 1 && range[0].columnCount() == 2)
deleteAction->setEnabled(true);
else
deleteAction->setEnabled(false);
}
void TestCaseEditWidget::fileListItemChanged(QTableWidgetItem *item)
{
if (! editTestCase) return;
if (item) {
if (item->column() == 0)
editTestCase->setInputFiles(item->row(), item->text());
else
editTestCase->setOutputFiles(item->row(), item->text());
}
}
void TestCaseEditWidget::fullScoreChanged(const QString &text)
{
if (! editTestCase) return;
editTestCase->setFullScore(text.toInt());
}
void TestCaseEditWidget::timeLimitChanged(const QString &text)
{
if (! editTestCase) return;
editTestCase->setTimeLimit(text.toInt());
}
void TestCaseEditWidget::memoryLimitChanged(const QString &text)
{
if (! editTestCase) return;
editTestCase->setMemoryLimit(text.toInt());
}