diff --git a/TODO.md b/TODO.md index d418b56..81d36d6 100644 --- a/TODO.md +++ b/TODO.md @@ -2,10 +2,8 @@ - finish verbose mode - set core.QCoreApplication_SetApplicationName("App") core.QCoreApplication_SetOrganizationName("Desertbit") - ImageProvider -- RunInEventLoop - signals & slots - docker container -- resources - call qml slots directly from go - dp for pixels - document, that slot names must not overlap with QObject names! \ No newline at end of file diff --git a/imageprovider.go b/imageprovider.go new file mode 100644 index 0000000..5eda0b1 --- /dev/null +++ b/imageprovider.go @@ -0,0 +1,90 @@ +/* + * GML - Go QML + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Roland Singer + * Copyright (c) 2019 Sebastian Borchers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package gml + +// #include +// +// extern void gml_imageprovider_request_go_slot(void* goPtr, char* id); +// static void gml_imageprovider_init() { +// gml_imageprovider_request_cb_register(gml_imageprovider_request_go_slot); +// } +import "C" +import ( + "fmt" + "runtime" + "unsafe" + + "github.com/desertbit/gml/pointer" +) + +func init() { + C.gml_imageprovider_init() +} + +type ImageProvider struct { + freed bool + ptr C.gml_imageprovider +} + +func NewImageProvider() *ImageProvider { + ip := &ImageProvider{} + ip.ptr = C.gml_imageprovider_new() + + // Always free the C++ value. + runtime.SetFinalizer(ip, freeImageProvider) + + // Check if something failed. + // This should never happen is signalizes a fatal error. + if ip.ptr == nil { + panic(fmt.Errorf("failed to create gml imageprovider: C pointer is nil")) + } + + return ip +} + +func freeImageProvider(ip *ImageProvider) { + if ip.freed { + return + } + ip.freed = true + C.gml_imageprovider_free(ip.ptr) +} + +func (ip *ImageProvider) Free() { + freeImageProvider(ip) +} + +//#####################// +//### Exported to C ###// +//#####################// + +//export gml_imageprovider_request_go_slot +func gml_imageprovider_request_go_slot(goPtr unsafe.Pointer, id *C.char) { + f := (pointer.Restore(goPtr)).(func()) + f() +} diff --git a/internal/binding/headers/gml.h b/internal/binding/headers/gml.h index 440a008..c9504cf 100644 --- a/internal/binding/headers/gml.h +++ b/internal/binding/headers/gml.h @@ -34,5 +34,6 @@ #include "gml_app.h" #include "gml_object.h" #include "gml_variant.h" +#include "gml_imageprovider.h" #endif diff --git a/internal/binding/headers/gml_imageprovider.h b/internal/binding/headers/gml_imageprovider.h new file mode 100644 index 0000000..14758d4 --- /dev/null +++ b/internal/binding/headers/gml_imageprovider.h @@ -0,0 +1,47 @@ +/* + * GML - Go QML + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Roland Singer + * Copyright (c) 2019 Sebastian Borchers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef GML_HEADER_IMAGEPROVIDER_H +#define GML_HEADER_IMAGEPROVIDER_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* gml_imageprovider; + +typedef void (*gml_imageprovider_request_cb_t)(void* _go_ptr, char* id); +void gml_imageprovider_request_cb_register(gml_imageprovider_request_cb_t cb); + +gml_imageprovider gml_imageprovider_new(); +void gml_imageprovider_free(gml_imageprovider ip); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/internal/binding/sources/gml_imageprovider.cpp b/internal/binding/sources/gml_imageprovider.cpp new file mode 100644 index 0000000..a20966a --- /dev/null +++ b/internal/binding/sources/gml_imageprovider.cpp @@ -0,0 +1,93 @@ +/* + * GML - Go QML + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Roland Singer + * Copyright (c) 2019 Sebastian Borchers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "gml_imageprovider.h" + +//########################// +//### Static Variables ###// +//########################// + +gml_imageprovider_request_cb_t gml_imageprovider_request_cb = NULL; + +//#############// +//### C API ###// +//#############// + +void gml_imageprovider_request_cb_register(gml_imageprovider_request_cb_t cb) { + gml_imageprovider_request_cb = cb; +} + +gml_imageprovider gml_imageprovider_new() { + try { + GmlImageProvider* gip = new GmlImageProvider(); + return (void*)gip; + } + catch (std::exception& e) { + //api_error_set_msg(err, e.what()); TODO: + return NULL; + } + catch (...) { + //api_error_set_unknown_msg(err); TODO: + return NULL; + } +} + +void gml_imageprovider_free(gml_imageprovider ip) { + if (ip == NULL) { + return; + } + GmlImageProvider* gip = (GmlImageProvider*)ip; + delete gip; + ip = NULL; +} + +//###################################// +//### GmlAsyncImageResponse Class ###// +//###################################// + +GmlAsyncImageResponse::GmlAsyncImageResponse( + const QString &id, + const QSize &requestedSize +) { + // Call to go. + +} + +QQuickTextureFactory* GmlAsyncImageResponse::textureFactory() const { + return QQuickTextureFactory::textureFactoryForImage(img); +} + +//###########################// +//### ImageProvider Class ###// +//###########################// + +QQuickImageResponse* GmlImageProvider::requestImageResponse( + const QString &id, + const QSize &requestedSize +) { + return new GmlAsyncImageResponse(id, requestedSize); +}; diff --git a/internal/binding/sources/gml_imageprovider.h b/internal/binding/sources/gml_imageprovider.h new file mode 100644 index 0000000..2ec994c --- /dev/null +++ b/internal/binding/sources/gml_imageprovider.h @@ -0,0 +1,56 @@ +/* + * GML - Go QML + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Roland Singer + * Copyright (c) 2019 Sebastian Borchers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef GML_IMAGEPROVIDER_H +#define GML_IMAGEPROVIDER_H + +#include "gml_includes.h" + +class GmlAsyncImageResponse : public QQuickImageResponse +{ +public: + GmlAsyncImageResponse( + const QString &id, + const QSize &requestedSize + ); + + QQuickTextureFactory *textureFactory() const override; + +private: + QImage img; +}; + +class GmlImageProvider : public QQuickAsyncImageProvider +{ +public: + QQuickImageResponse* requestImageResponse( + const QString &id, + const QSize &requestedSize + ) override; +}; + +#endif diff --git a/internal/binding/sources/gml_includes.h b/internal/binding/sources/gml_includes.h index 943345d..f65a1be 100644 --- a/internal/binding/sources/gml_includes.h +++ b/internal/binding/sources/gml_includes.h @@ -40,6 +40,10 @@ #include #include #include +#include +#include +#include +#include using std::cout; using std::cerr; diff --git a/samples/signals_slots/main.go b/samples/signals_slots/main.go index 183e23a..e3c5d93 100644 --- a/samples/signals_slots/main.go +++ b/samples/signals_slots/main.go @@ -54,15 +54,6 @@ func main() { log.Fatalln(err) } - err = app.AddImportPath("qrc://qml/") - if err != nil { - log.Fatalln(err) - } - err = app.AddImportPath("qrc://resources/") - if err != nil { - log.Fatalln(err) - } - b := &Bridge{} b.GMLInit() err = app.SetContextProperty("bridge", b) @@ -70,7 +61,7 @@ func main() { log.Fatalln(err) } - err = app.Load("qrc:/main.qml") + err = app.Load("qrc:/qml/main.qml") if err != nil { log.Fatalln(err) }