Eneboo - Documentación para desarrolladores
|
00001 /**************************************************************************** 00002 ** $Id: quickobjects.h 1.1.5 edited 2006-12-04T13:53:30$ 00003 ** 00004 ** Copyright (C) 2001-2006 Trolltech AS. All rights reserved. 00005 ** 00006 ** This file is part of the Qt Script for Applications framework (QSA). 00007 ** 00008 ** This file may be distributed and/or modified under the terms of the 00009 ** GNU General Public License version 2 as published by the Free Software 00010 ** Foundation and appearing in the file LICENSE.GPL included in the 00011 ** packaging of this file. 00012 ** 00013 ** Licensees holding a valid Qt Script for Applications license may use 00014 ** this file in accordance with the Qt Script for Applications License 00015 ** Agreement provided with the Software. 00016 ** 00017 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00018 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00019 ** 00020 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 00021 ** information about QSA Commercial License Agreements. 00022 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 00023 ** 00024 ** Contact info@trolltech.com if any conditions of this licensing are 00025 ** not clear to you. 00026 ** 00027 *****************************************************************************/ 00028 00029 #ifndef QUICKOBJECTS_H 00030 #define QUICKOBJECTS_H 00031 00032 #include "quickenv.h" 00033 00034 #include <qvariant.h> 00035 #include <qobject.h> 00036 #include <qptrvector.h> 00037 #include <qptrdict.h> 00038 #include <qmetaobject.h> 00039 #include "dlldefs.h" 00040 #include <qintdict.h> 00041 #include <qmap.h> 00042 00043 #include <qsclass.h> 00044 #include <qstypes.h> 00045 #include <qsfunction.h> 00046 00047 class QuickInterpreter; 00048 class QuickScriptVariant; 00049 class QuickScriptObjectFunc; 00050 class QuickPtrDispatchObject; 00051 class QSFactoryObjectProxy; 00052 00053 00054 extern void stopAllTimers(); 00055 00056 QSObject qsStartTimer(QSEnv *); 00057 void qsKillTimer(QSEnv *); 00058 void qsKillTimers(QSEnv *); 00059 00060 QUICKCORE_EXPORT QObjectList *extraChildren(QObject *o); 00061 //### AbanQ 00062 //QUICKCORE_EXPORT QObject *objectChild(QObject *wid, const char *objName, const char *inheritsClass, bool recursiveSearch); 00063 extern QUICKCORE_EXPORT QValueList<QString> *appArgs; 00064 00065 struct EventTarget { 00066 public: 00067 QuickInterpreter *eng; 00068 struct Target { 00069 Target() : ctx(0), qsctx(QSObject()) {} 00070 Target(QObject *c, QSObject qs, const QString &f) 00071 : ctx(c), qsctx(qs), func(f) {} 00072 QObject *ctx; 00073 QSObject qsctx; 00074 QString func; 00075 }; 00076 QValueList<Target> targets; 00077 00078 }; 00079 00080 struct QuickMetaData : public QMetaData { 00081 QuickMetaData(): id(-1) {} 00082 QuickMetaData(const QMetaData *md, int funcid): QMetaData(*md), id(funcid) {} 00083 int id; 00084 00085 bool operator==(const QuickMetaData &md) const { 00086 return (md.id == id && 00087 md.name == name && 00088 md.method == method && 00089 md.access == access); 00090 } 00091 }; 00092 00093 typedef QMap<int, EventTarget> QuickScriptEventMap; 00094 00095 class QuickScriptReceiver : public QObject 00096 { 00097 public: 00098 QuickScriptReceiver(QObject *o); 00099 ~QuickScriptReceiver(); 00100 00101 void setEventHandler(QuickInterpreter *ip, int id, QObject *scope, 00102 const QString &func, QSObject qsctx = QSObject()); 00103 void removeEventHandler(int id, QObject *scope, const QString &func, QSObject qsctx = QSObject()); 00104 bool qt_invoke(int id, QUObject *_o); 00105 void invalidate(); 00106 00107 private: 00108 QObject *qobj; 00109 QuickScriptEventMap *handler; 00110 }; 00111 00112 class QUICKCORE_EXPORT QSOT 00113 { 00114 public: 00115 enum QuickScriptObjectType { Property, Object, Slot, Enum, Unknown }; 00116 }; 00117 00118 class QUICKCORE_EXPORT QuickScriptProperty 00119 { 00120 public: 00121 QuickScriptProperty(QSOT::QuickScriptObjectType t, QuickScriptVariant *v, int on) : type(t), objNum(on) { 00122 id.var = v; 00123 } 00124 QuickScriptProperty(QSOT::QuickScriptObjectType t, QuickScriptObjectFunc *f, int on) : type(t), objNum(on) { 00125 id.func = f; 00126 } 00127 QuickScriptProperty(QSOT::QuickScriptObjectType t, int v, int on) : type(t), objNum(on) { 00128 id.enumValue = v; 00129 } 00130 QuickScriptProperty() : type(QSOT::Unknown) {} 00131 00132 QSOT::QuickScriptObjectType type; 00133 union { 00134 QuickScriptVariant *var; 00135 QuickScriptObjectFunc *func; 00136 int enumValue; 00137 } id; 00138 int objNum; 00139 }; 00140 00141 #if defined(Q_TEMPLATEDLL) 00142 // MOC_SKIP_BEGIN 00143 template class QUICKCORE_EXPORT QPtrList<QuickScriptReceiver>; 00144 template class QUICKCORE_EXPORT QMap<QObject *, QuickScriptReceiver *>; 00145 template class QUICKCORE_EXPORT QMap<QString, QSOT::QuickScriptObjectType>; 00146 template class QUICKCORE_EXPORT QPtrVector<QObject>; 00147 template class QUICKCORE_EXPORT QMap<QString, QuickScriptProperty>; 00148 template class QUICKCORE_EXPORT QValueList<QuickMetaData>; 00149 template class QUICKCORE_EXPORT QIntDict<char>; 00150 template class QUICKCORE_EXPORT QPtrList<QMetaProperty>; 00151 //template class QUICKCORE_EXPORT QValueList<QVariant>; 00152 // MOC_SKIP_END 00153 #endif 00154 00155 struct QUICKCORE_EXPORT EventId { 00156 EventId() : id(-1), obj(-1) {} 00157 EventId(int i, int o) : id(i), obj(o) {} 00158 int id, obj; 00159 }; 00160 00161 class QSWrapperClass; 00162 class QSUserData; 00163 00164 class QUICKCORE_EXPORT QSWrapperSharedWatcher: public QObject 00165 { 00166 friend class QSWrapperShared; 00167 Q_OBJECT 00168 class QSWrapperShared *parent; 00169 00170 public slots: 00171 void objectDestroyed(QObject *); 00172 }; 00173 00174 class QUICKCORE_EXPORT QSWrapperShared : public QSInstanceData 00175 { 00176 friend class QSWrapperClass; 00177 00178 public: 00179 enum ObjectType { FactoryObject, GlobalObject }; 00180 QSWrapperShared(const QSWrapperClass *cl); 00181 ~QSWrapperShared(); 00182 00183 void setUserData(QSUserData *d) { 00184 udata = d; 00185 } 00186 bool setEventHandler(QuickInterpreter *ip, const QString &event, QObject *ctx, 00187 const QString &func, QSObject qsctx = QSObject()); 00188 bool removeEventHandler(const QString &event, QObject *ctx, 00189 const QString &func, QSObject qsctx = QSObject()); 00190 const QPtrVector<QObject> *interfaceObjects() const { 00191 return &objects; 00192 } 00193 00194 QPtrVector<QObject> objects; // interfaces 00195 QMap<QObject *, QuickScriptReceiver *> receivers; // event receivers 00196 QMap<QString, QSOT::QuickScriptObjectType> hasPropCache; // caches 00197 QMap<QString, QuickScriptProperty> propertyCache; 00198 const QSWrapperClass *wrapperClass() const { 00199 return cls; 00200 } 00201 virtual void invalidate(); // overriding QSShared::invalidate() 00202 void invalidateWrapper(); 00203 00204 ObjectType objectType() const { 00205 return objTyp; 00206 } 00207 void setObjectType(ObjectType type) { 00208 objTyp = type; 00209 } 00210 00211 QSFactoryObjectProxy *creator; 00212 00213 private: 00214 EventId findEventId(const QString &event); 00215 const QSWrapperClass *cls; 00216 QSUserData *udata; 00217 ObjectType objTyp; 00218 QSWrapperSharedWatcher watcher; 00219 }; 00220 00221 class QUICKCORE_EXPORT QSUserData : public QObjectUserData 00222 { 00223 public: 00224 QSUserData(QSWrapperShared *d) : dat(d) { } 00225 ~QSUserData(); 00226 QSWrapperShared *data() const { 00227 return dat; 00228 } 00229 void setData(QSWrapperShared *d) { 00230 dat = d; 00231 } 00232 void invalidate() { 00233 /* dat->deref(); */ dat = 0; 00234 } 00235 private: 00236 QSWrapperShared *dat; 00237 }; 00238 00239 00240 class QUICKCORE_EXPORT QSWrapperClass : public QSWritableClass, 00241 public QuickEnvClass 00242 { 00243 friend class QSFactoryObjectProxy; 00244 public: 00245 QSWrapperClass(QSClass *b); 00246 ~QSWrapperClass(); 00247 QString name() const { 00248 return QString::fromLatin1("QObject"); 00249 } 00250 00251 virtual bool member(const QSObject *o, const QString &n, 00252 QSMember *m) const; 00253 virtual QSObject fetchValue(const QSObject *objPtr, 00254 const QSMember &mem) const; 00255 virtual void write(QSObject *objPtr, const QSMember &mem, 00256 const QSObject &val) const; 00257 00258 virtual void deref(QSObject *o) const; 00259 00260 bool toBoolean(const QSObject *) const; 00261 QString toString(const QSObject *) const; 00262 QVariant toVariant(const QSObject *obj, QVariant::Type) const; 00263 QString debugString(const QSObject *obj) const; 00264 QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00265 00266 QSObject wrap(const QPtrVector<QObject> &objs); 00267 QSWrapperShared *createShared(QObject *o) const; 00268 00269 QSWrapperShared *shared(const QSObject *obj) const; 00270 QPtrVector<QObject>& objectVector(const QSObject *obj) const; 00271 00272 static QObject *object(const QSObject *obj); 00273 00274 void invalidate(); 00275 00276 protected: 00277 virtual QSObject invoke(QSObject *objPtr, const QSMember &mem) const; 00278 00279 private: 00280 }; 00281 00282 class QUICKCORE_EXPORT QSPointerClass : public QSWrapperClass 00283 { 00284 public: 00285 QSPointerClass(QSClass *b) : QSWrapperClass(b) { } 00286 QString name() const { 00287 return QString::fromLatin1("Pointer"); 00288 } 00289 00290 double toNumber(const QSObject *) const; 00291 bool toBoolean(const QSObject *) const; 00292 QString debugString(const QSObject *obj) const; 00293 QString toString(const QSObject *) const; 00294 QVariant toVariant(const QSObject *obj, QVariant::Type) const; 00295 00296 QSObject wrapPointer(const char *name, void *p); 00297 QSObject wrapPointer(QuickPtrDispatchObject *ptr); 00298 00299 void *pointer(const QSObject *obj) const; 00300 const char *pointerType(const QSObject *obj) const; 00301 }; 00302 00303 class QSObjectConstructor; 00304 00305 class QSFactoryObjectProxy : public QSTypeClass, public QuickEnvClass 00306 { 00307 public: 00308 QSFactoryObjectProxy(QSClass *base, const QSObject &inst, 00309 QSObjectConstructor *ctor) 00310 : QSTypeClass(base), 00311 QuickEnvClass(base->env()), 00312 sinst(inst), 00313 constr(ctor) { } 00314 QString name() const { 00315 return QString::fromLatin1("FactoryObject"); 00316 } 00317 QSObject staticInstance() const { 00318 return sinst; 00319 } 00320 00321 virtual bool member(const QSObject *o, const QString &n, 00322 QSMember *m) const; 00323 virtual QSMemberMap members(const QSObject *obj) const; 00324 virtual QSObject fetchValue(const QSObject *o, 00325 const QSMember &mem) const; 00326 virtual void write(QSObject *objPtr, const QSMember &mem, 00327 const QSObject &val) const; 00328 00329 virtual ValueType valueType() const { 00330 return TypeClass; 00331 } 00332 00333 protected: 00334 virtual QSObject invoke(QSObject *objPtr, const QSMember &mem) const; 00335 00336 virtual void finalize(); 00337 private: 00338 QSObject sinst; 00339 QSObjectConstructor *constr; 00340 }; 00341 00342 class QSObjectConstructor : public QSClass, public QuickEnvClass 00343 { 00344 public: 00345 enum Type { Class, Form }; 00346 QSObjectConstructor(QSClass *b, const QString &className, Type t = Class); 00347 QString name() const { 00348 return QString::fromLatin1("ObjectConstructor"); 00349 } 00350 QString identifier() const { 00351 return cname; 00352 } 00353 QSObject construct(const QSList &args) const; 00354 QSObject cast(const QSList &args) const; 00355 bool member(const QSObject *objPtr, const QString &name, QSMember *m) const; 00356 QSObject fetchValue(const QSObject *o, const QSMember &mem) const; 00357 00358 QSFactoryObjectProxy *proxy() const { 00359 return m_proxy; 00360 } 00361 void setFactoryObjectProxy(QSFactoryObjectProxy *proxy) { 00362 m_proxy = proxy; 00363 } 00364 00365 private: 00366 QString cname; 00367 Type type; 00368 QSFactoryObjectProxy *m_proxy; 00369 }; 00370 00371 class QSVariantShared; 00372 00373 class QUICKCORE_EXPORT QSVariantClass : public QSSharedClass, 00374 public QuickEnvClass 00375 { 00376 public: 00377 QSVariantClass(QSClass *b) : QSSharedClass(b), QuickEnvClass(b->env()) { } 00378 QString name() const { 00379 return QString::fromLatin1("Variant"); 00380 } 00381 00382 virtual bool member(const QSObject *objPtr, const QString &n, 00383 QSMember *m) const; 00384 virtual QSObject fetchValue(const QSObject *objPtr, 00385 const QSMember &mem) const; 00386 virtual void write(QSObject *objPtr, const QSMember &mem, 00387 const QSObject &val) const; 00388 00389 bool toBoolean(const QSObject *) const; 00390 double toNumber(const QSObject *) const; 00391 00392 QString toString(const QSObject *) const; 00393 QSObject toPrimitive(const QSObject *obj, 00394 const QSClass *preferred) const; 00395 QVariant toVariant(const QSObject *obj, QVariant::Type) const; 00396 QString debugString(const QSObject *obj) const; 00397 00398 QSObject construct(const QVariant &v, const QMetaProperty *m) const; 00399 QSObject construct(const QSList &args) const; 00400 00401 QVariant *variant(const QSObject *) const; 00402 QSVariantShared *shared(const QSObject *obj) const; 00403 00404 protected: 00405 virtual QSObject invoke(QSObject *objPtr, const QSMember &mem) const; 00406 }; 00407 00408 class QUICKCORE_EXPORT QuickScriptVariant : public QSObject 00409 { 00410 public: 00411 QuickScriptVariant(QuickInterpreter *ip, 00412 const QVariant &v, const QMetaProperty *m = 0); 00413 00414 QVariant value() const; 00415 00416 const QMetaProperty *metaProperty() const; 00417 00418 bool isNative() const; 00419 QSObject toNative() const; 00420 00421 private: 00422 QSVariantShared *shared() const; 00423 }; 00424 00425 class QSApplicationClass : public QSWritableClass, public QuickEnvClass 00426 { 00427 public: 00428 QSApplicationClass(QSClass *b); 00429 QString name() const { 00430 return QString::fromLatin1("Application"); 00431 } 00432 00433 virtual bool member(const QSObject *o, const QString &n, 00434 QSMember *m) const; 00435 virtual QSObject fetchValue(const QSObject *objPtr, 00436 const QSMember &mem) const; 00437 00438 static void exit(QSEnv *); 00439 static QSObject startTimer(QSEnv *); 00440 static QSObject killTimer(QSEnv *); 00441 static QSObject killTimers(QSEnv *); 00442 }; 00443 00444 #endif