-
Notifications
You must be signed in to change notification settings - Fork 1
/
teamgraphicsitem.cpp
146 lines (122 loc) · 4.92 KB
/
teamgraphicsitem.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
#include "teamgraphicsitem.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QLinearGradient>
#include <QStyleOptionGraphicsItem>
#include "gradientcache.h"
#include "defines.h"
#include <QPen>
#include <QBrush>
#include <QPainter>
namespace DJ {
namespace View {
TeamGraphicsItem::TeamGraphicsItem(QList<ProblemGraphicsItem *> problemItems, QGraphicsItem *parent)
: QObject(), QGraphicsItem(parent) {
this->screenWidth = QApplication::desktop()->screenGeometry().width();
this->problemItems = problemItems;
this->highlighted = false;
this->setCacheMode(DeviceCoordinateCache);
QFont font("DejaVu Sans Mono");
font.setPixelSize(24);
font.setBold(true);
QFont italicFont("DejaVu Sans Mono");
italicFont.setPixelSize(24);
italicFont.setItalic(true);
italicFont.setBold(true);
QFontMetrics fm(font);
QFontMetrics fmi(italicFont);
for (int i = 0; i < this->problemItems.size(); i++) {
this->problemItems.at(i)->setParentItem(this);
this->problemItems.at(i)->setPos(LEFT_MARGIN + RANK_WIDTH +
(i * PROB_MARGIN) +
(i * this->problemItems.at(0)->getWidth()), fm.height() + NAME_PROBS_MARGIN);
GradientCache::getInstance()->setHeight(TEAMITEM_HEIGHT - qMax(fm.height(), fmi.height()) - NAME_PROBS_MARGIN - PROBS_BELOW_MARGIN);
this->problemItems.at(i)->setHeight(TEAMITEM_HEIGHT - qMax(fm.height(), fmi.height()) - NAME_PROBS_MARGIN - PROBS_BELOW_MARGIN);
}
this->rankItem = new QGraphicsSimpleTextItem(this);
this->rankItem->setPos(LEFT_MARGIN, 0);
this->rankItem->setPen(QPen(Qt::black));
this->rankItem->setBrush(QBrush(Qt::white));
this->rankItem->setFont(italicFont);
this->nameItem = new QGraphicsSimpleTextItem(this);
this->nameItem->setPos(LEFT_MARGIN + RANK_WIDTH, 0);
this->nameItem->setPen(QPen(Qt::black));
this->nameItem->setBrush(QBrush(Qt::white));
this->nameItem->setFont(font);
this->solvedItem = new QGraphicsSimpleTextItem(this);
this->solvedItem->setPos(LEFT_MARGIN + RANK_WIDTH + NAME_WIDTH, 0);
this->solvedItem->setPen(QPen(Qt::black));
this->solvedItem->setBrush(QBrush(Qt::white));
this->solvedItem->setFont(italicFont);
this->timeItem = new QGraphicsSimpleTextItem(this);
this->timeItem->setPos(LEFT_MARGIN + RANK_WIDTH + NAME_WIDTH + SOLVED_WIDTH, 0);
this->timeItem->setPen(QPen(Qt::black));
this->timeItem->setBrush(QBrush(Qt::white));
this->timeItem->setFont(font);
}
QRectF TeamGraphicsItem::boundingRect() const {
return QRectF(0, 0, screenWidth, TEAMITEM_HEIGHT);
}
ProblemGraphicsItem *TeamGraphicsItem::getProblemGraphicsItem(int i) {
return this->problemItems.at(i);
}
void TeamGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *) {
painter->setClipRect(option->exposedRect);
if (this->highlighted) {
painter->drawPixmap(0, 0, screenWidth, TEAMITEM_HEIGHT,
GradientCache::getInstance()->getOddEvenHighlightedGradient(2));
} else if (this->medal == NO_MEDAL) {
painter->drawPixmap(0, 0, screenWidth, TEAMITEM_HEIGHT,
GradientCache::getInstance()->getOddEvenHighlightedGradient(this->even ? 0 : 1));
} else {
painter->drawPixmap(0, 0, screenWidth, TEAMITEM_HEIGHT,
GradientCache::getInstance()->getMedalGradient(this->medal));
}
}
void TeamGraphicsItem::setEven(bool even) {
update();
this->even = even;
}
void TeamGraphicsItem::setHighlighted(bool highlighted) {
update();
this->highlighted = highlighted;
}
void TeamGraphicsItem::setRank(int rank) {
if (rank <= 0) {
this->rankItem->setText("");
} else {
this->rankItem->setText(QString::number(rank));
}
}
void TeamGraphicsItem::setName(QString name) {
this->nameItem->setText(name);
}
void TeamGraphicsItem::setSolved(int solved) {
QString txt = QString::number(solved);
QFontMetrics fm(this->solvedItem->font());
int fw = fm.width(txt);
if (solved < 0) {
this->solvedItem->setText("");
} else {
this->solvedItem->setText(txt);
}
this->solvedItem->setPos(LEFT_MARGIN + RANK_WIDTH + NAME_WIDTH + SOLVED_WIDTH - fw, 0);
}
void TeamGraphicsItem::setMedal(Medal medal) {
update();
this->medal = medal;
}
void TeamGraphicsItem::setTime(int time) {
QString txt = QString::number(time);
QFontMetrics fm(this->timeItem->font());
int fw = fm.width(txt);
if (time < 0) {
this->timeItem->setText("");
} else {
this->timeItem->setText(txt);
}
this->timeItem->setPos(LEFT_MARGIN + RANK_WIDTH + NAME_WIDTH + SOLVED_WIDTH + TIME_WIDTH - fw, 0);
}
} // namespace View
} // namespace DJ