kexi
metaobject.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "metaobject.h"
00021 #include "metamethod.h"
00022 #include "variable.h"
00023 #include "exception.h"
00024
00025 #include <qguardedptr.h>
00026 #include <qmetaobject.h>
00027
00028 #include <kdebug.h>
00029
00030 using namespace KoMacro;
00031
00032 namespace KoMacro {
00033
00038 class MetaObject::Private
00039 {
00040 public:
00041
00045 QGuardedPtr<QObject> const object;
00046
00050 Private(QObject* const object)
00051 : object(object)
00052 {
00053 }
00054 };
00055
00056 }
00057
00058 MetaObject::MetaObject(QObject* const object)
00059 : KShared()
00060 , d( new Private(object) )
00061 {
00062 }
00063
00064 MetaObject::~MetaObject()
00065 {
00066 delete d;
00067 }
00068
00069 QObject* const MetaObject::object() const
00070 {
00071 if(! d->object) {
00072 throw Exception(QString("Object is undefined."));
00073 }
00074 return d->object;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 int MetaObject::indexOfSignal(const char* signal) const
00090 {
00091 QMetaObject* metaobject = object()->metaObject();
00092 int signalid = metaobject->findSignal(signal, false);
00093 if(signalid < 0) {
00094 throw Exception(QString("Invalid signal \"%1\"").arg(signal));
00095 }
00096 return signalid;
00097 }
00098
00099 int MetaObject::indexOfSlot(const char* slot) const
00100 {
00101 QMetaObject* metaobject = object()->metaObject();
00102 int slotid = metaobject->findSlot(slot, false);
00103 if(slotid < 0) {
00104 throw Exception(QString("Invalid slot \"%1\"").arg(slot));
00105 }
00106 return slotid;
00107 }
00108
00109 KSharedPtr<MetaMethod> MetaObject::method(int index)
00110 {
00111 QObject* obj = object();
00112 MetaMethod::Type type = MetaMethod::Slot;
00113 QMetaObject* metaobject = obj->metaObject();
00114
00115 const QMetaData* metadata = metaobject->slot(index, true);
00116 if(! metadata) {
00117
00118
00119
00120 metadata = metaobject->signal(index, true);
00121 if(! metadata) {
00122 throw Exception(QString("Invalid method index \"%1\" in object \"%2\"").arg(index).arg(obj->name()));
00123 }
00124 type = MetaMethod::Signal;
00125 }
00126
00127 if(metadata->access != QMetaData::Public) {
00128 throw Exception(QString("Not allowed to access method \"%1\" in object \"%2\"").arg(metadata->name).arg(obj->name()));
00129 }
00130
00131 return new MetaMethod(metadata->name, type, this);
00132 }
00133
00134 KSharedPtr<MetaMethod> MetaObject::signal(const char* signal)
00135 {
00136 return method( indexOfSignal(signal) );
00137 }
00138
00139 KSharedPtr<MetaMethod> MetaObject::slot(const char* slot)
00140 {
00141 return method( indexOfSlot(slot) );
00142 }
00143
00144 KSharedPtr<Variable> MetaObject::invokeMethod(int index, Variable::List arguments)
00145 {
00146
00147 KSharedPtr<MetaMethod> m = method(index);
00148
00149 return m->invoke(arguments);
00150 }
00151
|