Skip to content

Commit

Permalink
Add experimental theme override
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Oct 3, 2022
1 parent 65b3838 commit 855b3af
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
54 changes: 44 additions & 10 deletions qtforkawesome/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <QFontDatabase>
#include <QGuiApplication>
#include <QHash>
#include <QIcon>
#include <QPainter>

/// \brief Contains classes provided by the QtForkAwesome library.
Expand All @@ -17,6 +19,7 @@ struct Renderer::InternalData {

int id;
QStringList fontFamilies;
QHash<QChar, QString> themeOverrides;
};

Renderer::InternalData::InternalData(int id)
Expand Down Expand Up @@ -69,28 +72,49 @@ Renderer::operator bool() const
return !m_d->fontFamilies.empty();
}

/*!
* \brief Renders the specified \a icon using the specified \a painter.
*/
void QtForkAwesome::Renderer::render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
/// \cond
static void renderInternally(QChar character, QPainter *painter, QFont &&font, const QRect &rect, const QColor &color)
{
if (!*this) {
return;
}
auto font = QFont(m_d->fontFamilies.front());
font.setPixelSize(rect.height());
painter->save();
painter->setFont(font);
painter->setPen(color);
painter->drawText(rect, QString(character), QTextOption(Qt::AlignCenter));
painter->restore();
}
/// \endcond

/*!
* \brief Renders the specified \a icon using the specified \a painter.
*/
void QtForkAwesome::Renderer::render(QChar character, QPainter *painter, const QRect &rect, const QColor &color) const
{
auto themeOverride = m_d->themeOverrides.find(character);
if (themeOverride != m_d->themeOverrides.end()) {
const auto icon = QIcon::fromTheme(*themeOverride);
if (!icon.isNull()) {
painter->drawPixmap(rect, icon.pixmap(rect.size(), QIcon::Normal, QIcon::On));
return;
}
}
if (*this) {
renderInternally(character, painter, QFont(m_d->fontFamilies.front()), rect, color);
}
}

/*!
* \brief Renders the specified \a character as pixmap of the specified \a size.
*/
QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QColor &color) const
{
auto themeOverride = m_d->themeOverrides.find(icon);
if (themeOverride != m_d->themeOverrides.end()) {
const auto icon = QIcon::fromTheme(*themeOverride);
if (!icon.isNull()) {
return icon.pixmap(size, QIcon::Normal, QIcon::On);
}
}

const auto scaleFactor =
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
!QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? 1.0 :
Expand All @@ -99,8 +123,10 @@ QPixmap QtForkAwesome::Renderer::pixmap(QChar icon, const QSize &size, const QCo
const auto scaledSize = QSize(size * scaleFactor);
auto pm = QPixmap(scaledSize);
pm.fill(QColor(Qt::transparent));
auto painter = QPainter(&pm);
render(icon, &painter, QRect(QPoint(), scaledSize), color);
if (*this) {
auto painter = QPainter(&pm);
renderInternally(icon, &painter, QFont(m_d->fontFamilies.front()), QRect(QPoint(), scaledSize), color);
}
pm.setDevicePixelRatio(scaleFactor);
return pm;
}
Expand All @@ -113,6 +139,14 @@ QPixmap Renderer::pixmap(Icon icon, const QSize &size, const QColor &color) cons
return pixmap(QChar(static_cast<IconBaseType>(icon)), size, color);
}

/*!
* \brief Uses the icon from the current icon theme obtained via QIcon::fromTheme() for \a character if it exists.
*/
void Renderer::addThemeOverride(QChar character, const QString &iconNameInTheme)
{
m_d->themeOverrides.insert(character, iconNameInTheme);
}

/*!
* \brief Returns the global instance (which is so far only used by the icon engine plugin).
*/
Expand Down
11 changes: 11 additions & 0 deletions qtforkawesome/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class QT_FORK_AWESOME_EXPORT Renderer {
QPixmap pixmap(QChar icon, const QSize &size, const QColor &color) const;
QPixmap pixmap(Icon icon, const QSize &size, const QColor &color) const;

void addThemeOverride(QChar character, const QString &iconNameInTheme);
void addThemeOverride(Icon icon, const QString &iconNameInTheme);

static Renderer &global();

private:
Expand All @@ -47,6 +50,14 @@ inline void Renderer::render(Icon icon, QPainter *painter, const QRect &rect, co
render(QChar(static_cast<IconBaseType>(icon)), painter, rect, color);
}

/*!
* \brief Uses the icon from the current icon theme obtained via QIcon::fromTheme() for \a icon if it exists.
*/
inline void Renderer::addThemeOverride(Icon icon, const QString &iconNameInTheme)
{
addThemeOverride(QChar(static_cast<IconBaseType>(icon)), iconNameInTheme);
}

} // namespace QtForkAwesome

#endif // QT_FORK_AWESOME_RENDERER

0 comments on commit 855b3af

Please sign in to comment.