10#include <QWaitCondition>
12#include <QCoreApplication>
13#include <QEasingCurve>
15#include <QtCore/private/qnumeric_p.h>
25 Update(QQuickTimeLineValue *_g, qreal _v)
27 Update(
const QQuickTimeLineCallback &_e)
28 :
g(
nullptr),
v(0),
e(
_e) {}
30 QQuickTimeLineValue *
g;
32 QQuickTimeLineCallback
e;
50 Op(
Type t,
int l, qreal v, qreal v2,
int o,
51 const QQuickTimeLineCallback &ev = QQuickTimeLineCallback(),
const QEasingCurve &es = QEasingCurve())
59 value2 = o.value2;
order = o
.order; event = o.event;
88 void add(QQuickTimeLineObject &,
const Op &);
110 if (g._t && g._t != q) {
111 qWarning() <<
"QQuickTimeLine: Cannot modify a QQuickTimeLineValue owned by"
112 <<
"another timeline.";
117 Ops::Iterator iter =
ops.find(&g);
118 if (iter ==
ops.end()) {
121 q->pause(g, syncPoint);
123 if (!iter->ops.isEmpty() &&
125 iter->ops.constLast().type ==
Op::Pause) {
127 iter->ops.last().length += o
.length;
135 if (iter->length >
length)
143 if (syncMode == QQuickTimeLine::LocalSync) {
150
151
152
153
154
155
174 }
else if (time == (op
.length)) {
177 qreal delta = op.value - base;
178 qreal pTime = (qreal)(time) / (qreal)op.length;
179 if (op.easing.type() == QEasingCurve::Linear)
180 return base + delta * pTime;
182 return base + delta * op.easing.valueForProgress(pTime);
187 }
else if (time == (op
.length)) {
188 return base + op.value;
190 qreal delta = op.value;
191 qreal pTime = (qreal)(time) / (qreal)op.length;
192 if (op.easing.type() == QEasingCurve::Linear)
193 return base + delta * pTime;
195 return base + delta * op.easing.valueForProgress(pTime);
201 qreal t = (qreal)(time) / 1000.0f;
202 qreal delta = op.value * t + 0.5f * op.value2 * t * t;
208 }
else if (time == (op
.length)) {
209 return base + op.value2;
211 qreal t = (qreal)(time) / 1000.0f;
212 qreal accel = -1.0f * 1000.0f * op.value / (qreal)op.length;
213 qreal delta = op.value * t + 0.5f * accel * t * t;
218 op.event.d0(op.event.d1);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
282
283
284QQuickTimeLine::QQuickTimeLine(QObject *parent)
287 d =
new QQuickTimeLinePrivate(
this);
291
292
293
294QQuickTimeLine::~QQuickTimeLine()
296 for (QQuickTimeLinePrivate::Ops::Iterator iter = d->ops.begin();
297 iter != d->ops.end();
299 iter.key()->_t =
nullptr;
301 delete d; d =
nullptr;
305
306
309
310
311QQuickTimeLine::SyncMode QQuickTimeLine::syncMode()
const
317
318
319void QQuickTimeLine::setSyncMode(SyncMode syncMode)
321 d->syncMode = syncMode;
325
326
327void QQuickTimeLine::pause(QQuickTimeLineObject &obj,
int time)
329 if (time <= 0)
return;
330 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Pause, time, 0., 0., d->order++);
335
336
337void QQuickTimeLine::callback(
const QQuickTimeLineCallback &callback)
339 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Execute, 0, 0, 0., d->order++, callback);
340 d->add(*callback.callbackObject(), op);
344
345
346void QQuickTimeLine::set(QQuickTimeLineValue &timeLineValue, qreal value)
348 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Set, 0, value, 0., d->order++);
349 d->add(timeLineValue, op);
353
354
355
356
357
358int QQuickTimeLine::accel(QQuickTimeLineValue &timeLineValue, qreal velocity, qreal acceleration)
360 if (qFuzzyIsNull(acceleration) || qt_is_nan(acceleration))
363 if ((velocity > 0.0f) == (acceleration > 0.0f))
364 acceleration = acceleration * -1.0f;
366 int time =
static_cast<
int>(-1000 * velocity / acceleration);
367 if (time <= 0)
return -1;
369 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Accel, time, velocity, acceleration, d->order++);
370 d->add(timeLineValue, op);
376
377
378
379
380
381
382
383
384
385int QQuickTimeLine::accel(QQuickTimeLineValue &timeLineValue, qreal velocity, qreal acceleration, qreal maxDistance)
387 if (qFuzzyIsNull(maxDistance) || qt_is_nan(maxDistance) || qFuzzyIsNull(acceleration) || qt_is_nan(acceleration))
390 Q_ASSERT(acceleration > 0.0f && maxDistance > 0.0f);
392 qreal maxAccel = (velocity * velocity) / (2.0f * maxDistance);
393 if (maxAccel > acceleration)
394 acceleration = maxAccel;
396 if ((velocity > 0.0f) == (acceleration > 0.0f))
397 acceleration = acceleration * -1.0f;
399 int time =
static_cast<
int>(-1000 * velocity / acceleration);
400 if (time <= 0)
return -1;
402 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Accel, time, velocity, acceleration, d->order++);
403 d->add(timeLineValue, op);
409
410
411
412
413
414
415int QQuickTimeLine::accelDistance(QQuickTimeLineValue &timeLineValue, qreal velocity, qreal distance)
417 if (qFuzzyIsNull(distance) || qt_is_nan(distance) || qFuzzyIsNull(velocity) || qt_is_nan(velocity))
420 Q_ASSERT((distance >= 0.0f) == (velocity >= 0.0f));
422 int time =
static_cast<
int>(1000 * (2.0f * distance) / velocity);
423 if (time <= 0)
return -1;
425 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::AccelDistance, time, velocity, distance, d->order++);
426 d->add(timeLineValue, op);
432
433
434
435void QQuickTimeLine::move(QQuickTimeLineValue &timeLineValue, qreal destination,
int time)
437 if (time <= 0)
return;
438 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Move, time, destination, 0.0f, d->order++);
439 d->add(timeLineValue, op);
443
444
445
446void QQuickTimeLine::move(QQuickTimeLineValue &timeLineValue, qreal destination,
const QEasingCurve &easing,
int time)
448 if (time <= 0)
return;
449 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::Move, time, destination, 0.0f, d->order++, QQuickTimeLineCallback(), easing);
450 d->add(timeLineValue, op);
454
455
456
457void QQuickTimeLine::moveBy(QQuickTimeLineValue &timeLineValue, qreal change,
int time)
459 if (time <= 0)
return;
460 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::MoveBy, time, change, 0.0f, d->order++);
461 d->add(timeLineValue, op);
465
466
467
468void QQuickTimeLine::moveBy(QQuickTimeLineValue &timeLineValue, qreal change,
const QEasingCurve &easing,
int time)
470 if (time <= 0)
return;
471 QQuickTimeLinePrivate::Op op(QQuickTimeLinePrivate::Op::MoveBy, time, change, 0.0f, d->order++, QQuickTimeLineCallback(), easing);
472 d->add(timeLineValue, op);
476
477
478void QQuickTimeLine::reset(QQuickTimeLineValue &timeLineValue)
480 if (!timeLineValue._t)
482 if (timeLineValue._t !=
this) {
483 qWarning() <<
"QQuickTimeLine: Cannot reset a QQuickTimeLineValue owned by another timeline.";
486 qCDebug(lcTl) <<
static_cast<QObject*>(
this) << timeLineValue.value();
487 remove(&timeLineValue);
488 timeLineValue._t =
nullptr;
491int QQuickTimeLine::duration()
const
497
498
499
500
501
502
503
504
505
506
507void QQuickTimeLine::sync(QQuickTimeLineValue &timeLineValue, QQuickTimeLineValue &syncTo)
509 QQuickTimeLinePrivate::Ops::Iterator iter = d->ops.find(&syncTo);
510 if (iter == d->ops.end())
512 int length = iter->length;
514 iter = d->ops.find(&timeLineValue);
515 if (iter == d->ops.end()) {
516 pause(timeLineValue, length);
518 int glength = iter->length;
519 pause(timeLineValue, length - glength);
524
525
526
527
528
529
530
531
532void QQuickTimeLine::sync(QQuickTimeLineValue &timeLineValue)
534 QQuickTimeLinePrivate::Ops::Iterator iter = d->ops.find(&timeLineValue);
535 if (iter == d->ops.end()) {
536 pause(timeLineValue, d->length);
538 pause(timeLineValue, d->length - iter->length);
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
571
572
573
574
575
576
577
580
581
582
583
584void QQuickTimeLine::setSyncPoint(
int sp)
590
591
592
593
594int QQuickTimeLine::syncPoint()
const
600
601
602
603bool QQuickTimeLine::isActive()
const
605 return !d->ops.isEmpty();
609
610
611
612
613
614
615
616
617
618
619
620void QQuickTimeLine::complete()
622 d->advance(d->length);
626
627
628
629
630
631
632
633
634
635
636
637void QQuickTimeLine::clear()
639 for (QQuickTimeLinePrivate::Ops::const_iterator iter = d->ops.cbegin(), cend = d->ops.cend(); iter != cend; ++iter)
640 iter.key()->_t =
nullptr;
647int QQuickTimeLine::time()
const
653
654
655
656
657
659void QQuickTimeLine::updateCurrentTime(
int v)
661 if (d->syncAdj == -1)
665 int timeChanged = v - d->prevTime;
671 d->advance(timeChanged);
675 if (d->ops.isEmpty()) {
678 d->clockRunning =
false;
681
682
683
684
685
686elseif(state()!=Running){
689 d->clockRunning =
true;
695void QQuickTimeLine::debugAnimation(QDebug d)
const
697 d <<
"QuickTimeLine(" << Qt::hex << (
const void *)
this << Qt::dec <<
")";
703 return lhs.first < rhs.first;
715 for (Ops::const_iterator iter =
ops.constBegin(), cend = ops.constEnd(); iter != cend; ++iter) {
716 const TimeLine &tl = *iter;
717 const Op &op = tl.ops.first();
718 int length = op.length - tl.consumedOpLength;
720 if (length < advanceTime) {
721 advanceTime = length;
722 if (advanceTime == 0)
730 QList<std::pair<
int, Update> > updates;
732 for (Ops::Iterator iter =
ops.begin(); iter !=
ops.end(); ) {
733 QQuickTimeLineValue *v =
static_cast<QQuickTimeLineValue *>(iter.key());
735 Q_ASSERT(!tl.ops.isEmpty());
738 Op &op = tl.ops.first();
739 if (advanceTime == 0 && op
.length != 0)
745 tl.base = v->value();
750 updates << std::make_pair(op.order, Update(op.event));
752 bool changed =
false;
753 qreal val = value(op, op.length, tl.base, &changed);
755 updates << std::make_pair(op.order, Update(v, val));
759 tl.ops.removeFirst();
764 bool changed =
false;
765 qreal val = value(op, tl.consumedOpLength, tl.base, &changed);
767 updates << std::make_pair(op.order, Update(v, val));
772 }
while(!tl.ops.isEmpty() && advanceTime == 0 && tl.ops.first().length == 0);
775 if (tl.ops.isEmpty()) {
776 iter =
ops.erase(iter);
779 if (tl.ops.first().type ==
Op::Pause && pauseTime != 0) {
781 if (pauseTime == -1 || opPauseTime < pauseTime)
782 pauseTime = opPauseTime;
793 std::sort(updates.begin(), updates.end());
794 updateQueue = &updates;
795 for (
int ii = 0; ii < updates.size(); ++ii) {
796 const Update &v = updates.at(ii).second;
803 updateQueue =
nullptr;
809void QQuickTimeLine::remove(QQuickTimeLineObject *v)
811 QQuickTimeLinePrivate::Ops::Iterator iter = d->ops.find(v);
812 Q_ASSERT(iter != d->ops.end());
814 int len = iter->length;
816 if (len == d->length) {
819 for (QQuickTimeLinePrivate::Ops::Iterator iter = d->ops.begin();
820 iter != d->ops.end();
823 if (iter->length > d->length)
824 d->length = iter->length;
828 if (d->ops.isEmpty()) {
830 d->clockRunning =
false;
831 }
else if (state() != Running) {
834 d->clockRunning =
true;
836 if (d->syncMode == QQuickTimeLine::LocalSync) {
844 if (d->updateQueue) {
845 for (
int ii = 0; ii < d->updateQueue->size(); ++ii) {
846 if (d->updateQueue->at(ii).second.g == v ||
847 d->updateQueue->at(ii).second.e.callbackObject() == v) {
848 d->updateQueue->removeAt(ii);
858
859
860
861
864
865
866
867
870
871
872
873
876
877
878
879
882
883
884
885
886
889QQuickTimeLineObject::QQuickTimeLineObject()
894QQuickTimeLineObject::~QQuickTimeLineObject()
902QQuickTimeLineCallback::QQuickTimeLineCallback()
903: d0(
nullptr), d1(
nullptr), d2(
nullptr)
907QQuickTimeLineCallback::QQuickTimeLineCallback(QQuickTimeLineObject *b, Callback f,
void *d)
912QQuickTimeLineCallback::QQuickTimeLineCallback(
const QQuickTimeLineCallback &o)
913: d0(o.d0), d1(o.d1), d2(o.d2)
917QQuickTimeLineCallback &QQuickTimeLineCallback::operator=(
const QQuickTimeLineCallback &o)
925QQuickTimeLineObject *QQuickTimeLineCallback::callbackObject()
const
932#include "moc_qquicktimeline_p_p.cpp"
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")
bool operator<(const std::pair< int, Update > &lhs, const std::pair< int, Update > &rhs)
Op & operator=(const Op &o)
Op(Type t, int l, qreal v, qreal v2, int o, const QQuickTimeLineCallback &ev=QQuickTimeLineCallback(), const QEasingCurve &es=QEasingCurve())
QQuickTimeLineCallback event
QList< std::pair< int, Update > > * updateQueue
qreal value(const Op &op, int time, qreal base, bool *) const
void add(QQuickTimeLineObject &, const Op &)
QQuickTimeLinePrivate(QQuickTimeLine *)
QHash< QQuickTimeLineObject *, TimeLine > Ops
Update(const QQuickTimeLineCallback &_e)
Update(QQuickTimeLineValue *_g, qreal _v)