From da639c816a100d9a10244d554630a23784db121e Mon Sep 17 00:00:00 2001 From: Martchus Date: Wed, 15 Sep 2021 19:56:09 +0200 Subject: [PATCH] Deduce default color from current palette or style --- iconengineplugin/CMakeLists.txt | 10 ++++++ iconengineplugin/iconengine.cpp | 58 ++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/iconengineplugin/CMakeLists.txt b/iconengineplugin/CMakeLists.txt index 108e098..bb484f7 100644 --- a/iconengineplugin/CMakeLists.txt +++ b/iconengineplugin/CMakeLists.txt @@ -17,6 +17,16 @@ use_qt_utilities(ONLY_HEADERS VISIBILITY PRIVATE) find_package(qtforkawesome${CONFIGURATION_PACKAGE_SUFFIX_QTFORKAWESOME} ${META_APP_VERSION} REQUIRED) use_qt_fork_awesome() +# use Qt Gui module +list(APPEND ADDITIONAL_QT_MODULES Gui) + +# configure styling support +option(ENABLE_STYLE_SUPPORT "whether to apply icon mode via QStyle (pulls in Qt Widgets dependency)" OFF) +if (ENABLE_STYLE_SUPPORT) + list(APPEND ADDITIONAL_QT_MODULES Widgets) + set(META_CUSTOM_CONFIG "#define ${META_PROJECT_VARNAME}_ENABLE_STYLE_SUPPORT 1\n") +endif () + include(BasicConfig) include(QtGuiConfig) include(QtConfig) diff --git a/iconengineplugin/iconengine.cpp b/iconengineplugin/iconengine.cpp index d093a6f..fa5c180 100644 --- a/iconengineplugin/iconengine.cpp +++ b/iconengineplugin/iconengine.cpp @@ -3,18 +3,26 @@ #include "../qtforkawesome/renderer.h" #include "../qtforkawesome/utils.h" +#include "resources/config.h" + #include -#include +#include #include #include +#include +#ifdef QT_FORK_AWESOME_ICON_ENGINE_ENABLE_STYLE_SUPPORT +#include +#include +#include +#endif namespace QtForkAwesome { IconEngine::IconEngine(Renderer &renderer) : m_renderer(renderer) , m_char(0) - , m_color(Qt::black) + , m_color() { } @@ -22,7 +30,7 @@ IconEngine::IconEngine(const IconEngine &other) : QIconEngine(other) , m_renderer(other.m_renderer) , m_char(0) - , m_color(Qt::black) + , m_color() { } @@ -32,16 +40,48 @@ IconEngine::~IconEngine() QPixmap IconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) { - Q_UNUSED(mode) Q_UNUSED(state) - return m_renderer.pixmap(QChar(m_char), size, m_color); + + auto color = m_color; + if (!m_color.isValid()) { + auto group = QPalette::Normal; + auto role = QPalette::Text; +#ifndef QT_FORK_AWESOME_ICON_ENGINE_ENABLE_STYLE_SUPPORT + switch (mode) { + case QIcon::Disabled: + group = QPalette::Disabled; + break; + case QIcon::Active: + group = QPalette::Active; + break; + case QIcon::Selected: + role = QPalette::HighlightedText; + break; + default: + ; + } +#endif + color = QGuiApplication::palette().color(group, role); + } + + auto pixmap = m_renderer.pixmap(QChar(m_char), size, color); +#ifdef QT_FORK_AWESOME_ICON_ENGINE_ENABLE_STYLE_SUPPORT + auto *const app = qobject_cast(QApplication::instance()); + if (auto *const style = app ? app->style() : nullptr) { + auto opt = QStyleOption(); + opt.palette = QGuiApplication::palette(); + if (const auto generated = style->generatedIconPixmap(mode, pixmap, &opt); !generated.isNull()) { + pixmap = generated; + } + } +#endif + + return pixmap; } void IconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) { - Q_UNUSED(mode) - Q_UNUSED(state) - m_renderer.render(QChar(m_char), painter, rect, m_color); + painter->drawPixmap(rect, pixmap(rect.size(), mode, state)); } void IconEngine::addFile(const QString &fileName, const QSize &, QIcon::Mode mode, QIcon::State state) @@ -56,7 +96,7 @@ void IconEngine::addFile(const QString &fileName, const QSize &, QIcon::Mode mod return; } m_char = static_cast(iconFromId(parts.at(0).toString())); - m_color = parts.size() > 1 ? QColor(parts.at(1)) : Qt::black; + m_color = parts.size() > 1 ? QColor(parts.at(1)) : QColor(); } QString IconEngine::key() const