src/corelib/statemachine/qeventtransition.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 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 #include "qeventtransition.h"
       
    43 
       
    44 #ifndef QT_NO_STATEMACHINE
       
    45 
       
    46 #include "qeventtransition_p.h"
       
    47 #include "qstate.h"
       
    48 #include "qstate_p.h"
       
    49 #include "qstatemachine.h"
       
    50 #include "qstatemachine_p.h"
       
    51 #include <qdebug.h>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 /*!
       
    56   \class QEventTransition
       
    57 
       
    58   \brief The QEventTransition class provides a QObject-specific transition for Qt events.
       
    59 
       
    60   \since 4.6
       
    61   \ingroup statemachine
       
    62 
       
    63   A QEventTransition object binds an event to a particular QObject.
       
    64   QEventTransition is part of \l{The State Machine Framework}.
       
    65 
       
    66   Example:
       
    67 
       
    68   \code
       
    69   QPushButton *button = ...;
       
    70   QState *s1 = ...;
       
    71   QState *s2 = ...;
       
    72   // If in s1 and the button receives an Enter event, transition to s2
       
    73   QEventTransition *enterTransition = new QEventTransition(button, QEvent::Enter);
       
    74   enterTransition->setTargetState(s2);
       
    75   s1->addTransition(enterTransition);
       
    76   // If in s2 and the button receives an Exit event, transition back to s1
       
    77   QEventTransition *leaveTransition = new QEventTransition(button, QEvent::Leave);
       
    78   leaveTransition->setTargetState(s1);
       
    79   s2->addTransition(leaveTransition);
       
    80   \endcode
       
    81 
       
    82   \section1 Subclassing
       
    83 
       
    84   When reimplementing the eventTest() function, you should first call the base
       
    85   implementation to verify that the event is a QStateMachine::WrappedEvent for
       
    86   the proper object and event type. You may then cast the event to a
       
    87   QStateMachine::WrappedEvent and get the original event by calling
       
    88   QStateMachine::WrappedEvent::event(), and perform additional checks on that
       
    89   object.
       
    90 
       
    91   \sa QState::addTransition()
       
    92 */
       
    93 
       
    94 /*!
       
    95     \property QEventTransition::eventSource
       
    96 
       
    97     \brief the event source that this event transition is associated with
       
    98 */
       
    99 
       
   100 /*!
       
   101     \property QEventTransition::eventType
       
   102 
       
   103     \brief the type of event that this event transition is associated with
       
   104 */
       
   105 QEventTransitionPrivate::QEventTransitionPrivate()
       
   106 {
       
   107     object = 0;
       
   108     eventType = QEvent::None;
       
   109     registered = false;
       
   110 }
       
   111 
       
   112 QEventTransitionPrivate *QEventTransitionPrivate::get(QEventTransition *q)
       
   113 {
       
   114     return q->d_func();
       
   115 }
       
   116 
       
   117 void QEventTransitionPrivate::unregister()
       
   118 {
       
   119     Q_Q(QEventTransition);
       
   120     if (!registered || !machine())
       
   121         return;
       
   122     QStateMachinePrivate::get(machine())->unregisterEventTransition(q);
       
   123 }
       
   124 
       
   125 void QEventTransitionPrivate::maybeRegister()
       
   126 {
       
   127     Q_Q(QEventTransition);
       
   128     if (!machine() || !machine()->configuration().contains(sourceState()))
       
   129         return;
       
   130     QStateMachinePrivate::get(machine())->registerEventTransition(q);
       
   131 }
       
   132 
       
   133 /*!
       
   134   Constructs a new QEventTransition object with the given \a sourceState.
       
   135 */
       
   136 QEventTransition::QEventTransition(QState *sourceState)
       
   137     : QAbstractTransition(*new QEventTransitionPrivate, sourceState)
       
   138 {
       
   139 }
       
   140 
       
   141 /*!
       
   142   Constructs a new QEventTransition object associated with events of the given
       
   143   \a type for the given \a object, and with the given \a sourceState.
       
   144 */
       
   145 QEventTransition::QEventTransition(QObject *object, QEvent::Type type,
       
   146                                    QState *sourceState)
       
   147     : QAbstractTransition(*new QEventTransitionPrivate, sourceState)
       
   148 {
       
   149     Q_D(QEventTransition);
       
   150     d->registered = false;
       
   151     d->object = object;
       
   152     d->eventType = type;
       
   153 }
       
   154 
       
   155 /*!
       
   156   \internal
       
   157 */
       
   158 QEventTransition::QEventTransition(QEventTransitionPrivate &dd, QState *parent)
       
   159     : QAbstractTransition(dd, parent)
       
   160 {
       
   161 }
       
   162 
       
   163 /*!
       
   164   \internal
       
   165 */
       
   166 QEventTransition::QEventTransition(QEventTransitionPrivate &dd, QObject *object,
       
   167                                    QEvent::Type type, QState *parent)
       
   168     : QAbstractTransition(dd, parent)
       
   169 {
       
   170     Q_D(QEventTransition);
       
   171     d->registered = false;
       
   172     d->object = object;
       
   173     d->eventType = type;
       
   174 }
       
   175 
       
   176 /*!
       
   177   Destroys this QObject event transition.
       
   178 */
       
   179 QEventTransition::~QEventTransition()
       
   180 {
       
   181 }
       
   182 
       
   183 /*!
       
   184   Returns the event type that this event transition is associated with.
       
   185 */
       
   186 QEvent::Type QEventTransition::eventType() const
       
   187 {
       
   188     Q_D(const QEventTransition);
       
   189     return d->eventType;
       
   190 }
       
   191 
       
   192 /*!
       
   193   Sets the event \a type that this event transition is associated with.
       
   194 */
       
   195 void QEventTransition::setEventType(QEvent::Type type)
       
   196 {
       
   197     Q_D(QEventTransition);
       
   198     if (d->eventType == type)
       
   199         return;
       
   200     d->unregister();
       
   201     d->eventType = type;
       
   202     d->maybeRegister();
       
   203 }
       
   204 
       
   205 /*!
       
   206   Returns the event source associated with this event transition.
       
   207 */
       
   208 QObject *QEventTransition::eventSource() const
       
   209 {
       
   210     Q_D(const QEventTransition);
       
   211     return d->object;
       
   212 }
       
   213 
       
   214 /*!
       
   215   Sets the event source associated with this event transition to be the given
       
   216   \a object.
       
   217 */
       
   218 void QEventTransition::setEventSource(QObject *object)
       
   219 {
       
   220     Q_D(QEventTransition);
       
   221     if (d->object == object)
       
   222         return;
       
   223     d->unregister();
       
   224     d->object = object;
       
   225     d->maybeRegister();
       
   226 }
       
   227 
       
   228 /*!
       
   229   \reimp
       
   230 */
       
   231 bool QEventTransition::eventTest(QEvent *event)
       
   232 {
       
   233     Q_D(const QEventTransition);
       
   234     if (event->type() == QEvent::StateMachineWrapped) {
       
   235         QStateMachine::WrappedEvent *we = static_cast<QStateMachine::WrappedEvent*>(event);
       
   236         return (we->object() == d->object)
       
   237             && (we->event()->type() == d->eventType);
       
   238     }
       
   239     return false;
       
   240 }
       
   241 
       
   242 /*!
       
   243   \reimp
       
   244 */
       
   245 void QEventTransition::onTransition(QEvent *event)
       
   246 {
       
   247     Q_UNUSED(event);
       
   248 }
       
   249 
       
   250 /*!
       
   251   \reimp
       
   252 */
       
   253 bool QEventTransition::event(QEvent *e)
       
   254 {
       
   255     return QAbstractTransition::event(e);
       
   256 }
       
   257 
       
   258 QT_END_NAMESPACE
       
   259 
       
   260 #endif //QT_NO_STATEMACHINE