Add a QQuickImageProvider

This commit is contained in:
Martchus 2021-10-10 23:13:40 +02:00
parent 5c818baf0b
commit 8d872ca461
6 changed files with 188 additions and 0 deletions

View File

@ -37,3 +37,8 @@ find_package(${PACKAGE_NAMESPACE_PREFIX}qtutilities${CONFIGURATION_PACKAGE_SUFFI
add_subdirectory(${META_PROJECT_NAME}) add_subdirectory(${META_PROJECT_NAME})
set(${PACKAGE_NAMESPACE_PREFIX}${META_PROJECT_NAME}${CONFIGURATION_PACKAGE_SUFFIX_QTFORKAWESOME}_DIR "${CMAKE_CURRENT_BINARY_DIR}/${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) 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 ()

View File

@ -77,6 +77,30 @@ To link against the plugin statically, find the CMake module
`qtforkawesomeiconengine` and add `Q_IMPORT_PLUGIN(ForkAwesomeIconEnginePlugin)` `qtforkawesomeiconengine` and add `Q_IMPORT_PLUGIN(ForkAwesomeIconEnginePlugin)`
to one of your source files. 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 <qtforkawesomequickimageprovider/provider.h>
```
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 ### Bundling
It is also possible to build the library as part of your project. Simply add It is also possible to build the library as part of your project. Simply add
it via `add_subdirectory`. Checkout the it via `add_subdirectory`. Checkout the

View File

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

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_QUICK_IMAGE_PROVIDER_GLOBAL
#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER_GLOBAL
#include <c++utilities/application/global.h>
#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

View File

@ -0,0 +1,62 @@
#include "./provider.h"
#include <qtforkawesome/renderer.h>
#include <qtforkawesome/utils.h>
#include <qtutilities/misc/compat.h>
#include <QGuiApplication>
#include <QPalette>
/// \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

View File

@ -0,0 +1,37 @@
#ifndef QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER
#define QT_FORK_AWESOME_QUICK_IMAGE_PROVIDER
#include "./global.h"
#include <QColor>
#include <QQuickImageProvider>
#include <QSize>
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