From 92fcf6970d65619cf5ed9cb4f0782628614ead33 Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Mon, 19 Jan 2015 22:23:18 +0100 Subject: [PATCH] Initial implementation of the BaseQAbstractListModel --- .../DOtherSide/BaseQAbstractListModel.cpp | 15 +++++++ .../DOtherSide/BaseQAbstractListModel.h | 18 ++++++++ DOtherSide/DOtherSide/CMakeLists.txt | 2 + DOtherSide/DOtherSide/DOtherSide.cpp | 13 ++++++ DOtherSide/DOtherSide/DOtherSide.h | 4 ++ Nim/Examples/AbstractItemModel/CMakeLists.txt | 2 + Nim/Examples/AbstractItemModel/main.nim | 12 ++++++ Nim/Examples/AbstractItemModel/main.qml | 30 +++++++++++++ Nim/Examples/CMakeLists.txt | 3 +- Nim/NimQml/NimQml.nim | 42 +++++++++++++++---- Nim/NimQml/NimQmlTypes.nim | 5 +++ 11 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 DOtherSide/DOtherSide/BaseQAbstractListModel.cpp create mode 100644 DOtherSide/DOtherSide/BaseQAbstractListModel.h create mode 100644 Nim/Examples/AbstractItemModel/CMakeLists.txt create mode 100644 Nim/Examples/AbstractItemModel/main.nim create mode 100644 Nim/Examples/AbstractItemModel/main.qml diff --git a/DOtherSide/DOtherSide/BaseQAbstractListModel.cpp b/DOtherSide/DOtherSide/BaseQAbstractListModel.cpp new file mode 100644 index 0000000..5763b91 --- /dev/null +++ b/DOtherSide/DOtherSide/BaseQAbstractListModel.cpp @@ -0,0 +1,15 @@ +#include "BaseQAbstractListModel.h" + +BaseQAbstractListModel::BaseQAbstractListModel() +{ +} + +int BaseQAbstractListModel::rowCount(const QModelIndex& index) const +{ + return 0; +} + +QVariant BaseQAbstractListModel::data(const QModelIndex& index, int role) const +{ + return QVariant(); +} diff --git a/DOtherSide/DOtherSide/BaseQAbstractListModel.h b/DOtherSide/DOtherSide/BaseQAbstractListModel.h new file mode 100644 index 0000000..c1b06ce --- /dev/null +++ b/DOtherSide/DOtherSide/BaseQAbstractListModel.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +/// This class act as a base class for D and Nim QAbstractListModel +class BaseQAbstractListModel : public QAbstractListModel +{ +public: + /// Constructor + BaseQAbstractListModel(); + + /// Return the model's row count + virtual int rowCount(const QModelIndex& index = QModelIndex()) const override; + + /// Return the QVariant at the given index + virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; + +}; diff --git a/DOtherSide/DOtherSide/CMakeLists.txt b/DOtherSide/DOtherSide/CMakeLists.txt index 7397864..132dbbc 100644 --- a/DOtherSide/DOtherSide/CMakeLists.txt +++ b/DOtherSide/DOtherSide/CMakeLists.txt @@ -12,10 +12,12 @@ find_package(Qt5Widgets) set(HEADERS_LIST DOtherSide.h + BaseQAbstractListModel.h ) set(SRC_LIST DOtherSide.cpp + BaseQAbstractListModel.cpp ) add_library(${PROJECT_NAME} SHARED ${SRC_LIST} ${HEADERS_LIST}) diff --git a/DOtherSide/DOtherSide/DOtherSide.cpp b/DOtherSide/DOtherSide/DOtherSide.cpp index 0d89242..9f09593 100644 --- a/DOtherSide/DOtherSide/DOtherSide.cpp +++ b/DOtherSide/DOtherSide/DOtherSide.cpp @@ -10,6 +10,7 @@ #include #include "DynamicQObject.h" +#include "BaseQAbstractListModel.h" void convert_to_cstring(const QString& source, CharPtr& destination, int& length) { @@ -415,3 +416,15 @@ void dos_qmodelindex_sibling(void* vptr, int row, int column, void* sibling) auto siblingIndex = reinterpret_cast(sibling); *siblingIndex = index->sibling(row, column); } + +void dos_qabstractlistmodel_create(void** vptr) +{ + auto model = new BaseQAbstractListModel(); + *vptr = model; +} + +void dos_qabstractlistmodel_delete(void* vptr) +{ + auto model = reinterpret_cast(vptr); + delete model; +} diff --git a/DOtherSide/DOtherSide/DOtherSide.h b/DOtherSide/DOtherSide/DOtherSide.h index c921e50..b8eaa23 100644 --- a/DOtherSide/DOtherSide/DOtherSide.h +++ b/DOtherSide/DOtherSide/DOtherSide.h @@ -109,6 +109,10 @@ extern "C" void dos_qmodelindex_child(void* vptr, int row, int column, void* child); void dos_qmodelindex_sibling(void* vptr, int row, int column, void* sibling); + // QAbstractListModel + void dos_qabstractlistmodel_create(void** vptr); + void dos_qabstractlistmodel_delete(void* vptr); + #ifdef __cplusplus } #endif diff --git a/Nim/Examples/AbstractItemModel/CMakeLists.txt b/Nim/Examples/AbstractItemModel/CMakeLists.txt new file mode 100644 index 0000000..227c09f --- /dev/null +++ b/Nim/Examples/AbstractItemModel/CMakeLists.txt @@ -0,0 +1,2 @@ +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +add_nim_executable(TARGET AbstractItemModel SOURCES main.nim PATHS ../../NimQml) \ No newline at end of file diff --git a/Nim/Examples/AbstractItemModel/main.nim b/Nim/Examples/AbstractItemModel/main.nim new file mode 100644 index 0000000..5eb92ad --- /dev/null +++ b/Nim/Examples/AbstractItemModel/main.nim @@ -0,0 +1,12 @@ +import NimQml +import macros +import typeinfo + +proc mainProc() = + var myListModel = newQAbstractListModel() + defer: myListModel.delete() + +when isMainModule: + mainProc() + GC_fullcollect() + diff --git a/Nim/Examples/AbstractItemModel/main.qml b/Nim/Examples/AbstractItemModel/main.qml new file mode 100644 index 0000000..0edd37b --- /dev/null +++ b/Nim/Examples/AbstractItemModel/main.qml @@ -0,0 +1,30 @@ +import QtQuick 2.2 +import QtQuick.Controls 1.2 +import QtQuick.Layouts 1.1 +import QtQuick.Window 2.1 + +ApplicationWindow +{ + width: 400 + height: 300 + title: "AbstractItemModel" + Component.onCompleted: visible = true + + Component + { + id: myListModelDelegate + Rectangle + { + width: 10 + height: 10 + color: "red" + } + } + + ListView + { + anchors.fill: parent + model: myListModel + delegate: myListModelDelegate + } +} \ No newline at end of file diff --git a/Nim/Examples/CMakeLists.txt b/Nim/Examples/CMakeLists.txt index 15abd67..a6d896a 100644 --- a/Nim/Examples/CMakeLists.txt +++ b/Nim/Examples/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(HelloWorld) add_subdirectory(SimpleData) add_subdirectory(SlotsAndProperties) add_subdirectory(QtObjectMacro) -add_subdirectory(ContactApp) \ No newline at end of file +add_subdirectory(ContactApp) +add_subdirectory(AbstractItemModel) \ No newline at end of file diff --git a/Nim/NimQml/NimQml.nim b/Nim/NimQml/NimQml.nim index 11dd89f..9b17c60 100644 --- a/Nim/NimQml/NimQml.nim +++ b/Nim/NimQml/NimQml.nim @@ -503,12 +503,12 @@ proc dos_qmodelindex_parent(modelIndex: RawQModelIndex, parent: RawQModelIndex) proc dos_qmodelindex_child(modelIndex: RawQModelIndex, row: cint, column: cint, parent: RawQModelIndex) {.cdecl, dynlib:"libDOtherSide.so", importc.} proc dos_qmodelindex_sibling(modelIndex: RawQModelIndex, row: cint, column: cint, sibling: RawQModelIndex) {.cdecl, dynlib:"libDOtherSide.so", importc.} -proc create(modelIndex: var QModelIndex) = +proc create*(modelIndex: var QModelIndex) = ## Create a new QModelIndex dos_qmodelindex_create(modelIndex.data) modelIndex.deleted = false -proc delete(modelIndex: QModelIndex) = +proc delete*(modelIndex: QModelIndex) = ## Delete the given QModelIndex if not modelIndex.deleted: debugMsg("QModelIndex", "delete") @@ -521,34 +521,58 @@ proc newQModelIndex*(): QModelIndex = newWithCondFinalizer(result, delete) result.create() -proc row(modelIndex: QModelIndex): cint = +proc row*(modelIndex: QModelIndex): cint = ## Return the index row dos_qmodelindex_row(modelIndex.data, result) -proc column(modelIndex: QModelIndex): cint = +proc column*(modelIndex: QModelIndex): cint = ## Return the index column dos_qmodelindex_column(modelIndex.data, result) -proc isValid(modelIndex: QModelIndex): bool = +proc isValid*(modelIndex: QModelIndex): bool = ## Return true if the index is valid, false otherwise dos_qmodelindex_isValid(modelIndex.data, result) -proc data(modelIndex: QModelIndex, role: cint): QVariant = +proc data*(modelIndex: QModelIndex, role: cint): QVariant = ## Return the model data associated to the given role result = newQVariant() dos_qmodelindex_data(modelIndex.data, role, result.data) -proc parent(modelIndex: QModelIndex): QModelIndex = +proc parent*(modelIndex: QModelIndex): QModelIndex = ## Return the parent index result = newQModelIndex() dos_qmodelindex_parent(modelIndex.data, result.data) -proc child(modelIndex: QModelIndex, row: cint, column: cint): QModelIndex = +proc child*(modelIndex: QModelIndex, row: cint, column: cint): QModelIndex = ## Return the child index associated to the given row and column result = newQModelIndex() dos_qmodelindex_child(modelIndex.data, row, column, result.data) -proc sibling(modelIndex: QModelIndex, row: cint, column: cint): QModelIndex = +proc sibling*(modelIndex: QModelIndex, row: cint, column: cint): QModelIndex = ## Return the sibling index associated to the given row and column result = newQModelIndex() dos_qmodelindex_sibling(modelIndex.data, row, column, result.data) + + +# QAbstractListModel +proc dos_qabstractlistmodel_create(modelIndex: var RawQAbstractListModel) {.cdecl, dynlib:"libDOtherSide.so", importc.} +proc dos_qabstractlistmodel_delete(modelIndex: RawQAbstractListModel) {.cdecl, dynlib:"libDOtherSide.so", importc.} + +proc create*(model: var QAbstractListModel) = + ## Create a new QAbstractListModel + debugMsg("QAbstractListModel", "create") + dos_qabstractlistmodel_create(model.data) + model.deleted = false + +proc delete*(model: QAbstractListModel) = + ## Delete the given QAbstractListModel + if not model.deleted: + debugMsg("QAbstractListModel", "delete") + dos_qabstractlistmodel_delete(model.data) + model.data = nil.RawQAbstractListModel + model.deleted = true + +proc newQAbstractListModel*(): QAbstractListModel = + ## Return a new QAbstractListModel + newWithCondFinalizer(result, delete) + result.create() diff --git a/Nim/NimQml/NimQmlTypes.nim b/Nim/NimQml/NimQmlTypes.nim index bc4f5ec..c1f5442 100644 --- a/Nim/NimQml/NimQmlTypes.nim +++ b/Nim/NimQml/NimQmlTypes.nim @@ -37,3 +37,8 @@ type QModelIndex = ref object of RootObj ## A QModelIndex data: RawQModelIndex deleted: bool + + RawQAbstractListModel = distinct pointer + QAbstractListModel = ref object of RootObj ## A QAbstactListModel + data: RawQAbstractListModel + deleted: bool