From fbf820d53830b1d2791a7d4d18b094e162ab8e39 Mon Sep 17 00:00:00 2001 From: Martchus Date: Mon, 13 Sep 2021 20:49:52 +0200 Subject: [PATCH] Add a basic QIconEnginePlugin for ForkAwesome --- CMakeLists.txt | 1 + README.md | 7 ++ iconengineplugin/CMakeLists.txt | 25 +++++++ iconengineplugin/global.h | 27 +++++++ iconengineplugin/iconengine.cpp | 72 +++++++++++++++++++ iconengineplugin/iconengine.h | 34 +++++++++ iconengineplugin/main.cpp | 33 +++++++++ iconengineplugin/qtforkawesomeiconengine.json | 3 + qtforkawesome/renderer.cpp | 2 +- 9 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 iconengineplugin/CMakeLists.txt create mode 100644 iconengineplugin/global.h create mode 100644 iconengineplugin/iconengine.cpp create mode 100644 iconengineplugin/iconengine.h create mode 100644 iconengineplugin/main.cpp create mode 100644 iconengineplugin/qtforkawesomeiconengine.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 1719f6e..542d8df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,3 +26,4 @@ find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.5.0 REQUIRED) find_package(qtutilities${CONFIGURATION_PACKAGE_SUFFIX_QTUTILITIES} 6.3.0 REQUIRED) add_subdirectory(${META_PROJECT_NAME}) +add_subdirectory(iconengineplugin) diff --git a/README.md b/README.md index bfe3a4d..a69a0e9 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,10 @@ renderer.pixmap(QtForkAwesome::Icon::Globe, QSize(64, 64), Qt::black); ``` There's also `renderer.render(…)` which takes a `QPainter` directly. + +A `QIconEnginePlugin` is provided as well. When it is loaded one can +create a `QIcon` using a file name with `.fa` extension, e.g.: + +``` +const auto icon = QIcon(QStringLiteral("qrcode:blue.fa")); +``` diff --git a/iconengineplugin/CMakeLists.txt b/iconengineplugin/CMakeLists.txt new file mode 100644 index 0000000..697b584 --- /dev/null +++ b/iconengineplugin/CMakeLists.txt @@ -0,0 +1,25 @@ +# additional meta data +set(META_PROJECT_NAME qtforkawesomeiconengine) +set(META_PROJECT_VARNAME QT_FORK_AWESOME_ICON_ENGINE) +set(META_APP_NAME "QIconEngine for ForkAwesome") +set(META_APP_DESCRIPTION "QIconEngine for ForkAwesome") +set(META_PROJECT_TYPE qtplugin) +set(META_PLUGIN_CATEGORY iconengines) + +set(HEADER_FILES iconengine.h) +set(SRC_FILES iconengine.cpp main.cpp) + +# use headers and CMake modules from c++utilities and qtutilities +use_cpp_utilities(ONLY_HEADERS VISIBILITY PUBLIC) +use_qt_utilities(ONLY_HEADERS VISIBILITY PRIVATE) + +# use main qtforkawesome library +find_package(qtforkawesome ${META_APP_VERSION} REQUIRED) +use_qt_fork_awesome() + +include(BasicConfig) +include(QtGuiConfig) +include(QtConfig) +include(WindowsResources) +include(LibraryTarget) +include(ConfigHeader) diff --git a/iconengineplugin/global.h b/iconengineplugin/global.h new file mode 100644 index 0000000..abfa392 --- /dev/null +++ b/iconengineplugin/global.h @@ -0,0 +1,27 @@ +// Created via CMake from template global.h.in +// WARNING! Any changes to this file will be overwritten by the next CMake run! + +#ifndef QT_FORK_AWESOME_ICON_ENGINE_GLOBAL +#define QT_FORK_AWESOME_ICON_ENGINE_GLOBAL + +#include + +#ifdef QT_FORK_AWESOME_ICON_ENGINE_STATIC +#define QT_FORK_AWESOME_ICON_ENGINE_EXPORT +#define QT_FORK_AWESOME_ICON_ENGINE_IMPORT +#else +#define QT_FORK_AWESOME_ICON_ENGINE_EXPORT CPP_UTILITIES_GENERIC_LIB_EXPORT +#define QT_FORK_AWESOME_ICON_ENGINE_IMPORT CPP_UTILITIES_GENERIC_LIB_IMPORT +#endif + +/*! + * \def QT_FORK_AWESOME_ICON_ENGINE_EXPORT + * \brief Marks the symbol to be exported by the qtforkawesomeiconengine library. + */ + +/*! + * \def QT_FORK_AWESOME_ICON_ENGINE_IMPORT + * \brief Marks the symbol to be imported from the qtforkawesomeiconengine library. + */ + +#endif // QT_FORK_AWESOME_ICON_ENGINE_GLOBAL diff --git a/iconengineplugin/iconengine.cpp b/iconengineplugin/iconengine.cpp new file mode 100644 index 0000000..d093a6f --- /dev/null +++ b/iconengineplugin/iconengine.cpp @@ -0,0 +1,72 @@ +#include "./iconengine.h" + +#include "../qtforkawesome/renderer.h" +#include "../qtforkawesome/utils.h" + +#include + +#include +#include +#include + +namespace QtForkAwesome { + +IconEngine::IconEngine(Renderer &renderer) + : m_renderer(renderer) + , m_char(0) + , m_color(Qt::black) +{ +} + +IconEngine::IconEngine(const IconEngine &other) + : QIconEngine(other) + , m_renderer(other.m_renderer) + , m_char(0) + , m_color(Qt::black) +{ +} + +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); +} + +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); +} + +void IconEngine::addFile(const QString &fileName, const QSize &, QIcon::Mode mode, QIcon::State state) +{ + Q_UNUSED(mode) + Q_UNUSED(state) + if (!fileName.endsWith(QLatin1String(".fa"), Qt::CaseInsensitive)) { + return; + } + const auto parts = QtUtilities::midRef(fileName, 0, fileName.size() - 3).split(QChar(':')); + if (parts.empty()) { + return; + } + m_char = static_cast(iconFromId(parts.at(0).toString())); + m_color = parts.size() > 1 ? QColor(parts.at(1)) : Qt::black; +} + +QString IconEngine::key() const +{ + return QStringLiteral("fa"); +} + +QIconEngine *IconEngine::clone() const +{ + return new IconEngine(*this); +} + +} // namespace QtForkAwesome diff --git a/iconengineplugin/iconengine.h b/iconengineplugin/iconengine.h new file mode 100644 index 0000000..7bad3d5 --- /dev/null +++ b/iconengineplugin/iconengine.h @@ -0,0 +1,34 @@ +#ifndef QT_FORK_AWESOME_ICON_ENGINE_H +#define QT_FORK_AWESOME_ICON_ENGINE_H + +#include + +#include "./global.h" + +#include "../qtforkawesome/iconfwd.h" + +namespace QtForkAwesome { + +class Renderer; + +class QT_FORK_AWESOME_ICON_ENGINE_EXPORT IconEngine : public QIconEngine { +public: + IconEngine(Renderer &renderer); + IconEngine(const IconEngine &other); + ~IconEngine() override; + + void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override; + QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; + void addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) override; + QString key() const override; + QIconEngine *clone() const override; + +private: + Renderer &m_renderer; + IconBaseType m_char; + QColor m_color; +}; + +} // namespace QtForkAwesome + +#endif // QT_FORK_AWESOME_ICON_ENGINE_H diff --git a/iconengineplugin/main.cpp b/iconengineplugin/main.cpp new file mode 100644 index 0000000..4d3baa9 --- /dev/null +++ b/iconengineplugin/main.cpp @@ -0,0 +1,33 @@ +#include "./iconengine.h" + +#include "../qtforkawesome/renderer.h" + +#include +#include +#include + +namespace QtForkAwesome { + +class QT_FORK_AWESOME_ICON_ENGINE_EXPORT IconEnginePlugin : public QIconEnginePlugin { + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QIconEngineFactoryInterface" FILE "qtforkawesomeiconengine.json") + +public: + QIconEngine *create(const QString &filename = QString()) override; + +private: + Renderer m_renderer; +}; + +QIconEngine *IconEnginePlugin::create(const QString &file) +{ + auto *const engine = new IconEngine(m_renderer); + if (!file.isNull()) { + engine->addFile(file, QSize(), QIcon::Normal, QIcon::Off); + } + return engine; +} + +} // namespace QtForkAwesome + +#include "main.moc" diff --git a/iconengineplugin/qtforkawesomeiconengine.json b/iconengineplugin/qtforkawesomeiconengine.json new file mode 100644 index 0000000..e8bdc94 --- /dev/null +++ b/iconengineplugin/qtforkawesomeiconengine.json @@ -0,0 +1,3 @@ +{ + "Keys": [ "fa" ] +} diff --git a/qtforkawesome/renderer.cpp b/qtforkawesome/renderer.cpp index c952ce1..34d61e5 100644 --- a/qtforkawesome/renderer.cpp +++ b/qtforkawesome/renderer.cpp @@ -56,7 +56,7 @@ Renderer::Renderer(const QByteArray &fontData) */ Renderer::~Renderer() { - if (m_d->id != InternalData::invalidId) { + if (QCoreApplication::instance() && m_d->id != InternalData::invalidId) { QFontDatabase::removeApplicationFont(m_d->id); } }