Kross::Api::QtObject Class Reference

#include <qtobject.h>

Inheritance diagram for Kross::Api::QtObject:

Inheritance graph
[legend]
Collaboration diagram for Kross::Api::QtObject:

Collaboration graph
[legend]
List of all members.

Detailed Description

Class to wrap QObject or inherited instances.

This class publishs all SIGNAL's, SLOT's and Q_PROPERTY's the QObject has.

Definition at line 45 of file qtobject.h.

Public Types

typedef KSharedPtr< QtObjectPtr

Public Member Functions

 QtObject (Object::Ptr parent, QObject *object, const QString &name=QString::null)
virtual ~QtObject ()
virtual const QString getClassName () const
QObjectgetObject ()
void addFunction (const QString &name, FunctionPtr function, const ArgumentList &=ArgumentList())
void addFunction (const QString &name, Function *function)
void addProxyFunction (const QString &name, INSTANCE *instance, METHOD method)
void addProxyFunction (const QString &name, INSTANCE *instance, METHOD method)
 Same as above, but with three arguments.
void addProxyFunction (const QString &name, INSTANCE *instance, METHOD method)
 Same as above, but with two arguments.
void addProxyFunction (const QString &name, INSTANCE *instance, METHOD method)
 Same as above, but with one argument.
void addProxyFunction (const QString &name, INSTANCE *instance, METHOD method)
 Same as above, but with no arguments.
bool isAFunction (const QString &name) const
virtual Object::Ptr call (const QString &name, List::Ptr arguments)
virtual Object::Ptr call (const QString &name, KSharedPtr< List > arguments)
Object::Ptr hasChild (List::Ptr args)
bool hasChild (const QString &name) const
Object::Ptr getChild (List::Ptr args)
Object::Ptr getChild (const QString &name) const
Object::Ptr getChildrenList (List::Ptr args)
Object::Ptr getChildrenDict (List::Ptr args)
Object::Ptr callChild (List::Ptr args)
const QStringgetName () const
virtual const QString toString ()
Object::Ptr getParent () const
QMap< QString, Object::PtrgetChildren () const
bool addChild (Object::Ptr object, const QString &name=QString::null)
void removeChild (const QString &name)
void removeAllChildren ()
virtual QStringList getCalls ()

Static Public Member Functions

static QUObject * toQUObject (const QString &signature, List::Ptr arguments)
template<class T>
static T * fromObject (Object::Ptr object)

Protected Attributes

ArgumentList m_arglist


Member Typedef Documentation

typedef KSharedPtr<QtObject> Kross::Api::QtObject::Ptr
 

Shared pointer to implement reference-counting.

Reimplemented from Kross::Api::Callable.

Definition at line 52 of file qtobject.h.


Constructor & Destructor Documentation

QtObject::QtObject Object::Ptr  parent,
QObject object,
const QString name = QString::null
 

Constructor.

Parameters:
parent The QObject instance this class is children of.
object The QObject instance this class wraps.
name The unique name this QtObject instance has. If not defined then the QObject::name() will be used.

Definition at line 38 of file qtobject.cpp.

References Kross::Api::Object::addChild(), and QObject::metaObject().

00039     : Kross::Api::Class<QtObject>(name.isEmpty() ? object->name() : name, parent)
00040     , m_object(object)
00041 {
00042     // Walk through the signals and slots the QObject has
00043     // and attach them as events to this QtObject.
00044 
00045     QStrList slotnames = m_object->metaObject()->slotNames(false);
00046     for(char* c = slotnames.first(); c; c = slotnames.next()) {
00047         QCString s = c;
00048         addChild( new EventSlot(s, this, object, s) );
00049     }
00050 
00051     QStrList signalnames = m_object->metaObject()->signalNames(false);
00052     for(char* c = signalnames.first(); c; c = signalnames.next()) {
00053         QCString s = c;
00054         addChild( new EventSignal(s, this, object, s) );
00055     }
00056 
00057     // Add functions to wrap QObject methods into callable
00058     // Kross objects.
00059 
00060     addFunction("propertyNames", &QtObject::propertyNames);
00061     addFunction("hasProperty", &QtObject::hasProperty,
00062         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00063     addFunction("getProperty", &QtObject::getProperty,
00064         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00065     addFunction("setProperty", &QtObject::setProperty,
00066         Kross::Api::ArgumentList()
00067             << Kross::Api::Argument("Kross::Api::Variant::String")
00068             << Kross::Api::Argument("Kross::Api::Variant"));
00069 
00070     addFunction("slotNames", &QtObject::slotNames);
00071     addFunction("hasSlot", &QtObject::hasSlot,
00072         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00073     addFunction("slot", &QtObject::callSlot,
00074         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00075 
00076     addFunction("signalNames", &QtObject::signalNames);
00077     addFunction("hasSignal", &QtObject::hasSignal,
00078         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00079     addFunction("signal", &QtObject::emitSignal,
00080         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00081 
00082     addFunction("connect", &QtObject::connectSignal,
00083         Kross::Api::ArgumentList()
00084             << Kross::Api::Argument("Kross::Api::Variant::String")
00085             << Kross::Api::Argument("Kross::Api::QtObject")
00086             << Kross::Api::Argument("Kross::Api::Variant::String"));
00087     addFunction("disconnect", &QtObject::disconnectSignal,
00088         Kross::Api::ArgumentList() << Kross::Api::Argument("Kross::Api::Variant::String"));
00089 }

QtObject::~QtObject  )  [virtual]
 

Destructor.

Definition at line 91 of file qtobject.cpp.

00092 {
00093 }


Member Function Documentation

bool Object::addChild Object::Ptr  object,
const QString name = QString::null
[inherited]
 

Add a new child. Replaces a possible already existing child with such a name.

Parameters:
name the name of the child
object The Object to add.
Returns:
true if the Object was added successfully else false.

Definition at line 79 of file object.cpp.

References QString::arg(), QString::isEmpty(), QString::isNull(), and QMap::replace().

Referenced by Kross::Api::MainModule::addKAction(), Kross::Api::MainModule::addQObject(), Kross::Api::MainModule::addSignal(), Kross::Api::MainModule::addSlot(), QtObject(), and TestPluginModule::TestPluginModule().

00080 {
00081     QString n = name.isNull() ? object->getName() : name;
00082 
00083 #ifdef KROSS_API_OBJECT_ADDCHILD_DEBUG
00084     kdDebug() << QString("Kross::Api::Object::addChild() object.name='%2' object.classname='%3'")
00085         .arg(n).arg(object->getClassName()) << endl;
00086 #endif
00087 
00088     if(n.isEmpty()) // prevent invalid items.
00089         return false; //throw Exception::Ptr( new Exception( QString("Failed to add child object to object '%1'. Invalid name for class '%2'.").arg(getName()).arg(object->getClassName()) ) );
00090 
00091     object->m_parent = this;
00092     m_children.replace(n, object);
00093     return true;
00094 }

void Kross::Api::Event< QtObject >::addFunction const QString name,
Function function
[inline, inherited]
 

Add a methodfunction to the list of functions this Object supports.

Parameters:
name The functionname. Each function this object holds should have an unique name to be still accessable.
function A Function instance which defines the methodfunction. This Event will be the owner of the Function instance and will take care of deleting it if this Event got deleted.
arglist A list of arguments for the function.

Definition at line 119 of file event.h.

References QMap::replace().

00120             {
00121                 m_functions.replace(name, function);
00122             }

void Kross::Api::Event< QtObject >::addFunction const QString name,
FunctionPtr  function,
const ArgumentList = ArgumentList()
[inline, inherited]
 

Add a Callable methodfunction to the list of functions this Object supports.

The FunctionPtr points to the concret Object::Ptr myfuncname(List::Ptr) method in the class defined with template T.

Parameters:
name The functionname. Each function this object holds should have an unique name to be still accessable.
function A pointer to the methodfunction that should handle calls.
arglist A list of arguments for the function.

Definition at line 101 of file event.h.

References QMap::replace().

00102             {
00103                 m_functions.replace(name, new VarFunction0<T>(static_cast<T*>(this), function));
00104             }

void Kross::Api::Event< QtObject >::addProxyFunction const QString name,
INSTANCE *  instance,
METHOD  method
[inline, inherited]
 

Template function to add a Kross::Api::ProxyFunction as builtin-function to this Event instance.

Definition at line 129 of file event.h.

References QMap::replace().

00130             {
00131                 m_functions.replace(name,
00132                     new Kross::Api::ProxyFunction <
00133                         INSTANCE, METHOD,
00134                         RET, ARG1, ARG2, ARG3, ARG4
00135                     > ( instance, method ) );
00136             }

virtual Object::Ptr Kross::Api::Object::call const QString name,
KSharedPtr< List arguments
[virtual, inherited]
 

Pass a call to the object and evaluated it recursive down the object-hierachy. Objects like Class are able to handle call's by just implementing this function. If the call is done the called() method will be executed recursive from bottom up the call hierachy.

Exceptions:
TypeException if the object or the name is not callable.
Parameters:
name Each call has a name that says what should be called. In the case of a Class the name is the functionname.
arguments The list of arguments passed to the call.
Returns:
The call-result as Object::Ptr instance or NULL if the call has no result.

Reimplemented in Kross::Api::EventScript, Kross::Api::EventSignal, and Kross::Api::EventSlot.

Referenced by Kross::Api::Callable::call(), and Kross::Api::Callable::callChild().

virtual Object::Ptr Kross::Api::Event< QtObject >::call const QString name,
List::Ptr  arguments
[inline, virtual, inherited]
 

Overloaded method to handle function-calls.

Exceptions:
AttributeException if argumentparameters arn't valid.
RuntimeException if the functionname isn't valid.
Parameters:
name The functionname. Each function this Object holds should have a different name cause they are access by they name. If name is QString::null or empty, a self-reference to this instance is returned.
arguments The list of arguments.
Returns:
An Object representing the call result or NULL if there doesn't exists such a function with defined name.

Reimplemented from Kross::Api::Callable.

Definition at line 210 of file event.h.

References Kross::Api::Function::call(), Kross::Api::Object::getName(), and QString::isNull().

00211             {
00212 #ifdef KROSS_API_EVENT_CALL_DEBUG
00213                 kdDebug() << QString("Event::call() name='%1' getName()='%2'").arg(name).arg(getName()) << endl;
00214 #endif
00215 
00216                 Function* function = m_functions[name];
00217                 if(function) {
00218 #ifdef KROSS_API_EVENT_CALL_DEBUG
00219                     kdDebug() << QString("Event::call() name='%1' is a builtin function.").arg(name) << endl;
00220 #endif
00221 
00222                     //FIXME checkArguments(arguments);
00223                     return function->call(arguments);
00224                 }
00225 
00226                 if(name.isNull()) {
00227                     // If no name is defined, we return a reference to our instance.
00228                     return this;
00229                 }
00230 
00231                 // Redirect the call to the Kross::Api::Callable we are inheritated from.
00232                 return Callable::call(name, arguments);
00233             }

Object::Ptr Callable::callChild List::Ptr  args  )  [inherited]
 

Wrapper for the Kross::Api::Object::call() method to call a children.

Definition at line 172 of file callable.cpp.

References Kross::Api::Object::call(), and Kross::Api::Variant::toString().

Referenced by Kross::Api::Callable::call().

00173 {
00174     //kdDebug() << QString("Kross::Api::Callable::callChild() getName()=%1").arg(getName()) << endl;
00175     return Object::call(Variant::toString(args->item(0)), args);
00176 }

template<class T>
static T* Kross::Api::Object::fromObject Object::Ptr  object  )  [inline, static, inherited]
 

Try to convert the Object instance to the template class T.

Exceptions:
TypeException if the cast failed.
Parameters:
object The Object to cast.
Returns:
The to a instance from template type T casted Object.

Definition at line 198 of file object.h.

00199             {
00200                 T* t = (T*) object.data();
00201                 if(! t)
00202                     throw KSharedPtr<Exception>( new Exception(QString("Object \"%1\" invalid.").arg(object ? object->getClassName() : "")) );
00203                 return t;
00204             }

virtual QStringList Kross::Api::Object::getCalls  )  [inline, virtual, inherited]
 

Return a list of supported callable objects.

Returns:
List of supported calls.

Reimplemented in Kross::Python::PythonObject.

Definition at line 186 of file object.h.

00186 { return QStringList(); }

Object::Ptr Object::getChild const QString name  )  const [inherited]
 

Return the defined child or NULL if there is no such object with that name avaible.

Parameters:
name The name of the Object to return.
Returns:
The Object matching to the defined name or NULL if there is no such Object.

Definition at line 69 of file object.cpp.

Referenced by Kross::Api::Callable::getChild().

00070 {
00071     return m_children[name];
00072 }

Object::Ptr Callable::getChild List::Ptr  args  )  [inherited]
 

Wrapper for the Kross::Api::Object::getChild() method to return a children this object has.

Definition at line 146 of file callable.cpp.

References Kross::Api::Object::getChild(), Kross::Api::Object::getName(), and Kross::Api::Variant::toString().

Referenced by Kross::Api::Callable::call().

00147 {
00148     QString s = Variant::toString(args->item(0));
00149     //kdDebug() << QString("Kross::Api::Callable::getChild() getName()=%1 childName=%2").arg(getName()).arg(s) << endl;
00150     Object::Ptr obj = Object::getChild(s);
00151     if(! obj)
00152         throw Exception::Ptr( new Exception(QString("The object '%1' has no child object '%2'").arg(getName()).arg(s)) );
00153     return obj;
00154 }

QMap< QString, Object::Ptr > Object::getChildren  )  const [inherited]
 

Return all children.

Returns:
A ObjectMap of children this Object has.

Definition at line 74 of file object.cpp.

Referenced by Kross::Api::Callable::getChildrenDict(), and Kross::Api::Callable::getChildrenList().

00075 {
00076     return m_children;
00077 }

Object::Ptr Callable::getChildrenDict List::Ptr  args  )  [inherited]
 

Wrapper for the Kross::Api::Object::getChild() method to return a dictonary of children this object has.

Returns:
a Dict filled with the children.

Definition at line 166 of file callable.cpp.

References Kross::Api::Object::getChildren().

Referenced by Kross::Api::Callable::call().

00167 {
00168     //kdDebug()<<"Kross::Api::Callable::getChildrenDict()"<<endl;
00169     return new Dict(Object::getChildren(), "Kross::Api::Callable::getChildrenDict::Dict");
00170 }

Object::Ptr Callable::getChildrenList List::Ptr  args  )  [inherited]
 

Wrapper for the Kross::Api::Object::getChildren() method to return a list of childrennames this object has.

Returns:
a List filled with a list of names of the children this object has.

Definition at line 156 of file callable.cpp.

References QMap::begin(), QMap::end(), and Kross::Api::Object::getChildren().

Referenced by Kross::Api::Callable::call().

00157 {
00158     QStringList list;
00159     QMap<QString, Object::Ptr> children = getChildren();
00160     QMap<QString, Object::Ptr>::Iterator it( children.begin() );
00161     for(; it != children.end(); ++it)
00162         list.append( it.key() );
00163     return new Variant(list);
00164 }

const QString QtObject::getClassName  )  const [virtual]
 

See also:
Kross::Api::Object::getClassName()

Reimplemented from Kross::Api::Callable.

Definition at line 95 of file qtobject.cpp.

00096 {
00097     return "Kross::Api::QtObject";
00098 }

const QString & Object::getName  )  const [inherited]
 

Return the name this object has.

Returns:
Name of this object.

Reimplemented in Kross::Api::ScriptContainer.

Definition at line 49 of file object.cpp.

Referenced by Kross::Api::Event< QtObject >::call(), Kross::Api::Callable::call(), Kross::Api::Callable::getChild(), and Kross::Api::Module::~Module().

00050 {
00051     return m_name;
00052 }

QObject * QtObject::getObject  ) 
 

Return the QObject instance this class wraps.

Returns:
The wrapped QObject.

Definition at line 100 of file qtobject.cpp.

00101 {
00102     return m_object;
00103 }

Object::Ptr Object::getParent  )  const [inherited]
 

Return the parent object or NULL if this object doesn't has a parent.

Returns:
The parent-Object or NULL if this Object doesn't has a parent.

Definition at line 59 of file object.cpp.

00060 {
00061     return m_parent;
00062 }

bool Object::hasChild const QString name  )  const [inherited]
 

Returns if the defined child is avaible.

Returns:
true if child exists else false.

Reimplemented in Kross::Api::MainModule.

Definition at line 64 of file object.cpp.

References QMap::contains().

Referenced by Kross::Api::MainModule::hasChild(), and Kross::Api::Callable::hasChild().

00065 {
00066     return m_children.contains(name);
00067 }

Object::Ptr Callable::hasChild List::Ptr  args  )  [inherited]
 

Wrapper for the Kross::Api::Object::hasChild() method to check if this object has children.

Definition at line 139 of file callable.cpp.

References Kross::Api::Object::hasChild(), and Kross::Api::Variant::toString().

Referenced by Kross::Api::Callable::call().

00140 {
00141     //kdDebug() << QString("Kross::Api::Callable::hasChild() getName()=%1").arg(getName()) << endl;
00142     return new Variant( Object::hasChild( Variant::toString(args->item(0)) ),
00143                         "Kross::Api::Callable::hasChild::Bool" );
00144 }

bool Kross::Api::Event< QtObject >::isAFunction const QString name  )  const [inline, inherited]
 

Check if a function is a member of this Callable

Parameters:
name the function name
Returns:
true if the function is available in this Callable

Definition at line 187 of file event.h.

References QMap::contains().

00188             {
00189                 return m_functions.contains(name);
00190             }

void Object::removeAllChildren  )  [inherited]
 

Remove all children.

Definition at line 104 of file object.cpp.

References QMap::clear().

00105 {
00106 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00107     kdDebug() << "Kross::Api::Object::removeAllChildren()" << endl;
00108 #endif
00109     m_children.clear();
00110 }

void Object::removeChild const QString name  )  [inherited]
 

Remove an existing child.

Parameters:
name The name of the Object to remove. If there doesn't exists an Object with such name just nothing will be done.

Definition at line 96 of file object.cpp.

References QMap::remove().

00097 {
00098 #ifdef KROSS_API_OBJECT_REMCHILD_DEBUG
00099     kdDebug() << QString("Kross::Api::Object::removeChild() name='%1'").arg(name) << endl;
00100 #endif
00101     m_children.remove(name);
00102 }

QUObject * QtObject::toQUObject const QString signature,
List::Ptr  arguments
[static]
 

Build a Qt QUObject struct out of the Qt signal or slot signature and the passed List arguments.

Exceptions:
RuntimeException If the try to translate arguments failed.
Parameters:
signature The Qt signal or slot signature.
arguments The optional List of arguments.
Returns:
A QUObject array.

Definition at line 105 of file qtobject.cpp.

References QString::find(), QString::findRev(), QStringList::split(), and Kross::Api::Variant::toString().

Referenced by Kross::Api::EventSignal::call().

00106 {
00107     int startpos = signature.find("(");
00108     int endpos = signature.findRev(")");
00109     if(startpos < 0 || startpos > endpos)
00110         throw Exception::Ptr( new Exception(QString("Invalid Qt signal or slot signature '%1'").arg(signature)) );
00111 
00112     //QString sig = signature.left(startpos);
00113     QString params = signature.mid(startpos + 1, endpos - startpos - 1);
00114     QStringList paramlist = QStringList::split(",", params); // this will fail on something like myslot(QMap<QString,QString> arg), but we don't care jet.
00115     uint paramcount = paramlist.size();
00116 
00117     // The first item in the QUObject-array is for the returnvalue
00118     // while everything >=1 are the passed parameters.
00119     QUObject* uo = new QUObject[ paramcount + 1 ];
00120     uo[0] = QUObject(); // empty placeholder for the returnvalue.
00121 
00122 //QString t;
00123 //for(int j=0; j<argcount; j++) t += "'" + Variant::toString(arguments->item(j)) + "' ";
00124 //kdDebug()<<"1 --------------------- ("<<argcount<<"): "<<t<<endl;
00125 
00126     // Fill parameters.
00127     uint argcount = arguments ? arguments->count() : 0;
00128     for(uint i = 0; i < paramcount; i++) {
00129         if(paramlist[i].find("QString") >= 0) {
00130             const QString s = (argcount > i) ? Variant::toString(arguments->item(i)) : QString::null;
00131             //kdDebug()<<"EventSlot::toQUObject s="<<s<<endl;
00132             static_QUType_QString.set( &(uo[i + 1]), s );
00133         }
00134         //TODO handle int, long, char*, QStringList, etc.
00135         else {
00136             throw Exception::Ptr( new Exception(QString("Unknown Qt signal or slot argument '%1' in signature '%2'.").arg(paramlist[i]).arg(signature)) );
00137         }
00138     }
00139 
00140 //kdDebug()<<"2 --------------------- "<<endl;
00141     return uo;
00142 }

const QString Object::toString  )  [virtual, inherited]
 

Returns:
a string representation of the object or it's content. This method is mainly used for debugging and testing purposes.

Reimplemented in Kross::Api::Dict, Kross::Api::Exception, Kross::Api::List, and Kross::Api::Variant.

Definition at line 54 of file object.cpp.

References QString::arg(), and Kross::Api::Object::getClassName().

00055 {
00056     return QString("%1 (%2)").arg(m_name).arg(getClassName());
00057 }


Member Data Documentation

ArgumentList Kross::Api::Callable::m_arglist [protected, inherited]
 

List of arguments this callable object supports.

Definition at line 123 of file callable.h.


The documentation for this class was generated from the following files:
Generated on Thu Feb 9 18:00:42 2006 for Kross by  doxygen 1.4.6