This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathqadrawableprovider.cpp
296 lines (229 loc) · 6.99 KB
/
qadrawableprovider.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
#include <QtCore>
#include <QImageReader>
#include <QPainter>
#include <QColor>
#include "quickandroid.h"
#include "qadrawableprovider.h"
static QImage scaleIfValid(const QImage &image,const QSize& requestedSize) {
QImage res = image;
if (requestedSize.isValid()) {
res = image.scaled(requestedSize,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
}
return res;
}
QADrawableProvider::QADrawableProvider() : QQuickImageProvider(QQmlImageProviderBase::Image)
{
// 50MB
setLimit(50 *1024 * 1024);
}
QString QADrawableProvider::basePath() const
{
return m_basePath;
}
void QADrawableProvider::setBasePath(const QString &basePath)
{
m_basePath = basePath;
if (m_basePath.indexOf("qrc://") == 0)
m_basePath.replace(QRegExp("^qrc://"),":");
}
QImage QADrawableProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
Q_UNUSED(requestedSize);
QImage image;
if (contains(id)) {
image = get(id);
*size = image.size();
image = scaleIfValid(image,requestedSize);
return image;
}
// ID Parser.
QUrl url(id);
QString filename = url.path();
QColor tintColor = parseTintColor(url.query());
QList<QPair<QString,qreal> > resolutions;
resolutions << QPair<QString,qreal>("xxxhdpi",4)
<< QPair<QString,qreal>("xxhdpi",3)
<< QPair<QString,qreal>("xhdpi",2)
<< QPair<QString,qreal>("hdpi",1.5)
<< QPair<QString,qreal>("mdpi",1)
<< QPair<QString,qreal>("ldpi",0.75);
qreal dp = QuickAndroid::dp(); // Try the best solution
int index = -1;
for (int i = 0 ; i < resolutions.size() ; i++ ) {
if (resolutions.at(i).second == dp) {
index = i;
break;
}
}
if (index < 0) {
qWarning() << "Unknown DP!?";
index = 0;
}
// Load from current DPI path
QString dpi = resolutions.at(index).first;
image = loadFileName(filename,dpi, tintColor);
if (!image.isNull()) {
insert(id,image);
*size = image.size();
image = scaleIfValid(image,requestedSize);
return image;
}
for (int i = 0 ; i < resolutions.size() ; i++ ) {
dpi = resolutions.at(i).first;
qreal imageDp = resolutions.at(i).second;
image = loadFileName(filename, dpi, tintColor, dp / imageDp);
if (image.isNull())
continue;
insert(id,image);
*size = image.size();
image = scaleIfValid(image,requestedSize);
return image;
}
// Load from drawable/ folder
image = loadPrefix(m_basePath + "/drawable/" + filename, tintColor, 1.0);
if (image.isNull()) {
qWarning() << QString("Failed to load image://drawable/%1").arg(id);
} else {
insert(id,image);
*size = image.size();
image = scaleIfValid(image,requestedSize);
}
return image;
}
bool QADrawableProvider::contains(const QString &id)
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
return storage.contains(id);
}
QImage QADrawableProvider::get(const QString &id)
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
return *storage[id];
}
void QADrawableProvider::insert(const QString &id, QImage image)
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
int cost = image.byteCount();
QImage *tmp = new QImage();
*tmp = image;
storage.insert(id,tmp,cost);
}
QImage QADrawableProvider::loadFileName(QString filename, QString dpi ,QColor tintColor, qreal scale)
{
QString pathFormat("%1/drawable-%2/%3");
QImage image;
QString path = pathFormat.arg(m_basePath).arg(dpi).arg(filename);
image = loadPrefix(path,tintColor, scale);
return image;
}
QImage QADrawableProvider::loadPrefix(QString prefix, QColor tintColor, qreal scale)
{
QImage image;
QStringList exts;
exts << ".png" << ".jpg" << ""; // In case extension is already included;
for (int i = 0 ; i < exts.size();i++) {
image = loadAbsPath(prefix + exts.at(i),tintColor, scale);
if (!image.isNull())
break;
}
return image;
}
QImage QADrawableProvider::loadAbsPath(QString path, QColor tintColor, qreal scale)
{
QImage image;
QFileInfo info(path);
if (!info.exists())
return image;
QImageReader reader(path);
if (scale != 1.0) {
QSize size = reader.size();
size.setWidth(size.width() * scale);
size.setHeight(size.height() * scale);
reader.setScaledSize(size);
}
image = reader.read();
if (!image.isNull() && tintColor.isValid()) {
image = colorize(image,tintColor);
}
if (image.isNull()) {
qWarning() << QString("Failed to read %1 : %2").arg(path).arg(reader.errorString());
}
return image;
}
QColor QADrawableProvider::parseTintColor(const QString &query)
{
QColor tintColor;
QStringList token = query.split("&");
Q_FOREACH(QString expression,token) {
QStringList pair = expression.split("=");
if (pair.size() != 2)
continue;
if (pair.at(0) != "tintColor")
continue;
QString color = pair.at(1);
if (QColor::isValidColor(color)) {
tintColor = QColor(color);
break;
}
color = QString("#") + color;
if (QColor::isValidColor(color)) {
tintColor = QColor(color);
break;
}
}
return tintColor;
}
QImage QADrawableProvider::colorize(QImage src, QColor tintColor)
{
if (src.format() != QImage::Format_ARGB32) {
src = src.convertToFormat(QImage::Format_ARGB32);
}
QImage dst = QImage(src.size(), src.format());
gray(dst,src);
QPainter painter(&dst);
QColor pureColor = tintColor;
pureColor.setAlpha(255);
painter.setCompositionMode(QPainter::CompositionMode_Screen);
painter.fillRect(0,0,dst.width(),dst.height(),pureColor);
painter.end();
if (tintColor.alpha() != 255) {
QImage buffer = QImage(src.size(), src.format());
buffer.fill(QColor("transparent"));
QPainter bufPainter(&buffer);
qreal opacity = tintColor.alpha() / 255.0;
bufPainter.setOpacity(opacity);
bufPainter.drawImage(0,0,dst);
bufPainter.end();
dst = buffer;
}
if (src.hasAlphaChannel()) {
dst.setAlphaChannel(src.alphaChannel());
}
return dst;
}
void QADrawableProvider::gray(QImage& dest,QImage& src)
{
for (int y = 0; y < src.height(); ++y) {
unsigned int *data = (unsigned int *)src.scanLine(y);
unsigned int *outData = (unsigned int*)dest.scanLine(y);
for (int x = 0 ; x < src.width(); x++) {
int val = qGray(data[x]);
outData[x] = qRgba(val,val,val,qAlpha(data[x]));
}
}
}
int QADrawableProvider::limit() const
{
return m_limit;
}
void QADrawableProvider::setLimit(int limit)
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
m_limit = limit;
}