diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c68ce73..3e14ac2 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -40,6 +40,7 @@ set(SRC_LIST src/DOtherSideTypesCpp.cpp src/DosQObjectImpl.cpp src/DosQAbstractItemModel.cpp + src/DosQQuickImageProvider.cpp ) include_directories(include include/Qt) diff --git a/lib/include/DOtherSide/DOtherSide.h b/lib/include/DOtherSide/DOtherSide.h index b2cebc3..adb99b6 100644 --- a/lib/include/DOtherSide/DOtherSide.h +++ b/lib/include/DOtherSide/DOtherSide.h @@ -111,12 +111,46 @@ DOS_API void DOS_CALL dos_qqmlapplicationengine_add_import_path(DosQQmlApplicati /// the engine and so it should die with the engine. DOS_API DosQQmlContext *DOS_CALL dos_qqmlapplicationengine_context(DosQQmlApplicationEngine *vptr); +/// \brief Calls the QQMLApplicationengine::addImageProvider +/// \param vptr The QQmlApplicationEngine +/// \param vptr_i A QQuickImageProvider, the QQmlApplicationEngine takes ownership of this pointer +DOS_API void DOS_CALL dos_qqmlapplicationengine_addImageProvider(DosQQmlApplicationEngine *vptr, const char* name, DosQQuickImageProvider *vptr_i); + /// \brief Free the memory allocated for the given QQmlApplicationEngine /// \param vptr The QQmlApplicationEngine DOS_API void DOS_CALL dos_qqmlapplicationengine_delete(DosQQmlApplicationEngine *vptr); /// @} +/// \defgroup QQuickImageProvider QQuickImageProvider +/// \brief Functions related to the QQuickImageProvider class +/// @{ + +/// \brief Create a new QQuickImageProvider +/// \return A new QQuickImageProvider +/// \note The returned QQuickImageProvider should be freed by using dos_qquickimageprovider_delete(DosQQuickImageProvider*) unless the QQuickImageProvider has been bound to a QQmlApplicationEngine +DOS_API DosQQuickImageProvider *DOS_CALL dos_qquickimageprovider_create(PixmapCallback callback); +/// \breif Frees a QQuickImageProvider +DOS_API void DOS_CALL dos_qquickimageprovider_delete(DosQQuickImageProvider *vptr); +/// @} + +/// \defgroup QPixmap QPixmap +/// \brief Functions related to the QPixmap class +/// @{ + +/// \brief Create a new QPixmap +DOS_API DosPixmap *DOS_CALL dos_qpixmap_create(int width, int height); +/// \brief Frees a QPixmap +DOS_API void DOS_CALL dos_qpixmap_delete(DosPixmap *vptr); +/// \brief Load image data into a QPixmap from an image file +DOS_API void DOS_CALL dos_qpixmap_load(DosPixmap *vptr, const char* filepath, const char* format); +/// \brief Load image data into a QPixmap from a buffer +DOS_API void DOS_CALL dos_qpixmap_loadFromData(DosPixmap *vptr, const unsigned char* data, unsigned int len); +/// \brief Fill a QPixmap with a single color +DOS_API void DOS_CALL dos_qpixmap_fill(DosPixmap *vptr, unsigned char r, unsigned char g, unsigned char b, unsigned char a); +/// @} + + /// \defgroup QQuickStyle QQuickStyle /// \brief Functions related to the QQuickStyle class /// @{ @@ -130,6 +164,7 @@ DOS_API void DOS_CALL dos_qquickstyle_set_fallback_style(const char *style); /// @} + /// \defgroup QQuickView QQuickView /// \brief Functions related to the QQuickView class /// @{ diff --git a/lib/include/DOtherSide/DOtherSideTypes.h b/lib/include/DOtherSide/DOtherSideTypes.h index 0d24301..2ba9372 100644 --- a/lib/include/DOtherSide/DOtherSideTypes.h +++ b/lib/include/DOtherSide/DOtherSideTypes.h @@ -61,6 +61,22 @@ typedef void DosQMetaObject; /// A pointer to a QObject typedef void DosQObject; +/// A pointer to a QQuickImageProvider +typedef void DosQQuickImageProvider; + +/// A pointer to a QPixmap +typedef void DosPixmap; + +/// A pixmap callback to be supplied to an image provider +/// \param id Image source id +/// \param width pointer to the width of the image +/// \param height pointer to the height of the image +/// \param requestedHeight sourceSize.height attribute +/// \param requestedWidth sourcesSize.width attribute +/// \note \p id is the trailing part of an image source url for example "image:/// +typedef DosPixmap* (*PixmapCallback)(const char *id, int *width, int *height, int requestedWidth, int requestedHeight); + + /// Called when a property is readed/written or a slot should be executed /// \param self The pointer of QObject in the binded language /// \param slotName The slotName as DosQVariant diff --git a/lib/include/DOtherSide/DOtherSideTypesCpp.h b/lib/include/DOtherSide/DOtherSideTypesCpp.h index 21071c1..1de0b01 100644 --- a/lib/include/DOtherSide/DOtherSideTypesCpp.h +++ b/lib/include/DOtherSide/DOtherSideTypesCpp.h @@ -152,5 +152,4 @@ struct QmlRegisterType { DeleteDObject deleteDObject; }; - } // namespace DOS diff --git a/lib/include/DOtherSide/DosQQuickImageProvider.h b/lib/include/DOtherSide/DosQQuickImageProvider.h new file mode 100644 index 0000000..748f67f --- /dev/null +++ b/lib/include/DOtherSide/DosQQuickImageProvider.h @@ -0,0 +1,20 @@ +#pragma once + +// std +#include + +// Qt +#include +#include + +#include "DOtherSideTypes.h" + +class DosImageProvider : public QQuickImageProvider +{ +public: + DosImageProvider(PixmapCallback callback); + QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize); + +private: + PixmapCallback m_pixmap_callback; +}; diff --git a/lib/src/DOtherSide.cpp b/lib/src/DOtherSide.cpp index f690d09..f8dc613 100644 --- a/lib/src/DOtherSide.cpp +++ b/lib/src/DOtherSide.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef QT_QUICKCONTROLS2_LIB #include #endif @@ -23,6 +24,7 @@ #include "DOtherSide/DosQObjectImpl.h" #include "DOtherSide/DosQAbstractItemModel.h" #include "DOtherSide/DosQDeclarative.h" +#include "DOtherSide/DosQQuickImageProvider.h" namespace { void register_meta_types() @@ -135,12 +137,60 @@ void dos_qqmlapplicationengine_add_import_path(::DosQQmlApplicationEngine *vptr, return engine->rootContext(); } +void dos_qqmlapplicationengine_addImageProvider(DosQQmlApplicationEngine *vptr, const char* name, DosQQuickImageProvider *vptr_i) +{ + auto engine = static_cast(vptr); + auto provider = static_cast(vptr_i); + engine->addImageProvider(QString(name), provider); +} + void dos_qqmlapplicationengine_delete(::DosQQmlApplicationEngine *vptr) { auto engine = static_cast(vptr); delete engine; } + +::DosQQuickImageProvider *dos_qquickimageprovider_create(PixmapCallback callback) +{ + return new DosImageProvider(callback); +} + +void dos_qquickimageprovider_delete(::DosQQuickImageProvider *vptr) +{ + auto provider = static_cast(vptr); + delete provider; +} + +::DosPixmap *dos_qpixmap_create(int width, int height) +{ + return new QPixmap(width, height); +} + +void dos_qpixmap_delete(DosPixmap *vptr) +{ + auto pixmap = static_cast(vptr); + delete pixmap; +} + +void dos_qpixmap_load(DosPixmap *vptr, const char* filepath, const char* format) +{ + auto pixmap = static_cast(vptr); + pixmap->load(QString(filepath), format); +} + +void dos_qpixmap_loadFromData(DosPixmap *vptr, const unsigned char* data, unsigned int len) +{ + auto pixmap = static_cast(vptr); + pixmap->loadFromData(data, len); +} + +void dos_qpixmap_fill(DosPixmap *vptr, unsigned char r, unsigned char g, unsigned char b, unsigned char a) +{ + auto pixmap = static_cast(vptr); + pixmap->fill(QColor(r, g, b, a)); +} + ::DosQQuickView *dos_qquickview_create() { return new QQuickView(); diff --git a/lib/src/DosQQuickImageProvider.cpp b/lib/src/DosQQuickImageProvider.cpp new file mode 100644 index 0000000..6d21879 --- /dev/null +++ b/lib/src/DosQQuickImageProvider.cpp @@ -0,0 +1,14 @@ +#include "DOtherSide/DosQQuickImageProvider.h" + +DosImageProvider::DosImageProvider(PixmapCallback callback) : QQuickImageProvider(QQuickImageProvider::Pixmap), + m_pixmap_callback(callback) +{ +} + +QPixmap DosImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) +{ + auto pixmap = m_pixmap_callback(id.toLatin1().data(), &size->rwidth(), &size->rheight(), size->width(), size->height()); + auto pixmap_copy = *(static_cast(pixmap)); + delete (static_cast(pixmap)); + return pixmap_copy; +}