QObjectBridge.h

00001 /*
00002  * @(#)QObjectBridge.h  0.2.0 / 2007-09-07
00003  *
00004  * Copyright (c) 2007, ETF and contributors. All rights reserved.
00005  *
00006  * This software is licensed under the LGPL, version 2.1, license.
00007  * A copy of the license is included in the file LICENSE-LGPL.txt.
00008  */
00009 #ifndef QOBJECTBRIDGE_H
00010 #define QOBJECTBRIDGE_H
00011 
00012 #include "JavaQxCore.h"
00013 
00014 /*^**************************************************************************^*/
00015 /*- QObjectBridge Class.                                                     -*/
00016 /*^**************************************************************************^*/
00017 
00024 class JAVAQX_EXPORT QObjectBridge : public QObject
00025 {
00026   Q_OBJECT
00027 protected:
00028   QObjectBridge(JNIEnv *env, jobject jniObj, QObject *qObj);
00029   QObjectBridge(JNIEnv *env, jobject jniObj);
00030   virtual ~QObjectBridge();
00031 private:
00032   QObjectBridge(const QObjectBridge &);
00033 public:
00034   void setName(JNIEnv *env, jstring name);
00035   void postEvent(QEvent *e);
00036   void sendEvent(QEvent *e);
00037 public:
00038   virtual void destroy(JNIEnv *env, bool notify = false);
00039 public:
00040   static void initialize(JNIEnv *env, jclass cls);
00041 protected:
00042   jlong bridgePointer();
00043   void setBridgePointer(jlong bridgePointer);
00044   void setQObject(QObject *qObj);
00045 protected:
00046   virtual jfieldID bridgePointerFID() const = 0;
00047 protected slots:
00048   virtual void objectDestroyed(QObject *qObj);
00049 protected:
00050   inline JNIObject *jniObject() const;
00051   inline QObject *qObject() const;
00052   inline bool notifyDelete() const;
00053   inline void setNotifyDelete(bool notifyDelete);
00054   inline bool connectAuto(const QObject *sender, const char *signal,
00055                           const char *member) const;
00056   inline bool connectDirect(const QObject *sender, const char *signal,
00057                             const char *member) const;
00058   inline bool connectQueued(const char *signal, const char *member) const;
00059   inline bool disconnect(const char *signal, const char *member);
00060   inline bool disconnect(const QObject *sender, const char *signal,
00061                          const char *member);
00062 protected:
00063   inline static jmethodID disposeMID();
00064 private:
00065   JNIObject *m_jniObject;
00066   QObject *m_qObject;
00067   bool m_notifyDelete;
00068 private:
00069   static jmethodID sm_disposeMID;
00070 public:
00071   static const char *const BRIDGE_PROPERTY_NAME;
00072   static const QVariant INVALID_QVARIANT;
00073 };
00074 
00075 /*^**************************************************************************^*/
00076 /*- QObjectBridge :: Protected Inline Member Functions.                      -*/
00077 /*^**************************************************************************^*/
00078 
00084 inline JNIObject *QObjectBridge::jniObject() const
00085 {
00086   return m_jniObject;
00087 }
00088 
00094 inline QObject *QObjectBridge::qObject() const
00095 {
00096   return m_qObject;
00097 }
00098 
00104 inline bool QObjectBridge::notifyDelete() const
00105 {
00106   return m_notifyDelete;
00107 }
00108 
00114 inline void QObjectBridge::setNotifyDelete(bool notifyDelete)
00115 {
00116   m_notifyDelete = notifyDelete;
00117 }
00118 
00128 inline bool QObjectBridge::connectAuto(const QObject *sender,
00129                                          const char *signal,
00130                                          const char *member) const
00131 {
00132   return connect(sender, signal, this, member, Qt::AutoConnection);
00133 }
00134 
00145 inline bool QObjectBridge::connectDirect(const QObject *sender,
00146                                          const char *signal,
00147                                          const char *member) const
00148 {
00149   return connect(sender, signal, this, member, Qt::DirectConnection);
00150 }
00151 
00161 inline bool QObjectBridge::connectQueued(const char *signal,
00162                                          const char *member) const
00163 {
00164   return connect(this, signal, this, member, Qt::QueuedConnection);
00165 }
00166 
00175 inline bool QObjectBridge::disconnect(const char *signal, const char *member)
00176 {
00177   return QObject::disconnect(this, signal, this, member);
00178 }
00179 
00189 inline bool QObjectBridge::disconnect(const QObject *sender, const char *signal,
00190                                       const char *member)
00191 {
00192   return QObject::disconnect(sender, signal, this, member);
00193 }
00194 
00195 /*^**************************************************************************^*/
00196 /*- QObjectBridge :: Protected Static Inline Member Function.                -*/
00197 /*^**************************************************************************^*/
00198 
00204 inline jmethodID QObjectBridge::disposeMID()
00205 {
00206   return sm_disposeMID;
00207 }
00208 
00209 /*^**************************************************************************^*/
00210 /*- Related Non-Member Function.                                             -*/
00211 /*^**************************************************************************^*/
00212 
00220 template <typename T>
00221 inline T qObjectBridgePointer(QObject *qObj)
00222 {
00223   return static_cast<T>(qVariantValue<QObject *>(
00224                           qObj->property(QObjectBridge::BRIDGE_PROPERTY_NAME)));
00225 }
00226 
00227 /*^**************************************************************************^*/
00228 /*- Macro Definition.                                                        -*/
00229 /*^**************************************************************************^*/
00230 
00231 #define qObjectBridge (reinterpret_cast<QObjectBridge *>(bridge))
00232 
00233 #endif // QOBJECTBRIDGE_H
00234 
00235 /*^*****************************************************************************
00236   File History:
00237 
00238  - 2007-05-20 20:44:27 Slobodan
00239    Initial version
00240 
00241 *****************************************************************************^*/

Copyright © 2007 ETF and contributors. All Rights Reserved.