src/corelib/statemachine/qabstractstate.cpp
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 #include "qabstractstate.h"
       
    43 
       
    44 #ifndef QT_NO_STATEMACHINE
       
    45 
       
    46 #include "qabstractstate_p.h"
       
    47 #include "qstate.h"
       
    48 #include "qstate_p.h"
       
    49 #include "qstatemachine.h"
       
    50 #include "qstatemachine_p.h"
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 /*!
       
    55   \class QAbstractState
       
    56 
       
    57   \brief The QAbstractState class is the base class of states of a QStateMachine.
       
    58 
       
    59   \since 4.6
       
    60   \ingroup statemachine
       
    61 
       
    62   The QAbstractState class is the abstract base class of states that are part
       
    63   of a QStateMachine. It defines the interface that all state objects have in
       
    64   common. QAbstractState is part of \l{The State Machine Framework}.
       
    65 
       
    66   The entered() signal is emitted when the state has been entered. The
       
    67   exited() signal is emitted when the state has been exited.
       
    68 
       
    69   The parentState() function returns the state's parent state. The machine()
       
    70   function returns the state machine that the state is part of.
       
    71 
       
    72   \section1 Subclassing
       
    73 
       
    74   The onEntry() function is called when the state is entered; reimplement this
       
    75   function to perform custom processing when the state is entered.
       
    76 
       
    77   The onExit() function is called when the state is exited; reimplement this
       
    78   function to perform custom processing when the state is exited.
       
    79 */
       
    80 
       
    81 QAbstractStatePrivate::QAbstractStatePrivate()    
       
    82 {
       
    83 }
       
    84 
       
    85 QAbstractStatePrivate *QAbstractStatePrivate::get(QAbstractState *q)
       
    86 {
       
    87     return q->d_func();
       
    88 }
       
    89 
       
    90 QStateMachine *QAbstractStatePrivate::machine() const
       
    91 {
       
    92     QObject *par = parent;
       
    93     while (par != 0) {
       
    94         if (QStateMachine *mach = qobject_cast<QStateMachine*>(par))
       
    95             return mach;
       
    96         par = par->parent();
       
    97     }
       
    98     return 0;
       
    99 }
       
   100 
       
   101 void QAbstractStatePrivate::callOnEntry(QEvent *e)
       
   102 {
       
   103     Q_Q(QAbstractState);
       
   104     q->onEntry(e);
       
   105 }
       
   106 
       
   107 void QAbstractStatePrivate::callOnExit(QEvent *e)
       
   108 {
       
   109     Q_Q(QAbstractState);
       
   110     q->onExit(e);
       
   111 }
       
   112 
       
   113 void QAbstractStatePrivate::emitEntered()
       
   114 {
       
   115     Q_Q(QAbstractState);
       
   116     emit q->entered();
       
   117 }
       
   118 
       
   119 void QAbstractStatePrivate::emitExited()
       
   120 {
       
   121     Q_Q(QAbstractState);
       
   122     emit q->exited();
       
   123 }
       
   124 
       
   125 /*!
       
   126   Constructs a new state with the given \a parent state.
       
   127 */
       
   128 QAbstractState::QAbstractState(QState *parent)
       
   129     : QObject(*new QAbstractStatePrivate, parent)
       
   130 {
       
   131 }
       
   132 
       
   133 /*!
       
   134   \internal
       
   135 */
       
   136 QAbstractState::QAbstractState(QAbstractStatePrivate &dd, QState *parent)
       
   137     : QObject(dd, parent)
       
   138 {
       
   139 }
       
   140 
       
   141 /*!
       
   142   Destroys this state.
       
   143 */
       
   144 QAbstractState::~QAbstractState()
       
   145 {
       
   146 }
       
   147 
       
   148 /*!
       
   149   Returns this state's parent state, or 0 if the state has no parent state.
       
   150 */
       
   151 QState *QAbstractState::parentState() const
       
   152 {
       
   153     return qobject_cast<QState*>(parent());
       
   154 }
       
   155 
       
   156 /*!
       
   157   Returns the state machine that this state is part of, or 0 if the state is
       
   158   not part of a state machine.
       
   159 */
       
   160 QStateMachine *QAbstractState::machine() const
       
   161 {
       
   162     Q_D(const QAbstractState);
       
   163     return d->machine();
       
   164 }
       
   165 
       
   166 /*!
       
   167   \fn QAbstractState::onExit(QEvent *event)
       
   168 
       
   169   This function is called when the state is exited. The given \a event is what
       
   170   caused the state to be exited. Reimplement this function to perform custom
       
   171   processing when the state is exited.
       
   172 */
       
   173 
       
   174 /*!
       
   175   \fn QAbstractState::onEntry(QEvent *event)
       
   176 
       
   177   This function is called when the state is entered. The given \a event is
       
   178   what caused the state to be entered. Reimplement this function to perform
       
   179   custom processing when the state is entered.
       
   180 */
       
   181 
       
   182 /*!
       
   183   \fn QAbstractState::entered()
       
   184 
       
   185   This signal is emitted when the state has been entered (after onEntry() has
       
   186   been called).
       
   187 */
       
   188 
       
   189 /*!
       
   190   \fn QAbstractState::exited()
       
   191 
       
   192   This signal is emitted when the state has been exited (after onExit() has
       
   193   been called).
       
   194 */
       
   195 
       
   196 /*!
       
   197   \reimp
       
   198 */
       
   199 bool QAbstractState::event(QEvent *e)
       
   200 {
       
   201     return QObject::event(e);
       
   202 }
       
   203 
       
   204 QT_END_NAMESPACE
       
   205 
       
   206 #endif //QT_NO_STATEMACHINE