list model test

This commit is contained in:
Sebastian Borchers 2019-01-15 10:37:59 +01:00
parent bc3e1fd070
commit f6423074d4
15 changed files with 182 additions and 39 deletions

View File

@ -29,7 +29,7 @@ package gml
// #include <gml.h>
//
// extern void gml_image_provider_request_go_slot(void* goPtr, gml_image_response, char* id, gml_image img);
// extern void gml_image_provider_request_go_slot(void* goPtr, gml_image_response img_resp, char* id, gml_image img);
// static void gml_image_provider_init() {
// gml_image_provider_request_cb_register(gml_image_provider_request_go_slot);
// }

View File

@ -44,6 +44,13 @@ extern "C" {
typedef void* gml_image_provider;
typedef void* gml_image_response;
gml_image_provider gml_image_provider_new(
void* go_ptr,
int aspect_ratio_mode,
int transformation_mode
);
void gml_image_provider_free(gml_image_provider ip);
typedef void (*gml_image_provider_request_cb_t)(
void* go_ptr,
gml_image_response img_resp,
@ -52,13 +59,6 @@ typedef void (*gml_image_provider_request_cb_t)(
);
void gml_image_provider_request_cb_register(gml_image_provider_request_cb_t cb);
gml_image_provider gml_image_provider_new(
void* go_ptr,
int aspect_ratio_mode,
int transformation_mode
);
void gml_image_provider_free(gml_image_provider ip);
void gml_image_response_emit_finished(
gml_image_response img_resp,
char* error_string

View File

@ -32,11 +32,25 @@
extern "C" {
#endif
#include "gml_variant.h"
typedef void* gml_list_model;
gml_list_model gml_list_model_new(void* go_ptr);
void gml_list_model_free(gml_list_model lm);
typedef int (*gml_list_model_row_count_cb_t)(
void* go_ptr
);
typedef gml_variant (*gml_list_model_data_cb_t)(
void* go_ptr,
int row
);
void gml_list_model_cb_register(
gml_list_model_row_count_cb_t rc_cb,
gml_list_model_data_cb_t d_cb
);
#ifdef __cplusplus
}
#endif

View File

@ -44,7 +44,7 @@ void gml_app_run_main_cb_register(gml_app_run_main_cb_t cb) {
gml_app gml_app_new(int argc, char** argv, gml_error err) {
try {
GmlApp* a = new GmlApp(argc, argv);
return (void*)a;
return (gml_app)a;
}
catch (std::exception& e) {
gml_error_set_msg(err, e.what());

View File

@ -34,7 +34,7 @@
gml_error gml_error_new() {
try {
GmlError* gerr = new GmlError();
return (void*)gerr;
return (gml_error)gerr;
}
catch (std::exception& e) {
gml_error_log_exception("new gml_error: " + string(e.what()));

View File

@ -35,7 +35,7 @@
gml_image gml_image_new() {
try {
QImage* qImg = new QImage();
return (void*)qImg;
return (gml_image)qImg;
}
catch (std::exception& e) {
gml_error_log_exception(e.what());

View File

@ -53,7 +53,7 @@ gml_image_provider gml_image_provider_new(
static_cast<Qt::AspectRatioMode>(aspect_ratio_mode),
static_cast<Qt::TransformationMode>(transformation_mode)
);
return (void*)gip;
return (gml_image_provider)gip;
}
catch (std::exception& e) {
gml_error_log_exception(e.what());

View File

@ -27,6 +27,13 @@
#include "gml_list_model.h"
//########################//
//### Static Variables ###//
//########################//
gml_list_model_row_count_cb_t gml_list_model_row_count_cb = NULL;
gml_list_model_data_cb_t gml_list_model_data_cb = NULL;
//#############//
//### C API ###//
//#############//
@ -55,6 +62,14 @@ void gml_list_model_free(gml_list_model lm) {
lm = NULL;
}
void gml_list_model_cb_register(
gml_list_model_row_count_cb_t rc_cb,
gml_list_model_data_cb_t d_cb
) {
gml_list_model_row_count_cb = rc_cb;
gml_list_model_data_cb = d_cb;
}
//##########################//
//### GmlListModel Class ###//
//##########################//
@ -63,3 +78,33 @@ GmlListModel::GmlListModel(
void* goPtr
) :
goPtr(goPtr) {}
int GmlListModel::rowCount(const QModelIndex& /*parent = QModelIndex()*/) const {
int rowCount = 0;
// Call to go.
try {
rowCount = gml_list_model_row_count_cb(goPtr);
}
catch (std::exception& e) {
gml_error_log_exception("list model row count: " + string(e.what()));
}
catch (...) {
gml_error_log_exception("list model row count");
}
return rowCount;
}
QVariant GmlListModel::data(const QModelIndex& index, int /*role = Qt::DisplayRole*/) const {
// Call to go.
gml_variant gmlV = gml_list_model_data_cb(goPtr, index.row());
// Create a copy.
QVariant v(*((QVariant*)gmlV));
// Delete, because Go is not responsible for memory anymore.
gml_variant_free(gmlV);
return v;
}

View File

@ -29,6 +29,7 @@
#define GML_LIST_MODEL_H
#include "gml_includes.h"
#include "gml_error.h"
class GmlListModel : public QAbstractListModel {
public:
@ -36,6 +37,9 @@ public:
void* goPtr
);
int rowCount(const QModelIndex& parent) const override;
QVariant data(const QModelIndex& index, int role) const override;
private:
void* goPtr;
};

View File

@ -44,7 +44,7 @@ void gml_variant_free(gml_variant vv) {
gml_variant gml_variant_new() {
try {
QVariant* v = new QVariant();
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -59,7 +59,7 @@ gml_variant gml_variant_new() {
gml_variant gml_variant_new_from_bool(u_int8_t b) {
try {
QVariant* v = new QVariant(bool(b));
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -74,7 +74,7 @@ gml_variant gml_variant_new_from_bool(u_int8_t b) {
gml_variant gml_variant_new_from_float(float f) {
try {
QVariant* v = new QVariant(f);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -89,7 +89,7 @@ gml_variant gml_variant_new_from_float(float f) {
gml_variant gml_variant_new_from_double(double d) {
try {
QVariant* v = new QVariant(d);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -104,7 +104,7 @@ gml_variant gml_variant_new_from_double(double d) {
gml_variant gml_variant_new_from_int(int i) {
try {
QVariant* v = new QVariant(i);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -134,7 +134,7 @@ gml_variant gml_variant_new_from_int8(int8_t i) {
gml_variant gml_variant_new_from_uint8(u_int8_t i) {
try {
QVariant* v = new QVariant(i);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -149,7 +149,7 @@ gml_variant gml_variant_new_from_uint8(u_int8_t i) {
gml_variant gml_variant_new_from_int16(int16_t i) {
try {
QVariant* v = new QVariant(i);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -164,7 +164,7 @@ gml_variant gml_variant_new_from_int16(int16_t i) {
gml_variant gml_variant_new_from_uint16(u_int16_t i) {
try {
QVariant* v = new QVariant(i);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -179,7 +179,7 @@ gml_variant gml_variant_new_from_uint16(u_int16_t i) {
gml_variant gml_variant_new_from_int32(int32_t i) {
try {
QVariant* v = new QVariant(i);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -194,7 +194,7 @@ gml_variant gml_variant_new_from_int32(int32_t i) {
gml_variant gml_variant_new_from_uint32(u_int32_t i) {
try {
QVariant* v = new QVariant(i);
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -209,7 +209,7 @@ gml_variant gml_variant_new_from_uint32(u_int32_t i) {
gml_variant gml_variant_new_from_int64(int64_t i) {
try {
QVariant* v = new QVariant((long long)(i));
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -224,7 +224,7 @@ gml_variant gml_variant_new_from_int64(int64_t i) {
gml_variant gml_variant_new_from_uint64(u_int64_t i) {
try {
QVariant* v = new QVariant((unsigned long long)(i));
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -239,7 +239,7 @@ gml_variant gml_variant_new_from_uint64(u_int64_t i) {
gml_variant gml_variant_new_from_qchar(int32_t r) {
try {
QVariant* v = new QVariant(QChar(r));
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -254,7 +254,7 @@ gml_variant gml_variant_new_from_qchar(int32_t r) {
gml_variant gml_variant_new_from_string(char* s) {
try {
QVariant* v = new QVariant(QString(s));
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));
@ -269,7 +269,7 @@ gml_variant gml_variant_new_from_string(char* s) {
gml_variant gml_variant_new_from_bytes(char* b, int size) {
try {
QVariant* v = new QVariant(QByteArray(b, size));
return (void*)v;
return (gml_variant)v;
}
catch (std::exception& e) {
gml_error_log_exception("variant: " + string(e.what()));

View File

@ -28,6 +28,15 @@
package gml
// #include <gml.h>
//
// extern int gml_list_model_row_count_go_slot(void* goPtr);
// extern gml_variant gml_list_model_data_go_slot(void* goPtr, int row);
// static void gml_list_model_init() {
// gml_list_model_cb_register(
// gml_list_model_row_count_go_slot,
// gml_list_model_data_go_slot
// );
// }
import "C"
import (
"fmt"
@ -38,23 +47,26 @@ import (
)
type ListModelHandler interface {
RowCount() int
Data(row int) interface{}
}
// Ensure the ListModel type implements the ListModelHandler interface.
var _ ListModelHandler = &ListModel{}
type ListModel struct {
*Object
freed bool
lm C.gml_list_model
ptr unsafe.Pointer
handler ListModelHandler
}
func NewListModel() *ListModel {
func NewListModel(handler ListModelHandler) *ListModel {
lm := &ListModel{}
lm.ptr = pointer.Save(lm)
//lm.lm = C.gml_imageprovider_new(lm.ptr)
lm.lm = C.gml_list_model_new(lm.ptr)
lm.Object = newObject(unsafe.Pointer(lm.lm))
// Always free the C++ value.
runtime.SetFinalizer(lm, freeListModel)
@ -77,6 +89,27 @@ func freeListModel(lm *ListModel) {
return
}
lm.freed = true
//C.gml_imageprovider_free(lm.lm)
C.gml_list_model_free(lm.lm)
pointer.Unref(lm.ptr)
}
//#####################//
//### Exported to C ###//
//#####################//
//export gml_list_model_row_count_go_slot
func gml_list_model_row_count_go_slot(goPtr unsafe.Pointer) C.int {
println("tdaaa")
return C.int((pointer.Restore(goPtr)).(*ListModel).handler.RowCount())
}
//export gml_list_model_data_go_slot
func gml_list_model_data_go_slot(goPtr unsafe.Pointer, row C.int) C.gml_variant {
data := (pointer.Restore(goPtr)).(*ListModel).handler.Data(int(row))
v := ToVariant(data)
// Release, because C++ is handling memory.
v.Release()
return v.ptr
}

View File

@ -43,6 +43,10 @@ type Object struct {
ptr unsafe.Pointer
}
func newObject(ptr unsafe.Pointer) *Object {
return &Object{ptr: ptr}
}
func (o *Object) GMLObject_Pointer() unsafe.Pointer {
if o.ptr == nil {
panic(fmt.Errorf("gml.Object pointer is nil: did you call GMLInit()?"))

View File

@ -30,6 +30,7 @@ package main
import (
"io/ioutil"
"log"
"strconv"
"github.com/desertbit/gml"
_ "github.com/desertbit/gml/samples/signals_slots/testy"
@ -48,6 +49,16 @@ func (b *Bridge) clicked(i int, v *gml.Variant) {
b.emitGreet(1, 2, 3, "foo", '本', 4, true, []byte{1, 2, 3})
}
type Model struct{}
func (m *Model) RowCount() int {
return 5
}
func (m *Model) Data(row int) interface{} {
return "Test: " + strconv.Itoa(row)
}
func main() {
app, err := gml.NewApp()
if err != nil {
@ -61,6 +72,12 @@ func main() {
log.Fatalln(err)
}
model := gml.NewListModel(&Model{})
err = app.SetContextProperty("modl", model)
if err != nil {
log.Fatalln(err)
}
err = app.AddImageProvider(
"imgprov",
gml.NewImageProvider(

View File

@ -1,4 +1,4 @@
import QtQuick 2.11
import QtQuick 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.4
@ -16,22 +16,44 @@ ApplicationWindow {
Button {
id: button
anchors {
top: parent.top
left: parent.left
}
width: 250
height: 50
text: "Trigger Signal"
font.pixelSize: 100
Layout.topMargin: 100
Layout.alignment: Qt.AlignHCenter
font.pixelSize: 38
onClicked: bridge.clicked(1, "Hallo")
}
Image {
width: 500
height: 100
source: "image://imgprov/test/15"
//source: "image://imgprov/test/15"
sourceSize.width: 100
sourceSize.height: 100
fillMode: Image.PreserveAspectFit
}
ListView {
anchors {
right: parent.right
top: parent.top
bottom: parent.bottom
}
width: 200
model: modl
delegate: Item {
Text {
color: "red"
text: display
}
}
Component.onCompleted: { console.log(modl) }
}
Connections {
target: bridge
onGreet: function(i1, i2, i3, s, r, b, bb, sb) {

View File

@ -127,6 +127,10 @@ func (v *Variant) Free() {
freeVariant(v)
}
func (v *Variant) Release() {
v.freed = true
}
func (v *Variant) Pointer() unsafe.Pointer {
return unsafe.Pointer(v.ptr)
}