-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagegenerator.cpp
More file actions
executable file
·68 lines (53 loc) · 1.82 KB
/
imagegenerator.cpp
File metadata and controls
executable file
·68 lines (53 loc) · 1.82 KB
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
#include "imagegenerator.h"
#include <QDebug>
#include <QPixmap>
#include <QFont>
#include <QPainter>
ImageGenerator::ImageGenerator() :
QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap)
{
}
QPixmap ImageGenerator::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
Q_UNUSED(size); Q_UNUSED(requestedSize);
qDebug() << id;
QStringList parts = id.split('/');
// first part is alignment (left, center, right)
QString alignment = parts.takeAt(0);
// second part is symbol
QString symbolSrc = parts.takeAt(0);
// third part font size
QString fontSize = parts.takeAt(0);
// and rest is text
QString text = parts.join("/");
QPixmap pixmap(120, 120);
pixmap.fill(QColor(Qt::black));
QPainter painter(&pixmap);
QFont f("Nokia Pure Text", fontSize.toInt());
Qt::AlignmentFlag align = Qt::AlignLeft;
if(alignment == "center") {
align = Qt::AlignHCenter;
} else if(alignment == "right") {
align = Qt::AlignRight;
}
QFontMetrics fm(f);
QRect boundingBox = fm.boundingRect(QRect(0, 0, 120, 120), align, text, 0, 0);
painter.setFont(f);
painter.setPen(Qt::white);
painter.drawText(boundingBox, align, text);
if(symbolSrc != "0") {
QPixmap symbol(":/img/" + symbolSrc + "_white.png");
int height = boundingBox.height();
int start = 0;
if(alignment == "center")
start = 60 - ((120 - height) / 2);
else if(alignment == "right")
start = 120 - (120 - height);
QRect target(start, height, 120 - height, 120 - height);
QRect source(0, 0, 100, 100);
if(height > 0) {
painter.drawPixmap(target, symbol, source);
}
}
return pixmap;
}