Initial implementation of the BaseQAbstractListModel

This commit is contained in:
Filippo Cucchetto 2015-01-19 22:23:18 +01:00
parent 733266b362
commit 92fcf6970d
11 changed files with 136 additions and 10 deletions

View File

@ -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();
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <QAbstractListModel>
/// 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;
};

View File

@ -12,10 +12,12 @@ find_package(Qt5Widgets)
set(HEADERS_LIST set(HEADERS_LIST
DOtherSide.h DOtherSide.h
BaseQAbstractListModel.h
) )
set(SRC_LIST set(SRC_LIST
DOtherSide.cpp DOtherSide.cpp
BaseQAbstractListModel.cpp
) )
add_library(${PROJECT_NAME} SHARED ${SRC_LIST} ${HEADERS_LIST}) add_library(${PROJECT_NAME} SHARED ${SRC_LIST} ${HEADERS_LIST})

View File

@ -10,6 +10,7 @@
#include <QtCore/QModelIndex> #include <QtCore/QModelIndex>
#include "DynamicQObject.h" #include "DynamicQObject.h"
#include "BaseQAbstractListModel.h"
void convert_to_cstring(const QString& source, CharPtr& destination, int& length) 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<QModelIndex*>(sibling); auto siblingIndex = reinterpret_cast<QModelIndex*>(sibling);
*siblingIndex = index->sibling(row, column); *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<BaseQAbstractListModel*>(vptr);
delete model;
}

View File

@ -109,6 +109,10 @@ extern "C"
void dos_qmodelindex_child(void* vptr, int row, int column, void* child); void dos_qmodelindex_child(void* vptr, int row, int column, void* child);
void dos_qmodelindex_sibling(void* vptr, int row, int column, void* sibling); 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 #ifdef __cplusplus
} }
#endif #endif

View File

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

View File

@ -0,0 +1,12 @@
import NimQml
import macros
import typeinfo
proc mainProc() =
var myListModel = newQAbstractListModel()
defer: myListModel.delete()
when isMainModule:
mainProc()
GC_fullcollect()

View File

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

View File

@ -3,3 +3,4 @@ add_subdirectory(SimpleData)
add_subdirectory(SlotsAndProperties) add_subdirectory(SlotsAndProperties)
add_subdirectory(QtObjectMacro) add_subdirectory(QtObjectMacro)
add_subdirectory(ContactApp) add_subdirectory(ContactApp)
add_subdirectory(AbstractItemModel)

View File

@ -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_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 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 ## Create a new QModelIndex
dos_qmodelindex_create(modelIndex.data) dos_qmodelindex_create(modelIndex.data)
modelIndex.deleted = false modelIndex.deleted = false
proc delete(modelIndex: QModelIndex) = proc delete*(modelIndex: QModelIndex) =
## Delete the given QModelIndex ## Delete the given QModelIndex
if not modelIndex.deleted: if not modelIndex.deleted:
debugMsg("QModelIndex", "delete") debugMsg("QModelIndex", "delete")
@ -521,34 +521,58 @@ proc newQModelIndex*(): QModelIndex =
newWithCondFinalizer(result, delete) newWithCondFinalizer(result, delete)
result.create() result.create()
proc row(modelIndex: QModelIndex): cint = proc row*(modelIndex: QModelIndex): cint =
## Return the index row ## Return the index row
dos_qmodelindex_row(modelIndex.data, result) dos_qmodelindex_row(modelIndex.data, result)
proc column(modelIndex: QModelIndex): cint = proc column*(modelIndex: QModelIndex): cint =
## Return the index column ## Return the index column
dos_qmodelindex_column(modelIndex.data, result) 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 ## Return true if the index is valid, false otherwise
dos_qmodelindex_isValid(modelIndex.data, result) 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 ## Return the model data associated to the given role
result = newQVariant() result = newQVariant()
dos_qmodelindex_data(modelIndex.data, role, result.data) dos_qmodelindex_data(modelIndex.data, role, result.data)
proc parent(modelIndex: QModelIndex): QModelIndex = proc parent*(modelIndex: QModelIndex): QModelIndex =
## Return the parent index ## Return the parent index
result = newQModelIndex() result = newQModelIndex()
dos_qmodelindex_parent(modelIndex.data, result.data) 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 ## Return the child index associated to the given row and column
result = newQModelIndex() result = newQModelIndex()
dos_qmodelindex_child(modelIndex.data, row, column, result.data) 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 ## Return the sibling index associated to the given row and column
result = newQModelIndex() result = newQModelIndex()
dos_qmodelindex_sibling(modelIndex.data, row, column, result.data) 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()

View File

@ -37,3 +37,8 @@ type
QModelIndex = ref object of RootObj ## A QModelIndex QModelIndex = ref object of RootObj ## A QModelIndex
data: RawQModelIndex data: RawQModelIndex
deleted: bool deleted: bool
RawQAbstractListModel = distinct pointer
QAbstractListModel = ref object of RootObj ## A QAbstactListModel
data: RawQAbstractListModel
deleted: bool