src/corelib/kernel/qobject.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QOBJECT_H
       
    43 #define QOBJECT_H
       
    44 
       
    45 #ifndef QT_NO_QOBJECT
       
    46 
       
    47 #include <QtCore/qobjectdefs.h>
       
    48 #include <QtCore/qstring.h>
       
    49 #include <QtCore/qbytearray.h>
       
    50 #include <QtCore/qlist.h>
       
    51 #ifdef QT_INCLUDE_COMPAT
       
    52 #include <QtCore/qcoreevent.h>
       
    53 #endif
       
    54 #include <QtCore/qscopedpointer.h>
       
    55 
       
    56 QT_BEGIN_HEADER
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 QT_MODULE(Core)
       
    61 
       
    62 class QEvent;
       
    63 class QTimerEvent;
       
    64 class QChildEvent;
       
    65 struct QMetaObject;
       
    66 class QVariant;
       
    67 class QObjectPrivate;
       
    68 class QObject;
       
    69 class QThread;
       
    70 class QWidget;
       
    71 #ifndef QT_NO_REGEXP
       
    72 class QRegExp;
       
    73 #endif
       
    74 #ifndef QT_NO_USERDATA
       
    75 class QObjectUserData;
       
    76 #endif
       
    77 
       
    78 typedef QList<QObject*> QObjectList;
       
    79 
       
    80 #if defined Q_CC_MSVC && _MSC_VER < 1300
       
    81 template<typename T> inline T qFindChild(const QObject *o, const QString &name = QString(), T = 0);
       
    82 template<typename T> inline QList<T> qFindChildren(const QObject *o, const QString &name = QString(), T = 0);
       
    83 # ifndef QT_NO_REGEXP
       
    84 template<typename T> inline QList<T> qFindChildren(const QObject *o, const QRegExp &re, T = 0);
       
    85 # endif
       
    86 #else
       
    87 template<typename T> inline T qFindChild(const QObject *, const QString & = QString());
       
    88 template<typename T> inline QList<T> qFindChildren(const QObject *, const QString & = QString());
       
    89 # ifndef QT_NO_REGEXP
       
    90 template<typename T> inline QList<T> qFindChildren(const QObject *, const QRegExp &);
       
    91 # endif
       
    92 #endif
       
    93 
       
    94 class QObjectData {
       
    95 public:
       
    96     virtual ~QObjectData() = 0;
       
    97     QObject *q_ptr;
       
    98     QObject *parent;
       
    99     QObjectList children;
       
   100 
       
   101     uint isWidget : 1;
       
   102     uint pendTimer : 1;
       
   103     uint blockSig : 1;
       
   104     uint wasDeleted : 1;
       
   105     uint ownObjectName : 1;
       
   106     uint sendChildEvents : 1;
       
   107     uint receiveChildEvents : 1;
       
   108     uint inEventHandler : 1;
       
   109     uint inThreadChangeEvent : 1;
       
   110     uint hasGuards : 1; //true iff there is one or more QPointer attached to this object
       
   111     uint unused : 22;
       
   112     int postedEvents;
       
   113     QMetaObject *metaObject; // assert dynamic
       
   114 };
       
   115 
       
   116 
       
   117 class Q_CORE_EXPORT QObject
       
   118 {
       
   119     Q_OBJECT
       
   120     Q_PROPERTY(QString objectName READ objectName WRITE setObjectName)
       
   121     Q_DECLARE_PRIVATE(QObject)
       
   122 
       
   123 public:
       
   124     Q_INVOKABLE explicit QObject(QObject *parent=0);
       
   125     virtual ~QObject();
       
   126 
       
   127     virtual bool event(QEvent *);
       
   128     virtual bool eventFilter(QObject *, QEvent *);
       
   129 
       
   130 #ifdef qdoc
       
   131     static QString tr(const char *sourceText, const char *comment = 0, int n = -1);
       
   132     static QString trUtf8(const char *sourceText, const char *comment = 0, int n = -1);
       
   133     virtual const QMetaObject *metaObject() const;
       
   134     static const QMetaObject staticMetaObject;
       
   135 #endif
       
   136 #ifdef QT_NO_TRANSLATION
       
   137     static QString tr(const char *sourceText, const char *, int)
       
   138         { return QString::fromLatin1(sourceText); }
       
   139     static QString tr(const char *sourceText, const char * = 0)
       
   140         { return QString::fromLatin1(sourceText); }
       
   141 #ifndef QT_NO_TEXTCODEC
       
   142     static QString trUtf8(const char *sourceText, const char *, int)
       
   143         { return QString::fromUtf8(sourceText); }
       
   144     static QString trUtf8(const char *sourceText, const char * = 0)
       
   145         { return QString::fromUtf8(sourceText); }
       
   146 #endif
       
   147 #endif //QT_NO_TRANSLATION
       
   148 
       
   149     QString objectName() const;
       
   150     void setObjectName(const QString &name);
       
   151 
       
   152     inline bool isWidgetType() const { return d_ptr->isWidget; }
       
   153 
       
   154     inline bool signalsBlocked() const { return d_ptr->blockSig; }
       
   155     bool blockSignals(bool b);
       
   156 
       
   157     QThread *thread() const;
       
   158     void moveToThread(QThread *thread);
       
   159 
       
   160     int startTimer(int interval);
       
   161     void killTimer(int id);
       
   162 
       
   163 #ifndef QT_NO_MEMBER_TEMPLATES
       
   164     template<typename T>
       
   165     inline T findChild(const QString &aName = QString()) const
       
   166     { return qFindChild<T>(this, aName); }
       
   167 
       
   168     template<typename T>
       
   169     inline QList<T> findChildren(const QString &aName = QString()) const
       
   170     { return qFindChildren<T>(this, aName); }
       
   171 
       
   172 #ifndef QT_NO_REGEXP
       
   173     template<typename T>
       
   174     inline QList<T> findChildren(const QRegExp &re) const
       
   175     { return qFindChildren<T>(this, re); }
       
   176 #endif
       
   177 #endif
       
   178 
       
   179 #ifdef QT3_SUPPORT
       
   180     QT3_SUPPORT QObject *child(const char *objName, const char *inheritsClass = 0,
       
   181                    bool recursiveSearch = true) const;
       
   182     QT3_SUPPORT QObjectList queryList(const char *inheritsClass = 0,
       
   183                           const char *objName = 0,
       
   184                           bool regexpMatch = true,
       
   185                           bool recursiveSearch = true) const;
       
   186 #endif
       
   187     inline const QObjectList &children() const { return d_ptr->children; }
       
   188 
       
   189     void setParent(QObject *);
       
   190     void installEventFilter(QObject *);
       
   191     void removeEventFilter(QObject *);
       
   192 
       
   193 
       
   194     static bool connect(const QObject *sender, const char *signal,
       
   195                         const QObject *receiver, const char *member, Qt::ConnectionType =
       
   196 #ifdef qdoc
       
   197                         Qt::AutoConnection
       
   198 #else
       
   199 #ifdef QT3_SUPPORT
       
   200                         Qt::AutoCompatConnection
       
   201 #else
       
   202                         Qt::AutoConnection
       
   203 #endif
       
   204 #endif
       
   205         );
       
   206     inline bool connect(const QObject *sender, const char *signal,
       
   207                         const char *member, Qt::ConnectionType type =
       
   208 #ifdef qdoc
       
   209                         Qt::AutoConnection
       
   210 #else
       
   211 #ifdef QT3_SUPPORT
       
   212                         Qt::AutoCompatConnection
       
   213 #else
       
   214                         Qt::AutoConnection
       
   215 #endif
       
   216 #endif
       
   217         ) const;
       
   218 
       
   219     static bool disconnect(const QObject *sender, const char *signal,
       
   220                            const QObject *receiver, const char *member);
       
   221     inline bool disconnect(const char *signal = 0,
       
   222                            const QObject *receiver = 0, const char *member = 0)
       
   223         { return disconnect(this, signal, receiver, member); }
       
   224     inline bool disconnect(const QObject *receiver, const char *member = 0)
       
   225         { return disconnect(this, 0, receiver, member); }
       
   226 
       
   227     void dumpObjectTree();
       
   228     void dumpObjectInfo();
       
   229 
       
   230 #ifndef QT_NO_PROPERTIES
       
   231     bool setProperty(const char *name, const QVariant &value);
       
   232     QVariant property(const char *name) const;
       
   233     QList<QByteArray> dynamicPropertyNames() const;
       
   234 #endif // QT_NO_PROPERTIES
       
   235 
       
   236 #ifndef QT_NO_USERDATA
       
   237     static uint registerUserData();
       
   238     void setUserData(uint id, QObjectUserData* data);
       
   239     QObjectUserData* userData(uint id) const;
       
   240 #endif // QT_NO_USERDATA
       
   241 
       
   242 Q_SIGNALS:
       
   243     void destroyed(QObject * = 0);
       
   244 
       
   245 public:
       
   246     inline QObject *parent() const { return d_ptr->parent; }
       
   247 
       
   248     inline bool inherits(const char *classname) const
       
   249         { return const_cast<QObject *>(this)->qt_metacast(classname) != 0; }
       
   250 
       
   251 public Q_SLOTS:
       
   252     void deleteLater();
       
   253 
       
   254 protected:
       
   255     QObject *sender() const;
       
   256     int receivers(const char* signal) const;
       
   257 
       
   258     virtual void timerEvent(QTimerEvent *);
       
   259     virtual void childEvent(QChildEvent *);
       
   260     virtual void customEvent(QEvent *);
       
   261 
       
   262     virtual void connectNotify(const char *signal);
       
   263     virtual void disconnectNotify(const char *signal);
       
   264 
       
   265 #ifdef QT3_SUPPORT
       
   266 public:
       
   267     QT3_SUPPORT_CONSTRUCTOR QObject(QObject *parent, const char *name);
       
   268     inline QT3_SUPPORT void insertChild(QObject *o)
       
   269         { if (o) o->setParent(this); }
       
   270     inline QT3_SUPPORT void removeChild(QObject *o)
       
   271         { if (o) o->setParent(0); }
       
   272     inline QT3_SUPPORT bool isA(const char *classname) const
       
   273         { return qstrcmp(classname, metaObject()->className()) == 0; }
       
   274     inline QT3_SUPPORT const char *className() const { return metaObject()->className(); }
       
   275     inline QT3_SUPPORT const char *name() const { return objectName().latin1_helper(); }
       
   276     inline QT3_SUPPORT const char *name(const char *defaultName) const
       
   277         { QString s = objectName(); return s.isEmpty()?defaultName:s.latin1_helper(); }
       
   278     inline QT3_SUPPORT void setName(const char *aName) { setObjectName(QLatin1String(aName)); }
       
   279 protected:
       
   280     inline QT3_SUPPORT bool checkConnectArgs(const char *signal,
       
   281                                   const QObject *,
       
   282                                   const char *member)
       
   283         { return QMetaObject::checkConnectArgs(signal, member); }
       
   284     static inline QT3_SUPPORT QByteArray normalizeSignalSlot(const char *signalSlot)
       
   285         { return QMetaObject::normalizedSignature(signalSlot); }
       
   286 #endif
       
   287 
       
   288 protected:
       
   289     QObject(QObjectPrivate &dd, QObject *parent = 0);
       
   290 
       
   291 protected:
       
   292     QScopedPointer<QObjectData> d_ptr;
       
   293 
       
   294     static const QMetaObject staticQtMetaObject;
       
   295 
       
   296     friend struct QMetaObject;
       
   297     friend class QApplication;
       
   298     friend class QApplicationPrivate;
       
   299     friend class QCoreApplication;
       
   300     friend class QCoreApplicationPrivate;
       
   301     friend class QWidget;
       
   302     friend class QThreadData;
       
   303 
       
   304 private:
       
   305     Q_DISABLE_COPY(QObject)
       
   306     Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *))
       
   307 };
       
   308 
       
   309 inline bool QObject::connect(const QObject *asender, const char *asignal,
       
   310                              const char *amember, Qt::ConnectionType atype) const
       
   311 { return connect(asender, asignal, this, amember, atype); }
       
   312 
       
   313 #ifndef QT_NO_USERDATA
       
   314 class Q_CORE_EXPORT QObjectUserData {
       
   315 public:
       
   316     virtual ~QObjectUserData();
       
   317 };
       
   318 #endif
       
   319 
       
   320 Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re,
       
   321                                            const QMetaObject &mo, QList<void *> *list);
       
   322 Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo);
       
   323 
       
   324 #if defined Q_CC_MSVC && _MSC_VER < 1300
       
   325 
       
   326 template<typename T>
       
   327 inline T qFindChild(const QObject *o, const QString &name, T)
       
   328 { return static_cast<T>(qt_qFindChild_helper(o, name, ((T)0)->staticMetaObject)); }
       
   329 
       
   330 template<typename T>
       
   331 inline QList<T> qFindChildren(const QObject *o, const QString &name, T)
       
   332 {
       
   333     QList<T> list;
       
   334     union {
       
   335         QList<T> *typedList;
       
   336         QList<void *> *voidList;
       
   337     } u;
       
   338     u.typedList = &list;
       
   339     qt_qFindChildren_helper(o, name, 0, ((T)0)->staticMetaObject, u.voidList);
       
   340     return list;
       
   341 }
       
   342 
       
   343 template<typename T>
       
   344 inline T qFindChild(const QObject *o, const QString &name)
       
   345 { return qFindChild<T>(o, name, T(0)); }
       
   346 
       
   347 template<typename T>
       
   348 inline T qFindChild(const QObject *o)
       
   349 { return qFindChild<T>(o, QString(), T(0)); }
       
   350 
       
   351 template<typename T>
       
   352 inline QList<T> qFindChildren(const QObject *o, const QString &name)
       
   353 { return qFindChildren<T>(o, name, T(0)); }
       
   354 
       
   355 template<typename T>
       
   356 inline QList<T> qFindChildren(const QObject *o)
       
   357 { return qFindChildren<T>(o, QString(), T(0)); }
       
   358 
       
   359 #ifndef QT_NO_REGEXP
       
   360 template<typename T>
       
   361 inline QList<T> qFindChildren(const QObject *o, const QRegExp &re, T)
       
   362 {
       
   363     QList<T> list;
       
   364     union {
       
   365         QList<T> *typedList;
       
   366         QList<void *> *voidList;
       
   367     } u;
       
   368     u.typedList = &list;
       
   369     qt_qFindChildren_helper(o, 0, &re, ((T)0)->staticMetaObject, u.voidList);
       
   370     return list;
       
   371 }
       
   372 
       
   373 template<typename T>
       
   374 inline QList<T> qFindChildren(const QObject *o, const QRegExp &re)
       
   375 { return qFindChildren<T>(o, re, T(0)); }
       
   376 
       
   377 #endif
       
   378 
       
   379 #ifdef Q_MOC_RUN
       
   380 # define Q_DECLARE_INTERFACE(IFace, IId) Q_DECLARE_INTERFACE(IFace, IId)
       
   381 #endif // Q_MOC_RUN
       
   382 
       
   383 
       
   384 template <class T> inline const char * qobject_interface_iid()
       
   385 { return 0; }
       
   386 
       
   387 template <class T> inline T qobject_cast_helper(QObject *object, T)
       
   388 { return static_cast<T>(((T)0)->staticMetaObject.cast(object)); }
       
   389 
       
   390 template <class T> inline T qobject_cast_helper(const QObject *object, T)
       
   391 { return static_cast<T>(const_cast<const QObject *>(((T)0)->staticMetaObject.cast(const_cast<QObject *>(object)))); }
       
   392 
       
   393 template <class T>
       
   394 inline T qobject_cast(QObject *object)
       
   395 { return qobject_cast_helper<T>(object, T(0)); }
       
   396 
       
   397 template <class T>
       
   398 inline T qobject_cast(const QObject *object)
       
   399 { return qobject_cast_helper<T>(object, T(0)); }
       
   400 
       
   401 #ifndef Q_MOC_RUN
       
   402 #  define Q_DECLARE_INTERFACE(IFace, IId) \
       
   403     template <> inline const char *qobject_interface_iid<IFace *>() \
       
   404     { return IId; } \
       
   405     template <> inline IFace *qobject_cast_helper<IFace *>(QObject *object, IFace *) \
       
   406     { return (IFace *)(object ? object->qt_metacast(IId) : 0); } \
       
   407     template <> inline IFace *qobject_cast_helper<IFace *>(const QObject *object, IFace *) \
       
   408     { return (IFace *)(object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0); }
       
   409 #endif // Q_MOC_RUN
       
   410 
       
   411 #else
       
   412 
       
   413 template<typename T>
       
   414 inline T qFindChild(const QObject *o, const QString &name)
       
   415 { return static_cast<T>(qt_qFindChild_helper(o, name, reinterpret_cast<T>(0)->staticMetaObject)); }
       
   416 
       
   417 template<typename T>
       
   418 inline QList<T> qFindChildren(const QObject *o, const QString &name)
       
   419 {
       
   420     QList<T> list;
       
   421     union {
       
   422         QList<T> *typedList;
       
   423         QList<void *> *voidList;
       
   424     } u;
       
   425     u.typedList = &list;
       
   426     qt_qFindChildren_helper(o, name, 0, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
       
   427     return list;
       
   428 }
       
   429 
       
   430 #ifndef QT_NO_REGEXP
       
   431 template<typename T>
       
   432 inline QList<T> qFindChildren(const QObject *o, const QRegExp &re)
       
   433 {
       
   434     QList<T> list;
       
   435     union {
       
   436         QList<T> *typedList;
       
   437         QList<void *> *voidList;
       
   438     } u;
       
   439     u.typedList = &list;
       
   440     qt_qFindChildren_helper(o, QString(), &re, reinterpret_cast<T>(0)->staticMetaObject, u.voidList);
       
   441     return list;
       
   442 }
       
   443 #endif
       
   444 
       
   445 template <class T>
       
   446 inline T qobject_cast(QObject *object)
       
   447 {
       
   448 #if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
       
   449     reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(object));
       
   450 #endif
       
   451     return static_cast<T>(reinterpret_cast<T>(0)->staticMetaObject.cast(object));
       
   452 }
       
   453 
       
   454 template <class T>
       
   455 inline T qobject_cast(const QObject *object)
       
   456 {
       
   457     // this will cause a compilation error if T is not const
       
   458     register T ptr = static_cast<T>(object);
       
   459     Q_UNUSED(ptr);
       
   460 
       
   461 #if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(QT_NO_QOBJECT_CHECK)
       
   462     reinterpret_cast<T>(0)->qt_check_for_QOBJECT_macro(*reinterpret_cast<T>(const_cast<QObject *>(object)));
       
   463 #endif
       
   464     return static_cast<T>(const_cast<QObject *>(reinterpret_cast<T>(0)->staticMetaObject.cast(const_cast<QObject *>(object))));
       
   465 }
       
   466 
       
   467 
       
   468 template <class T> inline const char * qobject_interface_iid()
       
   469 { return 0; }
       
   470 
       
   471 #ifndef Q_MOC_RUN
       
   472 #  define Q_DECLARE_INTERFACE(IFace, IId) \
       
   473     template <> inline const char *qobject_interface_iid<IFace *>() \
       
   474     { return IId; } \
       
   475     template <> inline IFace *qobject_cast<IFace *>(QObject *object) \
       
   476     { return reinterpret_cast<IFace *>((object ? object->qt_metacast(IId) : 0)); } \
       
   477     template <> inline IFace *qobject_cast<IFace *>(const QObject *object) \
       
   478     { return reinterpret_cast<IFace *>((object ? const_cast<QObject *>(object)->qt_metacast(IId) : 0)); }
       
   479 #endif // Q_MOC_RUN
       
   480 
       
   481 #endif
       
   482 
       
   483 #ifndef QT_NO_DEBUG_STREAM
       
   484 Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *);
       
   485 #endif
       
   486 
       
   487 QT_END_NAMESPACE
       
   488 
       
   489 QT_END_HEADER
       
   490 
       
   491 #endif
       
   492 
       
   493 #endif // QOBJECT_H