-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradientcache.cpp
178 lines (156 loc) · 5.86 KB
/
gradientcache.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
#include "gradientcache.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QBitmap>
namespace DJ {
namespace View {
GradientCache* GradientCache::instance = NULL;
GradientCache::GradientCache() : QObject() {
}
GradientCache *GradientCache::getInstance() {
if (instance == NULL) {
instance = new GradientCache;
}
return instance;
}
void GradientCache::clearCache() {
this->problemGradient.clear();
this->problemGradientHighlighted.clear();
this->medalGradient.clear();
this->oddEvenHighlightGradient.clear();
}
void GradientCache::setNumProbs(int n) {
if (this->numProbs != n) {
this->numProbs = n;
this->problemGradient.clear();
this->problemGradientHighlighted.clear();
}
}
void GradientCache::setHeight(int height) {
if (this->height != height) {
this->height = height;
this->problemGradient.clear();
this->problemGradientHighlighted.clear();
}
}
QPixmap GradientCache::getColorGradient(QColor color1, QColor color2) {
ColorPair colorPair(color1, color2);
if (!this->problemGradient.contains(colorPair)) {
QPen pen;
pen.setWidth(1);
pen.setColor(QColor(192, 192, 192));
int width = (NAME_WIDTH - (numProbs - 1) * PROB_MARGIN) / numProbs;
QPixmap pm(width, height);
QBitmap bm(width, height);
bm.clear();
pm.setMask(bm);
QPainter *painter = new QPainter(&pm);
QLinearGradient gradient(0, 0, 0, height-10);
gradient.setColorAt(0, colorPair.color1);
gradient.setColorAt(1, colorPair.color2);
QBrush brush(gradient);
painter->setPen(pen);
brush.setStyle(Qt::LinearGradientPattern);
painter->setBrush(brush);
QRectF rect(0.5, 0.5, width-1, height-1);
painter->drawRoundedRect(rect, 5, 5);
delete painter;
this->problemGradient[colorPair] = pm;
}
return this->problemGradient[colorPair];
}
QPixmap GradientCache::getColorGradientHighlighted(QColor color1, QColor color2) {
ColorPair colorPair(color1, color2);
if (!this->problemGradientHighlighted.contains(colorPair)) {
QPen pen;
pen.setWidth(2);
pen.setColor(QColor(255, 255, 0));
int width = (NAME_WIDTH - (numProbs - 1) * PROB_MARGIN) / numProbs;
QPixmap pm(width, height);
QBitmap bm(width, height);
bm.clear();
pm.setMask(bm);
QPainter *painter = new QPainter(&pm);
QLinearGradient gradient(0, 0, 0, height-10);
gradient.setColorAt(0, colorPair.color1);
gradient.setColorAt(1, colorPair.color2);
QBrush brush(gradient);
painter->setPen(pen);
brush.setStyle(Qt::LinearGradientPattern);
painter->setBrush(brush);
QRectF rect(1, 1, width-2, height-2);
painter->drawRoundedRect(rect, 5, 5);
delete painter;
this->problemGradientHighlighted[colorPair] = pm;
}
return this->problemGradientHighlighted[colorPair];
}
QPixmap GradientCache::getMedalGradient(Medal medal) {
if (!this->medalGradient.contains(medal)) {
int screenWidth = QApplication::desktop()->screenGeometry().width();
QPixmap pm(screenWidth, TEAMITEM_HEIGHT);
QPainter *painter = new QPainter(&pm);
QLinearGradient gradient(0, 0, screenWidth, 0);
switch (medal) {
case GOLD_MEDAL:
gradient.setColorAt(0, QColor(111, 81, 19));
gradient.setColorAt(0.5, QColor(251, 247, 200));
gradient.setColorAt(1, QColor(114, 84, 22));
break;
case SILVER_MEDAL:
gradient.setColorAt(0, QColor(153, 153, 153));
gradient.setColorAt(0.5, QColor(255, 255, 255));
gradient.setColorAt(1, QColor(155, 155, 155));
break;
case BRONZE_MEDAL:
gradient.setColorAt(0, QColor(97, 51, 2));
gradient.setColorAt(0.5, QColor(255, 253, 230));
gradient.setColorAt(1, QColor(101, 58, 5));
break;
default:
break;
}
painter->setPen(Qt::NoPen);
QBrush brush(gradient);
brush.setStyle(Qt::LinearGradientPattern);
painter->setBrush(brush);
painter->drawRect(0, 0, screenWidth, TEAMITEM_HEIGHT);
delete painter;
this->medalGradient[medal] = pm;
}
return this->medalGradient[medal];
}
QPixmap GradientCache::getOddEvenHighlightedGradient(int oddEvenHighlighted) {
if (!this->oddEvenHighlightGradient.contains(oddEvenHighlighted)) {
int screenWidth = QApplication::desktop()->screenGeometry().width();
QPixmap pm(screenWidth, TEAMITEM_HEIGHT);
QPainter *painter = new QPainter(&pm);
if (oddEvenHighlighted == 2) { // highlighted
painter->setBrush(QColor(92, 138, 221));
} else if (oddEvenHighlighted == 1) { // odd
QLinearGradient gradient(0, 0, screenWidth, 0);
gradient.setColorAt(0, QColor(0, 0, 0));
gradient.setColorAt(0.5, QColor(56, 56, 56));
gradient.setColorAt(1, QColor(0, 0, 0));
QBrush brush(gradient);
brush.setStyle(Qt::LinearGradientPattern);
painter->setBrush(brush);
} else if (oddEvenHighlighted == 0) { // even
QLinearGradient gradient(0, 0, screenWidth, 0);
gradient.setColorAt(0, QColor(30, 30, 30));
gradient.setColorAt(0.5, QColor(86, 86, 86));
gradient.setColorAt(1, QColor(30, 30, 30));
QBrush brush(gradient);
brush.setStyle(Qt::LinearGradientPattern);
painter->setBrush(brush);
}
painter->setPen(Qt::NoPen);
painter->drawRect(0, 0, screenWidth, TEAMITEM_HEIGHT);
delete painter;
this->oddEvenHighlightGradient[oddEvenHighlighted] = pm;
}
return this->oddEvenHighlightGradient[oddEvenHighlighted];
}
} // namespace View
} // namespace DJ