3#include "resources/config.h"
5#include <QFontDatabase>
6#include <QGuiApplication>
20 void setIcon(
const QIcon &icon);
21 void addIconName(
const QString &iconName);
22 const QIcon &locateIcon();
25 QStringList iconNames;
29void IconOverride::setIcon(
const QIcon &icon)
35void IconOverride::addIconName(
const QString &iconName)
37 iconNames.append(iconName);
38 if (!cachedIcon.isNull()) {
43const QIcon &IconOverride::locateIcon()
45 if (!cachedIcon.isNull()) {
48 for (
const auto &iconName : std::as_const(iconNames)) {
49 cachedIcon = QIcon::fromTheme(iconName);
50 if (!cachedIcon.isNull()) {
57struct Renderer::InternalData {
58 explicit InternalData(
int id);
59 static constexpr int invalidId = -1;
62 QStringList fontFamilies;
63 QHash<QChar, IconOverride> overrides;
64 QPaintDevice *paintDevice;
67Renderer::InternalData::InternalData(
int id)
69 , fontFamilies(id != invalidId ? QFontDatabase::applicationFontFamilies(id) : QStringList())
70 , paintDevice(nullptr)
86Renderer::Renderer(
const QString &fontFileName)
87 : m_d(std::make_unique<InternalData>(
88 QFontDatabase::addApplicationFont(fontFileName.isEmpty() ? QStringLiteral(
":/" META_FONT_FILE_NAME) : fontFileName)))
96 : m_d(std::make_unique<InternalData>(QFontDatabase::addApplicationFont(fontData)))
105 if (QCoreApplication::instance() && m_d->id != InternalData::invalidId) {
106 QFontDatabase::removeApplicationFont(m_d->id);
113Renderer::operator bool()
const
115 return !m_d->fontFamilies.empty();
119static void renderInternally(QChar character, QPainter *painter, QFont &&font,
const QRect &rect,
const QColor &color)
121 font.setPixelSize(rect.height());
123 painter->setFont(font);
124 painter->setPen(color);
125 painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
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);
142 renderInternally(character, painter, QFont(m_d->fontFamilies.front()), rect, color);
149QPixmap
Renderer::pixmap(QChar icon,
const QSize &size,
const QColor &color, qreal scaleFactor)
const
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);
157 if (!
static_cast<bool>(scaleFactor)) {
159#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
160 !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps)
164 (m_d->paintDevice ? m_d->paintDevice->devicePixelRatioF() : qGuiApp->devicePixelRatio());
167 const auto scaledSize = QSize(size * scaleFactor);
168 auto pm = QPixmap(scaledSize);
169 pm.fill(QColor(Qt::transparent));
171 auto painter = QPainter(&pm);
172 renderInternally(icon, &painter, QFont(m_d->fontFamilies.front()), QRect(QPoint(), scaledSize), color);
174 pm.setDevicePixelRatio(scaleFactor);
196 return pixmap(icon, size, color, 0.0);
217 m_d->overrides[character].addIconName(iconNameInTheme);
225 m_d->overrides[character].setIcon(
override);
233 m_d->overrides.clear();
244 m_d->paintDevice = paintDevice;
252 static auto globalRenderer =
Renderer();
253 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.
QPixmap pixmap(QChar icon, const QSize &size, const QColor &color, qreal scaleFactor) const
Renders the specified character as pixmap of the specified size.
void setAssociatedPaintDevice(QPaintDevice *paintDevice)
Sets the associated paintDevice.
~Renderer()
Destructs the renderer.
void render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
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