-
Notifications
You must be signed in to change notification settings - Fork 2
/
imagebuilder.cpp
319 lines (260 loc) · 8.95 KB
/
imagebuilder.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "imagebuilder.h"
#include "watchface.h"
#include "ElementInfo.h"
#include <QDebug>
#include <QDir>
#include <stdexcept>
#include <QPainter>
#include <QPainterPath>
#include <cmath>
ImageBuilder::ImageBuilder(WatchFace* face, const QString& symbolBase, const QString& faceDir, QObject *parent)
: QObject(parent)
, wf(face)
, base(faceDir)
, symbolBase(symbolBase) {
createDirs(faceDir);
}
QImage ImageBuilder::assembleImage() {
QImage scratch(wf->width(), wf->height(), QImage::Format_ARGB32);
QPainter p;
if (p.begin(&scratch)) {
p.setRenderHint(QPainter::Antialiasing);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(scratch.rect(), Qt::transparent);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
for (int i=0; i < WatchFace::MaxElements; ++i) {
ElementInfo* cfg = wf->elemInfo(i);
p.drawImage(cfg->x, cfg->y, cfg->image);
}
p.end();
}
return scratch;
}
void ImageBuilder::buildPower(ElementInfo* cfg) {
QImage scratch(wf->width(), wf->height(), QImage::Format_ARGB32);
int scale = cfg->sample.toInt();
createGaugeI(scratch, cfg, 100, cfg->fgCol0);
createGaugeI(scratch, cfg, scale, cfg->fgCol1);
cfg->image = scratch;
}
void ImageBuilder::buildSteps(ElementInfo* cfg) {
QImage scratch(wf->width(), wf->height(), QImage::Format_ARGB32);
int scale = cfg->sample.toInt();
createGaugeII(scratch, cfg, 100, cfg->fgCol0);
createGaugeII(scratch, cfg, scale, cfg->fgCol1);
cfg->image = scratch;
}
void ImageBuilder::createBackground() {
ElementInfo* cfg = wf->elemInfo(0);
QImage scratch(wf->width(), wf->height(), QImage::Format_ARGB32);
int w = wf->width();
int h = wf->height();
cfg->pp.clear();
cfg->pp.moveTo(w, w / 2);
cfg->pp.arcTo(QRectF(0, 0, w, w), 0, 180);
cfg->pp.lineTo(0, h - w / 2);
cfg->pp.arcTo(QRectF(0, h - w, w, w), -180, 180);
cfg->pp.closeSubpath();
drawImage(scratch, cfg, cfg->pp);
cfg->image = scratch;
}
void ImageBuilder::createDirs(const QString &basePath) {
QDir subDir(basePath);
base = subDir.absolutePath();
for (int i=0; i < WatchFace::MaxElements; ++i) {
const ElementInfo* cfg = wf->elementInfo(i);
if (!cfg) continue;
subDir.mkpath(cfg->path);
}
}
void ImageBuilder::createElement(int elem, ElementInfo* cfg) {
if (!cfg) cfg = wf->elemInfo(elem);
if (!cfg) return;
switch (cfg->type) {
case Background:
createBackground();
break;
case TextSequence: {
QStringList sl;
if (cfg->id == "month")
sl = wf->lMonths;
else if (cfg->id == "day of week")
sl = wf->lDows;
cfg->image = createImage(cfg, getSampleText(cfg->sample, sl));
} break;
case SingleDigit:
case FreeText:
cfg->image = createImage(cfg, cfg->sample);
break;
case Symbol:
if (cfg->image.isNull()) {
QString fileName = QString("%1/%2/%3.png").arg(base
, cfg->path
, cfg->sample);
if (cfg->path.startsWith("/"))
fileName = QString("%1/%2.png").arg(cfg->path
, cfg->sample);
cfg->image = QImage(fileName);
}
break;
case Gauge:
if (cfg->path.endsWith("top")) {
buildPower(cfg);
}
else if (cfg->path.endsWith("bottom")) {
buildSteps(cfg);
}
break;
default:
break;
}
}
void ImageBuilder::createGaugeI(QImage& img, ElementInfo* cfg, int scale, const QColor& c) {
QRect r = img.rect();
int w = r.width();
int h0 = r.height() - r.width() / 2 - 4;
int h1 = r.width() / 2;
int a1 = scale > 50 ? 3.6 * (scale - 50) : 0;
cfg->pp.clear();
cfg->pp.moveTo(w - 3, h0);
if (scale < 50) {
cfg->pp.lineTo(w - 3, h0 - scale * 6);
}
else {
cfg->pp.lineTo(w - 3, h1);
cfg->pp.arcTo(QRectF(3
, 3
, w - 6
, w - 6)
, 0 // start angle (degree)
, a1); // arc length (degree)
// same way back to avoid filling!
cfg->pp.arcTo(QRectF(3
, 3
, w - 6
, w - 6)
, a1
, -a1);
cfg->pp.lineTo(w - 3, h0);
}
drawGauge(img, cfg, cfg->pp, c);
}
void ImageBuilder::createGaugeII(QImage& img, ElementInfo* cfg, int scale, const QColor& c) {
QRect r = img.rect();
int w = r.width();
int h = r.height();
int h0 = w / 2 + 4;
int h1 = h - w / 2;
int a1 = scale > 50 ? 3.6 * (scale - 50) : 0;
cfg->pp.clear();
cfg->pp.moveTo(3, h0);
if (scale < 50) {
cfg->pp.lineTo(3, h0 + scale * 6);
}
else {
cfg->pp.lineTo(3, h1);
cfg->pp.arcTo(QRectF(3
, h - w + 3
, w - 6
, w - 6)
, -180
, a1);
// here we avoid filling without going same way back?!?
}
drawGauge(img, cfg, cfg->pp, c);
}
void ImageBuilder::createImage(ElementInfo* cfg) {
QFontMetrics fm(cfg->font);
QRectF r = fm.boundingRect(cfg->sample);
QImage scratch(r.width(), r.height(), QImage::Format_ARGB32);
drawImage(scratch, cfg, cfg->pp);
cfg->image = scratch;
}
QImage ImageBuilder::createImage(const ElementInfo* cfg, const QString& text) {
if (!cfg) throw new std::invalid_argument("missing cfg parameter");
QFont font = cfg->font;
QFontMetrics fm(font);
QRect r = text.size() == 1 ? fm.boundingRect("4") : fm.boundingRect(text);
QImage scratch(10 + (int)((double)r.width() * 1.2)
, 10 + r.height()
, QImage::Format_ARGB32);
if (cfg) {
QPainterPath pp;
QSize s = scratch.size();
qDebug() << "ImageBuilder::createImage( " << text << " )";
qDebug() << " fontMetrics:" << r.x() << "/" << r.y() << " - " << r.width() << "x" << r.height();
qDebug() << " image size:" << s.width() << "x" << s.height();
pp.addText(QPointF(5 - r.x(), -r.y() + 5), font, text);
drawImage(scratch, cfg, pp);
}
return scratch;
}
void ImageBuilder::drawGauge(QImage& img, ElementInfo* cfg, QPainterPath& pp, const QColor& c) {
QPainter p;
if (p.begin(&img)) {
p.setRenderHint(QPainter::Antialiasing);
if (c == cfg->fgCol0) {
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(img.rect(), Qt::transparent);
}
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.setPen(QPen(c, 8, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
p.drawPath(pp);
p.end();
}
}
void ImageBuilder::drawImage(QImage& img, const ElementInfo* cfg, QPainterPath& pp) {
QPainter p;
if (p.begin(&img)) {
p.setRenderHint(QPainter::Antialiasing);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(img.rect(), cfg->bgColor);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
QLinearGradient gradient(0, 0, 0, img.height() * 1.3);
gradient.setColorAt(0, cfg->fgCol1);
gradient.setColorAt(1, cfg->fgCol0);
p.setPen(QPen(cfg->fgCol1, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
p.setBrush(gradient);
p.drawPath(pp);
p.end();
}
}
QString ImageBuilder::getFaceDir() const {
return base;
}
QString ImageBuilder::getSampleText(const QString& sample, QStringList& list) {
QStringList sl = sample.split(";");
int n = fmax(0, sl.at(0).toInt());
int l = 3;
if (sl.size() > 1) l = sl.at(1).toInt();
if (n >= list.size()) n = list.size() - 1;
QString text = list.at(n);
if (l) text = text.mid(0, l);
return text;
}
void ImageBuilder::setFaceDir(const QString &basePath) {
base = basePath;
}
QImage ImageBuilder::testDigit() {
ElementInfo* cfg = wf->elemInfo(3); // function param
if (!cfg) throw new std::invalid_argument("missing cfg parameter");
QImage scratch;
for (int i=0; i < 10; ++i) {
QString text = QString("%1").arg(i);
QFont font = cfg->font;
QFontMetrics fm(font);
QRect r = text.size() == 1 ? fm.boundingRect("4") : fm.boundingRect(text);
scratch = QImage(10 + r.width() * 1.2, 10 + r.height(), QImage::Format_ARGB32);
if (cfg) {
QPainterPath pp;
QSize s = scratch.size();
qDebug() << "ImageBuilder::createImage( " << text << " )";
qDebug() << " fontMetrics:" << r.x() << "/" << r.y() << " - " << r.width() << "x" << r.height();
qDebug() << " image size:" << s.width() << "x" << s.height();
pp.addText(QPointF(5 - r.x(), -r.y() + 5), font, text);
drawImage(scratch, cfg, pp);
}
scratch.save(QString("/media/Scratch/Blah/%1.png").arg(i), "PNG");
}
return scratch;
}