Completed the initial implementation for the RoleNames

This commit is contained in:
Filippo Cucchetto 2015-01-31 16:30:12 +01:00
parent 634cc3ea39
commit 34776efd7b
6 changed files with 66 additions and 37 deletions

View File

@ -2,10 +2,12 @@
BaseQAbstractListModel::BaseQAbstractListModel(void* modelObject, BaseQAbstractListModel::BaseQAbstractListModel(void* modelObject,
RowCountCallback rowCountCallback, RowCountCallback rowCountCallback,
DataCallback dataCallback) DataCallback dataCallback,
RoleNamesCallback roleNamesCallback)
: m_modelObject(modelObject) : m_modelObject(modelObject)
, m_rowCountCallback(rowCountCallback) , m_rowCountCallback(rowCountCallback)
, m_dataCallback(dataCallback) , m_dataCallback(dataCallback)
, m_roleNamesCallback(roleNamesCallback)
{ {
} }
@ -35,6 +37,7 @@ void* BaseQAbstractListModel::modelObject()
QHash<int,QByteArray> BaseQAbstractListModel::roleNames() const QHash<int,QByteArray> BaseQAbstractListModel::roleNames() const
{ {
QHash<int, QByteArray> result; QHash<int, QByteArray> result;
m_roleNamesCallback(m_modelObject, &result);
return result; return result;
} }

View File

@ -10,7 +10,8 @@ class BaseQAbstractListModel : public QAbstractListModel
/// Constructor /// Constructor
BaseQAbstractListModel(void* modelObject, BaseQAbstractListModel(void* modelObject,
RowCountCallback rowCountCallback, RowCountCallback rowCountCallback,
DataCallback dataCallback); DataCallback dataCallback,
RoleNamesCallback roleNamesCallback);
/// Return the model's row count /// Return the model's row count
virtual int rowCount(const QModelIndex& index = QModelIndex()) const override; virtual int rowCount(const QModelIndex& index = QModelIndex()) const override;
@ -24,8 +25,11 @@ class BaseQAbstractListModel : public QAbstractListModel
/// Return the roleNames /// Return the roleNames
virtual QHash<int, QByteArray> roleNames() const override; virtual QHash<int, QByteArray> roleNames() const override;
private: private:
void* m_modelObject; void* m_modelObject;
RowCountCallback m_rowCountCallback; RowCountCallback m_rowCountCallback;
DataCallback m_dataCallback; DataCallback m_dataCallback;
RoleNamesCallback m_roleNamesCallback;
}; };

View File

@ -359,7 +359,6 @@ void dos_qobject_signal_emit(void* vptr, const char* name, int parametersCount,
dynamicQObject->emitSignal(QString::fromStdString(name), arguments); dynamicQObject->emitSignal(QString::fromStdString(name), arguments);
} }
void dos_qobject_property_create(void* vptr, void dos_qobject_property_create(void* vptr,
const char* name, const char* name,
int type, int type,
@ -436,9 +435,13 @@ void dos_qmodelindex_sibling(void* vptr, int row, int column, void* sibling)
void dos_qabstractlistmodel_create(void** vptr, void dos_qabstractlistmodel_create(void** vptr,
void* modelObject, void* modelObject,
RowCountCallback rowCountCallback, RowCountCallback rowCountCallback,
DataCallback dataCallback) DataCallback dataCallback,
RoleNamesCallback roleNamesCallaback)
{ {
auto model = new BaseQAbstractListModel(modelObject, rowCountCallback, dataCallback); auto model = new BaseQAbstractListModel(modelObject,
rowCountCallback,
dataCallback,
roleNamesCallaback);
*vptr = model; *vptr = model;
} }

View File

@ -123,7 +123,8 @@ void dos_qhash_int_qbytearray_value(QHashIntQByteArrayVoidPtr vptr, int key, cha
void dos_qabstractlistmodel_create(void** vptr, void dos_qabstractlistmodel_create(void** vptr,
void* callbackObject, void* callbackObject,
RowCountCallback rowCountCallback, RowCountCallback rowCountCallback,
DataCallback dataCallback); DataCallback dataCallback,
RoleNamesCallback roleNamesCallback);
void dos_qabstractlistmodel_delete(void* vptr); void dos_qabstractlistmodel_delete(void* vptr);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -18,5 +18,6 @@ typedef void (*Function)(void*);
typedef void (*DObjectCallback)(void*, void*, int, void**); typedef void (*DObjectCallback)(void*, void*, int, void**);
typedef void (*RowCountCallback) (void* model, QModelIndexVoidPtr index, IntPtr result); typedef void (*RowCountCallback) (void* model, QModelIndexVoidPtr index, IntPtr result);
typedef void (*DataCallback) (void* model, QModelIndexVoidPtr index, int role, QVariantVoidPtr result); typedef void (*DataCallback) (void* model, QModelIndexVoidPtr index, int role, QVariantVoidPtr result);
typedef void (*RoleNamesCallback) (void* model, QHashIntQByteArrayVoidPtr result);
#endif #endif

View File

@ -594,15 +594,51 @@ proc sibling*(modelIndex: QModelIndex, row: cint, column: cint): QModelIndex =
result = newQModelIndex() result = newQModelIndex()
dos_qmodelindex_sibling(modelIndex.data, row, column, result.data) dos_qmodelindex_sibling(modelIndex.data, row, column, result.data)
# QHashIntByteArray
proc dos_qhash_int_qbytearray_create(qHash: var RawQHashIntByteArray) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qhash_int_qbytearray_delete(qHash: RawQHashIntByteArray) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qhash_int_qbytearray_insert(qHash: RawQHashIntByteArray, key: int, value: cstring) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qhash_int_qbytearray_value(qHash: RawQHashIntByteArray, key: int, value: var cstring) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc create*(qHash: var QHashIntByteArray) =
## Create the QHash
debugMsg("QHashIntByteArray", "create")
dos_qhash_int_qbytearray_create(qHash.data)
qHash.deleted = false
proc delete*(qHash: QHashIntByteArray) =
## Delete the QHash
if not qHash.deleted:
debugMsg("QHashIntByteArray", "delete")
dos_qhash_int_qbytearray_delete(qHash.data)
qHash.deleted = true
proc insert*(qHash: QHashIntByteArray, key: int, value: cstring) =
## Insert the value at the given key
dos_qhash_int_qbytearray_insert(qHash.data, key, value)
proc value*(qHash: QHashIntByteArray, key: int): string =
## Return the value associated at the given key
var rawString: cstring
dos_qhash_int_qbytearray_value(qHash.data, key, rawString)
result = $rawString
dos_chararray_delete(rawString)
proc newQHashIntQByteArray*(): QHashIntByteArray =
## Create a new QHashIntQByteArray
newWithCondFinalizer(result, delete)
result.create()
# QAbstractListModel # QAbstractListModel
type RowCountCallback = proc(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelIndex, result: var cint) {.cdecl.} type RowCountCallback = proc(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelIndex, result: var cint) {.cdecl.}
type DataCallback = proc(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelIndex, role: cint, result: RawQVariant) {.cdecl.} type DataCallback = proc(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelIndex, role: cint, result: RawQVariant) {.cdecl.}
type RoleNamesCallback = proc(modelObject: ptr QAbstractListModelObj, result: RawQHashIntByteArray) {.cdecl.}
proc dos_qabstractlistmodel_create(model: var RawQAbstractListModel, proc dos_qabstractlistmodel_create(model: var RawQAbstractListModel,
modelPtr: ptr QAbstractListModelObj, modelPtr: ptr QAbstractListModelObj,
rowCountCallback: RowCountCallback, rowCountCallback: RowCountCallback,
dataCallback: DataCallback) {.cdecl, dynlib:"libDOtherSide.so", importc.} dataCallback: DataCallback,
roleNamesCallback: RoleNamesCallback) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qabstractlistmodel_delete(model: RawQAbstractListModel) {.cdecl, dynlib:"libDOtherSide.so", importc.} proc dos_qabstractlistmodel_delete(model: RawQAbstractListModel) {.cdecl, dynlib:"libDOtherSide.so", importc.}
method rowCount*(model: QAbstractListModel, index: QModelIndex): cint = method rowCount*(model: QAbstractListModel, index: QModelIndex): cint =
@ -626,11 +662,20 @@ proc dataCallback(modelObject: ptr QAbstractListModelObj, rawIndex: RawQModelInd
dos_qvariant_assign(result, variant.data) dos_qvariant_assign(result, variant.data)
variant.delete variant.delete
method roleNames*(model: QAbstractListModel): Table[int, cstring] =
discard()
proc roleNamesCallback(modelObject: ptr QAbstractListModelObj, hash: RawQHashIntByteArray) {.cdecl, exportc.} =
let model = cast[QAbstractListModel](modelObject)
let table = model.roleNames()
for pair in table.pairs:
dos_qhash_int_qbytearray_insert(hash, pair.key, pair.val)
proc create*(model: var QAbstractListModel) = proc create*(model: var QAbstractListModel) =
## Create a new QAbstractListModel ## Create a new QAbstractListModel
debugMsg("QAbstractListModel", "create") debugMsg("QAbstractListModel", "create")
let modelPtr = addr(model[]) let modelPtr = addr(model[])
dos_qabstractlistmodel_create(model.data, modelPtr, rowCountCallback, dataCallback) dos_qabstractlistmodel_create(model.data, modelPtr, rowCountCallback, dataCallback, roleNamesCallback)
model.deleted = false model.deleted = false
proc delete*(model: QAbstractListModel) = proc delete*(model: QAbstractListModel) =
@ -646,31 +691,3 @@ proc newQAbstractListModel*(): QAbstractListModel =
newWithCondFinalizer(result, delete) newWithCondFinalizer(result, delete)
result.create() result.create()
# RoleNames QHash
proc dos_qhash_int_qbytearray_create(qHash: var RawQHashIntByteArray) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qhash_int_qbytearray_delete(qHash: RawQHashIntByteArray) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qhash_int_qbytearray_insert(qHash: RawQHashIntByteArray, key: int, value: cstring) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qhash_int_qbytearray_value(qHash: RawQHashIntByteArray, key: int, value: var cstring) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc create*(qHash: var QHashIntByteArray) =
debugMsg("QHashIntByteArray", "create")
dos_qhash_int_qbytearray_create(qHash.data)
qHash.deleted = false
proc delete*(qHash: QHashIntByteArray) =
debugMsg("QHashIntByteArray", "delete")
dos_qhash_int_qbytearray_delete(qHash.data)
qHash.deleted = true
proc insert*(qHash: QHashIntByteArray, key: int, value: cstring) =
dos_qhash_int_qbytearray_insert(qHash.data, key, value)
proc value*(qHash: QHashIntByteArray, key: int): string =
var rawString: cstring
dos_qhash_int_qbytearray_value(qHash.data, key, rawString)
result = $rawString
dos_chararray_delete(rawString)
proc newQHashIntQByteArray*(): QHashIntByteArray =
newWithCondFinalizer(result, delete)
result.create()