Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
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 Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/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 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qsignaltransition.h" | - |
43 | | - |
44 | #ifndef QT_NO_STATEMACHINE | - |
45 | | - |
46 | #include "qsignaltransition_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 QSignalTransition | - |
57 | \inmodule QtCore | - |
58 | | - |
59 | \brief The QSignalTransition class provides a transition based on a Qt signal. | - |
60 | | - |
61 | \since 4.6 | - |
62 | \ingroup statemachine | - |
63 | | - |
64 | Typically you would use the overload of QState::addTransition() that takes a | - |
65 | sender and signal as arguments, rather than creating QSignalTransition | - |
66 | objects directly. QSignalTransition is part of \l{The State Machine | - |
67 | Framework}. | - |
68 | | - |
69 | You can subclass QSignalTransition and reimplement eventTest() to make a | - |
70 | signal transition conditional; the event object passed to eventTest() will | - |
71 | be a QStateMachine::SignalEvent object. Example: | - |
72 | | - |
73 | \code | - |
74 | class CheckedTransition : public QSignalTransition | - |
75 | { | - |
76 | public: | - |
77 | CheckedTransition(QCheckBox *check) | - |
78 | : QSignalTransition(check, SIGNAL(stateChanged(int))) {} | - |
79 | protected: | - |
80 | bool eventTest(QEvent *e) { | - |
81 | if (!QSignalTransition::eventTest(e)) | - |
82 | return false; | - |
83 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(e); | - |
84 | return (se->arguments().at(0).toInt() == Qt::Checked); | - |
85 | } | - |
86 | }; | - |
87 | | - |
88 | ... | - |
89 | | - |
90 | QCheckBox *check = new QCheckBox(); | - |
91 | check->setTristate(true); | - |
92 | | - |
93 | QState *s1 = new QState(); | - |
94 | QState *s2 = new QState(); | - |
95 | CheckedTransition *t1 = new CheckedTransition(check); | - |
96 | t1->setTargetState(s2); | - |
97 | s1->addTransition(t1); | - |
98 | \endcode | - |
99 | */ | - |
100 | | - |
101 | /*! | - |
102 | \property QSignalTransition::senderObject | - |
103 | | - |
104 | \brief the sender object that this signal transition is associated with | - |
105 | */ | - |
106 | | - |
107 | /*! | - |
108 | \property QSignalTransition::signal | - |
109 | | - |
110 | \brief the signal that this signal transition is associated with | - |
111 | */ | - |
112 | | - |
113 | QSignalTransitionPrivate::QSignalTransitionPrivate() | - |
114 | { | - |
115 | sender = 0; executed (the execution status of this line is deduced): sender = 0; | - |
116 | signalIndex = -1; executed (the execution status of this line is deduced): signalIndex = -1; | - |
117 | } executed: } Execution Count:60 | 60 |
118 | | - |
119 | QSignalTransitionPrivate *QSignalTransitionPrivate::get(QSignalTransition *q) | - |
120 | { | - |
121 | return q->d_func(); executed: return q->d_func(); Execution Count:1001483 | 1001483 |
122 | } | - |
123 | | - |
124 | void QSignalTransitionPrivate::unregister() | - |
125 | { | - |
126 | Q_Q(QSignalTransition); executed (the execution status of this line is deduced): QSignalTransition * const q = q_func(); | - |
127 | if ((signalIndex == -1) || !machine()) evaluated: (signalIndex == -1) yes Evaluation Count:100010 | yes Evaluation Count:100001 |
partially evaluated: !machine() no Evaluation Count:0 | yes Evaluation Count:100002 |
| 0-100010 |
128 | return; executed: return; Execution Count:100010 | 100010 |
129 | QStateMachinePrivate::get(machine())->unregisterSignalTransition(q); executed (the execution status of this line is deduced): QStateMachinePrivate::get(machine())->unregisterSignalTransition(q); | - |
130 | } executed: } Execution Count:100003 | 100003 |
131 | | - |
132 | void QSignalTransitionPrivate::maybeRegister() | - |
133 | { | - |
134 | Q_Q(QSignalTransition); executed (the execution status of this line is deduced): QSignalTransition * const q = q_func(); | - |
135 | if (QStateMachine *mach = machine()) evaluated: QStateMachine *mach = machine() yes Evaluation Count:200009 | yes Evaluation Count:57 |
| 57-200009 |
136 | QStateMachinePrivate::get(mach)->maybeRegisterSignalTransition(q); executed: QStateMachinePrivate::get(mach)->maybeRegisterSignalTransition(q); Execution Count:200009 | 200009 |
137 | } executed: } Execution Count:200067 | 200067 |
138 | | - |
139 | /*! | - |
140 | Constructs a new signal transition with the given \a sourceState. | - |
141 | */ | - |
142 | QSignalTransition::QSignalTransition(QState *sourceState) | - |
143 | : QAbstractTransition(*new QSignalTransitionPrivate, sourceState) | - |
144 | { | - |
145 | } executed: } Execution Count:6 | 6 |
146 | | - |
147 | /*! | - |
148 | Constructs a new signal transition associated with the given \a signal of | - |
149 | the given \a sender, and with the given \a sourceState. | - |
150 | */ | - |
151 | QSignalTransition::QSignalTransition(const QObject *sender, const char *signal, | - |
152 | QState *sourceState) | - |
153 | : QAbstractTransition(*new QSignalTransitionPrivate, sourceState) | - |
154 | { | - |
155 | Q_D(QSignalTransition); executed (the execution status of this line is deduced): QSignalTransitionPrivate * const d = d_func(); | - |
156 | d->sender = sender; executed (the execution status of this line is deduced): d->sender = sender; | - |
157 | d->signal = signal; executed (the execution status of this line is deduced): d->signal = signal; | - |
158 | d->maybeRegister(); executed (the execution status of this line is deduced): d->maybeRegister(); | - |
159 | } executed: } Execution Count:54 | 54 |
160 | | - |
161 | /*! | - |
162 | Destroys this signal transition. | - |
163 | */ | - |
164 | QSignalTransition::~QSignalTransition() | - |
165 | { | - |
166 | } | - |
167 | | - |
168 | /*! | - |
169 | Returns the sender object associated with this signal transition. | - |
170 | */ | - |
171 | QObject *QSignalTransition::senderObject() const | - |
172 | { | - |
173 | Q_D(const QSignalTransition); executed (the execution status of this line is deduced): const QSignalTransitionPrivate * const d = d_func(); | - |
174 | return const_cast<QObject *>(d->sender); executed: return const_cast<QObject *>(d->sender); Execution Count:135 | 135 |
175 | } | - |
176 | | - |
177 | /*! | - |
178 | Sets the \a sender object associated with this signal transition. | - |
179 | */ | - |
180 | void QSignalTransition::setSenderObject(const QObject *sender) | - |
181 | { | - |
182 | Q_D(QSignalTransition); executed (the execution status of this line is deduced): QSignalTransitionPrivate * const d = d_func(); | - |
183 | if (sender == d->sender) partially evaluated: sender == d->sender no Evaluation Count:0 | yes Evaluation Count:200000 |
| 0-200000 |
184 | return; | 0 |
185 | d->unregister(); executed (the execution status of this line is deduced): d->unregister(); | - |
186 | d->sender = sender; executed (the execution status of this line is deduced): d->sender = sender; | - |
187 | d->maybeRegister(); executed (the execution status of this line is deduced): d->maybeRegister(); | - |
188 | } executed: } Execution Count:200004 | 200004 |
189 | | - |
190 | /*! | - |
191 | Returns the signal associated with this signal transition. | - |
192 | */ | - |
193 | QByteArray QSignalTransition::signal() const | - |
194 | { | - |
195 | Q_D(const QSignalTransition); executed (the execution status of this line is deduced): const QSignalTransitionPrivate * const d = d_func(); | - |
196 | return d->signal; executed: return d->signal; Execution Count:10 | 10 |
197 | } | - |
198 | | - |
199 | /*! | - |
200 | Sets the \a signal associated with this signal transition. | - |
201 | */ | - |
202 | void QSignalTransition::setSignal(const QByteArray &signal) | - |
203 | { | - |
204 | Q_D(QSignalTransition); executed (the execution status of this line is deduced): QSignalTransitionPrivate * const d = d_func(); | - |
205 | if (signal == d->signal) partially evaluated: signal == d->signal no Evaluation Count:0 | yes Evaluation Count:9 |
| 0-9 |
206 | return; | 0 |
207 | d->unregister(); executed (the execution status of this line is deduced): d->unregister(); | - |
208 | d->signal = signal; executed (the execution status of this line is deduced): d->signal = signal; | - |
209 | d->maybeRegister(); executed (the execution status of this line is deduced): d->maybeRegister(); | - |
210 | } executed: } Execution Count:9 | 9 |
211 | | - |
212 | /*! | - |
213 | \reimp | - |
214 | | - |
215 | The default implementation returns true if the \a event is a | - |
216 | QStateMachine::SignalEvent object and the event's sender and signal index | - |
217 | match this transition, and returns false otherwise. | - |
218 | */ | - |
219 | bool QSignalTransition::eventTest(QEvent *event) | - |
220 | { | - |
221 | Q_D(const QSignalTransition); executed (the execution status of this line is deduced): const QSignalTransitionPrivate * const d = d_func(); | - |
222 | if (event->type() == QEvent::StateMachineSignal) { evaluated: event->type() == QEvent::StateMachineSignal yes Evaluation Count:73 | yes Evaluation Count:3166 |
| 73-3166 |
223 | if (d->signalIndex == -1) partially evaluated: d->signalIndex == -1 no Evaluation Count:0 | yes Evaluation Count:73 |
| 0-73 |
224 | return false; never executed: return false; | 0 |
225 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event); executed (the execution status of this line is deduced): QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent*>(event); | - |
226 | return (se->sender() == d->sender) executed: return (se->sender() == d->sender) && (se->signalIndex() == d->signalIndex); Execution Count:73 | 73 |
227 | && (se->signalIndex() == d->signalIndex); executed: return (se->sender() == d->sender) && (se->signalIndex() == d->signalIndex); Execution Count:73 | 73 |
228 | } | - |
229 | return false; executed: return false; Execution Count:3166 | 3166 |
230 | } | - |
231 | | - |
232 | /*! | - |
233 | \reimp | - |
234 | */ | - |
235 | void QSignalTransition::onTransition(QEvent *event) | - |
236 | { | - |
237 | Q_UNUSED(event); executed (the execution status of this line is deduced): (void)event;; | - |
238 | } executed: } Execution Count:61 | 61 |
239 | | - |
240 | /*! | - |
241 | \reimp | - |
242 | */ | - |
243 | bool QSignalTransition::event(QEvent *e) | - |
244 | { | - |
245 | return QAbstractTransition::event(e); executed: return QAbstractTransition::event(e); Execution Count:2 | 2 |
246 | } | - |
247 | | - |
248 | void QSignalTransitionPrivate::callOnTransition(QEvent *e) | - |
249 | { | - |
250 | Q_Q(QSignalTransition); executed (the execution status of this line is deduced): QSignalTransition * const q = q_func(); | - |
251 | | - |
252 | if (e->type() == QEvent::StateMachineSignal) { partially evaluated: e->type() == QEvent::StateMachineSignal yes Evaluation Count:61 | no Evaluation Count:0 |
| 0-61 |
253 | QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent *>(e); executed (the execution status of this line is deduced): QStateMachine::SignalEvent *se = static_cast<QStateMachine::SignalEvent *>(e); | - |
254 | int savedSignalIndex = se->m_signalIndex; executed (the execution status of this line is deduced): int savedSignalIndex = se->m_signalIndex; | - |
255 | se->m_signalIndex = originalSignalIndex; executed (the execution status of this line is deduced): se->m_signalIndex = originalSignalIndex; | - |
256 | q->onTransition(e); executed (the execution status of this line is deduced): q->onTransition(e); | - |
257 | se->m_signalIndex = savedSignalIndex; executed (the execution status of this line is deduced): se->m_signalIndex = savedSignalIndex; | - |
258 | } else { executed: } Execution Count:61 | 61 |
259 | q->onTransition(e); never executed (the execution status of this line is deduced): q->onTransition(e); | - |
260 | } | 0 |
261 | } | - |
262 | | - |
263 | QT_END_NAMESPACE | - |
264 | | - |
265 | #endif //QT_NO_STATEMACHINE | - |
266 | | - |
| | |