Add a basic QIconEnginePlugin for ForkAwesome

This commit is contained in:
Martchus 2021-09-13 20:49:52 +02:00
parent c153c863c4
commit fbf820d538
9 changed files with 203 additions and 1 deletions

View File

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

View File

@ -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"));
```

View File

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

27
iconengineplugin/global.h Normal file
View File

@ -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 <c++utilities/application/global.h>
#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

View File

@ -0,0 +1,72 @@
#include "./iconengine.h"
#include "../qtforkawesome/renderer.h"
#include "../qtforkawesome/utils.h"
#include <qtutilities/misc/compat.h>
#include <QCoreApplication>
#include <QPainter>
#include <QString>
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<IconBaseType>(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

View File

@ -0,0 +1,34 @@
#ifndef QT_FORK_AWESOME_ICON_ENGINE_H
#define QT_FORK_AWESOME_ICON_ENGINE_H
#include <QIconEngine>
#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

33
iconengineplugin/main.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "./iconengine.h"
#include "../qtforkawesome/renderer.h"
#include <QIcon>
#include <QIconEnginePlugin>
#include <QSize>
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"

View File

@ -0,0 +1,3 @@
{
"Keys": [ "fa" ]
}

View File

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