-
Notifications
You must be signed in to change notification settings - Fork 19
/
opencontestwidget.cpp
150 lines (137 loc) · 4.93 KB
/
opencontestwidget.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
/***************************************************************************
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 "opencontestwidget.h"
#include "ui_opencontestwidget.h"
#include "contest.h"
OpenContestWidget::OpenContestWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::OpenContestWidget)
{
ui->setupUi(this);
connect(ui->recentContest, SIGNAL(itemSelectionChanged()),
this, SIGNAL(selectionChanged()));
connect(ui->recentContest, SIGNAL(cellDoubleClicked(int, int)),
this, SIGNAL(rowDoubleClicked()));
connect(ui->addButton, SIGNAL(clicked()),
this, SLOT(addContest()));
connect(ui->deleteButton, SIGNAL(clicked()),
this, SLOT(deleteContest()));
connect(ui->recentContest, SIGNAL(currentCellChanged(int,int,int,int)),
this, SLOT(currentRowChanged()));
}
OpenContestWidget::~OpenContestWidget()
{
delete ui;
}
void OpenContestWidget::setRecentContest(const QStringList &list)
{
recentContest = list;
refreshContestList();
}
void OpenContestWidget::refreshContestList()
{
ui->recentContest->setRowCount(0);
for (int i = 0; i < recentContest.size(); ) {
QFile file(recentContest[i]);
if (! file.open(QFile::ReadOnly)) {
recentContest.removeAt(i);
continue;
}
QDataStream _in(&file);
unsigned checkNumber;
_in >> checkNumber;
if (checkNumber != unsigned(MagicNumber)) {
recentContest.removeAt(i);
continue;
}
quint16 checksum;
int len;
_in >> checksum >> len;
char *raw = new char[len];
_in.readRawData(raw, len);
if (qChecksum(raw, len) != checksum) {
delete[] raw;
recentContest.removeAt(i);
continue;
}
QByteArray data(raw, len);
delete[] raw;
data = qUncompress(data);
QDataStream in(data);
QString title;
in >> title;
ui->recentContest->setRowCount(i + 1);
ui->recentContest->setItem(i, 0, new QTableWidgetItem(title));
ui->recentContest->setItem(i, 1, new QTableWidgetItem(recentContest[i]));
ui->recentContest->item(i, 0)->setTextAlignment(Qt::AlignCenter);
i ++;
}
QHeaderView *header = ui->recentContest->horizontalHeader();
header->setResizeMode(QHeaderView::ResizeToContents);
}
void OpenContestWidget::addContest()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Add Contest"), QDir::homePath(),
tr("Lemon contest data file (*.cdf)"));
if (fileName.isEmpty()) return;
fileName = fileName.replace('/', QDir::separator());
QFile file(fileName);
if (! file.open(QFile::ReadOnly)) {
QMessageBox::warning(this, tr("Error"), tr("Cannot open selected file"), QMessageBox::Close);
return;
}
QDataStream in(&file);
unsigned checkNumber;
in >> checkNumber;
if (checkNumber != unsigned(MagicNumber)) {
QMessageBox::warning(this, tr("Error"), tr("Broken contest data file"), QMessageBox::Close);
return;
}
quint16 checksum;
int len;
in >> checksum >> len;
char *raw = new char[len];
in.readRawData(raw, len);
if (qChecksum(raw, len) != checksum) {
QMessageBox::warning(this, tr("Error"), tr("Broken contest data file"), QMessageBox::Close);
delete[] raw;
return;
}
delete[] raw;
recentContest.append(fileName);
refreshContestList();
}
void OpenContestWidget::deleteContest()
{
int index = ui->recentContest->currentRow();
recentContest.removeAt(index);
refreshContestList();
}
void OpenContestWidget::currentRowChanged()
{
int index = ui->recentContest->currentRow();
if (index != -1)
ui->deleteButton->setEnabled(true);
else
ui->deleteButton->setEnabled(false);
}
const QStringList& OpenContestWidget::getRecentContest() const
{
return recentContest;
}
int OpenContestWidget::getCurrentRow() const
{
return ui->recentContest->currentRow();
}