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