kexi

metaobject.cpp

00001 /***************************************************************************
00002  * This file is part of the KDE project
00003  * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
00004  * copyright (C) 2005 by Tobi Krebs (tobi.krebs@gmail.com)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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) ) // create the private d-pointer instance.
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 QStrList MetaObject::signalNames() const
00079 {
00080     return object()->metaObject()->signalNames();
00081 }
00082 
00083 QStrList MetaObject::slotNames() const
00084 {
00085     return object()->metaObject()->slotNames();
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         // Try to get a signal with that index iff we failed to determinate
00118         // a matching slot.
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     // kdDebug() << "MetaObject::invokeMethod(int index, Variable::List arguments)" << endl;
00147     KSharedPtr<MetaMethod> m = method(index);
00148     // kdDebug() << "MetaObject::invokeMethod(int index, Variable::List arguments) return" << endl;
00149     return m->invoke(arguments);
00150 }
00151 
KDE Home | KDE Accessibility Home | Description of Access Keys