[DQml] Refactored the ContactApp example by using the QObject UDAs

This commit is contained in:
Filippo Cucchetto 2015-05-10 15:04:36 +02:00
parent afb08fe225
commit ea57ce756a
6 changed files with 47 additions and 76 deletions

View File

@ -19,6 +19,7 @@ class QAbstractListModel : QObject
&roleNamesCallback,
&flagsCallback,
&headerDataCallback);
qobjectInit();
}
~this()

View File

@ -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()

View File

@ -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));

View File

@ -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;
}

View File

@ -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;

View File

@ -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};