From ea57ce756ad4b80fdbecc13ba222b3e43af98f13 Mon Sep 17 00:00:00 2001 From: Filippo Cucchetto Date: Sun, 10 May 2015 15:04:36 +0200 Subject: [PATCH] [DQml] Refactored the ContactApp example by using the QObject UDAs --- D/DQml/qabstractlistmodel.d | 1 + D/DQml/qobject.d | 4 ++- D/DQml/qobjectgenerators.d | 14 ++++++-- D/Examples/ContactApp/applicationlogic.d | 36 ++++++------------- D/Examples/ContactApp/contact.d | 46 +++++++++--------------- D/Examples/ContactApp/contactlist.d | 22 +++--------- 6 files changed, 47 insertions(+), 76 deletions(-) diff --git a/D/DQml/qabstractlistmodel.d b/D/DQml/qabstractlistmodel.d index 50e6ac6..dfd541c 100644 --- a/D/DQml/qabstractlistmodel.d +++ b/D/DQml/qabstractlistmodel.d @@ -19,6 +19,7 @@ class QAbstractListModel : QObject &roleNamesCallback, &flagsCallback, &headerDataCallback); + qobjectInit(); } ~this() diff --git a/D/DQml/qobject.d b/D/DQml/qobject.d index cbcaed5..6df1c1e 100644 --- a/D/DQml/qobject.d +++ b/D/DQml/qobject.d @@ -20,8 +20,10 @@ public class QObject { this.disableDosCalls = disableDosCalls; if (!this.disableDosCalls) + { dos_qobject_create(this.vptr, cast(void*)this, &staticSlotCallback); - qobjectInit(); + qobjectInit(); + } } ~this() diff --git a/D/DQml/qobjectgenerators.d b/D/DQml/qobjectgenerators.d index 08e1164..e0414e9 100644 --- a/D/DQml/qobjectgenerators.d +++ b/D/DQml/qobjectgenerators.d @@ -32,6 +32,10 @@ string GenerateVariantConversionCall(string typeName) return "toString()"; case "int": return "toInt()"; + case "bool": + return "toBool()"; + case "QVariant": + return ""; default: throw new Exception("error"); } @@ -116,6 +120,12 @@ string GenerateMetaType(string typeName) return "QMetaType.Int"; case "string": return "QMetaType.String"; + case "QObject": + return "QMetaType.QObject"; + case "QVariant": + return "QMetaType.QVariant"; + case "bool": + return "QMetaType.Bool"; default: throw new Exception(format("Unknown conversion from %s to QMetaType", typeName)); } @@ -145,7 +155,6 @@ string GenerateQObjectInit(QtInfo info) { string result = ""; result ~= "protected override void qobjectInit()\n"; - result ~= "{\n writeln(\"OK\");\n"; foreach (slot; info.slots) { @@ -198,7 +207,8 @@ mixin template InjectQObjectMacro() foreach (member; __traits(allMembers, T)) { - static if (isSomeFunction!(__traits(getMember, T, member))) + static if (__traits(compiles, __traits(getMember, T, member)) + && isSomeFunction!(__traits(getMember, T, member))) { // Retrieve the UDA auto attributes = __traits(getAttributes, __traits(getMember, T, member)); diff --git a/D/Examples/ContactApp/applicationlogic.d b/D/Examples/ContactApp/applicationlogic.d index 2a2c2d3..c667746 100644 --- a/D/Examples/ContactApp/applicationlogic.d +++ b/D/Examples/ContactApp/applicationlogic.d @@ -2,59 +2,43 @@ import dqml; import contactlist; import std.stdio; +@QtProperty(QObject.stringof, "contactList", "contactList", null, null) class ApplicationLogic : QObject { + mixin InjectQObjectMacro; + mixin(Q_OBJECT!(ApplicationLogic)); + 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() + + @QtSlot() + public QObject contactList() { return this.m_contactList; } + @QtSlot() public void onLoadTriggered() { writefln("Load Triggered"); } + @QtSlot() public void onSaveTriggered() { writefln("Save Triggered"); } + @QtSlot() 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 index 900c7cd..55eb289 100644 --- a/D/Examples/ContactApp/contact.d +++ b/D/Examples/ContactApp/contact.d @@ -1,69 +1,55 @@ import dqml; +@QtProperty(string.stringof, "firstName", "firstName", "setFirstName", "firstNameChanged") +@QtProperty(string.stringof, "lastName", "lastName", "setLastName", "lastNameChanged") class Contact : QObject { + mixin InjectQObjectMacro; + mixin(Q_OBJECT!(Contact)); + 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"); } + @QtSlot() public string firstName() { return this.m_firstName; } + @QtSlot() public void setFirstName(string firstName) { if (this.m_firstName != firstName) { this.m_firstName = firstName; - emit("firstNameChanged", firstName); + firstNameChanged(firstName); } } + @QtSignal() + public void firstNameChanged(string); + + @QtSlot() public string lastName() { return this.m_lastName; } + @QtSlot() public void setLastName(string lastName) { if (this.m_lastName != lastName) { this.m_lastName = lastName; - emit ("lastNameChanged", lastName); + 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; - } - } + @QtSignal() + public void lastNameChanged(string); private string m_firstName; private string m_lastName; diff --git a/D/Examples/ContactApp/contactlist.d b/D/Examples/ContactApp/contactlist.d index f4329b7..726a74c 100644 --- a/D/Examples/ContactApp/contactlist.d +++ b/D/Examples/ContactApp/contactlist.d @@ -5,13 +5,14 @@ import std.algorithm; class ContactList : QAbstractListModel { + mixin InjectQObjectMacro; + mixin(Q_OBJECT!(ContactList)); + 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) @@ -50,6 +51,7 @@ class ContactList : QAbstractListModel return this.m_roleNames; } + @QtSlot() public void add(string firstName, string lastName) { auto index = new QModelIndex(); @@ -60,6 +62,7 @@ class ContactList : QAbstractListModel endInsertRows(); } + @QtSlot() public void del(int pos) { if (pos < 0 || pos >= rowCount()) @@ -71,21 +74,6 @@ class ContactList : QAbstractListModel 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};