-
Notifications
You must be signed in to change notification settings - Fork 1
/
winnergraphicsitem.cpp
113 lines (93 loc) · 3.06 KB
/
winnergraphicsitem.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
#include "winnergraphicsitem.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QLinearGradient>
#include <QFont>
#include "defines.h"
namespace DJ {
namespace View {
WinnerGraphicsItem::WinnerGraphicsItem(QGraphicsItem *parent) :
QObject(), QGraphicsItem(parent) {
this->winner = "n2dg";
this->reAddItems();
}
QRectF WinnerGraphicsItem::boundingRect() const {
QRect screenSize = QApplication::desktop()->screenGeometry();
return QRectF(0, 0, screenSize.width(), screenSize.height());
}
void WinnerGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *) {
QLinearGradient gradient(0, 0, 0, this->boundingRect().height());
gradient.setColorAt(0, QColor(111, 81, 19));
gradient.setColorAt(0.5, QColor(251, 247, 200));
gradient.setColorAt(1, QColor(114, 84, 22));
QBrush brush(gradient);
brush.setStyle(Qt::LinearGradientPattern);
painter->setBrush(brush);
painter->setPen(Qt::NoPen);
painter->drawRect(this->boundingRect());
}
void WinnerGraphicsItem::setWinner(QString text) {
update();
this->winner = text;
this->reAddItems();
}
void WinnerGraphicsItem::setContestName(QString text) {
update();
this->contestName = text;
this->reAddItems();
}
void WinnerGraphicsItem::reAddItems() {
int centerHeight = this->boundingRect().height() / 2;
int centerWidth = this->boundingRect().width() / 2;
QFont font("DejaVu Sans Mono");
font.setPixelSize(75);
font.setBold(true);
QFontMetrics fm(font);
QString startingText = "Winner " + this->contestName;
QString currentLine = "";
int myWidth = this->boundingRect().width();
QStringList words = startingText.split(" ");
QStringList lines;
foreach (QString word, words) {
QString proposedLine = currentLine;
if (!proposedLine.isEmpty()) {
proposedLine += " ";
}
proposedLine += word;
int newWidth = fm.width(proposedLine);
if (newWidth <= myWidth) {
currentLine = proposedLine;
} else {
lines.append(currentLine);
currentLine = word;
}
}
if (!currentLine.isEmpty()) {
lines.append(currentLine);
}
lines.append("");
lines.append(this->winner);
qDeleteAll(this->textItems);
this->textItems.clear();
this->setCacheMode(DeviceCoordinateCache);
int i = 0;
int total = lines.size();
foreach (QString line, lines) {
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(this);
textItem->setFont(font);
QPen pen;
pen.setColor(Qt::black);
pen.setWidth(3);
textItem->setPen(pen);
textItem->setBrush(QBrush(Qt::white));
textItem->setText(line);
int offsetFromCenter = -((total * fm.height()) / 2) + (i * fm.height());
textItem->setPos(centerWidth - fm.width(textItem->text()) / 2, centerHeight + offsetFromCenter);
this->textItems.append(textItem);
++i;
}
}
} // namespace View
} // namespace DJ