| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/statemachine/qhistorystate.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 "qhistorystate.h" | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #ifndef QT_NO_STATEMACHINE | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | #include "qhistorystate_p.h" | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | /*! | - | ||||||||||||
| 43 | \class QHistoryState | - | ||||||||||||
| 44 | \inmodule QtCore | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | \brief The QHistoryState class provides a means of returning to a previously active substate. | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | \since 4.6 | - | ||||||||||||
| 49 | \ingroup statemachine | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | A history state is a pseudo-state that represents the child state that the | - | ||||||||||||
| 52 | parent state was in the last time the parent state was exited. A transition | - | ||||||||||||
| 53 | with a history state as its target is in fact a transition to one or more | - | ||||||||||||
| 54 | other child states of the parent state. QHistoryState is part of \l{The | - | ||||||||||||
| 55 | State Machine Framework}. | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | Use the setDefaultState() function to set the state that should be entered | - | ||||||||||||
| 58 | if the parent state has never been entered. Example: | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | \code | - | ||||||||||||
| 61 | QStateMachine machine; | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | QState *s1 = new QState(); | - | ||||||||||||
| 64 | QState *s11 = new QState(s1); | - | ||||||||||||
| 65 | QState *s12 = new QState(s1); | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | QHistoryState *s1h = new QHistoryState(s1); | - | ||||||||||||
| 68 | s1h->setDefaultState(s11); | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | machine.addState(s1); | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | QState *s2 = new QState(); | - | ||||||||||||
| 73 | machine.addState(s2); | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | QPushButton *button = new QPushButton(); | - | ||||||||||||
| 76 | // Clicking the button will cause the state machine to enter the child state | - | ||||||||||||
| 77 | // that s1 was in the last time s1 was exited, or the history state's default | - | ||||||||||||
| 78 | // state if s1 has never been entered. | - | ||||||||||||
| 79 | s1->addTransition(button, SIGNAL(clicked()), s1h); | - | ||||||||||||
| 80 | \endcode | - | ||||||||||||
| 81 | - | |||||||||||||
| 82 | If more than one default state has to be entered, or if the transition to the default state(s) | - | ||||||||||||
| 83 | has to be acted upon, the defaultTransition should be set instead. Note that the eventTest() | - | ||||||||||||
| 84 | method of that transition will never be called: the selection and execution of the transition is | - | ||||||||||||
| 85 | done automatically when entering the history state. | - | ||||||||||||
| 86 | - | |||||||||||||
| 87 | By default a history state is shallow, meaning that it won't remember nested | - | ||||||||||||
| 88 | states. This can be configured through the historyType property. | - | ||||||||||||
| 89 | */ | - | ||||||||||||
| 90 | - | |||||||||||||
| 91 | /*! | - | ||||||||||||
| 92 | \property QHistoryState::defaultTransition | - | ||||||||||||
| 93 | - | |||||||||||||
| 94 | \brief the default transition of this history state | - | ||||||||||||
| 95 | */ | - | ||||||||||||
| 96 | - | |||||||||||||
| 97 | /*! | - | ||||||||||||
| 98 | \property QHistoryState::defaultState | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | \brief the default state of this history state | - | ||||||||||||
| 101 | */ | - | ||||||||||||
| 102 | - | |||||||||||||
| 103 | /*! | - | ||||||||||||
| 104 | \property QHistoryState::historyType | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | \brief the type of history that this history state records | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | The default value of this property is QHistoryState::ShallowHistory. | - | ||||||||||||
| 109 | */ | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | /*! | - | ||||||||||||
| 112 | \enum QHistoryState::HistoryType | - | ||||||||||||
| 113 | - | |||||||||||||
| 114 | This enum specifies the type of history that a QHistoryState records. | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | \value ShallowHistory Only the immediate child states of the parent state | - | ||||||||||||
| 117 | are recorded. In this case a transition with the history state as its | - | ||||||||||||
| 118 | target will end up in the immediate child state that the parent was in the | - | ||||||||||||
| 119 | last time it was exited. This is the default. | - | ||||||||||||
| 120 | - | |||||||||||||
| 121 | \value DeepHistory Nested states are recorded. In this case a transition | - | ||||||||||||
| 122 | with the history state as its target will end up in the most deeply nested | - | ||||||||||||
| 123 | descendant state the parent was in the last time it was exited. | - | ||||||||||||
| 124 | */ | - | ||||||||||||
| 125 | - | |||||||||||||
| 126 | QHistoryStatePrivate::QHistoryStatePrivate() | - | ||||||||||||
| 127 | : QAbstractStatePrivate(HistoryState) | - | ||||||||||||
| 128 | , defaultTransition(0) | - | ||||||||||||
| 129 | , historyType(QHistoryState::ShallowHistory) | - | ||||||||||||
| 130 | { | - | ||||||||||||
| 131 | } executed 9 times by 2 tests: end of blockExecuted by:
| 9 | ||||||||||||
| 132 | - | |||||||||||||
| 133 | DefaultStateTransition::DefaultStateTransition(QHistoryState *source, QAbstractState *target) | - | ||||||||||||
| 134 | : QAbstractTransition() | - | ||||||||||||
| 135 | { | - | ||||||||||||
| 136 | setParent(source); | - | ||||||||||||
| 137 | setTargetState(target); | - | ||||||||||||
| 138 | } executed 7 times by 2 tests: end of blockExecuted by:
| 7 | ||||||||||||
| 139 | - | |||||||||||||
| 140 | /*! | - | ||||||||||||
| 141 | Constructs a new shallow history state with the given \a parent state. | - | ||||||||||||
| 142 | */ | - | ||||||||||||
| 143 | QHistoryState::QHistoryState(QState *parent) | - | ||||||||||||
| 144 | : QAbstractState(*new QHistoryStatePrivate, parent) | - | ||||||||||||
| 145 | { | - | ||||||||||||
| 146 | } executed 7 times by 2 tests: end of blockExecuted by:
| 7 | ||||||||||||
| 147 | /*! | - | ||||||||||||
| 148 | Constructs a new history state of the given \a type, with the given \a | - | ||||||||||||
| 149 | parent state. | - | ||||||||||||
| 150 | */ | - | ||||||||||||
| 151 | QHistoryState::QHistoryState(HistoryType type, QState *parent) | - | ||||||||||||
| 152 | : QAbstractState(*new QHistoryStatePrivate, parent) | - | ||||||||||||
| 153 | { | - | ||||||||||||
| 154 | Q_D(QHistoryState); | - | ||||||||||||
| 155 | d->historyType = type; | - | ||||||||||||
| 156 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||
| 157 | - | |||||||||||||
| 158 | /*! | - | ||||||||||||
| 159 | Destroys this history state. | - | ||||||||||||
| 160 | */ | - | ||||||||||||
| 161 | QHistoryState::~QHistoryState() | - | ||||||||||||
| 162 | { | - | ||||||||||||
| 163 | } | - | ||||||||||||
| 164 | - | |||||||||||||
| 165 | /*! | - | ||||||||||||
| 166 | Returns this history state's default transition. The default transition is | - | ||||||||||||
| 167 | taken when the history state has never been entered before. The target states | - | ||||||||||||
| 168 | of the default transition therefore make up the default state. | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | \since 5.6 | - | ||||||||||||
| 171 | */ | - | ||||||||||||
| 172 | QAbstractTransition *QHistoryState::defaultTransition() const | - | ||||||||||||
| 173 | { | - | ||||||||||||
| 174 | Q_D(const QHistoryState); | - | ||||||||||||
| 175 | return d->defaultTransition; executed 4 times by 1 test: return d->defaultTransition;Executed by:
| 4 | ||||||||||||
| 176 | } | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | /*! | - | ||||||||||||
| 179 | Sets this history state's default transition to be the given \a transition. | - | ||||||||||||
| 180 | This will set the source state of the \a transition to the history state. | - | ||||||||||||
| 181 | - | |||||||||||||
| 182 | Note that the eventTest method of the \a transition will never be called. | - | ||||||||||||
| 183 | - | |||||||||||||
| 184 | \since 5.6 | - | ||||||||||||
| 185 | */ | - | ||||||||||||
| 186 | void QHistoryState::setDefaultTransition(QAbstractTransition *transition) | - | ||||||||||||
| 187 | { | - | ||||||||||||
| 188 | Q_D(QHistoryState); | - | ||||||||||||
| 189 | if (d->defaultTransition != transition) {
| 0-1 | ||||||||||||
| 190 | d->defaultTransition = transition; | - | ||||||||||||
| 191 | transition->setParent(this); | - | ||||||||||||
| 192 | emit defaultTransitionChanged(QHistoryState::QPrivateSignal()); | - | ||||||||||||
| 193 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 194 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 195 | - | |||||||||||||
| 196 | /*! | - | ||||||||||||
| 197 | Returns this history state's default state. The default state indicates the | - | ||||||||||||
| 198 | state to transition to if the parent state has never been entered before. | - | ||||||||||||
| 199 | */ | - | ||||||||||||
| 200 | QAbstractState *QHistoryState::defaultState() const | - | ||||||||||||
| 201 | { | - | ||||||||||||
| 202 | Q_D(const QHistoryState); | - | ||||||||||||
| 203 | return d->defaultTransition ? d->defaultTransition->targetState() : Q_NULLPTR; executed 4 times by 1 test: return d->defaultTransition ? d->defaultTransition->targetState() : nullptr;Executed by:
| 2-4 | ||||||||||||
| 204 | } | - | ||||||||||||
| 205 | - | |||||||||||||
| 206 | /*! | - | ||||||||||||
| 207 | Sets this history state's default state to be the given \a state. | - | ||||||||||||
| 208 | \a state must be a sibling of this history state. | - | ||||||||||||
| 209 | - | |||||||||||||
| 210 | Note that this function does not set \a state as the initial state | - | ||||||||||||
| 211 | of its parent. | - | ||||||||||||
| 212 | */ | - | ||||||||||||
| 213 | void QHistoryState::setDefaultState(QAbstractState *state) | - | ||||||||||||
| 214 | { | - | ||||||||||||
| 215 | Q_D(QHistoryState); | - | ||||||||||||
| 216 | if (state && state->parentState() != parentState()) {
| 0-9 | ||||||||||||
| 217 | qWarning("QHistoryState::setDefaultState: state %p does not belong " | - | ||||||||||||
| 218 | "to this history state's group (%p)", state, parentState()); | - | ||||||||||||
| 219 | return; executed 2 times by 1 test: return;Executed by:
| 2 | ||||||||||||
| 220 | } | - | ||||||||||||
| 221 | if (!d->defaultTransition
| 0-7 | ||||||||||||
| 222 | || d->defaultTransition->targetStates().size() != 1
| 0 | ||||||||||||
| 223 | || d->defaultTransition->targetStates().first() != state) {
| 0 | ||||||||||||
| 224 | if (!d->defaultTransition || !qobject_cast<DefaultStateTransition*>(d->defaultTransition)) {
| 0-7 | ||||||||||||
| 225 | d->defaultTransition = new DefaultStateTransition(this, state); | - | ||||||||||||
| 226 | emit defaultTransitionChanged(QHistoryState::QPrivateSignal()); | - | ||||||||||||
| 227 | } else { executed 7 times by 2 tests: end of blockExecuted by:
| 7 | ||||||||||||
| 228 | d->defaultTransition->setTargetState(state); | - | ||||||||||||
| 229 | } never executed: end of block | 0 | ||||||||||||
| 230 | emit defaultStateChanged(QHistoryState::QPrivateSignal()); | - | ||||||||||||
| 231 | } executed 7 times by 2 tests: end of blockExecuted by:
| 7 | ||||||||||||
| 232 | } executed 7 times by 2 tests: end of blockExecuted by:
| 7 | ||||||||||||
| 233 | - | |||||||||||||
| 234 | /*! | - | ||||||||||||
| 235 | Returns the type of history that this history state records. | - | ||||||||||||
| 236 | */ | - | ||||||||||||
| 237 | QHistoryState::HistoryType QHistoryState::historyType() const | - | ||||||||||||
| 238 | { | - | ||||||||||||
| 239 | Q_D(const QHistoryState); | - | ||||||||||||
| 240 | return d->historyType; executed 5 times by 1 test: return d->historyType;Executed by:
| 5 | ||||||||||||
| 241 | } | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | /*! | - | ||||||||||||
| 244 | Sets the \a type of history that this history state records. | - | ||||||||||||
| 245 | */ | - | ||||||||||||
| 246 | void QHistoryState::setHistoryType(HistoryType type) | - | ||||||||||||
| 247 | { | - | ||||||||||||
| 248 | Q_D(QHistoryState); | - | ||||||||||||
| 249 | if (d->historyType != type) {
| 0-3 | ||||||||||||
| 250 | d->historyType = type; | - | ||||||||||||
| 251 | emit historyTypeChanged(QHistoryState::QPrivateSignal()); | - | ||||||||||||
| 252 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||
| 253 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||
| 254 | - | |||||||||||||
| 255 | /*! | - | ||||||||||||
| 256 | \reimp | - | ||||||||||||
| 257 | */ | - | ||||||||||||
| 258 | void QHistoryState::onEntry(QEvent *event) | - | ||||||||||||
| 259 | { | - | ||||||||||||
| 260 | Q_UNUSED(event); | - | ||||||||||||
| 261 | } never executed: end of block | 0 | ||||||||||||
| 262 | - | |||||||||||||
| 263 | /*! | - | ||||||||||||
| 264 | \reimp | - | ||||||||||||
| 265 | */ | - | ||||||||||||
| 266 | void QHistoryState::onExit(QEvent *event) | - | ||||||||||||
| 267 | { | - | ||||||||||||
| 268 | Q_UNUSED(event); | - | ||||||||||||
| 269 | } never executed: end of block | 0 | ||||||||||||
| 270 | - | |||||||||||||
| 271 | /*! | - | ||||||||||||
| 272 | \reimp | - | ||||||||||||
| 273 | */ | - | ||||||||||||
| 274 | bool QHistoryState::event(QEvent *e) | - | ||||||||||||
| 275 | { | - | ||||||||||||
| 276 | return QAbstractState::event(e); executed 9 times by 2 tests: return QAbstractState::event(e);Executed by:
| 9 | ||||||||||||
| 277 | } | - | ||||||||||||
| 278 | - | |||||||||||||
| 279 | /*! | - | ||||||||||||
| 280 | \fn QHistoryState::defaultStateChanged() | - | ||||||||||||
| 281 | \since 5.4 | - | ||||||||||||
| 282 | - | |||||||||||||
| 283 | This signal is emitted when the defaultState property is changed. | - | ||||||||||||
| 284 | - | |||||||||||||
| 285 | \sa QHistoryState::defaultState | - | ||||||||||||
| 286 | */ | - | ||||||||||||
| 287 | - | |||||||||||||
| 288 | /*! | - | ||||||||||||
| 289 | \fn QHistoryState::historyTypeChanged() | - | ||||||||||||
| 290 | \since 5.4 | - | ||||||||||||
| 291 | - | |||||||||||||
| 292 | This signal is emitted when the historyType property is changed. | - | ||||||||||||
| 293 | - | |||||||||||||
| 294 | \sa QHistoryState::historyType | - | ||||||||||||
| 295 | */ | - | ||||||||||||
| 296 | - | |||||||||||||
| 297 | /*! | - | ||||||||||||
| 298 | \fn QHistoryState::defaultTransitionChanged() | - | ||||||||||||
| 299 | \since 5.6 | - | ||||||||||||
| 300 | - | |||||||||||||
| 301 | This signal is emitted when the defaultTransition property is changed. | - | ||||||||||||
| 302 | - | |||||||||||||
| 303 | \sa QHistoryState::defaultTransition | - | ||||||||||||
| 304 | */ | - | ||||||||||||
| 305 | - | |||||||||||||
| 306 | QT_END_NAMESPACE | - | ||||||||||||
| 307 | - | |||||||||||||
| 308 | #endif //QT_NO_STATEMACHINE | - | ||||||||||||
| Source code | Switch to Preprocessed file |