-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreboardconfig.cpp
executable file
·164 lines (136 loc) · 4.57 KB
/
scoreboardconfig.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
/*
* This file is part of community judge client project developed by Shervin Kh.
* Copyright (C) 2014 Shervin Kh.
* License: GPLv3 Or Later
* Full license could be found in License file shipped with program or at http://www.gnu.org/licenses/
*/
#include "scoreboardconfig.h"
#include "mainwidget.h"
#include "connection.h"
#include "scoreboardspecsdialog.h"
#include <QtGui>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#endif
ScoreboardConfig::ScoreboardConfig(MainWidget *MW, QWidget *parent) :
QWidget(parent), mw(MW)
{
pageLabel = new QLabel("Page: ");
pageSB = new QSpinBox;
pageSB->setMinimum(1);
reqButton = new QPushButton("Re&quest");
reqButton->setMinimumSize(reqButton->sizeHint() * 1.3);
editButton = new QPushButton("&Edit");
editButton->setMinimumSize(editButton->sizeHint() * 1.3);
buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(editButton);
buttonLayout->addStretch();
buttonLayout->addWidget(pageLabel);
buttonLayout->addWidget(pageSB);
buttonLayout->addStretch();
buttonLayout->addWidget(reqButton);
buttonLayout->addStretch();
tw = new QTableWidget;
tw->setColumnCount(5);
tw->setHorizontalHeaderItem(0, new QTableWidgetItem("Scoreboard"));
tw->setHorizontalHeaderItem(1, new QTableWidgetItem("Type"));
tw->setHorizontalHeaderItem(2, new QTableWidgetItem("Max. Users"));
tw->setHorizontalHeaderItem(3, new QTableWidgetItem("Each Page"));
tw->setHorizontalHeaderItem(4, new QTableWidgetItem("Color Data"));
tw->resizeColumnsToContents();
#if QT_VERSION >= 0x050000
tw->setSizeAdjustPolicy(QTableWidget::AdjustToContents);
#endif
tw->setSelectionBehavior(QAbstractItemView::SelectRows);
totalLayout = new QVBoxLayout;
totalLayout->addLayout(buttonLayout);
totalLayout->addWidget(tw);
setLayout(totalLayout);
connect(reqButton, SIGNAL(clicked()), this, SLOT(request()));
connect(editButton, SIGNAL(clicked()), this, SLOT(editScoreboard()));
connect(tw, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(editScoreboard()));
}
void ScoreboardConfig::init()
{
data.clear();
updateTable();
}
void ScoreboardConfig::onSwitch()
{
}
void ScoreboardConfig::request()
{
DataPointer DS;
if (DS = mw->connection()->prepare(this, SLOT(requestDone(StatCode,QByteArray))))
{
(*DS) << static_cast<int>(StatCode::ScoreboardConfig);
(*DS) << (pageSB->value() - 1);
mw->connection()->send();
}
}
void ScoreboardConfig::requestDone(StatCode stat, const QByteArray &BA)
{
if (!mw->handleGeneralStatCodes(stat) && stat == StatCode::OperationSuccessful)
{
QDataStream DS(BA);
DS.setVersion(QDataStream::Qt_4_8);
data.clear();
DS >> data;
updateTable();
if (data.isEmpty())
QMessageBox::warning(this, "Error", "No Such Page");
}
}
void ScoreboardConfig::updateTable()
{
tw->setRowCount(0);
tw->setRowCount(data.length());
for (int i = 0; i < data.length(); i++)
{
QTableWidgetItem *col[tw->columnCount()];
for (int j = 0; j < tw->columnCount(); j++)
{
col[j] = new QTableWidgetItem;
col[j]->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}
col[0]->setText(data[i]["name"].toString());
col[1]->setText(scoreboardTypes[data[i]["type"].toInt()]);
col[2]->setText(data[i]["max"].toString());
col[3]->setText(data[i]["page"].toString());
col[4]->setText(data[i]["color"].toBool() ? "Yes" : "No");
for (int j = 0; j < tw->columnCount(); j++)
tw->setItem(i, j, col[j]);
}
tw->resizeColumnsToContents();
}
void ScoreboardConfig::editScoreboard()
{
lastRow = tw->currentRow();
if (lastRow != -1)
{
ScoreboardSpecsDialog dlg(data[lastRow]);
if (dlg.exec())
{
DataTable DT = dlg.resultScoreboard();
newData = DT;
DT.remove("name");
DataPointer DS;
if (DS = mw->connection()->prepare(this, SLOT(operationDone(StatCode,QByteArray))))
{
(*DS) << static_cast<int>(StatCode::ScoreboardEdit);
(*DS) << DT;
mw->connection()->send();
}
}
}
}
void ScoreboardConfig::operationDone(StatCode stat, const QByteArray &)
{
if (!mw->handleGeneralStatCodes(stat) && stat == StatCode::OperationSuccessful)
{
QMessageBox::information(this, "Successful", "Operation was successful");
data[lastRow] = newData;
updateTable();
}
}