2#include "qml_material/anim/motion_runner.hpp"
3#include "qml_material/style/motion_settings.hpp"
4#include <QQmlParserStatus>
21QML_MATERIAL_API std::optional<Vector> encode(
const QVariant&, ValueKind);
22QML_MATERIAL_API QVariant decode(
const Vector&, ValueKind);
23QML_MATERIAL_API QVariant velocityValue(
const Vector&, ValueKind);
25class QML_MATERIAL_API MotionValueBase :
public QObject,
public QQmlParserStatus {
28 Q_INTERFACES(QQmlParserStatus)
29 Q_PROPERTY(AnimationSpec spec READ spec WRITE setSpec RESET resetSpec NOTIFY specChanged FINAL)
30 Q_PROPERTY(
bool active READ active NOTIFY changed FINAL)
31 Q_PROPERTY(
bool running READ running NOTIFY changed FINAL)
32 Q_PROPERTY(
bool paused READ paused NOTIFY changed FINAL)
33 Q_PROPERTY(
int state READ state NOTIFY changed FINAL)
34 Q_PROPERTY(QString runId READ runId NOTIFY changed FINAL)
36 bool manualClock READ manualClock WRITE setManualClock NOTIFY manualClockChanged FINAL)
37 Q_PROPERTY(MotionSettings* motion READ settings WRITE setSettings NOTIFY settingsChanged FINAL)
41 Idle = MotionRunner::Idle,
42 Running = MotionRunner::Running,
43 Paused = MotionRunner::Paused,
44 Interacting = MotionRunner::Interacting
49 Finished = MotionRunner::Finished,
50 Completed = MotionRunner::Completed,
51 Cancelled = MotionRunner::Cancelled,
52 Interrupted = MotionRunner::Interrupted,
53 BoundReached = MotionRunner::BoundReached
56 void classBegin()
override { m_initializing =
true; }
57 void componentComplete()
override;
58 AnimationSpec spec()
const;
59 void setSpec(AnimationSpec);
61 bool active()
const {
return m_runner.active(); }
62 bool running()
const {
return m_runner.state() == MotionRunner::Running; }
63 bool paused()
const {
return m_runner.state() == MotionRunner::Paused; }
64 int state()
const {
return m_runner.state(); }
65 QString runId()
const {
return m_runner.id(); }
66 bool manualClock()
const {
return m_runner.manualClock(); }
67 void setManualClock(
bool);
68 MotionSettings* settings()
const {
return m_settings; }
69 void setSettings(MotionSettings*);
70 MotionRunner& runner() {
return m_runner; }
71 Q_INVOKABLE
void advance(qreal milliseconds) { m_runner.advance(milliseconds); }
72 Q_SIGNAL
void changed();
73 Q_SIGNAL
void specChanged();
74 Q_SIGNAL
void manualClockChanged();
75 Q_SIGNAL
void settingsChanged();
76 Q_SIGNAL
void ended(QString
id,
int reason, QVariant value, QVariant velocity);
79 MotionValueBase(anim::ValueKind, QObject*);
80 QVariant value()
const;
81 QVariant velocity()
const;
82 QVariant target()
const;
83 bool setInitial(
const QVariant&);
84 QString animate(
const QVariant&, AnimationSpec,
const QVariant& velocity = {});
85 bool snap(
const QVariant&);
86 bool update(
const QString&,
const QVariant&);
87 QString settle(
const QString&,
const QVariant&,
const QVariant&, AnimationSpec);
88 virtual void policyChanged();
89 const anim::ValueKind m_kind;
90 MotionRunner m_runner;
91 bool m_initializing =
true;
95 QPointer<MotionSettings> m_settings;
96 QMetaObject::Connection m_policyChanged, m_policyDestroyed;
97 std::optional<AnimationSpec> m_spec;
99class QML_MATERIAL_API AnimatableBase :
public MotionValueBase {
103 Q_INVOKABLE
void pause() { m_runner.pause(); }
104 Q_INVOKABLE
void resume() { m_runner.resume(); }
105 Q_INVOKABLE
void stop(QString
id = {}) { m_runner.stop(
id); }
106 Q_INVOKABLE
void complete(QString
id = {}) { m_runner.complete(
id); }
107 Q_INVOKABLE QString beginInteraction() {
return m_runner.beginInteraction(); }
108 Q_INVOKABLE
bool cancelInteraction(QString
id) {
return m_runner.cancelInteraction(
id); }
111 using MotionValueBase::MotionValueBase;
113class QML_MATERIAL_API Animatable :
public AnimatableBase {
116 Q_PROPERTY(qreal value READ value NOTIFY changed FINAL)
117 Q_PROPERTY(qreal velocity READ velocity NOTIFY changed FINAL)
118 Q_PROPERTY(qreal targetValue READ targetValue NOTIFY changed FINAL)
120 qreal initialValue READ initialValue WRITE setInitialValue NOTIFY initialValueChanged FINAL)
122 explicit Animatable(QObject* parent =
nullptr): AnimatableBase(anim::ValueKind::Real, parent) {}
123 qreal value()
const {
return MotionValueBase::value().value<qreal>(); }
124 qreal velocity()
const {
return MotionValueBase::velocity().value<qreal>(); }
125 qreal targetValue()
const {
return target().value<qreal>(); }
126 qreal initialValue()
const {
return m_initial; }
127 void setInitialValue(qreal v) {
128 if (m_initial == v || ! m_initializing || ! anim::encode(QVariant::fromValue(v), m_kind))
131 const QPointer<Animatable> guard(
this);
132 if (setInitial(QVariant::fromValue(v)) && guard) emit initialValueChanged();
134 Q_INVOKABLE QString animateTo(qreal v) {
return animate(QVariant::fromValue(v), spec()); }
135 Q_INVOKABLE QString animateTo(qreal v, AnimationSpec s) {
136 return animate(QVariant::fromValue(v), s);
138 Q_INVOKABLE QString animateTo(qreal v, AnimationSpec s, qreal velocity) {
139 return animate(QVariant::fromValue(v), s, QVariant::fromValue(velocity));
141 Q_INVOKABLE
bool snapTo(qreal v) {
return snap(QVariant::fromValue(v)); }
142 Q_INVOKABLE
bool updateInteraction(QString
id, qreal v) {
143 return update(
id, QVariant::fromValue(v));
145 Q_INVOKABLE QString settleInteraction(QString
id, qreal v, qreal velocity, AnimationSpec s) {
146 return settle(
id, QVariant::fromValue(v), QVariant::fromValue(velocity), s);
148 Q_SIGNAL
void initialValueChanged();
151 qreal m_initial { 0 };
154 Q_INVOKABLE QString animateDecay(qreal velocity, DecaySpec s) {
155 const auto v = anim::encode(QVariant::fromValue(velocity), m_kind);
156 return v ? m_runner.animateDecay(*v, s) : QString();
158 Q_INVOKABLE
bool setBounds(qreal lower, qreal upper) {
159 const auto lo = anim::encode(QVariant::fromValue(lower), m_kind),
160 hi = anim::encode(QVariant::fromValue(upper), m_kind);
161 return lo && hi && m_runner.setBounds(*lo, *hi);
164class QML_MATERIAL_API PointAnimatable :
public AnimatableBase {
167 Q_PROPERTY(QPointF value READ value NOTIFY changed FINAL)
168 Q_PROPERTY(QPointF velocity READ velocity NOTIFY changed FINAL)
169 Q_PROPERTY(QPointF targetValue READ targetValue NOTIFY changed FINAL)
170 Q_PROPERTY(QPointF initialValue READ initialValue WRITE setInitialValue NOTIFY
171 initialValueChanged FINAL)
173 explicit PointAnimatable(QObject* parent =
nullptr)
174 : AnimatableBase(anim::ValueKind::Point, parent) {}
175 QPointF value()
const {
return MotionValueBase::value().value<QPointF>(); }
176 QPointF velocity()
const {
return MotionValueBase::velocity().value<QPointF>(); }
177 QPointF targetValue()
const {
return target().value<QPointF>(); }
178 QPointF initialValue()
const {
return m_initial; }
179 void setInitialValue(QPointF v) {
180 if (m_initial == v || ! m_initializing || ! anim::encode(QVariant::fromValue(v), m_kind))
183 const QPointer<PointAnimatable> guard(
this);
184 if (setInitial(QVariant::fromValue(v)) && guard) emit initialValueChanged();
186 Q_INVOKABLE QString animateTo(QPointF v) {
return animate(QVariant::fromValue(v), spec()); }
187 Q_INVOKABLE QString animateTo(QPointF v, AnimationSpec s) {
188 return animate(QVariant::fromValue(v), s);
190 Q_INVOKABLE QString animateTo(QPointF v, AnimationSpec s, QPointF velocity) {
191 return animate(QVariant::fromValue(v), s, QVariant::fromValue(velocity));
193 Q_INVOKABLE
bool snapTo(QPointF v) {
return snap(QVariant::fromValue(v)); }
194 Q_INVOKABLE
bool updateInteraction(QString
id, QPointF v) {
195 return update(
id, QVariant::fromValue(v));
197 Q_INVOKABLE QString settleInteraction(QString
id, QPointF v, QPointF velocity,
199 return settle(
id, QVariant::fromValue(v), QVariant::fromValue(velocity), s);
201 Q_SIGNAL
void initialValueChanged();
204 QPointF m_initial { QPointF() };
207 Q_INVOKABLE QString animateDecay(QPointF velocity, DecaySpec s) {
208 const auto v = anim::encode(QVariant::fromValue(velocity), m_kind);
209 return v ? m_runner.animateDecay(*v, s) : QString();
211 Q_INVOKABLE
bool setBounds(QPointF lower, QPointF upper) {
212 const auto lo = anim::encode(QVariant::fromValue(lower), m_kind),
213 hi = anim::encode(QVariant::fromValue(upper), m_kind);
214 return lo && hi && m_runner.setBounds(*lo, *hi);
217class QML_MATERIAL_API SizeAnimatable :
public AnimatableBase {
220 Q_PROPERTY(QSizeF value READ value NOTIFY changed FINAL)
221 Q_PROPERTY(QSizeF velocity READ velocity NOTIFY changed FINAL)
222 Q_PROPERTY(QSizeF targetValue READ targetValue NOTIFY changed FINAL)
223 Q_PROPERTY(QSizeF initialValue READ initialValue WRITE setInitialValue NOTIFY
224 initialValueChanged FINAL)
226 explicit SizeAnimatable(QObject* parent =
nullptr)
227 : AnimatableBase(anim::ValueKind::Size, parent) {}
228 QSizeF value()
const {
return MotionValueBase::value().value<QSizeF>(); }
229 QSizeF velocity()
const {
return MotionValueBase::velocity().value<QSizeF>(); }
230 QSizeF targetValue()
const {
return target().value<QSizeF>(); }
231 QSizeF initialValue()
const {
return m_initial; }
232 void setInitialValue(QSizeF v) {
233 if (m_initial == v || ! m_initializing || ! anim::encode(QVariant::fromValue(v), m_kind))
236 const QPointer<SizeAnimatable> guard(
this);
237 if (setInitial(QVariant::fromValue(v)) && guard) emit initialValueChanged();
239 Q_INVOKABLE QString animateTo(QSizeF v) {
return animate(QVariant::fromValue(v), spec()); }
240 Q_INVOKABLE QString animateTo(QSizeF v, AnimationSpec s) {
241 return animate(QVariant::fromValue(v), s);
243 Q_INVOKABLE QString animateTo(QSizeF v, AnimationSpec s, QSizeF velocity) {
244 return animate(QVariant::fromValue(v), s, QVariant::fromValue(velocity));
246 Q_INVOKABLE
bool snapTo(QSizeF v) {
return snap(QVariant::fromValue(v)); }
247 Q_INVOKABLE
bool updateInteraction(QString
id, QSizeF v) {
248 return update(
id, QVariant::fromValue(v));
250 Q_INVOKABLE QString settleInteraction(QString
id, QSizeF v, QSizeF velocity, AnimationSpec s) {
251 return settle(
id, QVariant::fromValue(v), QVariant::fromValue(velocity), s);
253 Q_SIGNAL
void initialValueChanged();
256 QSizeF m_initial { QSizeF(0, 0) };
259 Q_INVOKABLE QString animateDecay(QSizeF velocity, DecaySpec s) {
260 const auto v = anim::encode(QVariant::fromValue(velocity), m_kind);
261 return v ? m_runner.animateDecay(*v, s) : QString();
263 Q_INVOKABLE
bool setBounds(QSizeF lower, QSizeF upper) {
264 const auto lo = anim::encode(QVariant::fromValue(lower), m_kind),
265 hi = anim::encode(QVariant::fromValue(upper), m_kind);
266 return lo && hi && m_runner.setBounds(*lo, *hi);
269class QML_MATERIAL_API ColorAnimatable :
public AnimatableBase {
272 Q_PROPERTY(QColor value READ value NOTIFY changed FINAL)
273 Q_PROPERTY(QVector4D velocity READ velocity NOTIFY changed FINAL)
274 Q_PROPERTY(QColor targetValue READ targetValue NOTIFY changed FINAL)
275 Q_PROPERTY(QColor initialValue READ initialValue WRITE setInitialValue NOTIFY
276 initialValueChanged FINAL)
278 explicit ColorAnimatable(QObject* parent =
nullptr)
279 : AnimatableBase(anim::ValueKind::Color, parent) {}
280 QColor value()
const {
return MotionValueBase::value().value<QColor>(); }
281 QVector4D velocity()
const {
return MotionValueBase::velocity().value<QVector4D>(); }
282 QColor targetValue()
const {
return target().value<QColor>(); }
283 QColor initialValue()
const {
return m_initial; }
284 void setInitialValue(QColor v) {
285 if (m_initial == v || ! m_initializing || ! anim::encode(QVariant::fromValue(v), m_kind))
288 const QPointer<ColorAnimatable> guard(
this);
289 if (setInitial(QVariant::fromValue(v)) && guard) emit initialValueChanged();
291 Q_INVOKABLE QString animateTo(QColor v) {
return animate(QVariant::fromValue(v), spec()); }
292 Q_INVOKABLE QString animateTo(QColor v, AnimationSpec s) {
293 return animate(QVariant::fromValue(v), s);
295 Q_INVOKABLE QString animateTo(QColor v, AnimationSpec s, QVector4D velocity) {
296 return animate(QVariant::fromValue(v), s, QVariant::fromValue(velocity));
298 Q_INVOKABLE
bool snapTo(QColor v) {
return snap(QVariant::fromValue(v)); }
299 Q_INVOKABLE
bool updateInteraction(QString
id, QColor v) {
300 return update(
id, QVariant::fromValue(v));
302 Q_INVOKABLE QString settleInteraction(QString
id, QColor v, QVector4D velocity,
304 return settle(
id, QVariant::fromValue(v), QVariant::fromValue(velocity), s);
306 Q_SIGNAL
void initialValueChanged();
309 QColor m_initial { QColor(Qt::transparent) };
311class QML_MATERIAL_API AnimatedValueBase :
public MotionValueBase {
314 Q_PROPERTY(
bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged FINAL)
316 bool enabled()
const {
return m_enabled; }
317 void setEnabled(
bool);
318 void componentComplete()
override;
319 Q_SIGNAL
void targetValueChanged();
320 Q_SIGNAL
void enabledChanged();
323 using MotionValueBase::MotionValueBase;
324 void follow(
const QVariant&);
325 QVariant requested()
const {
return m_requested.isValid() ? m_requested : target(); }
326 void policyChanged()
override;
329 QVariant m_requested;
330 bool m_enabled =
true;
332class QML_MATERIAL_API AnimatedReal :
public AnimatedValueBase {
335 Q_PROPERTY(qreal value READ value NOTIFY changed FINAL)
337 qreal targetValue READ targetValue WRITE setTargetValue NOTIFY targetValueChanged FINAL)
339 explicit AnimatedReal(QObject* p =
nullptr): AnimatedValueBase(anim::ValueKind::Real, p) {}
340 qreal value()
const {
return MotionValueBase::value().value<qreal>(); }
341 qreal targetValue()
const {
return requested().value<qreal>(); }
342 void setTargetValue(qreal v) { follow(QVariant::fromValue(v)); }
344class QML_MATERIAL_API AnimatedPoint :
public AnimatedValueBase {
347 Q_PROPERTY(QPointF value READ value NOTIFY changed FINAL)
349 QPointF targetValue READ targetValue WRITE setTargetValue NOTIFY targetValueChanged FINAL)
351 explicit AnimatedPoint(QObject* p =
nullptr): AnimatedValueBase(anim::ValueKind::Point, p) {}
352 QPointF value()
const {
return MotionValueBase::value().value<QPointF>(); }
353 QPointF targetValue()
const {
return requested().value<QPointF>(); }
354 void setTargetValue(QPointF v) { follow(QVariant::fromValue(v)); }
356class QML_MATERIAL_API AnimatedSize :
public AnimatedValueBase {
359 Q_PROPERTY(QSizeF value READ value NOTIFY changed FINAL)
361 QSizeF targetValue READ targetValue WRITE setTargetValue NOTIFY targetValueChanged FINAL)
363 explicit AnimatedSize(QObject* p =
nullptr): AnimatedValueBase(anim::ValueKind::Size, p) {}
364 QSizeF value()
const {
return MotionValueBase::value().value<QSizeF>(); }
365 QSizeF targetValue()
const {
return requested().value<QSizeF>(); }
366 void setTargetValue(QSizeF v) { follow(QVariant::fromValue(v)); }
368class QML_MATERIAL_API AnimatedColor :
public AnimatedValueBase {
371 Q_PROPERTY(QColor value READ value NOTIFY changed FINAL)
373 QColor targetValue READ targetValue WRITE setTargetValue NOTIFY targetValueChanged FINAL)
375 explicit AnimatedColor(QObject* p =
nullptr): AnimatedValueBase(anim::ValueKind::Color, p) {}
376 QColor value()
const {
return MotionValueBase::value().value<QColor>(); }
377 QColor targetValue()
const {
return requested().value<QColor>(); }
378 void setTargetValue(QColor v) { follow(QVariant::fromValue(v)); }