Added index to the rowCount callback

This commit is contained in:
Filippo Cucchetto 2015-01-26 19:43:25 +01:00
parent 470b2647b8
commit b7cbe8691c
7 changed files with 43 additions and 18 deletions

View File

@ -9,7 +9,9 @@ BaseQAbstractListModel::BaseQAbstractListModel(void* modelObject,
int BaseQAbstractListModel::rowCount(const QModelIndex& index) const
{
return m_rowCountCallback(m_modelObject);
auto newIndex = new QModelIndex();
*newIndex = index;
return m_rowCountCallback(m_modelObject, newIndex);
}
QVariant BaseQAbstractListModel::data(const QModelIndex& index, int role) const

View File

@ -5,7 +5,7 @@
/// This class act as a base class for D and Nim QAbstractListModel
class BaseQAbstractListModel : public QAbstractListModel
{
typedef int (*RowCountCallback) (void*);
typedef int (*RowCountCallback) (void* model, void* index);
public:
/// Constructor

View File

@ -112,7 +112,7 @@ extern "C"
void dos_qmodelindex_sibling(void* vptr, int row, int column, void* sibling);
// QAbstractListModel
typedef int(*RowCountCallback)(void*);
typedef int(*RowCountCallback)(void* model, void* index);
void dos_qabstractlistmodel_create(void** vptr,
void* callbackObject,

View File

@ -7,25 +7,37 @@ type MyQAbstractListModel = ref object of QAbstractListModel
proc create(self: MyQAbstractListModel) =
var qAbstractListModel = self.QAbstractListModel
qAbstractListModel.create()
qAbstractListModel.create
proc delete(self: MyQAbstractListModel) =
var qAbstractListModel = self.QAbstractListModel
qAbstractListModel.delete()
qAbstractListModel.delete
proc newMyQAbstractListModel(): MyQAbstractListModel =
new(result, delete)
result.create()
result.create
method rowCount(self: MyQAbstractListModel): cint =
return 103
method rowCount(self: MyQAbstractListModel, index: QModelIndex): cint =
echo "index valid: " & $index.isValid & " row: " & $index.row & " column: " & $index.column
return 3
proc mainProc() =
var myListModel = newMyQAbstractListModel()
defer: myListModel.delete()
let rows = myListModel.rowCount
echo rows
var app = newQApplication()
defer: app.delete
var myListModel = newMyQAbstractListModel()
defer: myListModel.delete
var engine = newQQmlApplicationEngine()
defer: engine.delete
var variant = newQVariant(myListModel)
defer: variant.delete
engine.rootContext.setContextProperty("myListModel", variant)
engine.load("main.qml")
app.exec()
when isMainModule:
mainProc()

View File

@ -17,7 +17,7 @@ ApplicationWindow
{
width: 10
height: 10
color: "red"
color: index % 2 == 0 ? "red" : "black"
}
}

View File

@ -524,6 +524,11 @@ proc create*(modelIndex: var QModelIndex) =
dos_qmodelindex_create(modelIndex.data)
modelIndex.deleted = false
proc create*(modelIndex: var QModelIndex, rawQModelIndex: RawQModelIndex) =
## Create a new QModelIndex
modelIndex.data = rawQModelIndex
modelIndex.deleted = false
proc delete*(modelIndex: QModelIndex) =
## Delete the given QModelIndex
if not modelIndex.deleted:
@ -537,6 +542,11 @@ proc newQModelIndex*(): QModelIndex =
newWithCondFinalizer(result, delete)
result.create()
proc newQModelIndex*(rawQModelIndex: RawQModelIndex): QModelIndex =
## Return a new QModelIndex given a raw index
newWithCondFinalizer(result, delete)
result.create(rawQModelIndex)
proc row*(modelIndex: QModelIndex): cint =
## Return the index row
dos_qmodelindex_row(modelIndex.data, result)
@ -571,20 +581,21 @@ proc sibling*(modelIndex: QModelIndex, row: cint, column: cint): QModelIndex =
# QAbstractListModel
type RowCountCallback = proc(modelObject: ptr QAbstractListModelObj): cint {.cdecl.}
type RowCountCallback = proc(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelIndex): cint {.cdecl.}
proc dos_qabstractlistmodel_create(model: var RawQAbstractListModel,
modelPtr: ptr QAbstractListModelObj,
rowCountCallback: RowCountCallback) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qabstractlistmodel_delete(model: RawQAbstractListModel) {.cdecl, dynlib:"libDOtherSide.so", importc.}
method rowCount*(model: QAbstractListModel): cint =
method rowCount*(model: QAbstractListModel, index: QModelIndex): cint =
## Return the model's row count
return 0
proc rowCountCallback(modelObject: ptr QAbstractListModelObj): cint {.cdecl, exportc.} =
proc rowCountCallback(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelIndex): cint {.cdecl, exportc.} =
let model = cast[QAbstractListModel](modelObject)
return model.rowCount
let index = newQModelIndex(rawIndex)
return model.rowCount(index)
proc create*(model: var QAbstractListModel) =
## Create a new QAbstractListModel

View File

@ -34,7 +34,7 @@ type
QQmlContext* = distinct pointer ## A QQmlContext
RawQModelIndex = distinct pointer
QModelIndex = ref object of RootObj ## A QModelIndex
QModelIndex* = ref object of RootObj ## A QModelIndex
data: RawQModelIndex
deleted: bool