| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/statemachine/qsignaltransition.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qsignaltransition.h" | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #ifndef QT_NO_STATEMACHINE | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | #include "qsignaltransition_p.h" | - | ||||||||||||
| 39 | #include "qstate.h" | - | ||||||||||||
| 40 | #include "qstate_p.h" | - | ||||||||||||
| 41 | #include "qstatemachine.h" | - | ||||||||||||
| 42 | #include "qstatemachine_p.h" | - | ||||||||||||
| 43 | #include <qdebug.h> | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | /*! | - | ||||||||||||
| 48 | \class QSignalTransition | - | ||||||||||||
| 49 | \inmodule QtCore | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | \brief The QSignalTransition class provides a transition based on a Qt signal. | - | ||||||||||||
| 52 | - | |||||||||||||
| 53 | \since 4.6 | - | ||||||||||||
| 54 | \ingroup statemachine | - | ||||||||||||
| 55 | - | |||||||||||||
| 56 | Typically you would use the overload of QState::addTransition() that takes a | - | ||||||||||||
| 57 | sender and signal as arguments, rather than creating QSignalTransition | - | ||||||||||||
| 58 | objects directly. QSignalTransition is part of \l{The State Machine | - | ||||||||||||
| 59 | Framework}. | - | ||||||||||||
| 60 | - | |||||||||||||
| 61 | You can subclass QSignalTransition and reimplement eventTest() to make a | - | ||||||||||||
| 62 | signal transition conditional; the event object passed to eventTest() will | - | ||||||||||||
| 63 | be a QStateMachine::SignalEvent object. Example: | - | ||||||||||||
| 64 | - | |||||||||||||
| 65 | \code | - | ||||||||||||
| 66 | class CheckedTransition : public QSignalTransition | - | ||||||||||||
| 67 | { | - | ||||||||||||
| 68 | public: | - | ||||||||||||
| 69 | CheckedTransition(QCheckBox *check) | - | ||||||||||||
| 70 | : QSignalTransition(check, SIGNAL(stateChanged(int))) {} | - | ||||||||||||
| 71 | protected: | - | ||||||||||||
| 72 | bool eventTest(QEvent *e) { | - | ||||||||||||
| 73 | if (!QSignalTransition::eventTest(e)) | - | ||||||||||||
| 74 | return false; | - | ||||||||||||
| 75 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e); | - | ||||||||||||
| 76 | return (se->arguments().at(0).toInt() == Qt::Checked); | - | ||||||||||||
| 77 | } | - | ||||||||||||
| 78 | }; | - | ||||||||||||
| 79 | - | |||||||||||||
| 80 | ... | - | ||||||||||||
| 81 | - | |||||||||||||
| 82 | QCheckBox *check = new QCheckBox(); | - | ||||||||||||
| 83 | check->setTristate(true); | - | ||||||||||||
| 84 | - | |||||||||||||
| 85 | QState *s1 = new QState(); | - | ||||||||||||
| 86 | QState *s2 = new QState(); | - | ||||||||||||
| 87 | CheckedTransition *t1 = new CheckedTransition(check); | - | ||||||||||||
| 88 | t1->setTargetState(s2); | - | ||||||||||||
| 89 | s1->addTransition(t1); | - | ||||||||||||
| 90 | \endcode | - | ||||||||||||
| 91 | */ | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | /*! | - | ||||||||||||
| 94 | \property QSignalTransition::senderObject | - | ||||||||||||
| 95 | - | |||||||||||||
| 96 | \brief the sender object that this signal transition is associated with | - | ||||||||||||
| 97 | */ | - | ||||||||||||
| 98 | - | |||||||||||||
| 99 | /*! | - | ||||||||||||
| 100 | \property QSignalTransition::signal | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | \brief the signal that this signal transition is associated with | - | ||||||||||||
| 103 | */ | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | QSignalTransitionPrivate::QSignalTransitionPrivate() | - | ||||||||||||
| 106 | { | - | ||||||||||||
| 107 | sender = 0; | - | ||||||||||||
| 108 | signalIndex = -1; | - | ||||||||||||
| 109 | } executed 74 times by 2 tests: end of blockExecuted by:
| 74 | ||||||||||||
| 110 | - | |||||||||||||
| 111 | void QSignalTransitionPrivate::unregister() | - | ||||||||||||
| 112 | { | - | ||||||||||||
| 113 | Q_Q(QSignalTransition); | - | ||||||||||||
| 114 | if ((signalIndex == -1) || !machine())
| 0-100010 | ||||||||||||
| 115 | return; executed 100010 times by 1 test: return;Executed by:
| 100010 | ||||||||||||
| 116 | QStateMachinePrivate::get(machine())->unregisterSignalTransition(q); | - | ||||||||||||
| 117 | } executed 100003 times by 1 test: end of blockExecuted by:
| 100003 | ||||||||||||
| 118 | - | |||||||||||||
| 119 | void QSignalTransitionPrivate::maybeRegister() | - | ||||||||||||
| 120 | { | - | ||||||||||||
| 121 | Q_Q(QSignalTransition); | - | ||||||||||||
| 122 | if (QStateMachine *mach = machine())
| 71-200010 | ||||||||||||
| 123 | QStateMachinePrivate::get(mach)->maybeRegisterSignalTransition(q); executed 200010 times by 2 tests: QStateMachinePrivate::get(mach)->maybeRegisterSignalTransition(q);Executed by:
| 200010 | ||||||||||||
| 124 | } executed 200081 times by 2 tests: end of blockExecuted by:
| 200081 | ||||||||||||
| 125 | - | |||||||||||||
| 126 | /*! | - | ||||||||||||
| 127 | Constructs a new signal transition with the given \a sourceState. | - | ||||||||||||
| 128 | */ | - | ||||||||||||
| 129 | QSignalTransition::QSignalTransition(QState *sourceState) | - | ||||||||||||
| 130 | : QAbstractTransition(*new QSignalTransitionPrivate, sourceState) | - | ||||||||||||
| 131 | { | - | ||||||||||||
| 132 | } executed 6 times by 1 test: end of blockExecuted by:
| 6 | ||||||||||||
| 133 | - | |||||||||||||
| 134 | /*! | - | ||||||||||||
| 135 | Constructs a new signal transition associated with the given \a signal of | - | ||||||||||||
| 136 | the given \a sender, and with the given \a sourceState. | - | ||||||||||||
| 137 | */ | - | ||||||||||||
| 138 | QSignalTransition::QSignalTransition(const QObject *sender, const char *signal, | - | ||||||||||||
| 139 | QState *sourceState) | - | ||||||||||||
| 140 | : QAbstractTransition(*new QSignalTransitionPrivate, sourceState) | - | ||||||||||||
| 141 | { | - | ||||||||||||
| 142 | Q_D(QSignalTransition); | - | ||||||||||||
| 143 | d->sender = sender; | - | ||||||||||||
| 144 | d->signal = signal; | - | ||||||||||||
| 145 | d->maybeRegister(); | - | ||||||||||||
| 146 | } executed 68 times by 2 tests: end of blockExecuted by:
| 68 | ||||||||||||
| 147 | - | |||||||||||||
| 148 | /*! | - | ||||||||||||
| 149 | Destroys this signal transition. | - | ||||||||||||
| 150 | */ | - | ||||||||||||
| 151 | QSignalTransition::~QSignalTransition() | - | ||||||||||||
| 152 | { | - | ||||||||||||
| 153 | } | - | ||||||||||||
| 154 | - | |||||||||||||
| 155 | /*! | - | ||||||||||||
| 156 | Returns the sender object associated with this signal transition. | - | ||||||||||||
| 157 | */ | - | ||||||||||||
| 158 | QObject *QSignalTransition::senderObject() const | - | ||||||||||||
| 159 | { | - | ||||||||||||
| 160 | Q_D(const QSignalTransition); | - | ||||||||||||
| 161 | return const_cast<QObject *>(d->sender); executed 161 times by 2 tests: return const_cast<QObject *>(d->sender);Executed by:
| 161 | ||||||||||||
| 162 | } | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | /*! | - | ||||||||||||
| 165 | Sets the \a sender object associated with this signal transition. | - | ||||||||||||
| 166 | */ | - | ||||||||||||
| 167 | void QSignalTransition::setSenderObject(const QObject *sender) | - | ||||||||||||
| 168 | { | - | ||||||||||||
| 169 | Q_D(QSignalTransition); | - | ||||||||||||
| 170 | if (sender == d->sender)
| 0-199230 | ||||||||||||
| 171 | return; never executed: return; | 0 | ||||||||||||
| 172 | d->unregister(); | - | ||||||||||||
| 173 | d->sender = sender; | - | ||||||||||||
| 174 | d->maybeRegister(); | - | ||||||||||||
| 175 | emit senderObjectChanged(QPrivateSignal()); | - | ||||||||||||
| 176 | } executed 200004 times by 1 test: end of blockExecuted by:
| 200004 | ||||||||||||
| 177 | - | |||||||||||||
| 178 | /*! | - | ||||||||||||
| 179 | Returns the signal associated with this signal transition. | - | ||||||||||||
| 180 | */ | - | ||||||||||||
| 181 | QByteArray QSignalTransition::signal() const | - | ||||||||||||
| 182 | { | - | ||||||||||||
| 183 | Q_D(const QSignalTransition); | - | ||||||||||||
| 184 | return d->signal; executed 10 times by 1 test: return d->signal;Executed by:
| 10 | ||||||||||||
| 185 | } | - | ||||||||||||
| 186 | - | |||||||||||||
| 187 | /*! | - | ||||||||||||
| 188 | Sets the \a signal associated with this signal transition. | - | ||||||||||||
| 189 | */ | - | ||||||||||||
| 190 | void QSignalTransition::setSignal(const QByteArray &signal) | - | ||||||||||||
| 191 | { | - | ||||||||||||
| 192 | Q_D(QSignalTransition); | - | ||||||||||||
| 193 | if (signal == d->signal)
| 0-9 | ||||||||||||
| 194 | return; never executed: return; | 0 | ||||||||||||
| 195 | d->unregister(); | - | ||||||||||||
| 196 | d->signal = signal; | - | ||||||||||||
| 197 | d->maybeRegister(); | - | ||||||||||||
| 198 | emit signalChanged(QPrivateSignal()); | - | ||||||||||||
| 199 | } executed 9 times by 1 test: end of blockExecuted by:
| 9 | ||||||||||||
| 200 | - | |||||||||||||
| 201 | /*! | - | ||||||||||||
| 202 | \reimp | - | ||||||||||||
| 203 | - | |||||||||||||
| 204 | The default implementation returns \c true if the \a event is a | - | ||||||||||||
| 205 | QStateMachine::SignalEvent object and the event's sender and signal index | - | ||||||||||||
| 206 | match this transition, and returns \c false otherwise. | - | ||||||||||||
| 207 | */ | - | ||||||||||||
| 208 | bool QSignalTransition::eventTest(QEvent *event) | - | ||||||||||||
| 209 | { | - | ||||||||||||
| 210 | Q_D(const QSignalTransition); | - | ||||||||||||
| 211 | if (event->type() == QEvent::StateMachineSignal) {
| 89-2203 | ||||||||||||
| 212 | if (d->signalIndex == -1)
| 0-89 | ||||||||||||
| 213 | return false; never executed: return false; | 0 | ||||||||||||
| 214 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event); | - | ||||||||||||
| 215 | return (se->sender() == d->sender) executed 89 times by 2 tests: return (se->sender() == d->sender) && (se->signalIndex() == d->signalIndex);Executed by:
| 9-89 | ||||||||||||
| 216 | && (se->signalIndex() == d->signalIndex); executed 89 times by 2 tests: return (se->sender() == d->sender) && (se->signalIndex() == d->signalIndex);Executed by:
| 3-89 | ||||||||||||
| 217 | } | - | ||||||||||||
| 218 | return false; executed 2203 times by 2 tests: return false;Executed by:
| 2203 | ||||||||||||
| 219 | } | - | ||||||||||||
| 220 | - | |||||||||||||
| 221 | /*! | - | ||||||||||||
| 222 | \reimp | - | ||||||||||||
| 223 | */ | - | ||||||||||||
| 224 | void QSignalTransition::onTransition(QEvent *event) | - | ||||||||||||
| 225 | { | - | ||||||||||||
| 226 | Q_UNUSED(event); | - | ||||||||||||
| 227 | } executed 71 times by 2 tests: end of blockExecuted by:
| 71 | ||||||||||||
| 228 | - | |||||||||||||
| 229 | /*! | - | ||||||||||||
| 230 | \reimp | - | ||||||||||||
| 231 | */ | - | ||||||||||||
| 232 | bool QSignalTransition::event(QEvent *e) | - | ||||||||||||
| 233 | { | - | ||||||||||||
| 234 | return QAbstractTransition::event(e); executed 2 times by 1 test: return QAbstractTransition::event(e);Executed by:
| 2 | ||||||||||||
| 235 | } | - | ||||||||||||
| 236 | - | |||||||||||||
| 237 | /*! | - | ||||||||||||
| 238 | \fn QSignalTransition::senderObjectChanged() | - | ||||||||||||
| 239 | \since 5.4 | - | ||||||||||||
| 240 | - | |||||||||||||
| 241 | This signal is emitted when the senderObject property is changed. | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | \sa QSignalTransition::senderObject | - | ||||||||||||
| 244 | */ | - | ||||||||||||
| 245 | - | |||||||||||||
| 246 | /*! | - | ||||||||||||
| 247 | \fn QSignalTransition::signalChanged() | - | ||||||||||||
| 248 | \since 5.4 | - | ||||||||||||
| 249 | - | |||||||||||||
| 250 | This signal is emitted when the signal property is changed. | - | ||||||||||||
| 251 | - | |||||||||||||
| 252 | \sa QSignalTransition::signal | - | ||||||||||||
| 253 | */ | - | ||||||||||||
| 254 | - | |||||||||||||
| 255 | void QSignalTransitionPrivate::callOnTransition(QEvent *e) | - | ||||||||||||
| 256 | { | - | ||||||||||||
| 257 | Q_Q(QSignalTransition); | - | ||||||||||||
| 258 | - | |||||||||||||
| 259 | if (e->type() == QEvent::StateMachineSignal) {
| 0-71 | ||||||||||||
| 260 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent *>(e); | - | ||||||||||||
| 261 | int savedSignalIndex = se->m_signalIndex; | - | ||||||||||||
| 262 | se->m_signalIndex = originalSignalIndex; | - | ||||||||||||
| 263 | q->onTransition(e); | - | ||||||||||||
| 264 | se->m_signalIndex = savedSignalIndex; | - | ||||||||||||
| 265 | } else { executed 71 times by 2 tests: end of blockExecuted by:
| 71 | ||||||||||||
| 266 | q->onTransition(e); | - | ||||||||||||
| 267 | } never executed: end of block | 0 | ||||||||||||
| 268 | } | - | ||||||||||||
| 269 | - | |||||||||||||
| 270 | - | |||||||||||||
| 271 | QT_END_NAMESPACE | - | ||||||||||||
| 272 | - | |||||||||||||
| 273 | #endif //QT_NO_STATEMACHINE | - | ||||||||||||
| Source code | Switch to Preprocessed file |