From 0b284798bcdcff9db9eb784324a2d9ab9c0e63a6 Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Sat, 25 Apr 2015 19:00:02 +0200 Subject: [PATCH 1/8] [DQml] Added support for QModelIndex --- D/DQml/CMakeLists.txt | 1 + D/DQml/dothersideinterface.di | 11 ++++++ D/DQml/dqml.d | 1 + D/DQml/qmodelindex.d | 72 +++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 D/DQml/qmodelindex.d diff --git a/D/DQml/CMakeLists.txt b/D/DQml/CMakeLists.txt index 352c23d..cdee26a 100644 --- a/D/DQml/CMakeLists.txt +++ b/D/DQml/CMakeLists.txt @@ -11,5 +11,6 @@ add_library(${PROJECT_NAME} STATIC chararray.d qguiapplication.d qapplication.d + qmodelindex.d ) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/D/DQml/dothersideinterface.di b/D/DQml/dothersideinterface.di index 5685a42..17ac6d7 100644 --- a/D/DQml/dothersideinterface.di +++ b/D/DQml/dothersideinterface.di @@ -87,4 +87,15 @@ extern(C) immutable(char)* notifySignal); void dos_qobject_delete(void*); + + // QModelIndex + void dos_qmodelindex_create(ref void* index); + void dos_qmodelindex_delete(void* index); + void dos_qmodelindex_row(void*, ref int result); + void dos_qmodelindex_column(void*, ref int result); + void dos_qmodelindex_isValid(void* index, ref bool result); + void dos_qmodelindex_data(void* index, int role, void* variant); + void dos_qmodelindex_parent(void* index, void* parent); + void dos_qmodelindex_child(void* index, int r, int c, void* child); + void dos_qmodelindex_sibling(void* index, int r, int c, void* sibling); } diff --git a/D/DQml/dqml.d b/D/DQml/dqml.d index 4655ad3..c54cccf 100644 --- a/D/DQml/dqml.d +++ b/D/DQml/dqml.d @@ -6,3 +6,4 @@ public import qobject; public import qqmlapplicationengine; public import qquickview; public import qmetatype; +public import qmodelindex; diff --git a/D/DQml/qmodelindex.d b/D/DQml/qmodelindex.d new file mode 100644 index 0000000..d340185 --- /dev/null +++ b/D/DQml/qmodelindex.d @@ -0,0 +1,72 @@ +import dothersideinterface; +import qvariant; + +class QModelIndex +{ + this() + { + dos_qmodelindex_create(this.ptr); + } + + ~this() + { + dos_qmodelindex_delete(this.ptr); + ptr = null; + } + + public void* internalPtr() + { + return this.ptr; + } + + public int row() + { + int result = -1; + dos_qmodelindex_row(this.ptr, result); + return result; + } + + public int column() + { + int result = -1; + dos_qmodelindex_column(this.ptr, result); + return result; + } + + public bool isValid() + { + bool result = false; + dos_qmodelindex_isValid(this.ptr, result); + return result; + } + + public QVariant data(int role) + { + auto result = new QVariant(); + dos_qmodelindex_data(this.ptr, role, result.rawData()); + return result; + } + + public QModelIndex parent() + { + auto result = new QModelIndex(); + dos_qmodelindex_parent(this.ptr, result.ptr); + return result; + } + + public QModelIndex child(int row, int column) + { + auto result = new QModelIndex(); + dos_qmodelindex_child(this.ptr, row, column, result.ptr); + return result; + } + + public QModelIndex sibling(int row, int column) + { + auto result = new QModelIndex(); + dos_qmodelindex_sibling(this.ptr, row, column, result.ptr); + return result; + } + + private void* ptr; +} From c9ffaf4f5909c5f4fa4284e7957ba003d9e851bd Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Mon, 27 Apr 2015 19:58:18 +0200 Subject: [PATCH 2/8] [DQml] Refactored and beautified the code Removed the int parameter from methods that return a string --- D/DQml/CMakeLists.txt | 1 - D/DQml/chararray.d | 56 ------ D/DQml/dothersideinterface.di | 12 +- D/DQml/main.d | 67 ------- D/DQml/qapplication.d | 32 ++-- D/DQml/qguiapplication.d | 32 ++-- D/DQml/qmetatype.d | 112 ++++++------ D/DQml/qobject.d | 190 ++++++++++---------- D/DQml/qqmlapplicationengine.d | 38 ++-- D/DQml/qqmlcontext.d | 43 ++--- D/DQml/qquickview.d | 64 +++---- D/DQml/qvariant.d | 252 +++++++++++++-------------- DOtherSide/DOtherSide/DOtherSide.cpp | 28 +-- DOtherSide/DOtherSide/DOtherSide.h | 40 ++--- 14 files changed, 406 insertions(+), 561 deletions(-) delete mode 100644 D/DQml/chararray.d delete mode 100644 D/DQml/main.d diff --git a/D/DQml/CMakeLists.txt b/D/DQml/CMakeLists.txt index cdee26a..1af989d 100644 --- a/D/DQml/CMakeLists.txt +++ b/D/DQml/CMakeLists.txt @@ -8,7 +8,6 @@ add_library(${PROJECT_NAME} STATIC qquickview.d qqmlapplicationengine.d qqmlcontext.d - chararray.d qguiapplication.d qapplication.d qmodelindex.d diff --git a/D/DQml/chararray.d b/D/DQml/chararray.d deleted file mode 100644 index fb052b8..0000000 --- a/D/DQml/chararray.d +++ /dev/null @@ -1,56 +0,0 @@ -import dothersideinterface; -import std.string; - -class CharArray -{ - this() - { - _size = 0; - dos_chararray_create(_data, _size); - } - - this(int size) - { - _size = size; - dos_chararray_create(_data, _size); - } - - this(char* data, int size) - { - _data = data; - _size = size; - } - - ~this() - { - dos_chararray_delete(_data); - } - - char* data() - { - return _data; - } - - int size() - { - return _size; - } - - ref char* dataRef() - { - return _data; - } - - ref int sizeRef() - { - return _size; - } - - override string toString() - { - return fromStringz(_data).dup; - } - - private char* _data; - private int _size; -} diff --git a/D/DQml/dothersideinterface.di b/D/DQml/dothersideinterface.di index 17ac6d7..77fd484 100644 --- a/D/DQml/dothersideinterface.di +++ b/D/DQml/dothersideinterface.di @@ -21,18 +21,16 @@ extern(C) // QQuickView void dos_qquickview_create(ref void*); void dos_qquickview_show(void*); - void dos_qquickview_source(void*, ref char *, ref int); + void dos_qquickview_source(void*, ref char *); void dos_qquickview_set_source(void*, immutable (char)* filename); void dos_qquickview_rootContext(void*, ref void*); void dos_qquickview_delete(void*); // CharArray - void dos_chararray_create(ref char*); - void dos_chararray_create(ref char*, int size); void dos_chararray_delete(char*); // QQmlContext - void dos_qqmlcontext_baseUrl(void*, ref char*, ref int); + void dos_qqmlcontext_baseUrl(void*, ref char*); void dos_qqmlcontext_setcontextproperty(void*, immutable (char)*, void*); // QVariant @@ -47,7 +45,7 @@ extern(C) void dos_qvariant_setInt(void*, int); void dos_qvariant_toBool(void*, ref bool); void dos_qvariant_setBool(void*, bool); - void dos_qvariant_toString(void*, ref char*, ref int); + void dos_qvariant_toString(void*, ref char*); void dos_qvariant_setString(void*, immutable(char)*); void dos_qvariant_toFloat(void*, ref float); void dos_qvariant_setFloat(void*, float); @@ -60,8 +58,8 @@ extern(C) // QObject void dos_qobject_create(ref void*, - void* dobject, - void function (void*, void*, int , void**)); + void* dobject, + void function (void*, void*, int , void**)); void dos_qobject_slot_create(void*, immutable (char)* name, diff --git a/D/DQml/main.d b/D/DQml/main.d deleted file mode 100644 index 2b4b769..0000000 --- a/D/DQml/main.d +++ /dev/null @@ -1,67 +0,0 @@ -import std.stdio; -import std.string; -import std.traits; -import std.conv; -import std.functional; -import core.memory; -import dotherside; - -class MyObject : QObject -{ - this() - { - foo = registerSlot("foo", &_foo); - bar = registerSlot("bar", &_bar); - nameChanged = registerSignal!(string)("nameChanged"); - tor = registerSlot("tor", &_tor); - } - - public QSlot!(void delegate(int)) foo; - private void _foo(int fooValue) - { - writeln("D: Called foo slot with argument ", fooValue , "!!"); - } - - public QSlot!(int delegate(int)) bar; - private int _bar(int barValue) - { - writeln("D: Called bar slot with argument " , barValue, "!!"); - return 666; - } - - public QSlot!(string delegate(string)) tor; - private string _tor (string torValue) - { - writeln("D: Called tor slot with argument ", torValue, "!!"); - return "2343"; - } - - public QSignal!(string) nameChanged; -} - -void main() -{ - try - { - auto app = new QGuiApplication; - scope(exit) destroy(app); - - auto view = new QQuickView; - scope(exit) destroy(view); - - auto myObject = new MyObject(); - scope(exit) destroy(myObject); - - auto context = view.rootContext(); - context.setContextProperty("myObject", new QVariant(myObject)); - - view.setSource("Test.qml"); - view.show(); - - myObject.nameChanged("prova"); - - app.exec(); - } - catch - {} -} diff --git a/D/DQml/qapplication.d b/D/DQml/qapplication.d index 4c73221..b43e1b2 100644 --- a/D/DQml/qapplication.d +++ b/D/DQml/qapplication.d @@ -2,23 +2,23 @@ import dothersideinterface; class QApplication { - this() - { - dos_qapplication_create(); - } + this() + { + dos_qapplication_create(); + } - ~this() - { - dos_qapplication_delete(); - } + ~this() + { + dos_qapplication_delete(); + } - void exec() - { - dos_qapplication_exec(); - } + void exec() + { + dos_qapplication_exec(); + } - void quit() - { - dos_qapplication_quit(); - } + void quit() + { + dos_qapplication_quit(); + } } diff --git a/D/DQml/qguiapplication.d b/D/DQml/qguiapplication.d index 4dbc8e6..9623f7e 100644 --- a/D/DQml/qguiapplication.d +++ b/D/DQml/qguiapplication.d @@ -2,23 +2,23 @@ import dothersideinterface; class QGuiApplication { - this() - { - dos_qguiapplication_create(); - } + this() + { + dos_qguiapplication_create(); + } - ~this() - { - dos_qguiapplication_delete(); - } + ~this() + { + dos_qguiapplication_delete(); + } - void exec() - { - dos_qguiapplication_exec(); - } + void exec() + { + dos_qguiapplication_exec(); + } - void quit() - { - dos_qguiapplication_quit(); - } + void quit() + { + dos_qguiapplication_quit(); + } } diff --git a/D/DQml/qmetatype.d b/D/DQml/qmetatype.d index 2e62b80..356e828 100644 --- a/D/DQml/qmetatype.d +++ b/D/DQml/qmetatype.d @@ -2,66 +2,66 @@ import qobject; import qvariant; /* -enum Type { - UnknownType = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5, - Double = 6, Long = 32, Short = 33, Char = 34, ULong = 35, UShort = 36, - UChar = 37, Float = 38, - VoidStar = 31, - QChar = 7, QString = 10, QStringList = 11, QByteArray = 12, - QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17, - QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22, - QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27, - QEasingCurve = 29, QUuid = 30, QVariant = 41, QModelIndex = 42, - QRegularExpression = 44, - QJsonValue = 45, QJsonObject = 46, QJsonArray = 47, QJsonDocument = 48, - QObjectStar = 39, SChar = 40, - Void = 43, - QVariantMap = 8, QVariantList = 9, QVariantHash = 28, - QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68, - QIcon = 69, QImage = 70, QPolygon = 71, QRegion = 72, QBitmap = 73, - QCursor = 74, QKeySequence = 75, QPen = 76, QTextLength = 77, QTextFormat = 78, - QMatrix = 79, QTransform = 80, QMatrix4x4 = 81, QVector2D = 82, - QVector3D = 83, QVector4D = 84, QQuaternion = 85, QPolygonF = 86, - QSizePolicy = 121, - User = 1024 - }; + enum Type { + UnknownType = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5, + Double = 6, Long = 32, Short = 33, Char = 34, ULong = 35, UShort = 36, + UChar = 37, Float = 38, + VoidStar = 31, + QChar = 7, QString = 10, QStringList = 11, QByteArray = 12, + QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17, + QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22, + QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27, + QEasingCurve = 29, QUuid = 30, QVariant = 41, QModelIndex = 42, + QRegularExpression = 44, + QJsonValue = 45, QJsonObject = 46, QJsonArray = 47, QJsonDocument = 48, + QObjectStar = 39, SChar = 40, + Void = 43, + QVariantMap = 8, QVariantList = 9, QVariantHash = 28, + QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68, + QIcon = 69, QImage = 70, QPolygon = 71, QRegion = 72, QBitmap = 73, + QCursor = 74, QKeySequence = 75, QPen = 76, QTextLength = 77, QTextFormat = 78, + QMatrix = 79, QTransform = 80, QMatrix4x4 = 81, QVector2D = 82, + QVector3D = 83, QVector4D = 84, QQuaternion = 85, QPolygonF = 86, + QSizePolicy = 121, + User = 1024 + }; */ public enum QMetaType { - Unknown = 0, - Bool = 1, - Int = 2, - String = 10, - VoidStr = 31, - QObject = 39, - QVariant = 41, - Void = 43 + Unknown = 0, + Bool = 1, + Int = 2, + String = 10, + VoidStr = 31, + QObject = 39, + QVariant = 41, + Void = 43 } public QMetaType GetMetaType(T)() - if (is (T == int) - || is (T == bool) - || is (T == string) - || is (T == void) - || is (T == QObject) - || is (T == QVariant) - || is (T == void*)) -{ - static if (is (T == bool)) - return QMetaType.Bool; - else if (is (T == int)) - return QMetaType.Int; - else if (is( T == void)) - return QMetaType.Void; - else if (is (T == string)) - return QMetaType.String; - else if (is (T == QObject)) - return QMetaType.QObject; - else if (is (T == QVariant)) - return QMetaType.QVariant; - else if (is (T == void*)) - return QMetaType.VoidStar; - else - return QMetaType.Unknown; -} + if (is (T == int) + || is (T == bool) + || is (T == string) + || is (T == void) + || is (T == QObject) + || is (T == QVariant) + || is (T == void*)) + { + static if (is (T == bool)) + return QMetaType.Bool; + else if (is (T == int)) + return QMetaType.Int; + else if (is( T == void)) + return QMetaType.Void; + else if (is (T == string)) + return QMetaType.String; + else if (is (T == QObject)) + return QMetaType.QObject; + else if (is (T == QVariant)) + return QMetaType.QVariant; + else if (is (T == void*)) + return QMetaType.VoidStar; + else + return QMetaType.Unknown; + } diff --git a/D/DQml/qobject.d b/D/DQml/qobject.d index 98d878d..4550949 100644 --- a/D/DQml/qobject.d +++ b/D/DQml/qobject.d @@ -11,101 +11,101 @@ import qvariant; public class QObject { - this() - { - dos_qobject_create(this.data, cast (void*) this, &staticSlotCallback); - } - - ~this() - { - dos_qobject_delete(this.data); - } - - public void* rawData() - { - return this.data; - } - - private extern (C) static void staticSlotCallback(void* qObjectPtr, - void* rawSlotName, - int numParameters, - void** parametersArray) - { - QVariant[] parameters = new QVariant[numParameters]; - for (int i = 0; i < numParameters; ++i) - parameters[i] = new QVariant(parametersArray[i]); - QObject qObject = cast(QObject) qObjectPtr; - QVariant slotName = new QVariant(rawSlotName); - qObject.onSlotCalled(slotName, parameters); - } - - protected void onSlotCalled(QVariant slotName, QVariant[] parameters) - { - } - - protected void registerSlot(string name, QMetaType[] types) - { - int index = -1; - int length = cast(int)types.length; - int[] array = to!(int[])(types); - dos_qobject_slot_create(this.data, - name.toStringz(), - length, - array.ptr, - index); - } - - protected void registerSignal(string name, QMetaType[] types) - { - int index = -1; - int length = cast(int)types.length; - int[] array = length > 0 ? to!(int[])(types) : null; - dos_qobject_signal_create(this.data, - name.toStringz(), - length, - array.ptr, - index); - } - - protected void registerProperty(string name, - QMetaType type, - string readSlotName, - string writeSlotName, - string notifySignalName) - { - dos_qobject_property_create(this.data, - name.toStringz(), - type, - readSlotName.toStringz(), - writeSlotName.toStringz(), - notifySignalName.toStringz()); - } - - protected void emit(T)(string signalName, T t) - { - emit(signalName, new QVariant(t)); - } - - protected void emit(string signalName, QVariant value) - { - QVariant[] array = [value]; - emit(signalName, array); - } - - protected void emit(string signalName, QVariant[] arguments = null) - { - int length = cast(int)arguments.length; - void*[] array = null; - if (length > 0) { - array = new void*[length]; - foreach (int i, QVariant v; arguments) - array[i] = v.rawData(); + this() + { + dos_qobject_create(this.data, cast (void*) this, &staticSlotCallback); } - dos_qobject_signal_emit(this.data, - signalName.toStringz(), - length, - array.ptr); - } - private void* data; + ~this() + { + dos_qobject_delete(this.data); + } + + public void* rawData() + { + return this.data; + } + + private extern (C) static void staticSlotCallback(void* qObjectPtr, + void* rawSlotName, + int numParameters, + void** parametersArray) + { + QVariant[] parameters = new QVariant[numParameters]; + for (int i = 0; i < numParameters; ++i) + parameters[i] = new QVariant(parametersArray[i]); + QObject qObject = cast(QObject) qObjectPtr; + QVariant slotName = new QVariant(rawSlotName); + qObject.onSlotCalled(slotName, parameters); + } + + protected void onSlotCalled(QVariant slotName, QVariant[] parameters) + { + } + + protected void registerSlot(string name, QMetaType[] types) + { + int index = -1; + int length = cast(int)types.length; + int[] array = to!(int[])(types); + dos_qobject_slot_create(this.data, + name.toStringz(), + length, + array.ptr, + index); + } + + protected void registerSignal(string name, QMetaType[] types) + { + int index = -1; + int length = cast(int)types.length; + int[] array = length > 0 ? to!(int[])(types) : null; + dos_qobject_signal_create(this.data, + name.toStringz(), + length, + array.ptr, + index); + } + + protected void registerProperty(string name, + QMetaType type, + string readSlotName, + string writeSlotName, + string notifySignalName) + { + dos_qobject_property_create(this.data, + name.toStringz(), + type, + readSlotName.toStringz(), + writeSlotName.toStringz(), + notifySignalName.toStringz()); + } + + protected void emit(T)(string signalName, T t) + { + emit(signalName, new QVariant(t)); + } + + protected void emit(string signalName, QVariant value) + { + QVariant[] array = [value]; + emit(signalName, array); + } + + protected void emit(string signalName, QVariant[] arguments = null) + { + int length = cast(int)arguments.length; + void*[] array = null; + if (length > 0) { + array = new void*[length]; + foreach (int i, QVariant v; arguments) + array[i] = v.rawData(); + } + dos_qobject_signal_emit(this.data, + signalName.toStringz(), + length, + array.ptr); + } + + private void* data; } diff --git a/D/DQml/qqmlapplicationengine.d b/D/DQml/qqmlapplicationengine.d index f315b3b..4015108 100644 --- a/D/DQml/qqmlapplicationengine.d +++ b/D/DQml/qqmlapplicationengine.d @@ -4,27 +4,27 @@ import std.string; class QQmlApplicationEngine { - public this() - { - dos_qqmlapplicationengine_create(data); - } + public this() + { + dos_qqmlapplicationengine_create(data); + } - public ~this() - { - dos_qqmlapplicationengine_delete(data); - } + public ~this() + { + dos_qqmlapplicationengine_delete(data); + } - public QQmlContext rootContext() - { - void* contextData; - dos_qqmlapplicationengine_context(data, contextData); - return new QQmlContext(contextData); - } + public QQmlContext rootContext() + { + void* contextData; + dos_qqmlapplicationengine_context(data, contextData); + return new QQmlContext(contextData); + } - public void load(string filename) - { - dos_qqmlapplicationengine_load(data, filename.toStringz()); - } + public void load(string filename) + { + dos_qqmlapplicationengine_load(data, filename.toStringz()); + } - private void* data; + private void* data; } diff --git a/D/DQml/qqmlcontext.d b/D/DQml/qqmlcontext.d index 05bdfca..15763d5 100644 --- a/D/DQml/qqmlcontext.d +++ b/D/DQml/qqmlcontext.d @@ -1,31 +1,32 @@ import dothersideinterface; import qvariant; -import chararray; +import std.string; class QQmlContext { - public this(void* data) - { - this.data = data; - } + public this(void* data) + { + this.data = data; + } - public void* rawData() - { - return data; - } + public void* rawData() + { + return data; + } - public string baseUrl() - { - auto array = new CharArray(); - scope(exit) destroy(array); - dos_qqmlcontext_baseUrl(data, array.dataRef(), array.sizeRef()); - return array.toString(); - } + public string baseUrl() + { + char* array; + dos_qqmlcontext_baseUrl(data, array); + string result = fromStringz(array).dup; + dos_chararray_delete(array); + return result; + } - public void setContextProperty(string name, QVariant value) - { - dos_qqmlcontext_setcontextproperty(data, name.ptr, value.rawData()); - } + public void setContextProperty(string name, QVariant value) + { + dos_qqmlcontext_setcontextproperty(data, name.ptr, value.rawData()); + } - private void* data; + private void* data; } diff --git a/D/DQml/qquickview.d b/D/DQml/qquickview.d index bf3a0e2..c5f0b34 100644 --- a/D/DQml/qquickview.d +++ b/D/DQml/qquickview.d @@ -1,45 +1,45 @@ import dothersideinterface; import qqmlcontext; -import chararray; import std.string; class QQuickView { - this() - { - dos_qquickview_create(data); - } + this() + { + dos_qquickview_create(data); + } - ~this() - { - dos_qquickview_delete(data); - } + ~this() + { + dos_qquickview_delete(data); + } - void show() - { - dos_qquickview_show(data); - } + void show() + { + dos_qquickview_show(data); + } - QQmlContext rootContext() - { - void* contextData; - dos_qquickview_rootContext(data, contextData); - return new QQmlContext(contextData); - } + QQmlContext rootContext() + { + void* contextData; + dos_qquickview_rootContext(data, contextData); + return new QQmlContext(contextData); + } - string source() - { - auto array = new CharArray(); - scope(exit) destroy(array); - dos_qquickview_source(data, array.dataRef(), array.sizeRef()); - return array.toString(); - } + string source() + { + char* array; + dos_qquickview_source(data, array); + string result = fromStringz(array).dup; + dos_chararray_delete(array); + return result; + } - void setSource(string filename) - { - immutable(char)* filenameAsCString = filename.toStringz(); - dos_qquickview_set_source(data, filenameAsCString); - } + void setSource(string filename) + { + immutable(char)* filenameAsCString = filename.toStringz(); + dos_qquickview_set_source(data, filenameAsCString); + } - private void* data; + private void* data; } diff --git a/D/DQml/qvariant.d b/D/DQml/qvariant.d index fac1f2d..bf44eba 100644 --- a/D/DQml/qvariant.d +++ b/D/DQml/qvariant.d @@ -1,160 +1,160 @@ import dothersideinterface; import qobject; import std.string; -import chararray; class QVariant { - public this() - { - dos_qvariant_create(this.data); - } + public this() + { + dos_qvariant_create(this.data); + } - public this(int value) - { - dos_qvariant_create_int(this.data, value); - } + public this(int value) + { + dos_qvariant_create_int(this.data, value); + } - public this(bool value) - { - dos_qvariant_create_bool(this.data, value); - } + public this(bool value) + { + dos_qvariant_create_bool(this.data, value); + } - public this(string value) - { - dos_qvariant_create_string(this.data, value.toStringz()); - } + public this(string value) + { + dos_qvariant_create_string(this.data, value.toStringz()); + } - public this(float value) - { - dos_qvariant_create_float(this.data, value); - } + public this(float value) + { + dos_qvariant_create_float(this.data, value); + } - public this(double value) - { - dos_qvariant_create_double(this.data, value); - } + public this(double value) + { + dos_qvariant_create_double(this.data, value); + } - public this(QObject value) - { - dos_qvariant_create_qobject(this.data, value.rawData()); - } + public this(QObject value) + { + dos_qvariant_create_qobject(this.data, value.rawData()); + } - public this(void* data, bool hasOwnership = false) - { - this.data = data; - this.hasOwnership = hasOwnership; - } + public this(void* data, bool hasOwnership = false) + { + this.data = data; + this.hasOwnership = hasOwnership; + } - ~this() - { - if (this.hasOwnership) - dos_qvariant_delete(this.data); - } + ~this() + { + if (this.hasOwnership) + dos_qvariant_delete(this.data); + } - public void* rawData() - { - return data; - } + public void* rawData() + { + return data; + } - public void setValue(int value) - { - dos_qvariant_setInt(this.data, value); - } + public void setValue(int value) + { + dos_qvariant_setInt(this.data, value); + } - public void setValue(bool value) - { - dos_qvariant_setBool(this.data, value); - } + public void setValue(bool value) + { + dos_qvariant_setBool(this.data, value); + } - public void setValue(string value) - { - dos_qvariant_setString(this.data, value.toStringz()); - } + public void setValue(string value) + { + dos_qvariant_setString(this.data, value.toStringz()); + } - public void setValue(QObject value) - { - dos_qvariant_setQObject(this.data, value.rawData()); - } + public void setValue(QObject value) + { + dos_qvariant_setQObject(this.data, value.rawData()); + } - public void setValue(float value) - { - dos_qvariant_setFloat(this.data, value); - } + public void setValue(float value) + { + dos_qvariant_setFloat(this.data, value); + } - public void setValue(double value) - { - dos_qvariant_setDouble(this.data, value); - } + public void setValue(double value) + { + dos_qvariant_setDouble(this.data, value); + } - public void getValue(ref int value) - { - value = toInt(); - } + public void getValue(ref int value) + { + value = toInt(); + } - public void getValue(ref bool value) - { - value = toBool(); - } + public void getValue(ref bool value) + { + value = toBool(); + } - public void getValue(ref string value) - { - value = toString(); - } + public void getValue(ref string value) + { + value = toString(); + } - public void getValue(ref float value) - { - value = toFloat(); - } + public void getValue(ref float value) + { + value = toFloat(); + } - public void getValue(ref double value) - { - value = toDouble(); - } + public void getValue(ref double value) + { + value = toDouble(); + } - public bool isNull() - { - bool result; - dos_qvariant_isnull(this.data, result); - return result; - } + public bool isNull() + { + bool result; + dos_qvariant_isnull(this.data, result); + return result; + } - public bool toBool() - { - bool result; - dos_qvariant_toBool(this.data, result); - return result; - } + public bool toBool() + { + bool result; + dos_qvariant_toBool(this.data, result); + return result; + } - public int toInt() - { - int result; - dos_qvariant_toInt(this.data, result); - return result; - } + public int toInt() + { + int result; + dos_qvariant_toInt(this.data, result); + return result; + } - public float toFloat() - { - float result; - dos_qvariant_toFloat(this.data, result); - return result; - } + public float toFloat() + { + float result; + dos_qvariant_toFloat(this.data, result); + return result; + } - public double toDouble() - { - double result; - dos_qvariant_toDouble(this.data, result); - return result; - } + public double toDouble() + { + double result; + dos_qvariant_toDouble(this.data, result); + return result; + } - public override string toString() - { - auto result = new CharArray(); - scope(exit) destroy(result); - dos_qvariant_toString(this.data, result.dataRef(), result.sizeRef()); - return result.toString(); - } + public override string toString() + { + char* array; + dos_qvariant_toString(this.data, array); + string result = fromStringz(array).dup; + dos_chararray_delete(array); + return result; + } - private void* data = null; - private bool hasOwnership = true; + private void* data = null; + private bool hasOwnership = true; } diff --git a/DOtherSide/DOtherSide/DOtherSide.cpp b/DOtherSide/DOtherSide/DOtherSide.cpp index abde915..459bf32 100644 --- a/DOtherSide/DOtherSide/DOtherSide.cpp +++ b/DOtherSide/DOtherSide/DOtherSide.cpp @@ -16,11 +16,10 @@ #include "BaseQAbstractListModel.h" #include "BaseQObject.h" -void convert_to_cstring(const QString& source, char** destination, int* length) +void convert_to_cstring(const QString& source, char** destination) { QByteArray array = source.toUtf8(); *destination = qstrdup(array.data()); - *length = qstrlen(array.data()); } void dos_qguiapplication_create() @@ -110,11 +109,11 @@ void dos_qquickview_delete(void* vptr) delete view; } -void dos_qquickview_source(void* vptr, char** result, int* length) +void dos_qquickview_source(void* vptr, char** result) { QQuickView* view = reinterpret_cast(vptr); QUrl url = view->source(); - convert_to_cstring(url.toString(), result, length); + convert_to_cstring(url.toString(), result); } void dos_qquickview_set_source(void* vptr, const char* filename) @@ -129,29 +128,16 @@ void dos_qquickview_rootContext(void* vptr, void** context) *context = view->rootContext(); } -void dos_chararray_create(char** ptr) -{ - *ptr = 0; -} - -void dos_chararray_create(char** ptr, int size) -{ - if (size > 0) - *ptr = new char[size]; - else - *ptr = 0; -} - void dos_chararray_delete(char* ptr) { if (ptr) delete[] ptr; } -void dos_qqmlcontext_baseUrl(void* vptr, char** result, int* length) +void dos_qqmlcontext_baseUrl(void* vptr, char** result) { QQmlContext* context = reinterpret_cast(vptr); QUrl url = context->baseUrl(); - convert_to_cstring(url.toString(), result, length); + convert_to_cstring(url.toString(), result); } void dos_qqmlcontext_setcontextproperty(void* vptr, const char* name, void* value) @@ -258,10 +244,10 @@ void dos_qvariant_toDouble(void* vptr, double* value) *value = variant->toDouble(); } -void dos_qvariant_toString(void* vptr, char** ptr, int* size) +void dos_qvariant_toString(void* vptr, char** ptr) { auto variant = reinterpret_cast(vptr); - convert_to_cstring(variant->toString(), ptr, size); + convert_to_cstring(variant->toString(), ptr); } void dos_qvariant_setInt(void* vptr, int value) diff --git a/DOtherSide/DOtherSide/DOtherSide.h b/DOtherSide/DOtherSide/DOtherSide.h index ec67223..b38685e 100644 --- a/DOtherSide/DOtherSide/DOtherSide.h +++ b/DOtherSide/DOtherSide/DOtherSide.h @@ -39,17 +39,16 @@ DOS_API void dos_qqmlapplicationengine_delete(void* vptr); // QQuickView DOS_API void dos_qquickview_create(void** vptr); DOS_API void dos_qquickview_show(void* vptr); -DOS_API void dos_qquickview_source(void* vptr, char** result, int* length); +DOS_API void dos_qquickview_source(void* vptr, char** result); DOS_API void dos_qquickview_set_source(void* vptr, const char* filename); DOS_API void dos_qquickview_delete(void* vptr); -DOS_API void dos_qquickview_rootContext(void* vptr, void** context); +DOS_API void dos_qquickview_rootContext(void* vptr, void** result); // QQmlContext -DOS_API void dos_qqmlcontext_baseUrl(void* vptr, char** result, int* length); +DOS_API void dos_qqmlcontext_baseUrl(void* vptr, char** result); DOS_API void dos_qqmlcontext_setcontextproperty(void* vptr, const char* name, void* value); // CharArray -DOS_API void dos_chararray_create(char** ptr, int size); DOS_API void dos_chararray_delete(char* ptr); // QVariant @@ -62,19 +61,19 @@ DOS_API void dos_qvariant_create_qvariant(void** vptr, void* value); DOS_API void dos_qvariant_create_float(void** vptr, float value); DOS_API void dos_qvariant_create_double(void** vptr, double value); DOS_API void dos_qvariant_create_qabstractlistmodel(void** vptr, void* value); -DOS_API void dos_qvariant_toInt(void* vptr, int* value); +DOS_API void dos_qvariant_toInt(void* vptr, int* result); DOS_API void dos_qvariant_setInt(void* vptr, int value); -DOS_API void dos_qvariant_toBool(void* vptr, bool* value); +DOS_API void dos_qvariant_toBool(void* vptr, bool* result); DOS_API void dos_qvariant_setBool(void* vptr, bool value); -DOS_API void dos_qvariant_toFloat(void* vptr, float* value); +DOS_API void dos_qvariant_toFloat(void* vptr, float* result); DOS_API void dos_qvariant_setFloat(void* vptr, float value); -DOS_API void dos_qvariant_toDouble(void* vptr, double* value); +DOS_API void dos_qvariant_toDouble(void* vptr, double* result); DOS_API void dos_qvariant_setDouble(void* vptr, double value); -DOS_API void dos_qvariant_toString(void* vptr, char** ptr, int* size); +DOS_API void dos_qvariant_toString(void* vptr, char** result); DOS_API void dos_qvariant_setString(void* vptr, const char* value); DOS_API void dos_qvariant_setQObject(void* vptr, void* value); DOS_API void dos_qvariant_setQAbstractListModel(void* vptr, void* value); -DOS_API void dos_qvariant_isnull(void* vptr, bool* isNull); +DOS_API void dos_qvariant_isnull(void* vptr, bool* result); DOS_API void dos_qvariant_delete(void* vptr); DOS_API void dos_qvariant_assign(void* vptr, void* other); @@ -82,24 +81,20 @@ DOS_API void dos_qvariant_assign(void* vptr, void* other); DOS_API void dos_qobject_create(void** vptr, void* dObjectPointer, DObjectCallback dObjectCallback); - DOS_API void dos_qobject_slot_create(void* vptr, const char* name, int parametersCount, int* parametersMetaTypes, int* slotIndex); - DOS_API void dos_qobject_signal_create(void* vptr, const char* name, int parametersCount, int* parametersMetaTypes, int* signalIndex); - DOS_API void dos_qobject_signal_emit(void* vptr, const char* name, int parametersCount, void** parameters); - DOS_API void dos_qobject_property_create(void* vptr, const char* name, int propertyMetaType, @@ -136,24 +131,13 @@ DOS_API void dos_qabstractlistmodel_create(void** vptr, RoleNamesCallback roleNamesCallback, FlagsCallback flagsCallback, HeaderDataCallback headerDataCallback); -DOS_API void dos_qabstractlistmodel_beginInsertRows(void* vptr, - QModelIndexVoidPtr parentIndex, - int first, - int last); +DOS_API void dos_qabstractlistmodel_beginInsertRows(void* vptr, void* parent, int first, int last); DOS_API void dos_qabstractlistmodel_endInsertRows(void* vptr); -DOS_API void dos_qabstractlistmodel_beginRemoveRows(void* vptr, - QModelIndexVoidPtr parentIndex, - int first, - int last); +DOS_API void dos_qabstractlistmodel_beginRemoveRows(void* vptr, void* parent, int first, int last); DOS_API void dos_qabstractlistmodel_endRemoveRows(void* vptr); DOS_API void dos_qabstractlistmodel_beginResetModel(void* vptr); DOS_API void dos_qabstractlistmodel_endResetModel(void* vptr); -DOS_API void dos_qabstractlistmodel_dataChanged(void* vptr, - QModelIndexVoidPtr topLeftIndex, - QModelIndexVoidPtr bottomRightIndex, - int* rolesArrayPtr, - int rolesArrayLength); - +DOS_API void dos_qabstractlistmodel_dataChanged(void* vptr, void* topLeft, void* bottomRight, int* rolesPtr, int rolesLength); DOS_API void dos_qabstractlistmodel_delete(void* vptr); #ifdef __cplusplus From 3bfed7600f1f2691fcd153e7a129f3e5e5f234d5 Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Mon, 27 Apr 2015 19:59:12 +0200 Subject: [PATCH 3/8] Updated the gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8b778ba..f7d14be 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ build .directory #*# nimcache -*.kdev4 \ No newline at end of file +*.kdev4 +*.user \ No newline at end of file From af9d84a26ed0ae2ff2152f2244504f35fee859ea Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Mon, 27 Apr 2015 21:23:25 +0200 Subject: [PATCH 4/8] [DQml] Added support from QHash --- D/DQml/dothersideinterface.di | 172 ++++++++++++++++++---------------- D/DQml/qhashintbytearray.d | 37 ++++++++ 2 files changed, 126 insertions(+), 83 deletions(-) create mode 100644 D/DQml/qhashintbytearray.d diff --git a/D/DQml/dothersideinterface.di b/D/DQml/dothersideinterface.di index 77fd484..6cbd43a 100644 --- a/D/DQml/dothersideinterface.di +++ b/D/DQml/dothersideinterface.di @@ -1,99 +1,105 @@ extern(C) { - // QApplication - void dos_qapplication_create(); - void dos_qapplication_exec(); - void dos_qapplication_delete(); - void dos_qapplication_quit(); + // QApplication + void dos_qapplication_create(); + void dos_qapplication_exec(); + void dos_qapplication_delete(); + void dos_qapplication_quit(); - // QGuiApplication - void dos_qguiapplication_create(); - void dos_qguiapplication_exec(); - void dos_qguiapplication_delete(); - void dos_qguiapplication_quit(); + // QGuiApplication + void dos_qguiapplication_create(); + void dos_qguiapplication_exec(); + void dos_qguiapplication_delete(); + void dos_qguiapplication_quit(); - // QQmlApplicationEngine - void dos_qqmlapplicationengine_create(ref void*); - void dos_qqmlapplicationengine_load(void*, immutable (char)* filename); - void dos_qqmlapplicationengine_context(void*, ref void*); - void dos_qqmlapplicationengine_delete(void*); + // QQmlApplicationEngine + void dos_qqmlapplicationengine_create(ref void*); + void dos_qqmlapplicationengine_load(void*, immutable (char)* filename); + void dos_qqmlapplicationengine_context(void*, ref void*); + void dos_qqmlapplicationengine_delete(void*); - // QQuickView - void dos_qquickview_create(ref void*); - void dos_qquickview_show(void*); - void dos_qquickview_source(void*, ref char *); - void dos_qquickview_set_source(void*, immutable (char)* filename); - void dos_qquickview_rootContext(void*, ref void*); - void dos_qquickview_delete(void*); + // QQuickView + void dos_qquickview_create(ref void*); + void dos_qquickview_show(void*); + void dos_qquickview_source(void*, ref char *); + void dos_qquickview_set_source(void*, immutable (char)* filename); + void dos_qquickview_rootContext(void*, ref void*); + void dos_qquickview_delete(void*); - // CharArray - void dos_chararray_delete(char*); + // CharArray + void dos_chararray_delete(char*); - // QQmlContext - void dos_qqmlcontext_baseUrl(void*, ref char*); - void dos_qqmlcontext_setcontextproperty(void*, immutable (char)*, void*); + // QQmlContext + void dos_qqmlcontext_baseUrl(void*, ref char*); + void dos_qqmlcontext_setcontextproperty(void*, immutable (char)*, void*); - // QVariant - void dos_qvariant_create(ref void*); - void dos_qvariant_create_int(ref void*, int); - void dos_qvariant_create_bool(ref void*, bool); - void dos_qvariant_create_string(ref void*, immutable(char)*); - void dos_qvariant_create_qobject(ref void*, void*); - void dos_qvariant_create_float(ref void*, float); - void dos_qvariant_create_double(ref void*, double); - void dos_qvariant_toInt(void*, ref int); - void dos_qvariant_setInt(void*, int); - void dos_qvariant_toBool(void*, ref bool); - void dos_qvariant_setBool(void*, bool); - void dos_qvariant_toString(void*, ref char*); - void dos_qvariant_setString(void*, immutable(char)*); - void dos_qvariant_toFloat(void*, ref float); - void dos_qvariant_setFloat(void*, float); - void dos_qvariant_toDouble(void*, ref double); - void dos_qvariant_setDouble(void*, double); - void dos_qvariant_isnull(void*, ref bool); - void dos_qvariant_delete(void*); - void dos_qvariant_assign(void*, void*); - void dos_qvariant_setQObject(void*, void*); + // QVariant + void dos_qvariant_create(ref void*); + void dos_qvariant_create_int(ref void*, int); + void dos_qvariant_create_bool(ref void*, bool); + void dos_qvariant_create_string(ref void*, immutable(char)*); + void dos_qvariant_create_qobject(ref void*, void*); + void dos_qvariant_create_float(ref void*, float); + void dos_qvariant_create_double(ref void*, double); + void dos_qvariant_toInt(void*, ref int); + void dos_qvariant_setInt(void*, int); + void dos_qvariant_toBool(void*, ref bool); + void dos_qvariant_setBool(void*, bool); + void dos_qvariant_toString(void*, ref char*); + void dos_qvariant_setString(void*, immutable(char)*); + void dos_qvariant_toFloat(void*, ref float); + void dos_qvariant_setFloat(void*, float); + void dos_qvariant_toDouble(void*, ref double); + void dos_qvariant_setDouble(void*, double); + void dos_qvariant_isnull(void*, ref bool); + void dos_qvariant_delete(void*); + void dos_qvariant_assign(void*, void*); + void dos_qvariant_setQObject(void*, void*); - // QObject - void dos_qobject_create(ref void*, - void* dobject, - void function (void*, void*, int , void**)); + // QObject + void dos_qobject_create(ref void*, + void* dobject, + void function (void*, void*, int , void**)); - void dos_qobject_slot_create(void*, - immutable (char)* name, - int parametersCount, - int* parametersMetaTypes, - ref int slotIndex); + void dos_qobject_slot_create(void*, + immutable (char)* name, + int parametersCount, + int* parametersMetaTypes, + ref int slotIndex); - void dos_qobject_signal_create(void*, - immutable(char)* name, - int parametersCount, - int* parametersMetaTypes, - ref int signalIndex); + void dos_qobject_signal_create(void*, + immutable(char)* name, + int parametersCount, + int* parametersMetaTypes, + ref int signalIndex); - void dos_qobject_signal_emit(void*, immutable(char)* name, - int parametersCount, - void** parameters); + void dos_qobject_signal_emit(void*, immutable(char)* name, + int parametersCount, + void** parameters); - void dos_qobject_property_create(void*, - immutable(char)* name, - int propertyMetaType, - immutable(char)* readSlot, - immutable(char)* writeSlot, - immutable(char)* notifySignal); + void dos_qobject_property_create(void*, + immutable(char)* name, + int propertyMetaType, + immutable(char)* readSlot, + immutable(char)* writeSlot, + immutable(char)* notifySignal); - void dos_qobject_delete(void*); + void dos_qobject_delete(void*); - // QModelIndex - void dos_qmodelindex_create(ref void* index); - void dos_qmodelindex_delete(void* index); - void dos_qmodelindex_row(void*, ref int result); - void dos_qmodelindex_column(void*, ref int result); - void dos_qmodelindex_isValid(void* index, ref bool result); - void dos_qmodelindex_data(void* index, int role, void* variant); - void dos_qmodelindex_parent(void* index, void* parent); - void dos_qmodelindex_child(void* index, int r, int c, void* child); - void dos_qmodelindex_sibling(void* index, int r, int c, void* sibling); + // QModelIndex + void dos_qmodelindex_create(ref void* index); + void dos_qmodelindex_delete(void* index); + void dos_qmodelindex_row(void*, ref int result); + void dos_qmodelindex_column(void*, ref int result); + void dos_qmodelindex_isValid(void* index, ref bool result); + void dos_qmodelindex_data(void* index, int role, void* variant); + void dos_qmodelindex_parent(void* index, void* parent); + void dos_qmodelindex_child(void* index, int r, int c, void* child); + void dos_qmodelindex_sibling(void* index, int r, int c, void* sibling); + + // QHashIntByteArray + void dos_qhash_int_qbytearray_create(ref void*); + void dos_qhash_int_qbytearray_delete(void*); + void dos_qhash_int_qbytearray_insert(void*, int, immutable(char)*); + void dos_qhash_int_qbytearray_value(void*, int, ref char*); } diff --git a/D/DQml/qhashintbytearray.d b/D/DQml/qhashintbytearray.d new file mode 100644 index 0000000..da8f714 --- /dev/null +++ b/D/DQml/qhashintbytearray.d @@ -0,0 +1,37 @@ +import dothersideinterface; +import qvariant; +import std.string; + +class QHashIntByteArray +{ + this() + { + dos_qhash_int_qbytearray_create(data); + } + + ~this() + { + dos_qhash_int_qbytearray_delete(data); + } + + public void insert(int key, string value) + { + dos_qhash_int_qbytearray_insert(key, value.toStringz()); + } + + public string value(int key) + { + char* array; + dos_qhash_int_qbytearray_value(key, array); + string result = fromStringz(array).dup; + dos_chararray_delete(array); + return result; + } + + public void* rawData() + { + return data; + } + + private void* data; +} From 6da18be1d76c2e6b5a7ab89b693d8421b149869c Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Tue, 28 Apr 2015 23:51:12 +0200 Subject: [PATCH 5/8] [DQml] Started support for QAbstractListModel --- D/DQml/CMakeLists.txt | 1 + D/DQml/dothersideinterface.di | 14 ++++++++ D/DQml/dqml.d | 1 + D/DQml/qabstractlistmodel.d | 64 +++++++++++++++++++++++++++++++++++ D/DQml/qobject.d | 43 +++++++++++++---------- 5 files changed, 106 insertions(+), 17 deletions(-) create mode 100644 D/DQml/qabstractlistmodel.d diff --git a/D/DQml/CMakeLists.txt b/D/DQml/CMakeLists.txt index 1af989d..58918db 100644 --- a/D/DQml/CMakeLists.txt +++ b/D/DQml/CMakeLists.txt @@ -11,5 +11,6 @@ add_library(${PROJECT_NAME} STATIC qguiapplication.d qapplication.d qmodelindex.d + qabstractlistmodel.d ) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/D/DQml/dothersideinterface.di b/D/DQml/dothersideinterface.di index 6cbd43a..95cb4ef 100644 --- a/D/DQml/dothersideinterface.di +++ b/D/DQml/dothersideinterface.di @@ -102,4 +102,18 @@ extern(C) void dos_qhash_int_qbytearray_delete(void*); void dos_qhash_int_qbytearray_insert(void*, int, immutable(char)*); void dos_qhash_int_qbytearray_value(void*, int, ref char*); + + // QAbstractListModel + void dos_qabstractlistmodel_create(ref void*, + void*, + void function (void*, void*, int, void**), + void function (void*, void*, ref int), + void function (void*, void*, ref int), + void function (void*, void*, int, void*), + void function (void*, void*, void*, int, ref bool), + void function (void*, void*), + void function (void*, void*, ref int), + void function (void*, int, int, int, void*)); + void dos_qabstractlistmodel_delete(void*); } + diff --git a/D/DQml/dqml.d b/D/DQml/dqml.d index c54cccf..216a1c0 100644 --- a/D/DQml/dqml.d +++ b/D/DQml/dqml.d @@ -7,3 +7,4 @@ public import qqmlapplicationengine; public import qquickview; public import qmetatype; public import qmodelindex; +public import qabstractlistmodel; diff --git a/D/DQml/qabstractlistmodel.d b/D/DQml/qabstractlistmodel.d new file mode 100644 index 0000000..42009ed --- /dev/null +++ b/D/DQml/qabstractlistmodel.d @@ -0,0 +1,64 @@ +import dothersideinterface; +import qobject; + +class QAbstractListModel : QObject +{ + this() + { + super(true); + dos_qabstractlistmodel_create(this.data, + cast(void*)this, + &staticSlotCallback, + &rowCountCallback, + &columnCountCallback, + &dataCallback, + &setDataCallback, + &roleNamesCallback, + &flagsCallback, + &headerDataCallback); + } + + ~this() + { + dos_qabstractlistmodel_delete(data); + } + + protected extern (C) static void rowCountCallback(void* modelPtr, + void* indexPtr, + ref int result) + {} + + protected extern (C) static void columnCountCallback(void* modelPtr, + void* indexPtr, + ref int result) + {} + + protected extern (C) static void dataCallback(void* modelPtr, + void* indexPtr, + int role, + void* result) + {} + + protected extern (C) static void setDataCallback(void* modelPtr, + void* indexPtr, + void* valuePtr, + int role, + ref bool result) + {} + + protected extern (C) static void roleNamesCallback(void* modelPtr, + void* result) + {} + + protected extern (C) static void flagsCallback(void* modelPtr, + void* index, + ref int result) + {} + + protected extern (C) static void headerDataCallback(void* modelPtr, + int section, + int orientation, + int role, + void* result) + {} +} diff --git a/D/DQml/qobject.d b/D/DQml/qobject.d index 4550949..160f04d 100644 --- a/D/DQml/qobject.d +++ b/D/DQml/qobject.d @@ -11,14 +11,22 @@ import qvariant; public class QObject { - this() + public this() { - dos_qobject_create(this.data, cast (void*) this, &staticSlotCallback); + this(false); + } + + protected this(bool disableDosCalls) + { + this.disableDosCalls = disableDosCalls; + if (!this.disableDosCalls) + dos_qobject_create(this.data, cast(void*)this, &staticSlotCallback); } ~this() { - dos_qobject_delete(this.data); + if (!this.disableDosCalls) + dos_qobject_delete(this.data); } public void* rawData() @@ -26,19 +34,6 @@ public class QObject return this.data; } - private extern (C) static void staticSlotCallback(void* qObjectPtr, - void* rawSlotName, - int numParameters, - void** parametersArray) - { - QVariant[] parameters = new QVariant[numParameters]; - for (int i = 0; i < numParameters; ++i) - parameters[i] = new QVariant(parametersArray[i]); - QObject qObject = cast(QObject) qObjectPtr; - QVariant slotName = new QVariant(rawSlotName); - qObject.onSlotCalled(slotName, parameters); - } - protected void onSlotCalled(QVariant slotName, QVariant[] parameters) { } @@ -107,5 +102,19 @@ public class QObject array.ptr); } - private void* data; + protected extern (C) static void staticSlotCallback(void* qObjectPtr, + void* rawSlotName, + int numParameters, + void** parametersArray) + { + QVariant[] parameters = new QVariant[numParameters]; + for (int i = 0; i < numParameters; ++i) + parameters[i] = new QVariant(parametersArray[i]); + QObject qObject = cast(QObject) qObjectPtr; + QVariant slotName = new QVariant(rawSlotName); + qObject.onSlotCalled(slotName, parameters); + } + + protected void* data; + private bool disableDosCalls; } From 40544b416800e1b6249be2c502efef4c7269d43d Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Fri, 1 May 2015 18:54:32 +0200 Subject: [PATCH 6/8] [DQml] Initial support for QAbstractListModels --- D/DQml/dothersideinterface.di | 7 ++ D/DQml/qabstractlistmodel.d | 187 +++++++++++++++++++++++++++------ D/DQml/qhashintbytearray.d | 25 +++-- D/DQml/qmodelindex.d | 9 +- D/DQml/qobject.d | 29 ++--- D/DQml/qqmlapplicationengine.d | 23 ++-- D/DQml/qqmlcontext.d | 14 +-- D/DQml/qquickview.d | 29 ++--- D/DQml/qvariant.d | 50 ++++----- 9 files changed, 261 insertions(+), 112 deletions(-) diff --git a/D/DQml/dothersideinterface.di b/D/DQml/dothersideinterface.di index 95cb4ef..d6081db 100644 --- a/D/DQml/dothersideinterface.di +++ b/D/DQml/dothersideinterface.di @@ -114,6 +114,13 @@ extern(C) void function (void*, void*), void function (void*, void*, ref int), void function (void*, int, int, int, void*)); + void dos_qabstractlistmodel_beginInsertRows(void* vptr, void* parent, int first, int last); + void dos_qabstractlistmodel_endInsertRows(void* vptr); + void dos_qabstractlistmodel_beginRemoveRows(void* vptr, void* parent, int first, int last); + void dos_qabstractlistmodel_endRemoveRows(void* vptr); + void dos_qabstractlistmodel_beginResetModel(void* vptr); + void dos_qabstractlistmodel_endResetModel(void* vptr); + void dos_qabstractlistmodel_dataChanged(void* vptr, void* topLeft, void* bottomRight, int* rolesPtr, int rolesLength); void dos_qabstractlistmodel_delete(void*); } diff --git a/D/DQml/qabstractlistmodel.d b/D/DQml/qabstractlistmodel.d index 42009ed..ed3c26f 100644 --- a/D/DQml/qabstractlistmodel.d +++ b/D/DQml/qabstractlistmodel.d @@ -1,12 +1,15 @@ import dothersideinterface; import qobject; +import qmodelindex; +import qvariant; +import std.string; class QAbstractListModel : QObject { this() { super(true); - dos_qabstractlistmodel_create(this.data, + dos_qabstractlistmodel_create(this.vptr, cast(void*)this, &staticSlotCallback, &rowCountCallback, @@ -20,45 +23,161 @@ class QAbstractListModel : QObject ~this() { - dos_qabstractlistmodel_delete(data); + dos_qabstractlistmodel_delete(this.vptr); } - protected extern (C) static void rowCountCallback(void* modelPtr, - void* indexPtr, - ref int result) - {} + public int rowCount(QModelIndex parentIndex) + { + return 0; + } - protected extern (C) static void columnCountCallback(void* modelPtr, - void* indexPtr, - ref int result) - {} + public int columnCount(QModelIndex parentIndex) + { + return 0; + } - protected extern (C) static void dataCallback(void* modelPtr, - void* indexPtr, - int role, - void* result) - {} + public QVariant data(QModelIndex index, int role) + { + return null; + } - protected extern (C) static void setDataCallback(void* modelPtr, - void* indexPtr, - void* valuePtr, - int role, - ref bool result) - {} + public bool setData(QModelIndex index, QVariant value, int role) + { + return false; + } - protected extern (C) static void roleNamesCallback(void* modelPtr, - void* result) - {} - - protected extern (C) static void flagsCallback(void* modelPtr, - void* index, - ref int result) - {} + public string[int] roleNames() + { + return null; + } - protected extern (C) static void headerDataCallback(void* modelPtr, - int section, - int orientation, + public int flags(QModelIndex index) + { + return 0; + } + + public QVariant headerData(int section, int orienation, int role) + { + return null; + } + + protected final void beginInsertRows(QModelIndex parent, int first, int last) + { + dos_qabstractlistmodel_beginInsertRows(this.vptr, + parent.voidPointer(), + first, + last); + } + + protected final void endInsertRows() + { + dos_qabstractlistmodel_endInsertRows(this.vptr); + } + + protected final void beginRemoveRows(QModelIndex parent, int first, int last) + { + dos_qabstractlistmodel_beginRemoveRows(this.vptr, + parent.voidPointer(), + first, + last); + } + + protected final void endRemoveRows() + { + dos_qabstractlistmodel_endRemoveRows(this.vptr); + } + + protected final void beginResetModel() + { + dos_qabstractlistmodel_beginResetModel(this.vptr); + } + + protected final void endResetModel() + { + dos_qabstractlistmodel_endResetModel(this.vptr); + } + + protected final void dataChanged(QModelIndex topLeft, QModelIndex bottomRight, int[] roles) + { + dos_qabstractlistmodel_dataChanged(this.vptr, + topLeft.voidPointer(), + bottomRight.voidPointer(), + roles.ptr, + cast(int)(roles.length)); + } + + private extern (C) static void rowCountCallback(void* modelPtr, + void* indexPtr, + ref int result) + { + auto model = cast(QAbstractListModel)(modelPtr); + auto index = new QModelIndex(indexPtr); + result = model.rowCount(index); + } + + private extern (C) static void columnCountCallback(void* modelPtr, + void* indexPtr, + ref int result) + { + auto model = cast(QAbstractListModel)(modelPtr); + auto index = new QModelIndex(indexPtr); + result = model.columnCount(index); + } + + private extern (C) static void dataCallback(void* modelPtr, + void* indexPtr, + int role, + void* result) + { + auto model = cast(QAbstractListModel)(modelPtr); + auto index = new QModelIndex(indexPtr); + auto value = model.data(index, role); + if (value is null) + return; + dos_qvariant_assign(result, value.voidPointer()); + } + + private extern (C) static void setDataCallback(void* modelPtr, + void* indexPtr, + void* valuePtr, int role, - void* result) - {} + ref bool result) + { + auto model = cast(QAbstractListModel)(modelPtr); + auto index = new QModelIndex(indexPtr); + auto value = new QVariant(valuePtr); + result = model.setData(index, value, role); + } + + private extern (C) static void roleNamesCallback(void* modelPtr, + void* result) + { + auto model = cast(QAbstractListModel)(modelPtr); + auto roles = model.roleNames(); + foreach(int key; roles.keys) { + dos_qhash_int_qbytearray_insert(result, key, roles[key].toStringz()); + } + } + + private extern (C) static void flagsCallback(void* modelPtr, + void* indexPtr, + ref int result) + { + auto model = cast(QAbstractListModel)(modelPtr); + auto index = new QModelIndex(indexPtr); + result = model.flags(index); + } + + private extern (C) static void headerDataCallback(void* modelPtr, + int section, + int orientation, + int role, + void* result) + { + auto model = cast(QAbstractListModel)(modelPtr); + QVariant value = model.headerData(section, orientation, role); + if (value is null) + return; + dos_qvariant_assign(result, value.voidPointer()); + } } diff --git a/D/DQml/qhashintbytearray.d b/D/DQml/qhashintbytearray.d index da8f714..ff59f4e 100644 --- a/D/DQml/qhashintbytearray.d +++ b/D/DQml/qhashintbytearray.d @@ -6,32 +6,37 @@ class QHashIntByteArray { this() { - dos_qhash_int_qbytearray_create(data); + dos_qhash_int_qbytearray_create(this.vptr); + } + + this(void* vptr) + { + this.vptr = vptr; } ~this() { - dos_qhash_int_qbytearray_delete(data); + dos_qhash_int_qbytearray_delete(this.vptr); + } + + public void* voidPointer() + { + return vptr; } public void insert(int key, string value) { - dos_qhash_int_qbytearray_insert(key, value.toStringz()); + dos_qhash_int_qbytearray_insert(this.vptr, key, value.toStringz()); } public string value(int key) { char* array; - dos_qhash_int_qbytearray_value(key, array); + dos_qhash_int_qbytearray_value(this.vptr, key, array); string result = fromStringz(array).dup; dos_chararray_delete(array); return result; } - public void* rawData() - { - return data; - } - - private void* data; + private void* vptr; } diff --git a/D/DQml/qmodelindex.d b/D/DQml/qmodelindex.d index d340185..3b9ac00 100644 --- a/D/DQml/qmodelindex.d +++ b/D/DQml/qmodelindex.d @@ -8,13 +8,18 @@ class QModelIndex dos_qmodelindex_create(this.ptr); } + this(void* ptr) + { + this.ptr = ptr; + } + ~this() { dos_qmodelindex_delete(this.ptr); ptr = null; } - public void* internalPtr() + public void* voidPointer() { return this.ptr; } @@ -43,7 +48,7 @@ class QModelIndex public QVariant data(int role) { auto result = new QVariant(); - dos_qmodelindex_data(this.ptr, role, result.rawData()); + dos_qmodelindex_data(this.ptr, role, result.voidPointer()); return result; } diff --git a/D/DQml/qobject.d b/D/DQml/qobject.d index 160f04d..8ce1550 100644 --- a/D/DQml/qobject.d +++ b/D/DQml/qobject.d @@ -20,18 +20,21 @@ public class QObject { this.disableDosCalls = disableDosCalls; if (!this.disableDosCalls) - dos_qobject_create(this.data, cast(void*)this, &staticSlotCallback); + dos_qobject_create(this.vptr, cast(void*)this, &staticSlotCallback); } ~this() { if (!this.disableDosCalls) - dos_qobject_delete(this.data); + { + dos_qobject_delete(this.vptr); + this.vptr = null; + } } - public void* rawData() + public void* voidPointer() { - return this.data; + return this.vptr; } protected void onSlotCalled(QVariant slotName, QVariant[] parameters) @@ -43,7 +46,7 @@ public class QObject int index = -1; int length = cast(int)types.length; int[] array = to!(int[])(types); - dos_qobject_slot_create(this.data, + dos_qobject_slot_create(this.vptr, name.toStringz(), length, array.ptr, @@ -55,7 +58,7 @@ public class QObject int index = -1; int length = cast(int)types.length; int[] array = length > 0 ? to!(int[])(types) : null; - dos_qobject_signal_create(this.data, + dos_qobject_signal_create(this.vptr, name.toStringz(), length, array.ptr, @@ -68,7 +71,7 @@ public class QObject string writeSlotName, string notifySignalName) { - dos_qobject_property_create(this.data, + dos_qobject_property_create(this.vptr, name.toStringz(), type, readSlotName.toStringz(), @@ -94,18 +97,18 @@ public class QObject if (length > 0) { array = new void*[length]; foreach (int i, QVariant v; arguments) - array[i] = v.rawData(); + array[i] = v.voidPointer(); } - dos_qobject_signal_emit(this.data, + dos_qobject_signal_emit(this.vptr, signalName.toStringz(), length, array.ptr); } protected extern (C) static void staticSlotCallback(void* qObjectPtr, - void* rawSlotName, - int numParameters, - void** parametersArray) + void* rawSlotName, + int numParameters, + void** parametersArray) { QVariant[] parameters = new QVariant[numParameters]; for (int i = 0; i < numParameters; ++i) @@ -115,6 +118,6 @@ public class QObject qObject.onSlotCalled(slotName, parameters); } - protected void* data; + protected void* vptr; private bool disableDosCalls; } diff --git a/D/DQml/qqmlapplicationengine.d b/D/DQml/qqmlapplicationengine.d index 4015108..0a71c34 100644 --- a/D/DQml/qqmlapplicationengine.d +++ b/D/DQml/qqmlapplicationengine.d @@ -4,27 +4,32 @@ import std.string; class QQmlApplicationEngine { - public this() + this() { - dos_qqmlapplicationengine_create(data); + dos_qqmlapplicationengine_create(this.vptr); } - public ~this() + ~this() { - dos_qqmlapplicationengine_delete(data); + dos_qqmlapplicationengine_delete(this.vptr); + } + + public void* voidPointer() + { + return this.vptr; } public QQmlContext rootContext() { - void* contextData; - dos_qqmlapplicationengine_context(data, contextData); - return new QQmlContext(contextData); + void* contextVPtr; + dos_qqmlapplicationengine_context(this.vptr, contextVPtr); + return new QQmlContext(contextVPtr); } public void load(string filename) { - dos_qqmlapplicationengine_load(data, filename.toStringz()); + dos_qqmlapplicationengine_load(this.vptr, filename.toStringz()); } - private void* data; + private void* vptr; } diff --git a/D/DQml/qqmlcontext.d b/D/DQml/qqmlcontext.d index 15763d5..076318a 100644 --- a/D/DQml/qqmlcontext.d +++ b/D/DQml/qqmlcontext.d @@ -4,20 +4,20 @@ import std.string; class QQmlContext { - public this(void* data) + public this(void* ptr) { - this.data = data; + this.vptr = vptr; } - public void* rawData() + public void* voidPointer() { - return data; + return vptr; } public string baseUrl() { char* array; - dos_qqmlcontext_baseUrl(data, array); + dos_qqmlcontext_baseUrl(vptr, array); string result = fromStringz(array).dup; dos_chararray_delete(array); return result; @@ -25,8 +25,8 @@ class QQmlContext public void setContextProperty(string name, QVariant value) { - dos_qqmlcontext_setcontextproperty(data, name.ptr, value.rawData()); + dos_qqmlcontext_setcontextproperty(vptr, name.ptr, value.voidPointer()); } - private void* data; + private void* vptr; } diff --git a/D/DQml/qquickview.d b/D/DQml/qquickview.d index c5f0b34..487a47e 100644 --- a/D/DQml/qquickview.d +++ b/D/DQml/qquickview.d @@ -6,40 +6,45 @@ class QQuickView { this() { - dos_qquickview_create(data); + dos_qquickview_create(this.vptr); } ~this() { - dos_qquickview_delete(data); + dos_qquickview_delete(this.vptr); } - - void show() + + public void* voidPointer() { - dos_qquickview_show(data); + return this.vptr; } - QQmlContext rootContext() + public void show() + { + dos_qquickview_show(this.vptr); + } + + public QQmlContext rootContext() { void* contextData; - dos_qquickview_rootContext(data, contextData); + dos_qquickview_rootContext(this.vptr, contextData); return new QQmlContext(contextData); } - string source() + public string source() { char* array; - dos_qquickview_source(data, array); + dos_qquickview_source(this.vptr, array); string result = fromStringz(array).dup; dos_chararray_delete(array); return result; } - void setSource(string filename) + public void setSource(string filename) { immutable(char)* filenameAsCString = filename.toStringz(); - dos_qquickview_set_source(data, filenameAsCString); + dos_qquickview_set_source(this.vptr, filenameAsCString); } - private void* data; + private void* vptr; } diff --git a/D/DQml/qvariant.d b/D/DQml/qvariant.d index bf44eba..25b2205 100644 --- a/D/DQml/qvariant.d +++ b/D/DQml/qvariant.d @@ -6,84 +6,84 @@ class QVariant { public this() { - dos_qvariant_create(this.data); + dos_qvariant_create(this.vptr); } public this(int value) { - dos_qvariant_create_int(this.data, value); + dos_qvariant_create_int(this.vptr, value); } public this(bool value) { - dos_qvariant_create_bool(this.data, value); + dos_qvariant_create_bool(this.vptr, value); } public this(string value) { - dos_qvariant_create_string(this.data, value.toStringz()); + dos_qvariant_create_string(this.vptr, value.toStringz()); } public this(float value) { - dos_qvariant_create_float(this.data, value); + dos_qvariant_create_float(this.vptr, value); } public this(double value) { - dos_qvariant_create_double(this.data, value); + dos_qvariant_create_double(this.vptr, value); } public this(QObject value) { - dos_qvariant_create_qobject(this.data, value.rawData()); + dos_qvariant_create_qobject(this.vptr, value.voidPointer()); } - public this(void* data, bool hasOwnership = false) + public this(void* vptr, bool hasOwnership = false) { - this.data = data; + this.vptr = vptr; this.hasOwnership = hasOwnership; } ~this() { if (this.hasOwnership) - dos_qvariant_delete(this.data); + dos_qvariant_delete(this.vptr); } - public void* rawData() + public void* voidPointer() { - return data; + return this.vptr; } public void setValue(int value) { - dos_qvariant_setInt(this.data, value); + dos_qvariant_setInt(this.vptr, value); } public void setValue(bool value) { - dos_qvariant_setBool(this.data, value); + dos_qvariant_setBool(this.vptr, value); } public void setValue(string value) { - dos_qvariant_setString(this.data, value.toStringz()); + dos_qvariant_setString(this.vptr, value.toStringz()); } public void setValue(QObject value) { - dos_qvariant_setQObject(this.data, value.rawData()); + dos_qvariant_setQObject(this.vptr, value.voidPointer()); } public void setValue(float value) { - dos_qvariant_setFloat(this.data, value); + dos_qvariant_setFloat(this.vptr, value); } public void setValue(double value) { - dos_qvariant_setDouble(this.data, value); + dos_qvariant_setDouble(this.vptr, value); } public void getValue(ref int value) @@ -114,47 +114,47 @@ class QVariant public bool isNull() { bool result; - dos_qvariant_isnull(this.data, result); + dos_qvariant_isnull(this.vptr, result); return result; } public bool toBool() { bool result; - dos_qvariant_toBool(this.data, result); + dos_qvariant_toBool(this.vptr, result); return result; } public int toInt() { int result; - dos_qvariant_toInt(this.data, result); + dos_qvariant_toInt(this.vptr, result); return result; } public float toFloat() { float result; - dos_qvariant_toFloat(this.data, result); + dos_qvariant_toFloat(this.vptr, result); return result; } public double toDouble() { double result; - dos_qvariant_toDouble(this.data, result); + dos_qvariant_toDouble(this.vptr, result); return result; } public override string toString() { char* array; - dos_qvariant_toString(this.data, array); + dos_qvariant_toString(this.vptr, array); string result = fromStringz(array).dup; dos_chararray_delete(array); return result; } - private void* data = null; + private void* vptr = null; private bool hasOwnership = true; } From 7c9453800e1640c6a9a34262a718a4717f2123f9 Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Fri, 1 May 2015 19:34:36 +0200 Subject: [PATCH 7/8] [DQml] Ported the AbstractItemModel example from Nim to D --- D/DQml/qabstractlistmodel.d | 2 +- D/DQml/qqmlcontext.d | 6 +-- D/Examples/AbstractItemModel/CMakeLists.txt | 11 +++++ D/Examples/AbstractItemModel/main.d | 26 +++++++++++ D/Examples/AbstractItemModel/main.qml | 25 +++++++++++ D/Examples/AbstractItemModel/model.d | 48 +++++++++++++++++++++ D/Examples/CMakeLists.txt | 3 +- 7 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 D/Examples/AbstractItemModel/CMakeLists.txt create mode 100644 D/Examples/AbstractItemModel/main.d create mode 100644 D/Examples/AbstractItemModel/main.qml create mode 100644 D/Examples/AbstractItemModel/model.d diff --git a/D/DQml/qabstractlistmodel.d b/D/DQml/qabstractlistmodel.d index ed3c26f..50e6ac6 100644 --- a/D/DQml/qabstractlistmodel.d +++ b/D/DQml/qabstractlistmodel.d @@ -33,7 +33,7 @@ class QAbstractListModel : QObject public int columnCount(QModelIndex parentIndex) { - return 0; + return 1; } public QVariant data(QModelIndex index, int role) diff --git a/D/DQml/qqmlcontext.d b/D/DQml/qqmlcontext.d index 076318a..dc1f53e 100644 --- a/D/DQml/qqmlcontext.d +++ b/D/DQml/qqmlcontext.d @@ -4,7 +4,7 @@ import std.string; class QQmlContext { - public this(void* ptr) + this(void* vptr) { this.vptr = vptr; } @@ -17,7 +17,7 @@ class QQmlContext public string baseUrl() { char* array; - dos_qqmlcontext_baseUrl(vptr, array); + dos_qqmlcontext_baseUrl(this.vptr, array); string result = fromStringz(array).dup; dos_chararray_delete(array); return result; @@ -25,7 +25,7 @@ class QQmlContext public void setContextProperty(string name, QVariant value) { - dos_qqmlcontext_setcontextproperty(vptr, name.ptr, value.voidPointer()); + dos_qqmlcontext_setcontextproperty(this.vptr, name.ptr, value.voidPointer()); } private void* vptr; diff --git a/D/Examples/AbstractItemModel/CMakeLists.txt b/D/Examples/AbstractItemModel/CMakeLists.txt new file mode 100644 index 0000000..55c948f --- /dev/null +++ b/D/Examples/AbstractItemModel/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) +project (DQmlAbstractItemModel C D ) +add_executable(${PROJECT_NAME} main.d model.d) +target_include_directories(${PROJECT_NAME} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../../DQml" + ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${PROJECT_NAME} PRIVATE + "${CMAKE_CURRENT_BINARY_DIR}/../../../DOtherSide/DOtherSide/libDOtherSide.so" + "${CMAKE_CURRENT_BINARY_DIR}/../../DQml/libDQml.a" +) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) \ No newline at end of file diff --git a/D/Examples/AbstractItemModel/main.d b/D/Examples/AbstractItemModel/main.d new file mode 100644 index 0000000..6c135f7 --- /dev/null +++ b/D/Examples/AbstractItemModel/main.d @@ -0,0 +1,26 @@ +import dqml; +import model; + +void main() +{ + try + { + auto app = new QGuiApplication(); + scope(exit) destroy(app); + + auto model = new ListModel(); + scope(exit) destroy(model); + + auto variant = new QVariant(); + variant.setValue(model); + + auto engine = new QQmlApplicationEngine(); + scope(exit) destroy(engine); + + engine.rootContext().setContextProperty("myListModel", variant); + engine.load("main.qml"); + app.exec(); + } + catch + {} +} diff --git a/D/Examples/AbstractItemModel/main.qml b/D/Examples/AbstractItemModel/main.qml new file mode 100644 index 0000000..d3aa3a3 --- /dev/null +++ b/D/Examples/AbstractItemModel/main.qml @@ -0,0 +1,25 @@ +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 + Label { text: "Name:" + name } + } + + ListView + { + anchors.fill: parent + model: myListModel + delegate: myListModelDelegate + } +} \ No newline at end of file diff --git a/D/Examples/AbstractItemModel/model.d b/D/Examples/AbstractItemModel/model.d new file mode 100644 index 0000000..22c5adc --- /dev/null +++ b/D/Examples/AbstractItemModel/model.d @@ -0,0 +1,48 @@ +import dqml; +import std.stdio; + + +class ListModel : QAbstractListModel +{ + this() + { + this.names = ["John", "Max", "Paul", "Anna"]; + this.roles[Roles.Name] = "name"; + } + + public override int rowCount(QModelIndex parent = null) + { + if (parent && parent.isValid()) + return 0; + return cast(int)(names.length); + } + + public override QVariant data(QModelIndex index, int role) + { + if (index is null) + return null; + + if (!index.isValid()) + return null; + + if (index.row() < 0 || index.row() >= rowCount()) + return null; + + switch(role) + { + case Roles.Name: + return new QVariant(names[index.row]); + default: + return null; + } + } + + public override string[int] roleNames() + { + return roles; + } + + private string[] names; + private string[int] roles; + private enum Roles : int { Name = 0 }; +} diff --git a/D/Examples/CMakeLists.txt b/D/Examples/CMakeLists.txt index 0161e19..1c6e188 100644 --- a/D/Examples/CMakeLists.txt +++ b/D/Examples/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(HelloWorld) add_subdirectory(SimpleData) -add_subdirectory(SlotsAndProperties) \ No newline at end of file +add_subdirectory(SlotsAndProperties) +add_subdirectory(AbstractItemModel) \ No newline at end of file From 15b9cccb0f36b0d9da59db25ec6a214d17bfa364 Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Sat, 2 May 2015 13:29:32 +0200 Subject: [PATCH 8/8] [DQml] Ported the ContactApp example from NimQml --- D/Examples/CMakeLists.txt | 3 +- D/Examples/ContactApp/CMakeLists.txt | 11 +++ D/Examples/ContactApp/applicationlogic.d | 60 +++++++++++++++ D/Examples/ContactApp/contact.d | 70 ++++++++++++++++++ D/Examples/ContactApp/contactlist.d | 92 +++++++++++++++++++++++ D/Examples/ContactApp/main.d | 26 +++++++ D/Examples/ContactApp/main.qml | 93 ++++++++++++++++++++++++ D/Examples/SlotsAndProperties/contact.d | 2 +- 8 files changed, 355 insertions(+), 2 deletions(-) create mode 100644 D/Examples/ContactApp/CMakeLists.txt create mode 100644 D/Examples/ContactApp/applicationlogic.d create mode 100644 D/Examples/ContactApp/contact.d create mode 100644 D/Examples/ContactApp/contactlist.d create mode 100644 D/Examples/ContactApp/main.d create mode 100644 D/Examples/ContactApp/main.qml diff --git a/D/Examples/CMakeLists.txt b/D/Examples/CMakeLists.txt index 1c6e188..e1a0218 100644 --- a/D/Examples/CMakeLists.txt +++ b/D/Examples/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(HelloWorld) add_subdirectory(SimpleData) add_subdirectory(SlotsAndProperties) -add_subdirectory(AbstractItemModel) \ No newline at end of file +add_subdirectory(AbstractItemModel) +add_subdirectory(ContactApp) \ No newline at end of file diff --git a/D/Examples/ContactApp/CMakeLists.txt b/D/Examples/ContactApp/CMakeLists.txt new file mode 100644 index 0000000..30b2ad5 --- /dev/null +++ b/D/Examples/ContactApp/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) +project (DQmlContactApp C D ) +add_executable(${PROJECT_NAME} main.d applicationlogic.d contact.d contactlist.d) +target_include_directories(${PROJECT_NAME} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../../DQml" + ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${PROJECT_NAME} PRIVATE + "${CMAKE_CURRENT_BINARY_DIR}/../../../DOtherSide/DOtherSide/libDOtherSide.so" + "${CMAKE_CURRENT_BINARY_DIR}/../../DQml/libDQml.a" +) +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/main.qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) \ No newline at end of file diff --git a/D/Examples/ContactApp/applicationlogic.d b/D/Examples/ContactApp/applicationlogic.d new file mode 100644 index 0000000..2a2c2d3 --- /dev/null +++ b/D/Examples/ContactApp/applicationlogic.d @@ -0,0 +1,60 @@ +import dqml; +import contactlist; +import std.stdio; + +class ApplicationLogic : QObject +{ + this(QApplication app) + { + this.m_app = app; + this.m_contactList = new ContactList(); + this.registerSlot("contactList", [QMetaType.QObject]); + this.registerSlot("onLoadTriggered", [QMetaType.Void]); + this.registerSlot("onSaveTriggered", [QMetaType.Void]); + this.registerSlot("onExitTriggered", [QMetaType.Void]); + this.registerProperty("contactList", QMetaType.QObject, "contactList", null, null); + } + + public ContactList contactList() + { + return this.m_contactList; + } + + public void onLoadTriggered() + { + writefln("Load Triggered"); + } + + public void onSaveTriggered() + { + writefln("Save Triggered"); + } + + public void onExitTriggered() + { + this.m_app.quit(); + } + + protected override void onSlotCalled(QVariant slotName, QVariant[] arguments) + { + switch(slotName.toString()) + { + case "contactList": + return arguments[0].setValue(contactList()); + case "onExitTriggered": + onExitTriggered(); + break; + case "onSaveTriggered": + onSaveTriggered(); + break; + case "onLoadTriggered": + onLoadTriggered(); + break; + default: + break; + } + } + + private QApplication m_app; + private ContactList m_contactList; +} diff --git a/D/Examples/ContactApp/contact.d b/D/Examples/ContactApp/contact.d new file mode 100644 index 0000000..900c7cd --- /dev/null +++ b/D/Examples/ContactApp/contact.d @@ -0,0 +1,70 @@ +import dqml; + +class Contact : QObject +{ + this(string firstName = "", string lastName = "") + { + this.m_firstName = firstName; + this.m_lastName = lastName; + this.registerSlot("firstName", [QMetaType.String]); + this.registerSlot("setFirstName", [QMetaType.Void, QMetaType.String]); + this.registerSignal("firstNameChanged", [QMetaType.String]); + this.registerSlot("lastName", [QMetaType.String]); + this.registerSlot("setLastName", [QMetaType.Void, QMetaType.String]); + this.registerSignal("lastNameChanged", [QMetaType.String]); + this.registerProperty("firstName", QMetaType.String, "firstName", "setFirstName", "firstNameChanged"); + this.registerProperty("lastName", QMetaType.String, "lastName", "setLastName", "lastNameChanged"); + } + + public string firstName() + { + return this.m_firstName; + } + + public void setFirstName(string firstName) + { + if (this.m_firstName != firstName) + { + this.m_firstName = firstName; + emit("firstNameChanged", firstName); + } + } + + public string lastName() + { + return this.m_lastName; + } + + public void setLastName(string lastName) + { + if (this.m_lastName != lastName) + { + this.m_lastName = lastName; + emit ("lastNameChanged", lastName); + } + } + + protected override void onSlotCalled(QVariant slotName, QVariant[] arguments) + { + switch (slotName.toString()) + { + case "firstName": + arguments[0].setValue(firstName()); + break; + case "setFirstName": + setFirstName(arguments[1].toString()); + break; + case "lastName": + arguments[0].setValue(lastName()); + break; + case "setLastName": + setLastName(arguments[1].toString()); + break; + default: + break; + } + } + + private string m_firstName; + private string m_lastName; +} diff --git a/D/Examples/ContactApp/contactlist.d b/D/Examples/ContactApp/contactlist.d new file mode 100644 index 0000000..f4329b7 --- /dev/null +++ b/D/Examples/ContactApp/contactlist.d @@ -0,0 +1,92 @@ +import dqml; +import contact; +import std.stdio; +import std.algorithm; + +class ContactList : QAbstractListModel +{ + this() + { + this.m_contacts = []; + this.m_roleNames[Roles.FirstName] = "firstName"; + this.m_roleNames[Roles.LastName] = "lastName"; + this.registerSlot("add", [QMetaType.Void, QMetaType.String, QMetaType.String]); + this.registerSlot("del", [QMetaType.Void, QMetaType.Int]); + } + + public override int rowCount(QModelIndex parent = null) + { + if (parent && parent.isValid()) + return 0; + return cast(int)(this.m_contacts.length); + } + + public override QVariant data(QModelIndex index, int role) + { + if (index is null) + return null; + + if (!index.isValid()) + return null; + + if (index.row() < 0 || index.row() >= rowCount()) + return null; + + auto contact = this.m_contacts[index.row]; + + switch(role) + { + case Roles.FirstName: + return new QVariant(contact.firstName()); + case Roles.LastName: + return new QVariant(contact.lastName()); + default: + return null; + } + } + + public override string[int] roleNames() + { + return this.m_roleNames; + } + + public void add(string firstName, string lastName) + { + auto index = new QModelIndex(); + auto pos = rowCount(); + writefln("Adding %s %s in pos %d", firstName, lastName, pos); + beginInsertRows(index, pos, pos); + this.m_contacts ~= new Contact(firstName, lastName); + endInsertRows(); + } + + public void del(int pos) + { + if (pos < 0 || pos >= rowCount()) + return; + auto index = new QModelIndex(); + writefln("Removing at pos %d", pos); + beginRemoveRows(index, pos, pos); + this.m_contacts = remove(this.m_contacts, pos); + endRemoveRows(); + } + + protected override void onSlotCalled(QVariant slotName, QVariant[] arguments) + { + switch (slotName.toString()) + { + case "add": + add(arguments[1].toString(), arguments[2].toString()); + break; + case "del": + del(arguments[1].toInt()); + break; + default: + break; + } + } + + private Contact[] m_contacts; + private string[int] m_roleNames; + private enum Roles : int { FirstName = 0, LastName}; +} diff --git a/D/Examples/ContactApp/main.d b/D/Examples/ContactApp/main.d new file mode 100644 index 0000000..a4a9f50 --- /dev/null +++ b/D/Examples/ContactApp/main.d @@ -0,0 +1,26 @@ +import dqml; +import applicationlogic; + +void main() +{ + try + { + auto app = new QApplication(); + scope(exit) destroy(app); + + auto logic = new ApplicationLogic(app); + scope(exit) destroy(logic); + + auto variant = new QVariant(); + variant.setValue(logic); + + auto engine = new QQmlApplicationEngine(); + scope(exit) destroy(engine); + + engine.rootContext().setContextProperty("logic", variant); + engine.load("main.qml"); + app.exec(); + } + catch + {} +} diff --git a/D/Examples/ContactApp/main.qml b/D/Examples/ContactApp/main.qml new file mode 100644 index 0000000..79786e1 --- /dev/null +++ b/D/Examples/ContactApp/main.qml @@ -0,0 +1,93 @@ +import QtQuick 2.2 +import QtQuick.Controls 1.2 +import QtQuick.Layouts 1.1 +import QtQuick.Window 2.1 + +ApplicationWindow { + + width: 500 + height: 300 + title: "ContactApp" + visible: true + + menuBar: MenuBar { + Menu { + title: "&File" + MenuItem { text: "&Load"; onTriggered: logic.onLoadTriggered() } + MenuItem { text: "&Save"; onTriggered: logic.onSaveTriggered() } + MenuItem { text: "&Exit"; onTriggered: logic.onExitTriggered() } + } + } + + ColumnLayout { + anchors.fill: parent + + Component { + id: tableTextDelegate + Label { + id: tableTextDelegateInstance + property var styleData: undefined + states: State { + when: styleData !== undefined + PropertyChanges { + target: tableTextDelegateInstance; + text: styleData.value; + color: styleData.textColor + } + } + } + } + + Component { + id: tableButtonDelegate + Button { + id: tableButtonDelegateInstance + property var styleData: undefined + text: "Delete" + onClicked: logic.contactList.del(styleData.row) + } + } + + Component { + id: tableItemDelegate + Loader { + id: tableItemDelegateInstance + sourceComponent: { + if (styleData.column === 0 || styleData.column === 1) + return tableTextDelegate + else if (styleData.column === 2) + return tableButtonDelegate + else + return tableTextDelegate + } + Binding { + target: tableItemDelegateInstance.item + property: "styleData" + value: styleData + } + } + } + + TableView { + model: logic.contactList + Layout.fillWidth: true + Layout.fillHeight: true + TableViewColumn { role: "firstName"; title: "FirstName"; width: 200 } + TableViewColumn { role: "lastName"; title: "LastName"; width: 200} + TableViewColumn { width: 100; } + itemDelegate: tableItemDelegate + } + + RowLayout { + Label { text: "FirstName" } + TextField { id: nameTextField; Layout.fillWidth: true; text: "" } + Label { text: "LastName" } + TextField { id: surnameTextField; Layout.fillWidth: true; text: "" } + Button { + text: "Add" + onClicked: logic.contactList.add(nameTextField.text, surnameTextField.text) + enabled: nameTextField.text !== "" && surnameTextField.text !== "" + } + } + } +} diff --git a/D/Examples/SlotsAndProperties/contact.d b/D/Examples/SlotsAndProperties/contact.d index 318b937..f92cd2f 100644 --- a/D/Examples/SlotsAndProperties/contact.d +++ b/D/Examples/SlotsAndProperties/contact.d @@ -27,7 +27,7 @@ class Contact : QObject } } - override protected void onSlotCalled(QVariant slotName, QVariant[] arguments) + protected override void onSlotCalled(QVariant slotName, QVariant[] arguments) { switch (slotName.toString()) {