Deduce default color from current palette or style

This commit is contained in:
Martchus 2021-09-15 19:56:09 +02:00
parent bec5b7bfd7
commit da639c816a
2 changed files with 59 additions and 9 deletions

View File

@ -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)

View File

@ -3,18 +3,26 @@
#include "../qtforkawesome/renderer.h"
#include "../qtforkawesome/utils.h"
#include "resources/config.h"
#include <qtutilities/misc/compat.h>
#include <QCoreApplication>
#include <QGuiApplication>
#include <QPainter>
#include <QString>
#include <QPalette>
#ifdef QT_FORK_AWESOME_ICON_ENGINE_ENABLE_STYLE_SUPPORT
#include <QApplication>
#include <QStyle>
#include <QStyleOption>
#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 *>(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<IconBaseType>(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