Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/util/qquickbehavior.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtQuick 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 The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at https://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 3 as published by the Free Software | - | ||||||||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
24 | ** | - | ||||||||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
35 | ** | - | ||||||||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
37 | ** | - | ||||||||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include "qquickbehavior_p.h" | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | #include "qquickanimation_p.h" | - | ||||||||||||||||||
43 | #include <qqmlcontext.h> | - | ||||||||||||||||||
44 | #include <qqmlinfo.h> | - | ||||||||||||||||||
45 | #include <private/qqmlproperty_p.h> | - | ||||||||||||||||||
46 | #include <private/qqmlengine_p.h> | - | ||||||||||||||||||
47 | #include <private/qabstractanimationjob_p.h> | - | ||||||||||||||||||
48 | #include <private/qquicktransition_p.h> | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | #include <private/qquickanimatorjob_p.h> | - | ||||||||||||||||||
51 | - | |||||||||||||||||||
52 | #include <private/qobject_p.h> | - | ||||||||||||||||||
53 | - | |||||||||||||||||||
54 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | class QQuickBehaviorPrivate : public QObjectPrivate, public QAnimationJobChangeListener | - | ||||||||||||||||||
57 | { | - | ||||||||||||||||||
58 | Q_DECLARE_PUBLIC(QQuickBehavior) | - | ||||||||||||||||||
59 | public: | - | ||||||||||||||||||
60 | QQuickBehaviorPrivate() : animation(nullptr), animationInstance(nullptr), enabled(true), finalized(false) | - | ||||||||||||||||||
61 | , blockRunningChanged(false) {} executed 560 times by 7 tests: end of block Executed by:
| 560 | ||||||||||||||||||
62 | - | |||||||||||||||||||
63 | void animationStateChanged(QAbstractAnimationJob *, QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState) override; | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | QQmlProperty property; | - | ||||||||||||||||||
66 | QVariant targetValue; | - | ||||||||||||||||||
67 | QPointer<QQuickAbstractAnimation> animation; | - | ||||||||||||||||||
68 | QAbstractAnimationJob *animationInstance; | - | ||||||||||||||||||
69 | bool enabled; | - | ||||||||||||||||||
70 | bool finalized; | - | ||||||||||||||||||
71 | bool blockRunningChanged; | - | ||||||||||||||||||
72 | }; | - | ||||||||||||||||||
73 | - | |||||||||||||||||||
74 | /*! | - | ||||||||||||||||||
75 | \qmltype Behavior | - | ||||||||||||||||||
76 | \instantiates QQuickBehavior | - | ||||||||||||||||||
77 | \inqmlmodule QtQuick | - | ||||||||||||||||||
78 | \ingroup qtquick-transitions-animations | - | ||||||||||||||||||
79 | \ingroup qtquick-interceptors | - | ||||||||||||||||||
80 | \brief Defines a default animation for a property change. | - | ||||||||||||||||||
81 | - | |||||||||||||||||||
82 | A Behavior defines the default animation to be applied whenever a | - | ||||||||||||||||||
83 | particular property value changes. | - | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | For example, the following Behavior defines a NumberAnimation to be run | - | ||||||||||||||||||
86 | whenever the \l Rectangle's \c width value changes. When the MouseArea | - | ||||||||||||||||||
87 | is clicked, the \c width is changed, triggering the behavior's animation: | - | ||||||||||||||||||
88 | - | |||||||||||||||||||
89 | \snippet qml/behavior.qml 0 | - | ||||||||||||||||||
90 | - | |||||||||||||||||||
91 | Note that a property cannot have more than one assigned Behavior. To provide | - | ||||||||||||||||||
92 | multiple animations within a Behavior, use ParallelAnimation or | - | ||||||||||||||||||
93 | SequentialAnimation. | - | ||||||||||||||||||
94 | - | |||||||||||||||||||
95 | If a \l{Qt Quick States}{state change} has a \l Transition that matches the same property as a | - | ||||||||||||||||||
96 | Behavior, the \l Transition animation overrides the Behavior for that | - | ||||||||||||||||||
97 | state change. For general advice on using Behaviors to animate state changes, see | - | ||||||||||||||||||
98 | \l{Using Qt Quick Behaviors with States}. | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | \sa {Animation and Transitions in Qt Quick}, {Qt Quick Examples - Animation#Behaviors}{Behavior example}, {Qt QML} | - | ||||||||||||||||||
101 | */ | - | ||||||||||||||||||
102 | - | |||||||||||||||||||
103 | - | |||||||||||||||||||
104 | QQuickBehavior::QQuickBehavior(QObject *parent) | - | ||||||||||||||||||
105 | : QObject(*(new QQuickBehaviorPrivate), parent) | - | ||||||||||||||||||
106 | { | - | ||||||||||||||||||
107 | } executed 560 times by 7 tests: end of block Executed by:
| 560 | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | QQuickBehavior::~QQuickBehavior() | - | ||||||||||||||||||
110 | { | - | ||||||||||||||||||
111 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
112 | delete d->animationInstance; | - | ||||||||||||||||||
113 | } executed 558 times by 7 tests: end of block Executed by:
| 558 | ||||||||||||||||||
114 | - | |||||||||||||||||||
115 | /*! | - | ||||||||||||||||||
116 | \qmlproperty Animation QtQuick::Behavior::animation | - | ||||||||||||||||||
117 | \default | - | ||||||||||||||||||
118 | - | |||||||||||||||||||
119 | This property holds the animation to run when the behavior is triggered. | - | ||||||||||||||||||
120 | */ | - | ||||||||||||||||||
121 | - | |||||||||||||||||||
122 | QQuickAbstractAnimation *QQuickBehavior::animation() | - | ||||||||||||||||||
123 | { | - | ||||||||||||||||||
124 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
125 | return d->animation; executed 10 times by 1 test: return d->animation; Executed by:
| 10 | ||||||||||||||||||
126 | } | - | ||||||||||||||||||
127 | - | |||||||||||||||||||
128 | void QQuickBehavior::setAnimation(QQuickAbstractAnimation *animation) | - | ||||||||||||||||||
129 | { | - | ||||||||||||||||||
130 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
131 | if (d->animation) {
| 2-194 | ||||||||||||||||||
132 | qmlWarning(this) << tr("Cannot change the animation assigned to a Behavior."); | - | ||||||||||||||||||
133 | return; executed 2 times by 1 test: return; Executed by:
| 2 | ||||||||||||||||||
134 | } | - | ||||||||||||||||||
135 | - | |||||||||||||||||||
136 | d->animation = animation; | - | ||||||||||||||||||
137 | if (d->animation) {
| 0-194 | ||||||||||||||||||
138 | d->animation->setDefaultTarget(d->property); | - | ||||||||||||||||||
139 | d->animation->setDisableUserControl(); | - | ||||||||||||||||||
140 | } executed 194 times by 7 tests: end of block Executed by:
| 194 | ||||||||||||||||||
141 | } executed 194 times by 7 tests: end of block Executed by:
| 194 | ||||||||||||||||||
142 | - | |||||||||||||||||||
143 | - | |||||||||||||||||||
144 | void QQuickBehaviorPrivate::animationStateChanged(QAbstractAnimationJob *, QAbstractAnimationJob::State newState,QAbstractAnimationJob::State) | - | ||||||||||||||||||
145 | { | - | ||||||||||||||||||
146 | if (!blockRunningChanged)
| 0-444 | ||||||||||||||||||
147 | animation->notifyRunningChanged(newState == QAbstractAnimationJob::Running); executed 444 times by 7 tests: animation->notifyRunningChanged(newState == QAbstractAnimationJob::Running); Executed by:
| 444 | ||||||||||||||||||
148 | } executed 444 times by 7 tests: end of block Executed by:
| 444 | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | /*! | - | ||||||||||||||||||
151 | \qmlproperty bool QtQuick::Behavior::enabled | - | ||||||||||||||||||
152 | - | |||||||||||||||||||
153 | This property holds whether the behavior will be triggered when the tracked | - | ||||||||||||||||||
154 | property changes value. | - | ||||||||||||||||||
155 | - | |||||||||||||||||||
156 | By default a Behavior is enabled. | - | ||||||||||||||||||
157 | */ | - | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | bool QQuickBehavior::enabled() const | - | ||||||||||||||||||
160 | { | - | ||||||||||||||||||
161 | Q_D(const QQuickBehavior); | - | ||||||||||||||||||
162 | return d->enabled; executed 6 times by 1 test: return d->enabled; Executed by:
| 6 | ||||||||||||||||||
163 | } | - | ||||||||||||||||||
164 | - | |||||||||||||||||||
165 | void QQuickBehavior::setEnabled(bool enabled) | - | ||||||||||||||||||
166 | { | - | ||||||||||||||||||
167 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
168 | if (d->enabled == enabled)
| 34-42 | ||||||||||||||||||
169 | return; executed 34 times by 2 tests: return; Executed by:
| 34 | ||||||||||||||||||
170 | d->enabled = enabled; | - | ||||||||||||||||||
171 | emit enabledChanged(); | - | ||||||||||||||||||
172 | } executed 42 times by 2 tests: end of block Executed by:
| 42 | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | void QQuickBehavior::write(const QVariant &value) | - | ||||||||||||||||||
175 | { | - | ||||||||||||||||||
176 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
177 | bool bypass = !d->enabled || !d->finalized || QQmlEnginePrivate::designerMode();
| 0-280 | ||||||||||||||||||
178 | if (!bypass)
| 30-258 | ||||||||||||||||||
179 | qmlExecuteDeferred(this); executed 258 times by 7 tests: qmlExecuteDeferred(this); Executed by:
| 258 | ||||||||||||||||||
180 | if (!d->animation || bypass) {
| 4-260 | ||||||||||||||||||
181 | if (d->animationInstance)
| 4-28 | ||||||||||||||||||
182 | d->animationInstance->stop(); executed 4 times by 1 test: d->animationInstance->stop(); Executed by:
| 4 | ||||||||||||||||||
183 | QQmlPropertyPrivate::write(d->property, value, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding); | - | ||||||||||||||||||
184 | d->targetValue = value; | - | ||||||||||||||||||
185 | return; executed 32 times by 5 tests: return; Executed by:
| 32 | ||||||||||||||||||
186 | } | - | ||||||||||||||||||
187 | - | |||||||||||||||||||
188 | bool behaviorActive = d->animation->isRunning(); | - | ||||||||||||||||||
189 | if (behaviorActive && value == d->targetValue)
| 0-204 | ||||||||||||||||||
190 | return; never executed: return; | 0 | ||||||||||||||||||
191 | - | |||||||||||||||||||
192 | d->targetValue = value; | - | ||||||||||||||||||
193 | - | |||||||||||||||||||
194 | if (d->animationInstance
| 68-188 | ||||||||||||||||||
195 | && (d->animationInstance->duration() != -1
| 14-54 | ||||||||||||||||||
196 | || d->animationInstance->isRenderThreadProxy())
| 0-54 | ||||||||||||||||||
197 | && !d->animationInstance->isStopped()) {
| 0-14 | ||||||||||||||||||
198 | d->blockRunningChanged = true; | - | ||||||||||||||||||
199 | d->animationInstance->stop(); | - | ||||||||||||||||||
200 | } never executed: end of block | 0 | ||||||||||||||||||
201 | // Render thread animations use "stop" to synchronize the property back | - | ||||||||||||||||||
202 | // to the item, so we need to read the value after. | - | ||||||||||||||||||
203 | const QVariant ¤tValue = d->property.read(); | - | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | // Don't unnecessarily wake up the animation system if no real animation | - | ||||||||||||||||||
206 | // is needed (value has not changed). If the Behavior was already | - | ||||||||||||||||||
207 | // running, let it continue as normal to ensure correct behavior and state. | - | ||||||||||||||||||
208 | if (!behaviorActive && d->targetValue == currentValue) {
| 34-204 | ||||||||||||||||||
209 | QQmlPropertyPrivate::write(d->property, value, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding); | - | ||||||||||||||||||
210 | return; executed 34 times by 3 tests: return; Executed by:
| 34 | ||||||||||||||||||
211 | } | - | ||||||||||||||||||
212 | - | |||||||||||||||||||
213 | QQuickStateOperation::ActionList actions; | - | ||||||||||||||||||
214 | QQuickStateAction action; | - | ||||||||||||||||||
215 | action.property = d->property; | - | ||||||||||||||||||
216 | action.fromValue = currentValue; | - | ||||||||||||||||||
217 | action.toValue = value; | - | ||||||||||||||||||
218 | actions << action; | - | ||||||||||||||||||
219 | - | |||||||||||||||||||
220 | QList<QQmlProperty> after; | - | ||||||||||||||||||
221 | QAbstractAnimationJob *prev = d->animationInstance; | - | ||||||||||||||||||
222 | d->animationInstance = d->animation->transition(actions, after, QQuickAbstractAnimation::Forward); | - | ||||||||||||||||||
223 | - | |||||||||||||||||||
224 | if (d->animationInstance && d->animation->threadingModel() == QQuickAbstractAnimation::RenderThread)
| 0-222 | ||||||||||||||||||
225 | d->animationInstance = new QQuickAnimatorProxyJob(d->animationInstance, d->animation); never executed: d->animationInstance = new QQuickAnimatorProxyJob(d->animationInstance, d->animation); | 0 | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | if (prev && prev != d->animationInstance)
| 0-158 | ||||||||||||||||||
228 | delete prev; executed 64 times by 3 tests: delete prev; Executed by:
| 64 | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | if (d->animationInstance) {
| 0-222 | ||||||||||||||||||
231 | if (d->animationInstance != prev)
| 0-222 | ||||||||||||||||||
232 | d->animationInstance->addAnimationChangeListener(d, QAbstractAnimationJob::StateChange); executed 222 times by 7 tests: d->animationInstance->addAnimationChangeListener(d, QAbstractAnimationJob::StateChange); Executed by:
| 222 | ||||||||||||||||||
233 | d->animationInstance->start(); | - | ||||||||||||||||||
234 | d->blockRunningChanged = false; | - | ||||||||||||||||||
235 | } executed 222 times by 7 tests: end of block Executed by:
| 222 | ||||||||||||||||||
236 | if (!after.contains(d->property))
| 10-212 | ||||||||||||||||||
237 | QQmlPropertyPrivate::write(d->property, value, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding); executed 10 times by 2 tests: QQmlPropertyPrivate::write(d->property, value, QQmlPropertyData::BypassInterceptor | QQmlPropertyData::DontRemoveBinding); Executed by:
| 10 | ||||||||||||||||||
238 | } executed 222 times by 7 tests: end of block Executed by:
| 222 | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | void QQuickBehavior::setTarget(const QQmlProperty &property) | - | ||||||||||||||||||
241 | { | - | ||||||||||||||||||
242 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
243 | d->property = property; | - | ||||||||||||||||||
244 | if (d->animation)
| 24-534 | ||||||||||||||||||
245 | d->animation->setDefaultTarget(property); executed 24 times by 4 tests: d->animation->setDefaultTarget(property); Executed by:
| 24 | ||||||||||||||||||
246 | - | |||||||||||||||||||
247 | QQmlEnginePrivate *engPriv = QQmlEnginePrivate::get(qmlEngine(this)); | - | ||||||||||||||||||
248 | static int finalizedIdx = -1; | - | ||||||||||||||||||
249 | if (finalizedIdx < 0)
| 14-544 | ||||||||||||||||||
250 | finalizedIdx = metaObject()->indexOfSlot("componentFinalized()"); executed 14 times by 7 tests: finalizedIdx = metaObject()->indexOfSlot("componentFinalized()"); Executed by:
| 14 | ||||||||||||||||||
251 | engPriv->registerFinalizeCallback(this, finalizedIdx); | - | ||||||||||||||||||
252 | } executed 558 times by 7 tests: end of block Executed by:
| 558 | ||||||||||||||||||
253 | - | |||||||||||||||||||
254 | void QQuickBehavior::componentFinalized() | - | ||||||||||||||||||
255 | { | - | ||||||||||||||||||
256 | Q_D(QQuickBehavior); | - | ||||||||||||||||||
257 | d->finalized = true; | - | ||||||||||||||||||
258 | } executed 558 times by 7 tests: end of block Executed by:
| 558 | ||||||||||||||||||
259 | - | |||||||||||||||||||
260 | QT_END_NAMESPACE | - | ||||||||||||||||||
261 | - | |||||||||||||||||||
262 | #include "moc_qquickbehavior_p.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |