src/qt3support/tools/q3signal.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 Qt3Support 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 #include "q3signal.h"
       
    43 #include "qmetaobject.h"
       
    44 #include "qpointer.h"
       
    45 #include "q3cstring.h"
       
    46 
       
    47 QT_BEGIN_NAMESPACE
       
    48 
       
    49 /*!
       
    50     \class Q3Signal
       
    51     \brief The Q3Signal class can be used to send signals for classes
       
    52     that don't inherit QObject.
       
    53 
       
    54     \compat
       
    55 
       
    56     If you want to send signals from a class that does not inherit
       
    57     QObject, you can create an internal Q3Signal object to emit the
       
    58     signal. You must also provide a function that connects the signal
       
    59     to an outside object slot.  This is how we used to implement
       
    60     signals in Qt 3's QMenuData class, which was not a QObject. In Qt
       
    61     4, menus contain actions, which are QObjects.
       
    62 
       
    63     In general, we recommend inheriting QObject instead. QObject
       
    64     provides much more functionality.
       
    65 
       
    66     You can set a single QVariant parameter for the signal with
       
    67     setValue().
       
    68 
       
    69     Note that QObject is a \e private base class of Q3Signal, i.e. you
       
    70     cannot call any QObject member functions from a Q3Signal object.
       
    71 
       
    72     Example:
       
    73     \snippet doc/src/snippets/code/src_qt3support_tools_q3signal.cpp 0
       
    74 */
       
    75 
       
    76 /*!
       
    77     Constructs a signal object called \a name, with the parent object
       
    78     \a parent. These arguments are passed directly to QObject.
       
    79 */
       
    80 
       
    81 Q3Signal::Q3Signal(QObject *parent, const char *name)
       
    82     : QObject(parent, name)
       
    83 {
       
    84 #ifndef QT_NO_VARIANT
       
    85     val = 0;
       
    86 #endif
       
    87 }
       
    88 
       
    89 /*!
       
    90     Destroys the signal. All connections are removed, as is the case
       
    91     with all QObjects.
       
    92 */
       
    93 Q3Signal::~Q3Signal()
       
    94 {
       
    95 }
       
    96 #ifndef QT_NO_VARIANT
       
    97 // Returns true if it matches ".+(.*int.*"
       
    98 static inline bool intSignature(const char *member)
       
    99 {
       
   100     Q3CString s(member);
       
   101     int p = s.find('(');
       
   102     return p > 0 && p < s.findRev("int");
       
   103 }
       
   104 #endif
       
   105 /*!
       
   106     Connects the signal to \a member in object \a receiver.
       
   107     Returns true if the connection is successful.
       
   108 
       
   109     \sa disconnect(), QObject::connect()
       
   110 */
       
   111 
       
   112 bool Q3Signal::connect(const QObject *receiver, const char *member)
       
   113 {
       
   114 #ifndef QT_NO_VARIANT
       
   115     if (intSignature(member))
       
   116 #endif
       
   117 	return QObject::connect((QObject *)this, SIGNAL(intSignal(int)), receiver, member);
       
   118 #ifndef QT_NO_VARIANT
       
   119     return QObject::connect((QObject *)this, SIGNAL(signal(QVariant)),
       
   120 			     receiver, member);
       
   121 #endif
       
   122 }
       
   123 
       
   124 /*!
       
   125     Disonnects the signal from \a member in object \a receiver.
       
   126     Returns true if the connection existed and the disconnect
       
   127     was successful.
       
   128 
       
   129     \sa connect(), QObject::disconnect()
       
   130 */
       
   131 
       
   132 bool Q3Signal::disconnect(const QObject *receiver, const char *member)
       
   133 {
       
   134     if (!member)
       
   135 	return QObject::disconnect((QObject *)this, 0, receiver, member);
       
   136 #ifndef QT_NO_VARIANT
       
   137     if (intSignature(member))
       
   138 #endif
       
   139 	return QObject::disconnect((QObject *)this, SIGNAL(intSignal(int)), receiver, member);
       
   140 #ifndef QT_NO_VARIANT
       
   141     return QObject::disconnect((QObject *)this, SIGNAL(signal(QVariant)),
       
   142 				receiver, member);
       
   143 #endif
       
   144 }
       
   145 
       
   146 
       
   147 /*!
       
   148   \fn bool Q3Signal::isBlocked() const
       
   149   \obsolete
       
   150   Returns true if the signal is blocked, or false if it is not blocked.
       
   151 
       
   152   The signal is not blocked by default.
       
   153 
       
   154   \sa block(), QObject::signalsBlocked()
       
   155 */
       
   156 
       
   157 /*!
       
   158   \fn void Q3Signal::block(bool b)
       
   159   \obsolete
       
   160   Blocks the signal if \a b is true, or unblocks the signal if \a b is false.
       
   161 
       
   162   An activated signal disappears into hyperspace if it is blocked.
       
   163 
       
   164   \sa isBlocked(), activate(), QObject::blockSignals()
       
   165 */
       
   166 
       
   167 
       
   168 /*!
       
   169     \fn void Q3Signal::activate()
       
   170 
       
   171     Emits the signal. If the platform supports QVariant and a
       
   172     parameter has been set with setValue(), this value is passed in
       
   173     the signal.
       
   174 */
       
   175 void  Q3Signal::activate()
       
   176 {
       
   177 #ifndef QT_NO_VARIANT
       
   178     /* Create this Q3GuardedPtr on this, if we get destroyed after the intSignal (but before the variant signal)
       
   179        we cannot just emit the signal (because val has been destroyed already) */
       
   180     QPointer<Q3Signal> me = this;
       
   181     if(me)
       
   182 	emit intSignal(val.toInt());
       
   183     if(me)
       
   184 	emit signal(val);
       
   185 #else
       
   186     emit intSignal(0);
       
   187 #endif
       
   188 }
       
   189 
       
   190 #ifndef QT_NO_VARIANT
       
   191 /*!
       
   192     Sets the signal's parameter to \a value
       
   193 */
       
   194 void Q3Signal::setValue(const QVariant &value)
       
   195 {
       
   196     val = value;
       
   197 }
       
   198 
       
   199 /*!
       
   200     Returns the signal's parameter
       
   201 */
       
   202 QVariant Q3Signal::value() const
       
   203 {
       
   204     return val;
       
   205 }
       
   206 /*! \fn void Q3Signal::signal(const QVariant &)
       
   207     \internal
       
   208 */
       
   209 /*! \fn void Q3Signal::intSignal(int)
       
   210     \internal
       
   211 */
       
   212 
       
   213 /*! \obsolete */
       
   214 void Q3Signal::setParameter(int value)
       
   215 {
       
   216     val = value;
       
   217 }
       
   218 
       
   219 /*! \obsolete */
       
   220 int Q3Signal::parameter() const
       
   221 {
       
   222     return val.toInt();
       
   223 }
       
   224 #endif //QT_NO_VARIANT
       
   225 
       
   226 QT_END_NAMESPACE