3#include "resources/config.h"
5#include <QFontDatabase>
6#include <QGuiApplication>
18 void setIcon(
const QIcon &icon);
19 void addIconName(
const QString &iconName);
20 const QIcon &locateIcon();
23 QStringList iconNames;
27void IconOverride::setIcon(
const QIcon &icon)
33void IconOverride::addIconName(
const QString &iconName)
35 iconNames.append(iconName);
36 if (!cachedIcon.isNull()) {
41const QIcon &IconOverride::locateIcon()
43 if (!cachedIcon.isNull()) {
46 for (
const auto &iconName : iconNames) {
47 cachedIcon = QIcon::fromTheme(iconName);
48 if (!cachedIcon.isNull()) {
55struct Renderer::InternalData {
56 explicit InternalData(
int id);
57 static constexpr int invalidId = -1;
60 QStringList fontFamilies;
61 QHash<QChar, IconOverride> overrides;
62 QPaintDevice *paintDevice;
65Renderer::InternalData::InternalData(
int id)
67 , fontFamilies(id != invalidId ? QFontDatabase::applicationFontFamilies(id) : QStringList())
68 , paintDevice(nullptr)
84Renderer::Renderer(
const QString &fontFileName)
85 : m_d(std::make_unique<InternalData>(
86 QFontDatabase::addApplicationFont(fontFileName.isEmpty() ? QStringLiteral(
":/" META_FONT_FILE_NAME) : fontFileName)))
94 : m_d(std::make_unique<InternalData>(QFontDatabase::addApplicationFont(fontData)))
103 if (QCoreApplication::instance() && m_d->id != InternalData::invalidId) {
104 QFontDatabase::removeApplicationFont(m_d->id);
111Renderer::operator bool()
const
113 return !m_d->fontFamilies.empty();
117static void renderInternally(QChar character, QPainter *painter, QFont &&font,
const QRect &rect,
const QColor &color)
119 font.setPixelSize(rect.height());
121 painter->setFont(font);
122 painter->setPen(color);
123 painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
133 if (
auto override = m_d->overrides.find(character);
override != m_d->overrides.end()) {
134 if (
const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
135 painter->drawPixmap(rect, overrideIcon.pixmap(rect.size(), QIcon::Normal, QIcon::On));
140 renderInternally(character, painter, QFont(m_d->fontFamilies.front()), rect, color);
147QPixmap
Renderer::pixmap(QChar icon,
const QSize &size,
const QColor &color, qreal scaleFactor)
const
149 if (
auto override = m_d->overrides.find(icon);
override != m_d->overrides.end()) {
150 if (
const auto &overrideIcon = override->locateIcon(); !overrideIcon.isNull()) {
151 return overrideIcon.pixmap(size, QIcon::Normal, QIcon::On);
155 if (!
static_cast<bool>(scaleFactor)) {
157#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
158 !QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps)
162 (m_d->paintDevice ? m_d->paintDevice->devicePixelRatioF() : qGuiApp->devicePixelRatio());
165 const auto scaledSize = QSize(size * scaleFactor);
166 auto pm = QPixmap(scaledSize);
167 pm.fill(QColor(Qt::transparent));
169 auto painter = QPainter(&pm);
170 renderInternally(icon, &painter, QFont(m_d->fontFamilies.front()), QRect(QPoint(), scaledSize), color);
172 pm.setDevicePixelRatio(scaleFactor);
194 return pixmap(icon, size, color, 0.0);
215 m_d->overrides[character].addIconName(iconNameInTheme);
223 m_d->overrides[character].setIcon(
override);
231 m_d->overrides.clear();
242 m_d->paintDevice = paintDevice;
250 static auto globalRenderer =
Renderer();
251 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