| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qsignal.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of QSignal class
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 941201
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qsignal.h"
|
|---|
| 39 | #include "qmetaobject.h"
|
|---|
| 40 | #include "qguardedptr.h"
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \class QSignal qsignal.h
|
|---|
| 44 | \brief The QSignal class can be used to send signals for classes
|
|---|
| 45 | that don't inherit QObject.
|
|---|
| 46 |
|
|---|
| 47 | \ingroup io
|
|---|
| 48 | \ingroup misc
|
|---|
| 49 |
|
|---|
| 50 | If you want to send signals from a class that does not inherit
|
|---|
| 51 | QObject, you can create an internal QSignal object to emit the
|
|---|
| 52 | signal. You must also provide a function that connects the signal
|
|---|
| 53 | to an outside object slot. This is how we have implemented
|
|---|
| 54 | signals in the QMenuData class, which is not a QObject.
|
|---|
| 55 |
|
|---|
| 56 | In general, we recommend inheriting QObject instead. QObject
|
|---|
| 57 | provides much more functionality.
|
|---|
| 58 |
|
|---|
| 59 | You can set a single QVariant parameter for the signal with
|
|---|
| 60 | setValue().
|
|---|
| 61 |
|
|---|
| 62 | Note that QObject is a \e private base class of QSignal, i.e. you
|
|---|
| 63 | cannot call any QObject member functions from a QSignal object.
|
|---|
| 64 |
|
|---|
| 65 | Example:
|
|---|
| 66 | \code
|
|---|
| 67 | #include <qsignal.h>
|
|---|
| 68 |
|
|---|
| 69 | class MyClass
|
|---|
| 70 | {
|
|---|
| 71 | public:
|
|---|
| 72 | MyClass();
|
|---|
| 73 | ~MyClass();
|
|---|
| 74 |
|
|---|
| 75 | void doSomething();
|
|---|
| 76 |
|
|---|
| 77 | void connect( QObject *receiver, const char *member );
|
|---|
| 78 |
|
|---|
| 79 | private:
|
|---|
| 80 | QSignal *sig;
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | MyClass::MyClass()
|
|---|
| 84 | {
|
|---|
| 85 | sig = new QSignal;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | MyClass::~MyClass()
|
|---|
| 89 | {
|
|---|
| 90 | delete sig;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void MyClass::doSomething()
|
|---|
| 94 | {
|
|---|
| 95 | // ... does something
|
|---|
| 96 | sig->activate(); // emits the signal
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void MyClass::connect( QObject *receiver, const char *member )
|
|---|
| 100 | {
|
|---|
| 101 | sig->connect( receiver, member );
|
|---|
| 102 | }
|
|---|
| 103 | \endcode
|
|---|
| 104 | */
|
|---|
| 105 |
|
|---|
| 106 | /*!
|
|---|
| 107 | Constructs a signal object called \a name, with the parent object
|
|---|
| 108 | \a parent. These arguments are passed directly to QObject.
|
|---|
| 109 | */
|
|---|
| 110 |
|
|---|
| 111 | QSignal::QSignal( QObject *parent, const char *name )
|
|---|
| 112 | : QObject( parent, name )
|
|---|
| 113 | {
|
|---|
| 114 | isSignal = TRUE;
|
|---|
| 115 | #ifndef QT_NO_VARIANT
|
|---|
| 116 | val = 0;
|
|---|
| 117 | #endif
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /*!
|
|---|
| 121 | Destroys the signal. All connections are removed, as is the case
|
|---|
| 122 | with all QObjects.
|
|---|
| 123 | */
|
|---|
| 124 | QSignal::~QSignal()
|
|---|
| 125 | {
|
|---|
| 126 | }
|
|---|
| 127 | #ifndef QT_NO_VARIANT
|
|---|
| 128 | // Returns TRUE if it matches ".+(.*int.*"
|
|---|
| 129 | static inline bool intSignature( const char *member )
|
|---|
| 130 | {
|
|---|
| 131 | QCString s( member );
|
|---|
| 132 | int p = s.find( '(' );
|
|---|
| 133 | return p > 0 && p < s.findRev( "int" );
|
|---|
| 134 | }
|
|---|
| 135 | #endif
|
|---|
| 136 | /*!
|
|---|
| 137 | Connects the signal to \a member in object \a receiver.
|
|---|
| 138 |
|
|---|
| 139 | \sa disconnect(), QObject::connect()
|
|---|
| 140 | */
|
|---|
| 141 |
|
|---|
| 142 | bool QSignal::connect( const QObject *receiver, const char *member )
|
|---|
| 143 | {
|
|---|
| 144 | #ifndef QT_NO_VARIANT
|
|---|
| 145 | if ( intSignature( member ) )
|
|---|
| 146 | #endif
|
|---|
| 147 | return QObject::connect( (QObject *)this, SIGNAL(intSignal(int)), receiver, member );
|
|---|
| 148 | #ifndef QT_NO_VARIANT
|
|---|
| 149 | return QObject::connect( (QObject *)this, SIGNAL(signal(const QVariant&)),
|
|---|
| 150 | receiver, member );
|
|---|
| 151 | #endif
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /*!
|
|---|
| 155 | Disonnects the signal from \a member in object \a receiver.
|
|---|
| 156 |
|
|---|
| 157 | \sa connect(), QObject::disconnect()
|
|---|
| 158 | */
|
|---|
| 159 |
|
|---|
| 160 | bool QSignal::disconnect( const QObject *receiver, const char *member )
|
|---|
| 161 | {
|
|---|
| 162 | if (!member)
|
|---|
| 163 | return QObject::disconnect( (QObject *)this, 0, receiver, member);
|
|---|
| 164 | #ifndef QT_NO_VARIANT
|
|---|
| 165 | if ( intSignature( member ) )
|
|---|
| 166 | #endif
|
|---|
| 167 | return QObject::disconnect( (QObject *)this, SIGNAL(intSignal(int)), receiver, member );
|
|---|
| 168 | #ifndef QT_NO_VARIANT
|
|---|
| 169 | return QObject::disconnect( (QObject *)this, SIGNAL(signal(const QVariant&)),
|
|---|
| 170 | receiver, member );
|
|---|
| 171 | #endif
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 | /*!
|
|---|
| 176 | \fn bool QSignal::isBlocked() const
|
|---|
| 177 | \obsolete
|
|---|
| 178 | Returns TRUE if the signal is blocked, or FALSE if it is not blocked.
|
|---|
| 179 |
|
|---|
| 180 | The signal is not blocked by default.
|
|---|
| 181 |
|
|---|
| 182 | \sa block(), QObject::signalsBlocked()
|
|---|
| 183 | */
|
|---|
| 184 |
|
|---|
| 185 | /*!
|
|---|
| 186 | \fn void QSignal::block( bool b )
|
|---|
| 187 | \obsolete
|
|---|
| 188 | Blocks the signal if \a b is TRUE, or unblocks the signal if \a b is FALSE.
|
|---|
| 189 |
|
|---|
| 190 | An activated signal disappears into hyperspace if it is blocked.
|
|---|
| 191 |
|
|---|
| 192 | \sa isBlocked(), activate(), QObject::blockSignals()
|
|---|
| 193 | */
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 | /*!
|
|---|
| 197 | \fn void QSignal::activate()
|
|---|
| 198 |
|
|---|
| 199 | Emits the signal. If the platform supports QVariant and a
|
|---|
| 200 | parameter has been set with setValue(), this value is passed in
|
|---|
| 201 | the signal.
|
|---|
| 202 | */
|
|---|
| 203 | void QSignal::activate()
|
|---|
| 204 | {
|
|---|
| 205 | #ifndef QT_NO_VARIANT
|
|---|
| 206 | /* Create this QGuardedPtr on this, if we get destroyed after the intSignal (but before the variant signal)
|
|---|
| 207 | we cannot just emit the signal (because val has been destroyed already) */
|
|---|
| 208 | QGuardedPtr<QSignal> me = this;
|
|---|
| 209 | if( me )
|
|---|
| 210 | emit intSignal( val.toInt() );
|
|---|
| 211 | if( me )
|
|---|
| 212 | emit signal( val );
|
|---|
| 213 | #else
|
|---|
| 214 | emit intSignal(0);
|
|---|
| 215 | #endif
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | #ifndef QT_NO_VARIANT
|
|---|
| 219 | /*!
|
|---|
| 220 | Sets the signal's parameter to \a value
|
|---|
| 221 | */
|
|---|
| 222 | void QSignal::setValue( const QVariant &value )
|
|---|
| 223 | {
|
|---|
| 224 | val = value;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /*!
|
|---|
| 228 | Returns the signal's parameter
|
|---|
| 229 | */
|
|---|
| 230 | QVariant QSignal::value() const
|
|---|
| 231 | {
|
|---|
| 232 | return val;
|
|---|
| 233 | }
|
|---|
| 234 | /*! \fn void QSignal::signal( const QVariant & )
|
|---|
| 235 | \internal
|
|---|
| 236 | */
|
|---|
| 237 | /*! \fn void QSignal::intSignal( int )
|
|---|
| 238 | \internal
|
|---|
| 239 | */
|
|---|
| 240 |
|
|---|
| 241 | #ifndef QT_NO_COMPAT
|
|---|
| 242 | /*! \obsolete */
|
|---|
| 243 | void QSignal::setParameter( int value )
|
|---|
| 244 | {
|
|---|
| 245 | val = value;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /*! \obsolete */
|
|---|
| 249 | int QSignal::parameter() const
|
|---|
| 250 | {
|
|---|
| 251 | return val.toInt();
|
|---|
| 252 | }
|
|---|
| 253 | #endif
|
|---|
| 254 | #endif //QT_NO_VARIANT
|
|---|