diff --git a/src/nimqml.nim b/src/nimqml.nim index 1e7e3a8..bbf755e 100644 --- a/src/nimqml.nim +++ b/src/nimqml.nim @@ -11,8 +11,11 @@ template debugMsg(typeName: string, procName: string) = message &= procName debugMsg(message) +include private/dotherside.nim +include private/nimqmltypes.nim include private/qmetaobject.nim - +include private/qvariant.nim +include private/qobject.nim #include private.nimqmltypes #var qobjectRegistry = initTable[ptr QObjectObj, bool]() #include private/qvariant.nim diff --git a/src/private/dotherside.nim b/src/private/dotherside.nim index 555b7d2..285557a 100644 --- a/src/private/dotherside.nim +++ b/src/private/dotherside.nim @@ -1,5 +1,11 @@ type + NimQObject = pointer DosQMetaObject* = distinct pointer + DosQObject* = distinct pointer + DosQVariant* = distinct pointer + DosQVariantArray* {.unchecked.} = array[0..0, DosQVariant] + DosQMetaType = cint + DosQMetaTypeArray* {.unchecked.} = array[0..0, DosQMetaType] DosSignalDefinition* = object name*: cstring @@ -31,11 +37,45 @@ type count*: cint definitions*: pointer + DosQObjectCallBack = proc(nimobject: NimQObject, slotName: DosQVariant, numArguments: cint, arguments: ptr DosQVariantArray) {.cdecl.} + + +# Conversion proc resetToNil*[T](x: var T) = x = nil.pointer.T proc isNil*[T](x: T): bool = x.pointer == nil +# QVariant +proc dos_qvariant_create(variant: var DosQVariant) {.cdecl, importc.} +proc dos_qvariant_create_int(variant: var DosQVariant, value: cint) {.cdecl, importc.} +proc dos_qvariant_create_bool(variant: var DosQVariant, value: bool) {.cdecl, importc.} +proc dos_qvariant_create_string(variant: var DosQVariant, value: cstring) {.cdecl, importc.} +proc dos_qvariant_create_qobject(variant: var DosQVariant, value: DosQObject) {.cdecl, importc.} +proc dos_qvariant_create_qvariant(variant: var DosQVariant, value: DosQVariant) {.cdecl, importc.} +proc dos_qvariant_create_float(variant: var DosQVariant, value: cfloat) {.cdecl, importc.} +proc dos_qvariant_create_double(variant: var DosQVariant, value: cdouble) {.cdecl, importc.} +proc dos_qvariant_delete(variant: DosQVariant) {.cdecl, importc.} +proc dos_qvariant_isnull(variant: DosQVariant, isNull: var bool) {.cdecl, importc.} +proc dos_qvariant_toInt(variant: DosQVariant, value: var cint) {.cdecl, importc.} +proc dos_qvariant_toBool(variant: DosQVariant, value: var bool) {.cdecl, importc.} +proc dos_qvariant_toString(variant: DosQVariant, value: var cstring) {.cdecl, importc.} +proc dos_qvariant_setInt(variant: DosQVariant, value: cint) {.cdecl, importc.} +proc dos_qvariant_setBool(variant: DosQVariant, value: bool) {.cdecl, importc.} +proc dos_qvariant_setString(variant: DosQVariant, value: cstring) {.cdecl, importc.} +proc dos_qvariant_assign(leftValue: DosQVariant, rightValue: DosQVariant) {.cdecl, importc.} +proc dos_qvariant_toFloat(variant: DosQVariant, value: var cfloat) {.cdecl, importc.} +proc dos_qvariant_setFloat(variant: DosQVariant, value: float) {.cdecl, importc.} +proc dos_qvariant_toDouble(variant: DosQVariant, value: var cdouble) {.cdecl, importc.} +proc dos_qvariant_setDouble(variant: DosQVariant, value: cdouble) {.cdecl, importc.} +proc dos_qvariant_setQObject(variant: DosQVariant, value: DosQObject) {.cdecl, importc.} +proc dos_chararray_delete(str: cstring) {.cdecl, importc.} + # QObject proc dos_qobject_qmetaobject*(vptr: var DosQmetaObject) {.cdecl, importc.} +proc dos_qobject_create(qobject: var DosQObject, nimobject: NimQObject, metaObject: DosQMetaObject, dosQObjectCallback: DosQObjectCallBack) {.cdecl, importc.} +proc dos_qobject_objectName(qobject: DosQObject, result: var cstring) {.cdecl, importc.} +proc dos_qobject_setObjectName(qobject: DosQObject, name: cstring) {.cdecl, importc.} +proc dos_qobject_signal_emit(qobject: DosQObject, signalName: cstring, argumentsCount: cint, arguments: ptr DosQVariantArray) {.cdecl, importc.} +proc dos_qobject_delete(qobject: DosQObject) {.cdecl, importc.} # QAbstractListModel proc dos_qabstractlistmodel_qmetaobject*(vptr: var DosQmetaObject) {.cdecl importc.} diff --git a/src/private/nimqmltypes.nim b/src/private/nimqmltypes.nim index c37f88e..5ebe728 100644 --- a/src/private/nimqmltypes.nim +++ b/src/private/nimqmltypes.nim @@ -1,82 +1,30 @@ -import tables - type - RawQVariant = distinct pointer - QVariant* = ref object of RootObj ## A QVariant - data: RawQVariant - deleted: bool + QObject* = ref object of RootObj + vptr: DosQObject - RawQQmlApplicationEngine = distinct pointer - QQmlApplicationEngine* = ref object of RootObj ## A QQmlApplicationEngine - data: RawQQmlApplicationEngine - deleted: bool + QVariant* = ref object of RootObj + vptr: DosQVariant - QApplication* = ref object of RootObj ## A QApplication - deleted: bool + QMetaType* = cint - QGuiApplication* = ref object of RootObj ## A QGuiApplication - deleted: bool + SignalDefinition* = object + name: string + parametersTypes: seq[QMetaType] - RawQObject = distinct pointer - QObjectObj = object of RootObj - data: RawQObject - slots: Table[string, cint] - signals: Table[string, cint] - properties: Table[string, cint] - deleted: bool - QObject* = ref QObjectObj + SlotDefinition* = object + name: string + returnMetaType: QMetaType + parametersTypes: seq[QMetaType] - RawBaseQObjectObj = distinct pointer - BaseQObjectObj = object of QObjectObj - BaseQObject* = ref BaseQObjectObj ## A QObject + PropertyDefinition* = object + name: string + propertyMetaType: QMetaType + readSlot: string + writeSlot: string + notifySignal: string - RawQAbstractListModel = distinct pointer - QAbstractListModelObj = object of QObjectObj - QAbstractListModel* = ref QAbstractListModelObj ## A QAbstractListModel - - RawQQuickView = distinct pointer - QQuickView = ref object of RootObj ## A QQuickView - data: RawQQuickView - deleted: bool - - QQmlContext* = distinct pointer ## A QQmlContext - - RawQModelIndex = distinct pointer - QModelIndex* = ref object of RootObj ## A QModelIndex - data: RawQModelIndex - deleted: bool - - RawQHashIntByteArray = distinct pointer - QHashIntByteArrayObj = object of RootObj - data: RawQHashIntByteArray - deleted: bool - QHashIntByteArray* = ref QHashIntByteArrayObj ## A QHash - - QtItemFlag* {.pure.} = enum - None = 0.cint, - IsSelectable = 1.cint, - IsEditable = 2.cint, - IsDragEnabled = 4.cint, - IsDropEnabled = 8.cint, - IsUserCheckable = 16.cint, - IsEnabled = 32.cint, - IsTristate = 64.cint, - NeverHasChildren = 128.cint - - QtOrientation {.pure.} = enum - Horizontal = 1.cint, - Vertical = 2.cint - - QMetaType* {.pure.} = enum ## \ - ## Qt metatypes values used for specifing the - ## signals and slots argument and return types. - ## - ## This enum mimic the QMetaType::Type C++ enum - UnknownType = cint(0), - Bool = cint(1), - Int = cint(2), - QString = cint(10), - VoidStar = cint(31), - QObjectStar = cint(39), - QVariant = cint(41), - Void = cint(43), + QMetaObject* = ref object of RootObj + vptr: DosQMetaObject + signals: seq[SignalDefinition] + slots: seq[SlotDefinition] + properties: seq[PropertyDefinition] diff --git a/src/private/qmetaobject.nim b/src/private/qmetaobject.nim index 98143a9..6025d11 100644 --- a/src/private/qmetaobject.nim +++ b/src/private/qmetaobject.nim @@ -1,30 +1,3 @@ -import dotherside - -type - QMetaType = cint - - SignalDefinition* = object - name: string - parametersTypes: seq[QMetaType] - - SlotDefinition* = object - name: string - returnMetaType: QMetaType - parametersTypes: seq[QMetaType] - - PropertyDefinition* = object - name: string - propertyMetaType: QMetaType - readSlot: string - writeSlot: string - notifySignal: string - - QMetaObject* = ref object of RootObj - vptr: DosQMetaObject - signals: seq[SignalDefinition] - slots: seq[SlotDefinition] - properties: seq[PropertyDefinition] - proc delete*(metaObject: QMetaObject) = ## Delete a QMetaObject debugMsg("QMetaObject", "delete") diff --git a/src/private/qobject.nim b/src/private/qobject.nim index 47f3630..5744164 100644 --- a/src/private/qobject.nim +++ b/src/private/qobject.nim @@ -1,121 +1,69 @@ -type - RawQVariantArray {.unchecked.} = array[0..0, RawQVariant] - RawQVariantArrayPtr = ptr RawQVariantArray - RawQVariantSeq = seq[RawQVariant] +let staticMetaObjectInstance = newQObjectMetaObject() -proc toVariantSeq(args: RawQVariantArrayPtr, numArgs: cint): seq[QVariant] = - result = @[] - for i in 0..numArgs-1: - result.add(newQVariant(args[i])) +proc staticMetaObject*(c: type QObject): QMetaObject = + ## Return the metaObject of QObject + staticMetaObjectInstance -proc toRawVariantSeq(args: openarray[QVariant]): RawQVariantSeq = - result = @[] - for variant in args: - result.add(variant.data) +proc staticMetaObject*(self: QObject): QMetaObject = + ## Return the metaObject of QObject + staticMetaObjectInstance -proc delete(sequence: seq[QVariant]) = - for variant in sequence: - variant.delete +proc objectName*(self: QObject): string = + ## Return the QObject name + var str: cstring + dos_qobject_objectName(self.vptr, str) + result = $str + dos_chararray_delete(str) -proc toCIntSeq(metaTypes: openarray[QMetaType]): seq[cint] = - result = @[] - for metaType in metaTypes: - result.add(cint(metaType)) +proc `objectName=`*(self: QObject, name: string) = + ## Sets the Qobject name + dos_qobject_setObjectName(self.vptr, name.cstring) -type QObjectCallBack = proc(nimobject: ptr QObjectObj, slotName: RawQVariant, numArguments: cint, arguments: RawQVariantArrayPtr) {.cdecl.} +method metaObject*(self: QObject): QMetaObject {.base.} = + ## Return the metaObject + QObject.staticMetaObject -proc dos_qobject_create(qobject: var RawQObject, nimobject: ptr QObjectObj, qobjectCallback: QObjectCallBack) {.cdecl, importc.} -proc dos_qobject_delete(qobject: RawQObject) {.cdecl, importc.} -proc dos_qobject_slot_create(qobject: RawQObject, slotName: cstring, argumentsCount: cint, argumentsMetaTypes: ptr cint, slotIndex: var cint) {.cdecl, importc.} -proc dos_qobject_signal_create(qobject: RawQObject, signalName: cstring, argumentsCount: cint, argumentsMetaTypes: ptr cint, signalIndex: var cint) {.cdecl, importc.} -proc dos_qobject_signal_emit(qobject: RawQObject, signalName: cstring, argumentsCount: cint, arguments: ptr RawQVariant) {.cdecl, importc.} -proc dos_qobject_property_create(qobject: RawQObject, propertyName: cstring, propertyType: cint, readSlot: cstring, writeSlot: cstring, notifySignal: cstring) {.cdecl, importc.} +proc emit*(qobject: QObject, signalName: string, arguments: openarray[QVariant] = []) = + ## Emit the signal with the given name and values + var dosArguments: seq[DosQVariant] = @[] + for argument in arguments: + dosArguments.add(argument.vptr) + let dosNumArguments = dosArguments.len.cint + let dosArgumentsPtr: ptr DosQVariant = if dosArguments.len > 0: dosArguments[0].unsafeAddr else: nil + dos_qobject_signal_emit(qobject.vptr, signalName.cstring, dosNumArguments, cast[ptr DosQVariantArray](dosArgumentsPtr)) -method onSlotCalled*(nimobject: QObject, slotName: string, args: openarray[QVariant]) {.base.} = - ## Called from the NimQml bridge when a slot is called from Qml. - ## Subclasses can override the given method for handling the slot call +method onSlotCalled*(self: QObject, slotName: string, arguments: openarray[QVariant]) {.base.} = + ## Called from the dotherside library when a slot is called from Qml. discard() -proc qobjectCallback(nimObject: ptr QObjectObj, slotName: RawQVariant, numArguments: cint, arguments: RawQVariantArrayPtr) {.cdecl, exportc.} = - if qobjectRegistry[nimObject]: - let qobject = cast[QObject](nimObject) - GC_ref(qobject) - let slotNameAsQVariant = newQVariant(slotName) - defer: slotNameAsQVariant.delete - let argumentsAsQVariant = arguments.toVariantSeq(numArguments) - defer: argumentsAsQVariant.delete - # Forward to args to the slot - qobject.onSlotCalled(slotNameAsQVariant.stringVal, argumentsAsQVariant) - # Update the slot return value - dos_qvariant_assign(arguments[0], argumentsAsQVariant[0].data) - GC_unref(qobject) +proc qobjectCallback(qobjectPtr: pointer, slotNamePtr: DosQVariant, numArguments: cint, dosArguments: ptr DosQVariantArray) {.cdecl, exportc.} = + ## Called from the dotherside library for invoking a slot + let qobject = cast[QObject](qobjectPtr) + GC_ref(qobject) + # Retrieve slot name + let slotName = newQVariant(slotNamePtr) + defer: slotName.delete + # Retrieve arguments + let arguments = toQVariantSequence(dosArguments[]) + defer: arguments.delete + # Forward to args to the slot + qobject.onSlotCalled(slotName.stringVal, arguments) + # Update the slot return value + dos_qvariant_assign(dosArguments[0], arguments[0].vptr) + GC_unref(qobject) -proc create*(qobject: QObject) = - ## Create a new QObject - debugMsg("QObject", "create") - qobject.deleted = false - qobject.slots = initTable[string,cint]() - qobject.signals = initTable[string, cint]() - qobject.properties = initTable[string, cint]() - let qobjectPtr = addr(qobject[]) - dos_qobject_create(qobject.data, qobjectPtr, qobjectCallback) - qobjectRegistry[qobjectPtr] = true +proc setup*(self: QObject) = + ## Initialize a new QObject + dos_qobject_create(self.vptr, addr(self[]), QObject.staticMetaObject.vptr, qobjectCallback) -proc delete*(qobject: QObject) = - ## Delete the given QObject - if not qobject.deleted: - debugMsg("QObject", "delete") - let qobjectPtr = addr(qobject[]) - qobjectRegistry.del qobjectPtr - dos_qobject_delete(qobject.data) - qobject.data = nil.RawQObject - qobject.deleted = true +proc delete*(self: QObject) = + ## Delete a QObject + if self.vptr.isNil: + return + dos_qobject_delete(self.vptr) + self.vptr.resetToNil proc newQObject*(): QObject = - ## Return a new QObject + ## Create a new QObject new(result, delete) - result.create() - -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), addr(copy[0].cint), index) - qobject.slots[slotName] = index - -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, copy.len.cint, addr(copy[0].cint), index) - else: - dos_qobject_signal_create(qobject.data, signalName, 0, nil, index) - qobject.signals[signalName] = index - -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. - assert propertyName != nil, "property name cannot be nil" - # don't convert a nil string, else we get a strange memory address - let cReadSlot: cstring = if readSlot == nil: cast[cstring](nil) else: readSlot - let cWriteSlot: cstring = if writeSlot == nil: cast[cstring](nil) else: writeSlot - let cNotifySignal: cstring = if notifySignal == nil: cast[cstring](nil) else: notifySignal - dos_qobject_property_create(qobject.data, propertyName, propertyType.cint, cReadSlot, cWriteSlot, cNotifySignal) - -proc emit*(qobject: QObject, signalName: string, args: openarray[QVariant] = []) = - ## Emit the signal with the given name and values - if args.len > 0: - var copy = args.toRawVariantSeq - dos_qobject_signal_emit(qobject.data, signalName, copy.len.cint, addr(copy[0])) - else: - dos_qobject_signal_emit(qobject.data, signalName, 0, nil) + result.setup() diff --git a/src/private/qvariant.nim b/src/private/qvariant.nim index 239bed7..04f6280 100644 --- a/src/private/qvariant.nim +++ b/src/private/qvariant.nim @@ -1,179 +1,166 @@ -proc dos_qvariant_create(variant: var RawQVariant) {.cdecl, importc.} -proc dos_qvariant_create_int(variant: var RawQVariant, value: cint) {.cdecl, importc.} -proc dos_qvariant_create_bool(variant: var RawQVariant, value: bool) {.cdecl, importc.} -proc dos_qvariant_create_string(variant: var RawQVariant, value: cstring) {.cdecl, importc.} -proc dos_qvariant_create_qobject(variant: var RawQVariant, value: RawQObject) {.cdecl, importc.} -proc dos_qvariant_create_qvariant(variant: var RawQVariant, value: RawQVariant) {.cdecl, importc.} -proc dos_qvariant_create_float(variant: var RawQVariant, value: cfloat) {.cdecl, importc.} -proc dos_qvariant_create_double(variant: var RawQVariant, value: cdouble) {.cdecl, importc.} -proc dos_qvariant_delete(variant: RawQVariant) {.cdecl, importc.} -proc dos_qvariant_isnull(variant: RawQVariant, isNull: var bool) {.cdecl, importc.} -proc dos_qvariant_toInt(variant: RawQVariant, value: var cint) {.cdecl, importc.} -proc dos_qvariant_toBool(variant: RawQVariant, value: var bool) {.cdecl, importc.} -proc dos_qvariant_toString(variant: RawQVariant, value: var cstring) {.cdecl, importc.} -proc dos_qvariant_setInt(variant: RawQVariant, value: cint) {.cdecl, importc.} -proc dos_qvariant_setBool(variant: RawQVariant, value: bool) {.cdecl, importc.} -proc dos_qvariant_setString(variant: RawQVariant, value: cstring) {.cdecl, importc.} -proc dos_qvariant_assign(leftValue: RawQVariant, rightValue: RawQVariant) {.cdecl, importc.} -proc dos_qvariant_toFloat(variant: RawQVariant, value: var cfloat) {.cdecl, importc.} -proc dos_qvariant_setFloat(variant: RawQVariant, value: float) {.cdecl, importc.} -proc dos_qvariant_toDouble(variant: RawQVariant, value: var cdouble) {.cdecl, importc.} -proc dos_qvariant_setDouble(variant: RawQVariant, value: cdouble) {.cdecl, importc.} -proc dos_qvariant_setQObject(variant: RawQVariant, value: RawQObject) {.cdecl, importc.} -proc dos_chararray_delete(rawCString: cstring) {.cdecl, importc.} +proc setup*(variant: QVariant) = + ## Setup a new QVariant + dos_qvariant_create(variant.vptr) + variant.vptr.resetToNil -proc create*(variant: QVariant) = - ## Create a new QVariant - dos_qvariant_create(variant.data) - variant.deleted = false +proc setup*(variant: QVariant, value: cint) = + ## Setup a new QVariant given a cint value + dos_qvariant_create_int(variant.vptr, value) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: cint) = - ## Create a new QVariant given a cint value - dos_qvariant_create_int(variant.data, value) - variant.deleted = false +proc setup*(variant: QVariant, value: bool) = + ## Setup a new QVariant given a bool value + dos_qvariant_create_bool(variant.vptr, value) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: bool) = - ## Create a new QVariant given a bool value - dos_qvariant_create_bool(variant.data, value) - variant.deleted = false +proc setup*(variant: QVariant, value: string) = + ## Setup a new QVariant given a string value + dos_qvariant_create_string(variant.vptr, value) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: string) = - ## Create a new QVariant given a string value - dos_qvariant_create_string(variant.data, value) - variant.deleted = false +proc setup*(variant: QVariant, value: QObject) = + ## Setup a new QVariant given a QObject + dos_qvariant_create_qobject(variant.vptr, value.vptr.DosQObject) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: QObject) = - ## Create a new QVariant given a QObject - dos_qvariant_create_qobject(variant.data, value.data.RawQObject) - variant.deleted = false - -proc create*(variant: QVariant, value: RawQVariant) = - ## Create a new QVariant given another QVariant. +proc setup*(variant: QVariant, value: DosQVariant) = + ## Setup a new QVariant given another QVariant. ## The inner value of the QVariant is copied - dos_qvariant_create_qvariant(variant.data, value) - variant.deleted = false + dos_qvariant_create_qvariant(variant.vptr, value) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: cfloat) = - ## Create a new QVariant given a cfloat value - dos_qvariant_create_float(variant.data, value) - variant.deleted = false +proc setup*(variant: QVariant, value: cfloat) = + ## Setup a new QVariant given a cfloat value + dos_qvariant_create_float(variant.vptr, value) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: cdouble) = - ## Create a new QVariant given a cdouble value - dos_qvariant_create_double(variant.data, value) - variant.deleted = false +proc setup*(variant: QVariant, value: cdouble) = + ## Setup a new QVariant given a cdouble value + dos_qvariant_create_double(variant.vptr, value) + variant.vptr.resetToNil -proc create*(variant: QVariant, value: QVariant) = - ## Create a new QVariant given another QVariant. +proc setup*(variant: QVariant, value: QVariant) = + ## Setup a new QVariant given another QVariant. ## The inner value of the QVariant is copied - create(variant, value.data) + setup(variant, value.vptr) proc delete*(variant: QVariant) = ## Delete a QVariant - if not variant.deleted: - debugMsg("QVariant", "delete") - dos_qvariant_delete(variant.data) - variant.data = nil.RawQVariant - variant.deleted = true + if variant.vptr.isNil: + return + debugMsg("QVariant", "delete") + dos_qvariant_delete(variant.vptr) + variant.vptr.resetToNil proc newQVariant*(): QVariant = ## Return a new QVariant new(result, delete) - result.create() + result.setup() proc newQVariant*(value: cint): QVariant = ## Return a new QVariant given a cint new(result, delete) - result.create(value) + result.setup(value) proc newQVariant*(value: bool): QVariant = ## Return a new QVariant given a bool new(result, delete) - result.create(value) + result.setup(value) proc newQVariant*(value: string): QVariant = ## Return a new QVariant given a string new(result, delete) - result.create(value) + result.setup(value) proc newQVariant*(value: QObject): QVariant = ## Return a new QVariant given a QObject new(result, delete) - result.create(value) + result.setup(value) -proc newQVariant*(value: RawQVariant): QVariant = +proc newQVariant*(value: DosQVariant): QVariant = ## Return a new QVariant given a raw QVariant pointer new(result, delete) - result.create(value) + result.setup(value) proc newQVariant*(value: QVariant): QVariant = ## Return a new QVariant given another QVariant new(result, delete) - result.create(value) + result.setup(value) proc newQVariant*(value: float): QVariant = ## Return a new QVariant given a float new(result, delete) - result.create(value) + result.setup(value) proc isNull*(variant: QVariant): bool = ## Return true if the QVariant value is null, false otherwise - dos_qvariant_isnull(variant.data, result) + dos_qvariant_isnull(variant.vptr, result) proc intVal*(variant: QVariant): int = ## Return the QVariant value as int var rawValue: cint - dos_qvariant_toInt(variant.data, rawValue) + dos_qvariant_toInt(variant.vptr, rawValue) result = rawValue.cint proc `intVal=`*(variant: QVariant, value: int) = ## Sets the QVariant value int value var rawValue = value.cint - dos_qvariant_setInt(variant.data, rawValue) + dos_qvariant_setInt(variant.vptr, rawValue) proc boolVal*(variant: QVariant): bool = ## Return the QVariant value as bool - dos_qvariant_toBool(variant.data, result) + dos_qvariant_toBool(variant.vptr, result) proc `boolVal=`*(variant: QVariant, value: bool) = ## Sets the QVariant bool value - dos_qvariant_setBool(variant.data, value) + dos_qvariant_setBool(variant.vptr, value) proc floatVal*(variant: QVariant): float = ## Return the QVariant value as float var rawValue: cfloat - dos_qvariant_toFloat(variant.data, rawValue) + dos_qvariant_toFloat(variant.vptr, rawValue) result = rawValue.cfloat proc `floatVal=`*(variant: QVariant, value: float) = ## Sets the QVariant float value - dos_qvariant_setFloat(variant.data, value.cfloat) + dos_qvariant_setFloat(variant.vptr, value.cfloat) proc doubleVal*(variant: QVariant): cdouble = ## Return the QVariant value as double var rawValue: cdouble - dos_qvariant_toDouble(variant.data, rawValue) + dos_qvariant_toDouble(variant.vptr, rawValue) result = rawValue proc `doubleVal=`*(variant: QVariant, value: cdouble) = ## Sets the QVariant double value - dos_qvariant_setDouble(variant.data, value) + dos_qvariant_setDouble(variant.vptr, value) proc stringVal*(variant: QVariant): string = ## Return the QVariant value as string var rawCString: cstring - dos_qvariant_toString(variant.data, rawCString) + dos_qvariant_toString(variant.vptr, rawCString) result = $rawCString dos_chararray_delete(rawCString) proc `stringVal=`*(variant: QVariant, value: string) = ## Sets the QVariant string value - dos_qvariant_setString(variant.data, value) + dos_qvariant_setString(variant.vptr, value) proc `qobjectVal=`*(variant: QVariant, value: QObject) = ## Sets the QVariant qobject value - dos_qvariant_setQObject(variant.data, value.data.RawQObject) + dos_qvariant_setQObject(variant.vptr, value.vptr.DosQObject) proc assign*(leftValue: QVariant, rightValue: QVariant) = ## Assign a QVariant with another. The inner value of the QVariant is copied - dos_qvariant_assign(leftValue.data, rightValue.data) + dos_qvariant_assign(leftValue.vptr, rightValue.vptr) + +proc toQVariantSequence(a: openarray[DosQVariant]): seq[QVariant] = + ## Convert an array of DosQVariant to a sequence of QVariant + result = @[] + for x in a: + result.add(newQVariant(x)) + +proc delete(a: openarray[QVariant]) = + ## Delete an array of QVariants + for x in a: + x.delete