-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconengine.cpp
295 lines (249 loc) · 9.67 KB
/
iconengine.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
#include "iconengine.h"
QList<IconEngine::cacheIcon> IconEngine::memorySizes = QList<IconEngine::cacheIcon>();
IconEngine::IconEngine(QString iconName) : QIconEngine()
{
icName = iconName;
if (iconName != "") {
/*pendingIcons = QtConcurrent::run([=](QString icName) -> QList<IconEngine::iconInfo> {
return load(icName);
}, iconName);*/
/*bool loadIcons = true;
for (int i = 0; i < memorySizes.count(); i++) {
cacheIcon icon = memorySizes.at(i);
if (icon.name == icName) {
loadIcons = false;
i = memorySizes.count();
}
}
if (loadIcons) {*/
listOfIcons = load(icName);
/*for (iconInfo icons : listOfIcons) {
QImage px(icons.fileName);
tintImage(px, QApplication::palette("QLabel").color(QPalette::WindowText));
cacheIcon cache;
cache.name = icName;
cache.size = icons.size;
cache.pm = QPixmap::fromImage(px);
memorySizes.append(cache);
}*/
//}
}
}
QList<IconEngine::iconInfo> IconEngine::load(QString icName) {
QList<IconEngine::iconInfo> retval;
QString theme = QIcon::themeName();
//Theme search paths: ~/.local/usr/share/icons /usr/share/icons
auto loader = [=](QString icName) {
QList<IconEngine::iconInfo> retval;
if (theme.startsWith("local:")) {
retval = getMatchingIcon(QDir::homePath() + "/.local/share/icons/" + theme.mid(6), icName);
} else {
retval = getMatchingIcon("/usr/share/icons/" + theme, icName);
}
if (retval.count() != 0) return retval;
retval = getMatchingIcon("/usr/share/icons/", icName, false);
if (retval.count() != 0) return retval;
retval = getMatchingIcon("/usr/share/icons/hicolor/", icName);
if (retval.count() != 0) return retval;
retval = getMatchingIcon("/usr/share/pixmaps/", icName);
if (retval.count() != 0) return retval;
retval = getMatchingIcon(QDir::homePath() + "/.local/share/icons/hicolor/", icName);
return retval;
};
//Search for right-to-left icons first
if (QApplication::layoutDirection() == Qt::RightToLeft) {
retval = loader(icName + "-rtl");
if (retval.count() != 0) return retval;
}
return loader(icName);
}
QString IconEngine::iconName() const {
if (listOfIcons.count() == 0) {
return "";
} else {
return icName;
}
}
QImage IconEngine::extractImage(QSharedMemory *sharedMemory) {
QBuffer imageData;
QDataStream input(&imageData);
QImage image;
if (!sharedMemory->isAttached()) {
sharedMemory->attach(QSharedMemory::ReadOnly);
}
sharedMemory->lock();
imageData.setData((char*) sharedMemory->data(), sharedMemory->size());
imageData.open(QBuffer::ReadOnly);
input >> image;
sharedMemory->unlock();
return image;
}
void IconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) {
/*for (cacheIcon icon : memorySizes) {
if ((icon.name == icName) && (icon.size == rect.width())) {
painter->drawPixmap(rect, icon.pm);
return;
}
}*/
/*if (pendingIcons.isRunning()) {
pendingIcons.waitForFinished();
}
listOfIcons = pendingIcons.result();*/
if (listOfIcons.count() > 0) {
QString fileName;
int currentIcon = -1;
QImage px;
bool imageLoaded = false;
for (int i = 0; i < listOfIcons.count(); i++) {
iconInfo info = listOfIcons.at(i);
if (info.size == rect.width()) {
if (!info.iconData->isAttached()) {
if (!info.iconData->attach()) {
fileName = info.fileName;
}
} else if (info.iconData->isAttached() && info.iconData->size() != 0) {
px = extractImage(info.iconData);
if (!px.isNull()) {
imageLoaded = true;
} else {
fileName = info.fileName;
}
} else {
fileName = info.fileName;
}
currentIcon = i;
}
}
if (!imageLoaded) {
if (fileName == "") {
int currentSize;
for (int i = 0; i < listOfIcons.count(); i++) {
iconInfo info = listOfIcons.at(i);
if ((info.size > rect.width()) && (info.size < currentSize)) {
fileName = info.fileName;
currentSize = info.size;
currentIcon = i;
}
}
if (fileName == "") {
fileName = listOfIcons.first().fileName;
currentIcon = 0;
}
}
QString suffix = fileName;
suffix = suffix.mid(suffix.lastIndexOf(".") + 1);
if (suffix == "svg") {
px = QImage(rect.size(), QImage::Format_ARGB32);
px.fill(Qt::transparent);
QSvgRenderer renderer(fileName);
QPainter painter(&px);
renderer.render(&painter);
} else {
px = QImage(fileName);
}
tintImage(px, QApplication::palette("QLabel").color(QPalette::WindowText));
QBuffer imageData;
imageData.open(QBuffer::ReadWrite);
QDataStream output(&imageData);
output << px;
iconInfo icInfo = listOfIcons.at(currentIcon);
//icInfo.iconData = new QSharedMemory("ts-qtpl.icon." + icName + "." + QString::number(rect.size().width()));
icInfo.iconData->create(imageData.size());
icInfo.iconData->lock();
memcpy((char*) icInfo.iconData->data(), imageData.data().data(), qMin(icInfo.iconData->size(), (int) imageData.size()));
icInfo.iconData->unlock();
listOfIcons.replace(currentIcon, icInfo);
}
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->drawImage(rect, px);
}
}
QList<IconEngine::iconInfo> IconEngine::getMatchingIcon(QString searchPath, QString icName, bool subdirectories) {
QList<iconInfo> retval;
QDir path = searchPath;
if (path.exists()) {
QDirIterator* iterator;
if (subdirectories) {
iterator = new QDirIterator(path, QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
} else {
iterator = new QDirIterator(path);
}
while (iterator->hasNext()) {
iterator->next();
QFileInfo info = iterator->fileInfo();
QString baseName = info.completeBaseName();
//QString baseName = iterator->filePath();
//baseName = baseName.mid(baseName.lastIndexOf("/") + 1);
//baseName = baseName.left(baseName.indexOf("."));
if (baseName.toLower() == icName.toLower()) {
QString suffix = iterator->filePath();
suffix = suffix.mid(suffix.lastIndexOf(".") + 1);
/*if (suffix == "svg") {
for (int i = 0; i < 4; i++) {
iconInfo icInfo;
icInfo.fileName = iterator->filePath();
icInfo.size = 16 + i * 8;
retval.append(icInfo);
}
} else {*/
QImage ic(iterator->filePath());
iconInfo icInfo;
icInfo.fileName = iterator->filePath();
icInfo.size = ic.size().width();
icInfo.iconData = new QSharedMemory("ts-qtpl.icon." + icName + "." + QString::number(ic.size().width()));
retval.append(icInfo);
//}
}
}
delete iterator;
}
return retval;
}
QIconEngine* IconEngine::clone() const {
return new IconEngine(icName);
}
QPixmap IconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) {
QPixmap px(size);
px.fill(Qt::transparent);
QPainter painter(&px);
this->paint(&painter, QRect(0, 0, size.width(), size.height()), mode, state);
painter.end();
return px;
//return QIconEngine::pixmap(size, mode, state);
}
void IconEngine::tintImage(QImage &image, QColor tint) const {
//bool doPaint = true;
int failNum = 0;
for (int y = 0; y < image.height(); y++) {
for (int x = 0; x < image.width(); x++) {
QColor pixelCol = image.pixelColor(x, y);
//int blue = pixelCol.blue(), green = pixelCol.green(), red = pixelCol.red();
if ((pixelCol.blue() > pixelCol.green() - 10 && pixelCol.blue() < pixelCol.green() + 10) &&
(pixelCol.green() > pixelCol.red() - 10 && pixelCol.green() < pixelCol.red() + 10)) {
} else {
failNum++;
//doPaint = false;
}
}
}
if (failNum < (image.size().width() * image.size().height()) / 8) {
QPainter painter(&image);
painter.setCompositionMode(QPainter::CompositionMode_SourceAtop);
painter.fillRect(0, 0, image.width(), image.height(), tint);
painter.end();
}
}
QList<QSize> IconEngine::availableSizes(QIcon::Mode mode, QIcon::State state) const {
QList<QSize> retval;
for (iconInfo icon : listOfIcons) {
retval.append(QSize(icon.size, icon.size));
}
return retval;
}
bool IconEngine::isNull() {
if (listOfIcons.count() == 0) {
return true;
} else {
return false;
}
}