3#include "resources/config.h"
5#include <QFontDatabase>
6#include <QGuiApplication>
17 void setIcon(
const QIcon &icon);
18 void addIconName(
const QString &iconName);
19 const QIcon &locateIcon();
22 QStringList iconNames;
26void IconOverride::setIcon(
const QIcon &icon)
32void IconOverride::addIconName(
const QString &iconName)
34 iconNames.append(iconName);
35 if (!cachedIcon.isNull()) {
40const QIcon &IconOverride::locateIcon()
42 if (!cachedIcon.isNull()) {
45 for (
const auto &iconName : iconNames) {
46 cachedIcon = QIcon::fromTheme(iconName);
47 if (!cachedIcon.isNull()) {
54struct Renderer::InternalData {
55 explicit InternalData(
int id);
56 static constexpr int invalidId = -1;
59 QStringList fontFamilies;
60 QHash<QChar, IconOverride> overrides;
63Renderer::InternalData::InternalData(
int id)
65 , fontFamilies(id != invalidId ? QFontDatabase::applicationFontFamilies(id) : QStringList())
81Renderer::Renderer(
const QString &fontFileName)
82 : m_d(std::make_unique<InternalData>(
83 QFontDatabase::addApplicationFont(fontFileName.isEmpty() ? QStringLiteral(
":/" META_FONT_FILE_NAME) : fontFileName)))
91 : m_d(std::make_unique<InternalData>(QFontDatabase::addApplicationFont(fontData)))
100 if (QCoreApplication::instance() && m_d->id != InternalData::invalidId) {
101 QFontDatabase::removeApplicationFont(m_d->id);
108Renderer::operator bool()
const
110 return !m_d->fontFamilies.empty();
114static void renderInternally(QChar character, QPainter *painter, QFont &&font,
const QRect &rect,
const QColor &color)
116 font.setPixelSize(rect.height());
118 painter->setFont(font);
119 painter->setPen(color);
120 painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
130 if (
auto override = m_d->overrides.find(character);
override != m_d->overrides.end()) {
131 if (
const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
132 painter->drawPixmap(rect, overrideIcon.pixmap(rect.size(), QIcon::Normal, QIcon::On));
137 renderInternally(character, painter, QFont(m_d->fontFamilies.front()), rect, color);
146 if (
auto override = m_d->overrides.find(icon);
override != m_d->overrides.end()) {
147 if (
const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
148 return overrideIcon.pixmap(size, QIcon::Normal, QIcon::On);
152 const auto scaleFactor =
153#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
154 !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? 1.0 :
156 qGuiApp->devicePixelRatio();
157 const auto scaledSize = QSize(size * scaleFactor);
158 auto pm = QPixmap(scaledSize);
159 pm.fill(QColor(Qt::transparent));
161 auto painter = QPainter(&pm);
162 renderInternally(icon, &painter, QFont(m_d->fontFamilies.front()), QRect(QPoint(), scaledSize), color);
164 pm.setDevicePixelRatio(scaleFactor);
181 m_d->overrides[character].addIconName(iconNameInTheme);
189 m_d->overrides[character].setIcon(
override);
197 m_d->overrides.clear();
205 static auto globalRenderer =
Renderer();
206 return globalRenderer;
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.
static Renderer & global()
Returns the global instance (which is so far only used by the icon engine plugin).
void clearOverrides()
Clears all overrides added via addThemeOverride() or addOverride().
void addThemeOverride(QChar character, const QString &iconNameInTheme)
Uses the icon from the current icon theme obtained via QIcon::fromTheme() for character if it exists.
void addOverride(QChar character, const QIcon &override)
Uses the specified override icon for character if it is not null.
~Renderer()
Destructs the renderer.
QPixmap pixmap(QChar icon, const QSize &size, const QColor &color) const
Renders the specified character as pixmap of the specified size.
void render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
Renders the specified icon using the specified painter.
Contains classes provided by the QtForkAwesome library.
Icon
The Icon enum specifies a ForkAwesome icon for calling QtForkAwesome::Renderer::render().
std::remove_reference_t< decltype(QChar().unicode())> IconBaseType