-
Notifications
You must be signed in to change notification settings - Fork 1
/
competition.cpp
131 lines (105 loc) · 3.37 KB
/
competition.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
#include "competition.h"
#include "accountmanager.h"
#include "competitionview.h"
#include <QtGui>
Competition::Competition(QWidget *parent, AccountManager *am) :
QWidget(parent)
{
this->am = am;
cv = new CompetitionView(this, am);
text = new QLabel(tr("<h1>Competitions</h1>"));
ovp = new QLabel(tr("Overal Progress: "));
ovpb = new QProgressBar;
pb = new QLabel(tr("Partial Progress: "));
norpb = new QProgressBar;
progress = new QHBoxLayout;
progress->addWidget(ovp);
progress->addWidget(ovpb);
progress->addStretch();
progress->addWidget(pb);
progress->addWidget(norpb);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateStatus()));
refrate = new QLabel(tr("Refresh every "));
refresh = new QLineEdit("60");
refresh->setValidator(new QIntValidator(1, 3600));
refrate->setBuddy(refresh);
persec = new QLabel(tr("seconds"));
setbut = new QPushButton(tr("Set"));
refnow = new QPushButton(tr("Refresh Now!"));
connect(refnow, SIGNAL(clicked()), this, SLOT(updateStatus()));
connect(setbut, SIGNAL(clicked()), this, SLOT(changeTime()));
stat = new QLabel(tr("Status: "));
status = new QLabel(tr("<font color=blue>Synced!</font>"));
downBox = new QHBoxLayout;
downBox->addWidget(refrate);
downBox->addWidget(refresh);
downBox->addWidget(persec);
downBox->addWidget(setbut);
downBox->addWidget(refnow);
downBox->addStretch();
downBox->addWidget(stat);
downBox->addWidget(status);
layou = new QVBoxLayout;
layou->addWidget(text);
layou->addWidget(cv);
layou->addLayout(progress);
layou->addLayout(downBox);
setLayout(layou);
QTimer::singleShot(0, this, SLOT(updateStatus()));
connect(am, SIGNAL(changed()), this, SLOT(updateStatus()));
timer->start(60000);
}
void Competition::updateStatus()
{
status->setText(tr("<font color=green>Syncing...</font>"));
if (am->ID() == "Not Set")
{
status->setText("<font color=red>Failed to sync!</font>");
return;
}
bool res = true;
if (!am->updateSubmits(ovpb, norpb))
res = false;
infos.clear();
foreach(QString str, am->friendsID())
{
AccountProcessor m = am->ownInfo();
AccountProcessor h = am->infoOf(str);
if (h.numAccepts() > m.numAccepts())
infos.append(CompetitionInfo(MoreAc, str));
else if (h.numAccepts() >= m.numAccepts()-10)
infos.append(CompetitionInfo(NearAc, str));
QSet<int> diff = h.acceptedProblems()-m.acceptedProblems();
if (diff.size() > 0)
infos.append(CompetitionInfo(DiffAc, str));
if (am->submitsOf(str).size() > 0)
infos.append(CompetitionInfo(DiffSub, str));
}
cv->setData(infos);
if (res)
status->setText("<font color=blue>Synced!</font>");
else
status->setText("<font color=red>Failed to sync!</font>");
ovpb->reset();
norpb->reset();
}
void Competition::changeTime()
{
int interval = refresh->text().toInt();
if (interval > 0)
timer->start(interval*1000);
}
QDataStream & operator<<(QDataStream &ds, const Competition &in)
{
ds << in.refresh->text();
return ds;
}
QDataStream & operator>>(QDataStream &ds, Competition &in)
{
QString tmp;
ds >> tmp;
in.refresh->setText(tmp);
in.changeTime();
return ds;
}