Qt ForkAwesome 0.2.2
Library that bundles ForkAwesome for use within Qt applications
Loading...
Searching...
No Matches
renderer.cpp
Go to the documentation of this file.
1#include "./renderer.h"
2
3#include "resources/config.h"
4
5#include <QFontDatabase>
6#include <QGuiApplication>
7#include <QHash>
8#include <QIcon>
9#include <QPaintDevice>
10#include <QPainter>
11
12#include <utility>
13
15namespace QtForkAwesome {
16
18
19struct IconOverride {
20 void setIcon(const QIcon &icon);
21 void addIconName(const QString &iconName);
22 const QIcon &locateIcon();
23
24private:
25 QStringList iconNames;
26 QIcon cachedIcon;
27};
28
29void IconOverride::setIcon(const QIcon &icon)
30{
31 iconNames.clear();
32 cachedIcon = icon;
33}
34
35void IconOverride::addIconName(const QString &iconName)
36{
37 iconNames.append(iconName);
38 if (!cachedIcon.isNull()) {
39 cachedIcon = QIcon();
40 }
41}
42
43const QIcon &IconOverride::locateIcon()
44{
45 if (!cachedIcon.isNull()) {
46 return cachedIcon;
47 }
48 for (const auto &iconName : std::as_const(iconNames)) {
49 cachedIcon = QIcon::fromTheme(iconName);
50 if (!cachedIcon.isNull()) {
51 return cachedIcon;
52 }
53 }
54 return cachedIcon;
55}
56
57struct Renderer::InternalData {
58 explicit InternalData(int id);
59 static constexpr int invalidId = -1;
60
61 int id;
62 QStringList fontFamilies;
63 QHash<QChar, IconOverride> overrides;
64 QPaintDevice *paintDevice;
65};
66
67Renderer::InternalData::InternalData(int id)
68 : id(id)
69 , fontFamilies(id != invalidId ? QFontDatabase::applicationFontFamilies(id) : QStringList())
70 , paintDevice(nullptr)
71{
72}
73
75
86Renderer::Renderer(const QString &fontFileName)
87 : m_d(std::make_unique<InternalData>(
88 QFontDatabase::addApplicationFont(fontFileName.isEmpty() ? QStringLiteral(":/" META_FONT_FILE_NAME) : fontFileName)))
89{
90}
91
95Renderer::Renderer(const QByteArray &fontData)
96 : m_d(std::make_unique<InternalData>(QFontDatabase::addApplicationFont(fontData)))
97{
98}
99
104{
105 if (QCoreApplication::instance() && m_d->id != InternalData::invalidId) {
106 QFontDatabase::removeApplicationFont(m_d->id);
107 }
108}
109
113Renderer::operator bool() const
114{
115 return !m_d->fontFamilies.empty();
116}
117
119static void renderInternally(QChar character, QPainter *painter, QFont &&font, const QRect &rect, const QColor &color)
120{
121 font.setPixelSize(rect.height());
122 painter->save();
123 painter->setFont(font);
124 painter->setPen(color);
125 painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
126 painter->restore();
127}
129
133void QtForkAwesome::Renderer::render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
134{
135 if (auto override = m_d->overrides.find(character); override != m_d->overrides.end()) {
136 if (const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
137 overrideIcon.paint(painter, rect, Qt::AlignCenter, QIcon::Normal, QIcon::On);
138 return;
139 }
140 }
141 if (*this) {
142 renderInternally(character, painter, QFont(m_d->fontFamilies.front()), rect, color);
143 }
144}
145
149QPixmap Renderer::pixmap(QChar icon, const QSize &size, const QColor &color, qreal scaleFactor) const
150{
151 if (auto override = m_d->overrides.find(icon); override != m_d->overrides.end()) {
152 if (const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
153 return overrideIcon.pixmap(size, QIcon::Normal, QIcon::On);
154 }
155 }
156
157 if (!static_cast<bool>(scaleFactor)) {
158 scaleFactor =
159#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
160 !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps)
161 ? 1.0
162 :
163#endif
164 (m_d->paintDevice ? m_d->paintDevice->devicePixelRatioF() : qGuiApp->devicePixelRatio());
165 }
166
167 const auto scaledSize = QSize(size * scaleFactor);
168 auto pm = QPixmap(scaledSize);
169 pm.fill(QColor(Qt::transparent));
170 if (*this) {
171 auto painter = QPainter(&pm);
172 renderInternally(icon, &painter, QFont(m_d->fontFamilies.front()), QRect(QPoint(), scaledSize), color);
173 }
174 pm.setDevicePixelRatio(scaleFactor);
175 return pm;
176}
177
181QPixmap Renderer::pixmap(Icon icon, const QSize &size, const QColor &color, qreal scaleFactor) const
182{
183 return pixmap(QChar(static_cast<IconBaseType>(icon)), size, color, scaleFactor);
184}
185
194QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QColor &color) const
195{
196 return pixmap(icon, size, color, 0.0);
197}
198
207QPixmap Renderer::pixmap(Icon icon, const QSize &size, const QColor &color) const
208{
209 return pixmap(QChar(static_cast<IconBaseType>(icon)), size, color, 0.0);
210}
211
215void Renderer::addThemeOverride(QChar character, const QString &iconNameInTheme)
216{
217 m_d->overrides[character].addIconName(iconNameInTheme);
218}
219
223void Renderer::addOverride(QChar character, const QIcon &override)
224{
225 m_d->overrides[character].setIcon(override);
226}
227
232{
233 m_d->overrides.clear();
234}
235
242void Renderer::setAssociatedPaintDevice(QPaintDevice *paintDevice)
243{
244 m_d->paintDevice = paintDevice;
245}
246
251{
252 static auto globalRenderer = Renderer();
253 return globalRenderer;
254}
255
256} // namespace QtForkAwesome
Allows rendering a QtForkAwesome::Icon (or an arbitrary QChar using an arbitrary font file).
Renderer(const QString &fontFileName=QString())
Constructs a new renderer with the given fontFileName.
Definition renderer.cpp:86
static Renderer & global()
Returns the global instance (which is so far only used by the icon engine plugin).
Definition renderer.cpp:250
void clearOverrides()
Clears all overrides added via addThemeOverride() or addOverride().
Definition renderer.cpp:231
void addThemeOverride(QChar character, const QString &iconNameInTheme)
Uses the icon from the current icon theme obtained via QIcon::fromTheme() for character if it exists.
Definition renderer.cpp:215
void addOverride(QChar character, const QIcon &override)
Uses the specified override icon for character if it is not null.
Definition renderer.cpp:223
QPixmap pixmap(QChar icon, const QSize &size, const QColor &color, qreal scaleFactor) const
Renders the specified character as pixmap of the specified size.
Definition renderer.cpp:149
void setAssociatedPaintDevice(QPaintDevice *paintDevice)
Sets the associated paintDevice.
Definition renderer.cpp:242
~Renderer()
Destructs the renderer.
Definition renderer.cpp:103
void render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
Contains classes provided by the QtForkAwesome library.
Definition renderer.h:20
Icon
The Icon enum specifies a ForkAwesome icon for calling QtForkAwesome::Renderer::render().
Definition icon.h:11
std::remove_reference_t< decltype(QChar().unicode())> IconBaseType
Definition iconfwd.h:10