From 8d872ca4618e23ffb642ec99be0cc21ead2209be Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 10 Oct 2021 23:13:40 +0200 Subject: [PATCH] Add a QQuickImageProvider --- CMakeLists.txt | 5 ++ README.md | 24 +++++++ .../CMakeLists.txt | 33 ++++++++++ qtforkawesomequickimageprovider/global.h | 27 ++++++++ qtforkawesomequickimageprovider/provider.cpp | 62 +++++++++++++++++++ qtforkawesomequickimageprovider/provider.h | 37 +++++++++++ 6 files changed, 188 insertions(+) create mode 100644 qtforkawesomequickimageprovider/CMakeLists.txt create mode 100644 qtforkawesomequickimageprovider/global.h create mode 100644 qtforkawesomequickimageprovider/provider.cpp create mode 100644 qtforkawesomequickimageprovider/provider.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fa36047..640aaa7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,3 +37,8 @@ find_package(${PACKAGE_NAMESPACE_PREFIX}qtutilities${CONFIGURATION_PACKAGE_SUFFI add_subdirectory(${META_PROJECT_NAME}) set(${PACKAGE_NAMESPACE_PREFIX}${META_PROJECT_NAME}${CONFIGURATION_PACKAGE_SUFFIX_QTFORKAWESOME}_DIR "${CMAKE_CURRENT_BINARY_DIR}/${META_PROJECT_NAME}") add_subdirectory(iconengineplugin) + +option(ENABLE_QT_QUICK_IMAGE_PROVIDER "enables building the QQuickImageProvider" OFF) +if (ENABLE_QT_QUICK_IMAGE_PROVIDER) + add_subdirectory(${META_PROJECT_NAME}quickimageprovider) +endif () diff --git a/README.md b/README.md index 00e1100..4c9ed9f 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,30 @@ To link against the plugin statically, find the CMake module `qtforkawesomeiconengine` and add `Q_IMPORT_PLUGIN(ForkAwesomeIconEnginePlugin)` to one of your source files. +### QQuickImageProvider +A `QQuickImageProvider` is provided as well in form of the additional library +`qtforkawesomequickimageprovider` which can be enabled by adding +`-DENABLE_QT_QUICK_IMAGE_PROVIDER:BOOL=ON` to the CMake arguments. + +Then just include the header: + +``` +#include +``` + +Create an instance and add it to your `QQmlEngine`: + +``` +engine->addImageProvider(QStringLiteral("fa"), new QtForkAwesome::QuickImageProvider(renderer)); +``` + +And use it like this: +``` +Image { + source: "image://fa/syncthing" +} +``` + ### Bundling It is also possible to build the library as part of your project. Simply add it via `add_subdirectory`. Checkout the diff --git a/qtforkawesomequickimageprovider/CMakeLists.txt b/qtforkawesomequickimageprovider/CMakeLists.txt new file mode 100644 index 0000000..ebbfbd0 --- /dev/null +++ b/qtforkawesomequickimageprovider/CMakeLists.txt @@ -0,0 +1,33 @@ +# additional meta data +set(META_PROJECT_NAME qtforkawesomequickimageprovider) +set(META_PROJECT_VARNAME QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER) +set(META_APP_NAME "QQuickImageProvider for ForkAwesome") +set(META_APP_DESCRIPTION "QQuickImageProvider for ForkAwesome") + +# add project files +set(HEADER_FILES provider.h) +set(SRC_FILES provider.cpp) +set(DOC_FILES ../README.md) + +# use headers and CMake modules from c++utilities and qtutilities +use_cpp_utilities(ONLY_HEADERS VISIBILITY PUBLIC) +use_qt_utilities(ONLY_HEADERS VISIBILITY PRIVATE) + +if (NAMESPACE) + set(NAMESPACE_PREFIX "${NAMESPACE}-") +endif () + +# use main qtforkawesome library +find_package(${NAMESPACE_PREFIX}qtforkawesome${CONFIGURATION_PACKAGE_SUFFIX_QTFORKAWESOME} ${META_APP_VERSION} REQUIRED) +use_qt_fork_awesome() + +# use Qt Gui module +list(APPEND ADDITIONAL_QT_MODULES Gui Quick) + +# include modules to apply configuration +include(BasicConfig) +include(QtConfig) +include(WindowsResources) +include(LibraryTarget) +include(Doxygen) +include(ConfigHeader) diff --git a/qtforkawesomequickimageprovider/global.h b/qtforkawesomequickimageprovider/global.h new file mode 100644 index 0000000..af4d076 --- /dev/null +++ b/qtforkawesomequickimageprovider/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_QUICK_IMAGE_PROVIDER_GLOBAL +#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_GLOBAL + +#include + +#ifdef QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_STATIC +#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_EXPORT +#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_IMPORT +#else +#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_EXPORT CPP_UTILITIES_GENERIC_LIB_EXPORT +#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_IMPORT CPP_UTILITIES_GENERIC_LIB_IMPORT +#endif + +/*! + * \def QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_EXPORT + * \brief Marks the symbol to be exported by the qtforkawesomequickimageprovider library. + */ + +/*! + * \def QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_IMPORT + * \brief Marks the symbol to be imported from the qtforkawesomequickimageprovider library. + */ + +#endif // QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_GLOBAL diff --git a/qtforkawesomequickimageprovider/provider.cpp b/qtforkawesomequickimageprovider/provider.cpp new file mode 100644 index 0000000..f4addf7 --- /dev/null +++ b/qtforkawesomequickimageprovider/provider.cpp @@ -0,0 +1,62 @@ +#include "./provider.h" + +#include +#include + +#include + +#include +#include + +/// \brief Contains classes provided by the QtForkAwesome library. +namespace QtForkAwesome { + +QuickImageProvider::QuickImageProvider(Renderer &renderer, const QColor &defaultColor, const QSize &defaultSize, QQuickImageProvider::ImageType type) + : QQuickImageProvider(type) + , m_renderer(renderer) + , m_defaultColor(defaultColor) + , m_defaultSize(defaultSize) +{ +} + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +QPixmap QuickImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options) +#else +QPixmap QuickImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) +#endif +{ +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + Q_UNUSED(options) +#endif + const auto parts = id.splitRef(QChar(':')); + if (parts.empty()) { + return QPixmap(); + } + const auto icon = iconFromId(parts.front().toString()); + if (!isIconValid(icon)) { + return QPixmap(); + } + auto color = parts.size() > 1 ? QColor(parts.at(1).toString()) : m_defaultColor; + if (!color.isValid()) { + color = QGuiApplication::palette().color(QPalette::Normal, QPalette::Text); + } + const auto renderSize = requestedSize.isValid() ? requestedSize : m_defaultSize; + if (size) { + *size = renderSize; + } + return m_renderer.pixmap(icon, renderSize, color); +} + +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +QImage QuickImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options) +{ + return requestPixmap(id, size, requestedSize, options).toImage(); +} +#else +QImage QuickImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) +{ + return requestPixmap(id, size, requestedSize).toImage(); +} +#endif + +} // namespace QtForkAwesome diff --git a/qtforkawesomequickimageprovider/provider.h b/qtforkawesomequickimageprovider/provider.h new file mode 100644 index 0000000..06ea38e --- /dev/null +++ b/qtforkawesomequickimageprovider/provider.h @@ -0,0 +1,37 @@ +#ifndef QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER +#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER + +#include "./global.h" + +#include +#include +#include + +QT_FORWARD_DECLARE_CLASS(QPixmap) +QT_FORWARD_DECLARE_CLASS(QSize) + +namespace QtForkAwesome { + +class Renderer; + +class QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_EXPORT QuickImageProvider : public QQuickImageProvider { +public: + QuickImageProvider(Renderer &renderer, const QColor &defaultColor = QColor(), const QSize &defaultSize = QSize(64, 64), + QQuickImageProvider::ImageType type = QQuickImageProvider::Pixmap); +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options); + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options); +#else + QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override; +#endif + +private: + Renderer &m_renderer; + QColor m_defaultColor; + QSize m_defaultSize; +}; + +} // namespace QtForkAwesome + +#endif // QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER