Merge pull request #8 from cowboy-coders/assorted_fixes_devel

Nice spot on the bug of passing a pointer to a ref. This for sure wasn't what i meant!
This commit is contained in:
filcuc 2015-01-07 21:51:56 +01:00
commit 7dcfad82d9
2 changed files with 27 additions and 21 deletions

View File

@ -22,6 +22,8 @@ type QMetaType* {.pure.} = enum ## \
QVariant = cint(41),
Void = cint(43)
var qobjectRegistry = initTable[ptr QObjectObj, QObject]()
proc debugMsg(message: string) =
echo "NimQml: ", message
@ -88,11 +90,11 @@ proc intVal*(variant: QVariant): int =
## Return the QVariant value as int
var rawValue: cint
dos_qvariant_toInt(variant, rawValue)
result = cast[int](rawValue)
result = rawValue.cint
proc `intVal=`*(variant: QVariant, value: int) =
## Sets the QVariant value int value
var rawValue = cast[cint](value)
var rawValue = value.cint
dos_qvariant_setInt(variant, rawValue)
proc boolVal*(variant: QVariant): bool =
@ -177,9 +179,9 @@ proc toCIntSeq(metaTypes: openarray[QMetaType]): seq[cint] =
for metaType in metaTypes:
result.add(cint(metaType))
type QObjectCallBack = proc(nimobject: ptr QObject, slotName: QVariant, numArguments: cint, arguments: QVariantArrayPtr) {.cdecl.}
type QObjectCallBack = proc(nimobject: ptr QObjectObj, slotName: QVariant, numArguments: cint, arguments: QVariantArrayPtr) {.cdecl.}
proc dos_qobject_create(qobject: var DynamicQObject, nimobject: ptr QObject, qobjectCallback: QObjectCallBack) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qobject_create(qobject: var DynamicQObject, nimobject: ptr QObjectObj, qobjectCallback: QObjectCallBack) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qobject_delete(qobject: DynamicQObject) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qobject_slot_create(qobject: DynamicQObject, slotName: cstring, argumentsCount: cint, argumentsMetaTypes: ptr cint, slotIndex: var cint) {.cdecl, dynlib:"libDOtherSide.so", importc.}
proc dos_qobject_signal_create(qobject: DynamicQObject, signalName: cstring, argumentsCount: cint, argumentsMetaTypes: ptr cint, signalIndex: var cint) {.cdecl, dynlib:"libDOtherSide.so", importc.}
@ -191,58 +193,63 @@ method onSlotCalled*(nimobject: QObject, slotName: string, args: openarray[QVari
## Subclasses can override the given method for handling the slot call
discard()
proc qobjectCallback(nimobject: ptr QObject, slotName: QVariant, numArguments: cint, arguments: QVariantArrayPtr) {.cdecl, exportc.} =
proc qobjectCallback(nimobject: ptr QObjectObj, slotName: QVariant, numArguments: cint, arguments: QVariantArrayPtr) {.cdecl, exportc.} =
let qobject = qobjectRegistry[nimobject]
assert qobject != nil, "expecting valid QObject"
# forward to the QObject subtype instance
nimobject[].onSlotCalled(slotName.stringVal, arguments.toVariantSeq(numArguments))
qobject.onSlotCalled(slotName.stringVal, arguments.toVariantSeq(numArguments))
proc create*(qobject: var QObject) =
proc create*(qobject: QObject) =
## Create a new QObject
let internalRef = qobject
let qobjectPtr = addr(qobject[])
qobjectRegistry[qobjectPtr] = internalRef
qobject.name = "QObject"
qobject.slots = initTable[string,cint]()
qobject.signals = initTable[string, cint]()
dos_qobject_create(qobject.data, addr(qobject), qobjectCallback)
dos_qobject_create(qobject.data, qobjectPtr, qobjectCallback)
proc delete*(qobject: QObject) =
## Delete the given QObject
let qobjectPtr = addr(qobject[])
qobjectRegistry.del qobjectPtr
dos_qobject_delete(qobject.data)
proc registerSlot*(qobject: var QObject,
proc registerSlot*(qobject: QObject,
slotName: string,
metaTypes: openarray[QMetaType]) =
## Register a slot in the QObject with the given name and signature
# Copy the metatypes array
var copy = toCIntSeq(metatypes)
var index: cint
dos_qobject_slot_create(qobject.data, slotName, cint(copy.len), cast[ptr cint](addr(copy[0])), index)
dos_qobject_slot_create(qobject.data, slotName, cint(copy.len), addr(copy[0].cint), index)
qobject.slots[slotName] = index
proc registerSignal*(qobject: var QObject,
proc registerSignal*(qobject: QObject,
signalName: string,
metatypes: openarray[QMetaType]) =
## Register a signal in the QObject with the given name and signature
var index: cint
if metatypes.len > 0:
var copy = toCIntSeq(metatypes)
dos_qobject_signal_create(qobject.data, signalName, cast[cint](copy.len), cast[ptr cint](addr(copy[0])), index)
dos_qobject_signal_create(qobject.data, signalName, copy.len.cint, addr(copy[0].cint), index)
else:
dos_qobject_signal_create(qobject.data, signalName, 0, cast[ptr cint](0), index)
dos_qobject_signal_create(qobject.data, signalName, 0, nil, index)
qobject.signals[signalName] = index
proc registerProperty*(qobject: var QObject,
proc registerProperty*(qobject: QObject,
propertyName: string,
propertyType: QMetaType,
readSlot: string,
writeSlot: string,
notifySignal: string) =
## Register a property in the QObject with the given name and type.
dos_qobject_property_create(qobject.data, propertyName, cast[cint](propertyType), readSlot, writeSlot, notifySignal)
dos_qobject_property_create(qobject.data, propertyName, propertyType.cint, readSlot, writeSlot, notifySignal)
proc emit*(qobject: QObject, signalName: string, args: openarray[QVariant] = []) =
## Emit the signal with the given name and values
if args.len > 0:
var copy: seq[QVariant]
for i in 0..args.len-1:
copy.add(args[i])
var copy = @args
dos_qobject_signal_emit(qobject.data, signalName, args.len.cint, addr(copy[0]))
else:
dos_qobject_signal_emit(qobject.data, signalName, 0, nil)
@ -275,5 +282,3 @@ proc delete(view: QQuickView) =
## Delete the given QQuickView
dos_qquickview_delete(view)

View File

@ -7,10 +7,11 @@ type
QApplication* = distinct pointer ## A QApplication
DynamicQObject* = distinct pointer
## internal representation of a QObject, as recognised by DOtherSide
QObject* {.inheritable.} = ref object of RootObj ## A QObject
QObjectObj* = object of RootObj ## A QObject
name*: string
data*: DynamicQObject
slots*: Table[string, cint]
signals*: Table[string, cint]
properties*: Table[string, cint]
QObject* = ref QObjectObj
QQuickView* = distinct pointer ## A QQuickView