00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef QOBJECT_BINDING_H
00026 #define QOBJECT_BINDING_H
00027
00028 #include <QtCore/QObjectCleanupHandler>
00029 #include <QtCore/QDebug>
00030 #include <QtCore/QBool>
00031
00032 #include <kjs/function.h>
00033 #include <kdemacros.h>
00034
00035 #include "binding_support.h"
00036 #include "object_binding.h"
00037
00038
00045 #define START_QOBJECT_METHOD( METHODNAME, TYPE) \
00046 KJS::JSValue *METHODNAME( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) \
00047 { \
00048 Q_UNUSED( args ); \
00049 KJS::JSValue *result = KJS::jsNull(); \
00050 KJSEmbed::QObjectBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::QObjectBinding>(exec, self ); \
00051 if( imp ) \
00052 { \
00053 TYPE *object = imp->qobject<TYPE>(); \
00054 if( object ) \
00055 {
00056
00060 #define END_QOBJECT_METHOD \
00061 } \
00062 else \
00063 KJS::throwError(exec, KJS::ReferenceError, QString("QO: The internal object died %1:%2.").arg(__FILE__).arg(__LINE__));\
00064 } \
00065 else \
00066 KJS::throwError(exec, KJS::ReferenceError, QString("QObject died."));\
00067 return result; \
00068 }
00069
00070 class QObject;
00071 class QMetaMethod;
00072
00073 namespace KJSEmbed {
00074
00075 KJS_BINDING( QObjectFactory )
00076
00077 class EventProxy;
00078
00079 class KJSEMBED_EXPORT QObjectBinding : public ObjectBinding
00080 {
00081 public:
00082
00083 QObjectBinding( KJS::ExecState *exec, QObject *object );
00084 virtual ~QObjectBinding();
00085
00086 static void publishQObject( KJS::ExecState *exec, KJS::JSObject *target, QObject *object);
00087
00094 enum Access {
00095 None = 0x00,
00096
00097 ScriptableSlots = 0x01,
00098 NonScriptableSlots = 0x02,
00099 PrivateSlots = 0x04,
00100 ProtectedSlots = 0x08,
00101 PublicSlots = 0x10,
00102 AllSlots = ScriptableSlots|NonScriptableSlots|PrivateSlots|ProtectedSlots|PublicSlots,
00103
00104 ScriptableSignals = 0x100,
00105 NonScriptableSignals = 0x200,
00106 PrivateSignals = 0x400,
00107 ProtectedSignals = 0x800,
00108 PublicSignals = 0x1000,
00109 AllSignals = ScriptableSignals|NonScriptableSignals|PrivateSignals|ProtectedSignals|PublicSignals,
00110
00111 ScriptableProperties = 0x10000,
00112 NonScriptableProperties = 0x20000,
00113 AllProperties = ScriptableProperties|NonScriptableProperties,
00114
00115 GetParentObject = 0x100000,
00116 SetParentObject = 0x200000,
00117 ChildObjects = 0x400000,
00118 AllObjects = GetParentObject|SetParentObject|ChildObjects
00119 };
00120
00121 Q_DECLARE_FLAGS(AccessFlags, Access)
00122
00123
00126 AccessFlags access() const;
00127
00131 void setAccess(AccessFlags access);
00132
00136 void put(KJS::ExecState *exec, const KJS::Identifier &propertyName, KJS::JSValue *value, int attr=KJS::None);
00137
00141 bool canPut(KJS::ExecState *exec, const KJS::Identifier &propertyName) const;
00142
00147 bool getOwnPropertySlot( KJS::ExecState *exec, const KJS::Identifier &propertyName, KJS::PropertySlot &slot );
00148
00152 static KJS::JSValue *propertyGetter( KJS::ExecState *exec, KJS::JSObject*, const KJS::Identifier& name, const KJS::PropertySlot& );
00153
00158 KJS::UString toString(KJS::ExecState *exec) const;
00159
00164 KJS::UString className() const;
00165
00170 void watchObject( QObject *object );
00171
00176 template <typename T>
00177 T *qobject() const
00178 {
00179 QObject* object = QObjectBinding::object<QObject>();
00180 if (object)
00181 return qobject_cast<T*>(object);
00182 else
00183 return 0;
00184 }
00185
00186 private:
00187 EventProxy *m_evproxy;
00188 QObjectCleanupHandler *m_cleanupHandler;
00189 AccessFlags m_access;
00190 };
00191
00192 Q_DECLARE_OPERATORS_FOR_FLAGS(QObjectBinding::AccessFlags)
00193
00194 class KJSEMBED_EXPORT SlotBinding : public KJS::InternalFunctionImp
00195 {
00196 public:
00197 SlotBinding(KJS::ExecState *exec, const QMetaMethod &memberName);
00198 KJS::JSValue *callAsFunction( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args );
00199 bool implementsCall() const { return true; }
00200 bool implementsConstruct() const { return false; }
00201
00202 protected:
00203 QByteArray m_memberName;
00204 };
00205
00230 KJSEMBED_EXPORT KJS::JSObject *createQObject(KJS::ExecState *exec, QObject *value, KJSEmbed::ObjectBinding::Ownership owner = KJSEmbed::ObjectBinding::JSOwned);
00231
00232
00233 }
00234 #endif
00235
00236