-
Notifications
You must be signed in to change notification settings - Fork 11
/
svgicons.cpp
103 lines (90 loc) · 3.08 KB
/
svgicons.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
/* Adapted from:
* Cantata - Qt5 Graphical MPD Client for Linux, Windows, macOS, Haiku
* File: support/monoicon.cpp
* Copyright: 2011-2018 Craig Drummond
* License: GPL-3.0+
* Homepage: https://github.com/CDrummond/cantata
*/
/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2018 <[email protected]>
*
* Arqiver is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arqiver is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @license GPL-3.0+ <https://spdx.org/licenses/GPL-3.0+.html>
*/
#include <QFile>
#include <QIconEngine>
#include <QSvgRenderer>
#include <QPainter>
#include <QPixmapCache>
#include <QRect>
#include <QFontDatabase>
#include <QApplication>
#include <QPalette>
#include "svgicons.h"
namespace Arqiver {
class symbolicIconEngine : public QIconEngine {
public:
symbolicIconEngine(const QString &file) : fileName(file) {}
~symbolicIconEngine() override {}
symbolicIconEngine * clone() const override {
return new symbolicIconEngine(fileName);
}
void paint(QPainter *painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) override {
Q_UNUSED(state)
QColor col;
if (mode == QIcon::Disabled)
col = QApplication::palette().color(QPalette::Disabled, QPalette::WindowText);
else if (mode == QIcon::Selected)
col = QApplication::palette().highlightedText().color();
else
col = QApplication::palette().windowText().color();
QString key = fileName
+ "-" + QString::number(rect.width())
+ "-" + QString::number(rect.height())
+ "-" + col.name();
QPixmap pix;
if (!QPixmapCache::find(key, &pix)) {
pix = QPixmap(rect.width(), rect.height());
pix.fill(Qt::transparent);
if (!fileName.isEmpty()) {
QSvgRenderer renderer;
QFile f(fileName);
QByteArray bytes;
if (f.open(QIODevice::ReadOnly))
bytes = f.readAll();
if (!bytes.isEmpty())
bytes.replace("#000", col.name().toLatin1());
renderer.load(bytes);
QPainter p(&pix);
renderer.render(&p, QRect(0, 0, rect.width(), rect.height()));
}
QPixmapCache::insert(key, pix);
}
painter->drawPixmap(rect.topLeft(), pix);
}
QPixmap pixmap(const QSize& size, QIcon::Mode mode, QIcon::State state) override {
QPixmap pix(size);
pix.fill(Qt::transparent);
QPainter painter(&pix);
paint(&painter, QRect (QPoint (0, 0), size), mode, state);
return pix;
}
private:
QString fileName;
};
QIcon symbolicIcon::icon(const QString& fileName) {
return QIcon(new symbolicIconEngine(fileName));
}
}