changeset 322:7c2cf27391c4 signals

A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
author eldar_ins@eldar-laptop
date Sun, 27 Dec 2009 21:13:38 +0500
parents d458ed66e871
children 7a3c43424dca
files examples/layouts/borderlayout/borderlayout.d generator/dgenerator.cpp generator/typesystem_core-java.java qt/core/QMetaObject.d qt/d2/qt/Signal.d qt/qtd/MOC.d
diffstat 6 files changed, 133 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/examples/layouts/borderlayout/borderlayout.d	Sat Dec 26 05:10:16 2009 +0500
+++ b/examples/layouts/borderlayout/borderlayout.d	Sun Dec 27 21:13:38 2009 +0500
@@ -75,40 +75,47 @@
         }
     }
 
+    void addWidget(QWidget widget, Position position)
+    {
+        add(cast(IQLayoutItem) new QWidgetItem(widget), position);
+    }
+
+    void add(IQLayoutItem item, Position position)
+    {
+        list ~= new ItemWrapper(item, position);
+    }
+
+override
+{
     void addItem(IQLayoutItem item)
     {
         add(item, Position.West);
     }
 
-    void addWidget(QWidget widget, Position position)
-    {
-        add(cast(IQLayoutItem) new QWidgetItem(widget), position);
-    }
-
-    int expandingDirections()
+    int expandingDirections() const
     {
         return Qt.Horizontal | Qt.Vertical;
     }
 
-    bool hasHeightForWidth()
+    bool hasHeightForWidth() const
     {
         return false;
     }
 
-    int count()
+    int count() const
     {
         return list.length;
     }
 
-    QLayoutItem itemAt(int index)
+    IQLayoutItem itemAt(int index) const
     {
         if(index >= 0 && index < list.length) 
-            return list[index].item;
+            return cast(IQLayoutItem) list[index].item;
         else
             return null;
     }
 
-    QSize minimumSize()
+    QSize minimumSize() const
     {
         return calculateSize(SizeType.MinimumSize);
     }
@@ -182,7 +189,7 @@
                 centerHeight));
     }
 
-    QSize sizeHint()
+    QSize sizeHint() const
     {
         return calculateSize(SizeType.SizeHint);
     }
@@ -195,11 +202,7 @@
         }
         return null;
     }
-
-    void add(IQLayoutItem item, Position position)
-    {
-        list ~= new ItemWrapper(item, position);
-    }
+}
 
 private:
 
@@ -217,12 +220,12 @@
 
     enum SizeType { MinimumSize, SizeHint };
 
-    QSize calculateSize(SizeType sizeType)
+    QSize calculateSize(SizeType sizeType) const
     {
         QSize totalSize;
 
         for (int i = 0; i < list.length; ++i) {
-            ItemWrapper wrapper = list[i];
+            const ItemWrapper wrapper = list[i];
             Position position = wrapper.position;
             QSize itemSize;
 
--- a/generator/dgenerator.cpp	Sat Dec 26 05:10:16 2009 +0500
+++ b/generator/dgenerator.cpp	Sun Dec 27 21:13:38 2009 +0500
@@ -2791,11 +2791,9 @@
       {
           AbstractMetaFunction *fn = signal_funcs.at(i);
   s << "        index = _staticMetaObject.indexOfMethod_Cpp(__signalSignatures[" << staticId << "]);" << endl
-    << "        _staticMetaObject.addMethod(new QMetaSignal(signature!(\"" << fn->name() << "\"";
-          if(fn->arguments().size()-j > 0)
-              s << ", ";
+    << "        _staticMetaObject.addMethod(new QMetaSignal(signature!(";
           writeMetaMethodArguments(s, fn, j);
-  s << "), index));" << endl << endl;
+  s << ")(\"" << fn->name() << "\"), index));" << endl << endl;
           AbstractMetaArgumentList args = fn->arguments();
           if(args.size() && j<args.size())
               hasDefault = !args.at(args.size() - 1 - j)->defaultValueExpression().isEmpty();
@@ -2815,11 +2813,9 @@
       {
           AbstractMetaFunction *fn = slot_funcs.at(i);
   s << "        index = _staticMetaObject.indexOfMethod_Cpp(__slotSignatures[" << staticId << "]);" << endl
-    << "        _staticMetaObject.addMethod(new QMetaSlot(signature!(\"" << fn->name() << "\"";
-          if(fn->arguments().size()-j > 0)
-              s << ", ";
+    << "        _staticMetaObject.addMethod(new QMetaSlot(signature!(";
           writeMetaMethodArguments(s, fn, j);
-  s << "), index));" << endl << endl;
+  s << ")(\"" << fn->name() << "\"), index));" << endl << endl;
           AbstractMetaArgumentList args = fn->arguments();
           if(args.size() && j<args.size())
               hasDefault = !args.at(args.size() - 1 - j)->defaultValueExpression().isEmpty();
--- a/generator/typesystem_core-java.java	Sat Dec 26 05:10:16 2009 +0500
+++ b/generator/typesystem_core-java.java	Sun Dec 27 21:13:38 2009 +0500
@@ -155,11 +155,9 @@
         return result;
     }
     
-    static void connect(QObject sender, string signal, QObject receiver, string method)
+    static bool connect(QObject sender, string signal, QObject receiver, string method)
     {
-        int signalIndex = sender.metaObject.lookUpSignal(signal);
-        int methodIndex = receiver.metaObject.lookUpMethod(method);
-        QMetaObject.connect(sender, signalIndex, receiver, methodIndex);
+        return QMetaObject.connectImpl(sender, signal, receiver, method);
     }
 }// class
 
--- a/qt/core/QMetaObject.d	Sat Dec 26 05:10:16 2009 +0500
+++ b/qt/core/QMetaObject.d	Sun Dec 27 21:13:38 2009 +0500
@@ -5,6 +5,8 @@
 import qt.QtdObject;
 
 import std.algorithm;
+import std.string;
+import std.stdio;
 
 class Meta
 {
@@ -40,6 +42,15 @@
         signature = signature_;
         indexOfMethod = indexOfMethod_;
     }
+    
+    string args() const
+    {
+        int openBracket = indexOf(signature, '(');
+        if(signature.length - openBracket - 2 > 0)
+            return signature[openBracket + 1 .. $-1];
+        else
+            return "";
+    }
 }
 
 class QMetaSignal : QMetaMethod
@@ -161,27 +172,48 @@
         _methods ~= method_;
     }
     
-    int lookUpMethod(string slot)
+    QMetaMethod lookUpMethod(string slot)
     {
         foreach (method; _methods)
             if (method.signature == slot)
-                return method.indexOfMethod;
+                return method;
         if (_base)
             return _base.lookUpMethod(slot);
         else
-            return -1;
+            return null;
     }
     
-    int lookUpSignal(string signal)
+    QMetaSignal lookUpSignal(string signal)
     {
-//        auto signalBegin = signal[0..$-1];
         foreach (method; _methods)
             if (method.signature == signal && cast(QMetaSignal)method)
-                return method.indexOfMethod;
+                return cast(QMetaSignal)method;
         if (_base)
             return _base.lookUpSignal(signal);
         else
-            return -1;
+            return null;
+    }
+
+    QMetaMethod[] lookUpMethodOverloads(string methodName)
+    {
+        typeof(return) result;
+        foreach (method; _methods)
+            if (startsWith(method.signature, methodName))
+                result ~= method;
+        if (_base)
+            result ~= _base.lookUpMethodOverloads(methodName);
+        return result;
+    }
+
+    QMetaSignal[] lookUpSignalOverloads(string signalName)
+    {
+        typeof(return) result;
+        foreach (method; _methods)
+            if (startsWith(method.signature, signalName) && cast(QMetaSignal)method)
+                result ~= cast(QMetaSignal)method;
+        if (_base)
+            result ~= _base.lookUpSignalOverloads(signalName);
+        return result;
     }
     
     private QMetaObject lookupDerived(void*[] moIds)
@@ -271,7 +303,65 @@
     {
         return qtd_QMetaObject_methodCount(_nativeId);
     }
+    
+    static bool connectImpl(QObject sender, string signalString, QObject receiver, string methodString)
+    {
+        QMetaSignal[] signals;
+        QMetaMethod[] methods;
+        QMetaSignal signal;
+        QMetaMethod method;
 
+        
+        if(indexOf(signalString, '(') > 0)
+            signal = sender.metaObject.lookUpSignal(signalString);
+        else
+            signals = sender.metaObject.lookUpSignalOverloads(signalString); // parameters not specified. Looking for a match
+
+        if(indexOf(methodString, '(') > 0) 
+            method = receiver.metaObject.lookUpMethod(methodString);
+        else
+            methods = receiver.metaObject.lookUpMethodOverloads(methodString); // parameters not specified. Looking for a match
+
+        if(!signal && !method)
+        {
+            foreach(sig; signals)
+                foreach(meth; methods)
+                    if(startsWith(sig.args, meth.args))
+                    {
+                        signal = sig;
+                        method = meth;
+                        goto doConnect;
+                    }
+        }
+        else if (!signal)
+        {
+            foreach(sig; signals)
+                if(startsWith(sig.args, method.args))
+                {
+                    signal = sig;
+                    break;
+                }
+        }
+        else if (!method)
+        {
+            foreach(meth; methods)
+                if(startsWith(signal.args, meth.args))
+                {
+                    method = meth;
+                    break;
+                }
+        } 
+        
+doConnect:
+        if(!signal && !method)
+        {
+            writeln(stderr, "QMetaObject: Signal and slots cannot be found");
+            return false;
+        }
+        int signalIndex = signal.indexOfMethod;
+        int methodIndex = method.indexOfMethod;
+        return QMetaObject.connect(sender, signalIndex, receiver, methodIndex);
+    }
 }
 
 extern(C) void qtd_QMetaObject_activate_3(void* sender, void* m, int local_signal_index, void **argv);
--- a/qt/d2/qt/Signal.d	Sat Dec 26 05:10:16 2009 +0500
+++ b/qt/d2/qt/Signal.d	Sun Dec 27 21:13:38 2009 +0500
@@ -186,7 +186,7 @@
     NewSlot
 }
 
-string signature_impl(T...)(string name)
+string signature(T...)(string name)
 {
     string res = name ~ "(";
     foreach(i, _; T)
@@ -202,27 +202,6 @@
     return res;
 }
 
-template signature(string name, T...)
-{
-    enum signature = signature_impl!(T)(name);
-}
-
-template lastSignalIndex(T)
-{
-    static if (T.stringof == "QObject")
-        enum lastSignalIndex = lastSignalIndexImpl!(T, 0);
-    else
-        mixin ("enum lastSignalIndex = lastSignalIndexImpl!(T, " ~ "T.lastSignalIndex_" ~ (BaseClassesTuple!(T)[0]).stringof ~ ");");
-}
-
-template lastSignalIndexImpl(T, int index)
-{
-    static if (is(typeof(mixin("T." ~ signalPrefix ~ toStringNow!(index)))))
-        enum lastSignalIndexImpl = lastSignalIndexImpl!(T, index + 1);
-    else
-        enum lastSignalIndexImpl = index - 1;
-}
-
 // ------------------------------------------------------------------
 
 string[] getSymbols(C)(string prefix)
--- a/qt/qtd/MOC.d	Sat Dec 26 05:10:16 2009 +0500
+++ b/qt/qtd/MOC.d	Sun Dec 27 21:13:38 2009 +0500
@@ -423,7 +423,11 @@
 {
     string ret;
     foreach(i, _; Args)
-        ret ~= ", " ~ Args[i].stringof;
+    {
+        if (i > 0)
+            ret ~= ", ";
+        ret ~= Args[i].stringof;
+    }
     return ret;
 }
 string genMetaMethodsConstr(alias Funcs)(string className)
@@ -433,7 +437,7 @@
     foreach(i, bogus; Repeat!(void, funcsCount))
     {
         res ~= "        index++;\n" ~
-               "        _staticMetaObject.addMethod(new " ~ className ~ "(signature!(\"" ~ MetaEntryName!(Funcs.at[i].at) ~ "\"" ~ dDeclArgs!(MetaEntryArgs!(Funcs.at[i].at))()~ "), index));\n\n";
+               "        _staticMetaObject.addMethod(new " ~ className ~ "(signature!(" ~ dDeclArgs!(MetaEntryArgs!(Funcs.at[i].at))()~ ")(\"" ~ MetaEntryName!(Funcs.at[i].at) ~ "\"), index));\n\n";
     }
     return res;
 }
@@ -505,18 +509,7 @@
 template Q_OBJECT_BIND()
 {
 }
-/*
-template Q_OBJECT()
-{
-//    pragma(msg, toStringNow!(lastSignalIndex!(typeof(this))));
-    mixin ("enum lastSignalIndex_" ~ typeof(this).stringof ~ " = " ~ toStringNow!(lastSignalIndex!(typeof(this))) ~ ";");
-    
-    alias TupleWrapper!(findSymbols!(slotPrefix,   typeof(this), ByOwner!(typeof(this)))) Slots;
-    alias TupleWrapper!(findSymbols!(signalPrefix, typeof(this), ByOwner!(typeof(this)))) Signals;
-    pragma(msg, generateMetaInfo!((typeof(this)).stringof, Signals, Slots)());
-    mixin(generateMetaInfo!((typeof(this)).stringof, Signals, Slots)());
-}
-*/
+
 // ------------------------------------------------------------------------------------------
 
 string generateSignalEmitters(alias Funcs)()