aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquickitemanimation.cpp
blob: 23694e2de3398ce34d2c3ed8afa240519e27f1fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qquickitemanimation_p.h"
#include "qquickitemanimation_p_p.h"
#include "qquickstateoperations_p.h"

#include <private/qqmlproperty_p.h>
#if QT_CONFIG(quick_path)
#include <private/qquickpath_p.h>
#endif
#include "private/qparallelanimationgroupjob_p.h"
#include "private/qsequentialanimationgroupjob_p.h"

#include <QtCore/qmath.h>
#include <QtGui/qtransform.h>
#include <QtQml/qqmlinfo.h>

QT_BEGIN_NAMESPACE

/*!
    \qmltype ParentAnimation
    \instantiates QQuickParentAnimation
    \inqmlmodule QtQuick
    \ingroup qtquick-animation-properties
    \since 5.0
    \inherits Animation
    \brief Animates changes in parent values.

    ParentAnimation is used to animate a parent change for an \l Item.

    For example, the following ParentChange changes \c blueRect to become
    a child of \c redRect when it is clicked. The inclusion of the
    ParentAnimation, which defines a NumberAnimation to be applied during
    the transition, ensures the item animates smoothly as it moves to
    its new parent:

    \snippet qml/parentanimation.qml 0

    A ParentAnimation can contain any number of animations. These animations will
    be run in parallel; to run them sequentially, define them within a
    SequentialAnimation.

    In some cases, such as when reparenting between items with clipping enabled, it is useful
    to animate the parent change via another item that does not have clipping
    enabled. Such an item can be set using the \l via property.

    ParentAnimation is typically used within a \l Transition in conjunction
    with a ParentChange. When used in this manner, it animates any
    ParentChange that has occurred during the state change. This can be
    overridden by setting a specific target item using the \l target property.

    \sa {Animation and Transitions in Qt Quick}, {Qt Quick Examples - Animation}
*/
QQuickParentAnimation::QQuickParentAnimation(QObject *parent)
    : QQuickAnimationGroup(*(new QQuickParentAnimationPrivate), parent)
{
}

QQuickParentAnimation::~QQuickParentAnimation()
{
}

/*!
    \qmlproperty Item QtQuick::ParentAnimation::target
    The item to reparent.

    When used in a transition, if no target is specified, all
    ParentChange occurrences are animated by the ParentAnimation.
*/
QQuickItem *QQuickParentAnimation::target() const
{
    Q_D(const QQuickParentAnimation);
    return d->target;
}

void QQuickParentAnimation::setTargetObject(QQuickItem *target)
{
    Q_D(QQuickParentAnimation);
    if (target == d->target)
        return;

    d->target = target;
    emit targetChanged();
}

/*!
    \qmlproperty Item QtQuick::ParentAnimation::newParent
    The new parent to animate to.

    If the ParentAnimation is defined within a \l Transition,
    this value defaults to the value defined in the end state of the
    \l Transition.
*/
QQuickItem *QQuickParentAnimation::newParent() const
{
    Q_D(const QQuickParentAnimation);
    return d->newParent;
}

void QQuickParentAnimation::setNewParent(QQuickItem *newParent)
{
    Q_D(QQuickParentAnimation);
    if (newParent == d->newParent)
        return;

    d->newParent = newParent;
    emit newParentChanged();
}

/*!
    \qmlproperty Item QtQuick::ParentAnimation::via
    The item to reparent via. This provides a way to do an unclipped animation
    when both the old parent and new parent are clipped.

    \qml
    ParentAnimation {
        target: myItem
        via: topLevelItem
        // ...
    }
    \endqml

    \note This only works when the ParentAnimation is used in a \l Transition
    in conjunction with a ParentChange.
*/
QQuickItem *QQuickParentAnimation::via() const
{
    Q_D(const QQuickParentAnimation);
    return d->via;
}

void QQuickParentAnimation::setVia(QQuickItem *via)
{
    Q_D(QQuickParentAnimation);
    if (via == d->via)
        return;

    d->via = via;
    emit viaChanged();
}

//### mirrors same-named function in QQuickItem
QPointF QQuickParentAnimationPrivate::computeTransformOrigin(QQuickItem::TransformOrigin origin, qreal width, qreal height) const
{
    switch (origin) {
    default:
    case QQuickItem::TopLeft:
        return QPointF(0, 0);
    case QQuickItem::Top:
        return QPointF(width / 2., 0);
    case QQuickItem::TopRight:
        return QPointF(width, 0);
    case QQuickItem::Left:
        return QPointF(0, height / 2.);
    case QQuickItem::Center:
        return QPointF(width / 2., height / 2.);
    case QQuickItem::Right:
        return QPointF(width, height / 2.);
    case QQuickItem::BottomLeft:
        return QPointF(0, height);
    case QQuickItem::Bottom:
        return QPointF(width / 2., height);
    case QQuickItem::BottomRight:
        return QPointF(width, height);
    }
}

struct QQuickParentAnimationData : public QAbstractAnimationAction
{
    QQuickParentAnimationData() : reverse(false) {}
    ~QQuickParentAnimationData() { qDeleteAll(pc); }

    QQuickStateActions actions;
    //### reverse should probably apply on a per-action basis
    bool reverse;
    QList<QQuickParentChange *> pc;
    void doAction() override
    {
        for (int ii = 0; ii < actions.count(); ++ii) {
            const QQuickStateAction &action = actions.at(ii);
            if (reverse)
                action.event->reverse();
            else
                action.event->execute();
        }
    }
};

QAbstractAnimationJob* QQuickParentAnimation::transition(QQuickStateActions &actions,
                        QQmlProperties &modified,
                        TransitionDirection direction,
                        QObject *defaultTarget)
{
    Q_D(QQuickParentAnimation);

    QQuickParentAnimationData *data = new QQuickParentAnimationData;
    QQuickParentAnimationData *viaData = new QQuickParentAnimationData;

    bool hasExplicit = false;
    if (d->target && d->newParent) {
        data->reverse = false;
        QQuickStateAction myAction;
        QQuickParentChange *pc = new QQuickParentChange;
        pc->setObject(d->target);
        pc->setParent(d->newParent);
        myAction.event = pc;
        data->pc << pc;
        data->actions << myAction;
        hasExplicit = true;
        if (d->via) {
            viaData->reverse = false;
            QQuickStateAction myVAction;
            QQuickParentChange *vpc = new QQuickParentChange;
            vpc->setObject(d->target);
            vpc->setParent(d->via);
            myVAction.event = vpc;
            viaData->pc << vpc;
            viaData->actions << myVAction;
        }
        //### once actions have concept of modified,
        //    loop to match appropriate ParentChanges and mark as modified
    }

    if (!hasExplicit)
    for (int i = 0; i < actions.size(); ++i) {
        QQuickStateAction &action = actions[i];
        if (action.event && action.event->type() == QQuickStateActionEvent::ParentChange
            && (!d->target || static_cast<QQuickParentChange*>(action.event)->object() == d->target)) {

            QQuickParentChange *pc = static_cast<QQuickParentChange*>(action.event);
            QQuickStateAction myAction = action;
            data->reverse = action.reverseEvent;

            //### this logic differs from PropertyAnimation
            //    (probably a result of modified vs. done)
            if (d->newParent) {
                QQuickParentChange *epc = new QQuickParentChange;
                epc->setObject(static_cast<QQuickParentChange*>(action.event)->object());
                epc->setParent(d->newParent);
                myAction.event = epc;
                data->pc << epc;
                data->actions << myAction;
                pc = epc;
            } else {
                action.actionDone = true;
                data->actions << myAction;
            }

            if (d->via) {
                viaData->reverse = false;
                QQuickStateAction myAction;
                QQuickParentChange *vpc = new QQuickParentChange;
                vpc->setObject(pc->object());
                vpc->setParent(d->via);
                myAction.event = vpc;
                viaData->pc << vpc;
                viaData->actions << myAction;
                QQuickStateAction dummyAction;
                QQuickStateAction &xAction = pc->xIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
                QQuickStateAction &yAction = pc->yIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
                QQuickStateAction &sAction = pc->scaleIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
                QQuickStateAction &rAction = pc->rotationIsSet() && i < actions.size()-1 ? actions[++i] : dummyAction;
                QQuickItem *target = pc->object();
                QQuickItem *targetParent = action.reverseEvent ? pc->originalParent() : pc->parent();

                //### this mirrors the logic in QQuickParentChange.
                bool ok;
                const QTransform &transform = targetParent->itemTransform(d->via, &ok);
                if (transform.type() >= QTransform::TxShear || !ok) {
                    qmlWarning(this) << QQuickParentAnimation::tr("Unable to preserve appearance under complex transform");
                    ok = false;
                }

                qreal scale = 1;
                qreal rotation = 0;
                bool isRotate = (transform.type() == QTransform::TxRotate) || (transform.m11() < 0);
                if (ok && !isRotate) {
                    if (transform.m11() == transform.m22())
                        scale = transform.m11();
                    else {
                        qmlWarning(this) << QQuickParentAnimation::tr("Unable to preserve appearance under non-uniform scale");
                        ok = false;
                    }
                } else if (ok && isRotate) {
                    if (transform.m11() == transform.m22())
                        scale = qSqrt(transform.m11()*transform.m11() + transform.m12()*transform.m12());
                    else {
                        qmlWarning(this) << QQuickParentAnimation::tr("Unable to preserve appearance under non-uniform scale");
                        ok = false;
                    }

                    if (scale != 0)
                        rotation = qRadiansToDegrees(qAtan2(transform.m12() / scale, transform.m11() / scale));
                    else {
                        qmlWarning(this) << QQuickParentAnimation::tr("Unable to preserve appearance under scale of 0");
                        ok = false;
                    }
                }

                const QPointF &point = transform.map(QPointF(xAction.toValue.toReal(),yAction.toValue.toReal()));
                qreal x = point.x();
                qreal y = point.y();
                if (ok && target->transformOrigin() != QQuickItem::TopLeft) {
                    qreal w = target->width();
                    qreal h = target->height();
                    if (pc->widthIsSet() && i < actions.size() - 1)
                        w = actions.at(++i).toValue.toReal();
                    if (pc->heightIsSet() && i < actions.size() - 1)
                        h = actions.at(++i).toValue.toReal();
                    const QPointF &transformOrigin
                            = d->computeTransformOrigin(target->transformOrigin(), w,h);
                    qreal tempxt = transformOrigin.x();
                    qreal tempyt = transformOrigin.y();
                    QTransform t;
                    t.translate(-tempxt, -tempyt);
                    t.rotate(rotation);
                    t.scale(scale, scale);
                    t.translate(tempxt, tempyt);
                    const QPointF &offset = t.map(QPointF(0,0));
                    x += offset.x();
                    y += offset.y();
                }

                if (ok) {
                    //qDebug() << x << y << rotation << scale;
                    xAction.toValue = x;
                    yAction.toValue = y;
                    sAction.toValue = sAction.toValue.toReal() * scale;
                    rAction.toValue = rAction.toValue.toReal() + rotation;
                }
            }
        }
    }

    if (data->actions.count()) {
        QSequentialAnimationGroupJob *topLevelGroup = new QSequentialAnimationGroupJob;
        QActionAnimation *viaAction = d->via ? new QActionAnimation : nullptr;
        QActionAnimation *targetAction = new QActionAnimation;
        //we'll assume the common case by far is to have children, and always create ag
        QParallelAnimationGroupJob *ag = new QParallelAnimationGroupJob;

        if (d->via)
            viaAction->setAnimAction(viaData);
        targetAction->setAnimAction(data);

        //take care of any child animations
        bool valid = d->defaultProperty.isValid();
        QAbstractAnimationJob* anim;
        for (int ii = 0; ii < d->animations.count(); ++ii) {
            if (valid)
                d->animations.at(ii)->setDefaultTarget(d->defaultProperty);
            anim = d->animations.at(ii)->transition(actions, modified, direction, defaultTarget);
            if (anim)
                ag->appendAnimation(anim);
        }

        //TODO: simplify/clarify logic
        bool forwards = direction == QQuickAbstractAnimation::Forward;
        if (forwards) {
            topLevelGroup->appendAnimation(d->via ? viaAction : targetAction);
            topLevelGroup->appendAnimation(ag);
            if (d->via)
                topLevelGroup->appendAnimation(targetAction);
        } else {
            if (d->via)
                topLevelGroup->appendAnimation(targetAction);
            topLevelGroup->appendAnimation(ag);
            topLevelGroup->appendAnimation(d->via ? viaAction : targetAction);
        }
        return initInstance(topLevelGroup);
    } else {
        delete data;
        delete viaData;
    }
    return nullptr;
}

/*!
    \qmltype AnchorAnimation
    \instantiates QQuickAnchorAnimation
    \inqmlmodule QtQuick
    \ingroup qtquick-animation-properties
    \inherits Animation
    \brief Animates changes in anchor values.

    AnchorAnimation is used to animate an anchor change.

    In the following snippet we animate the addition of a right anchor to a \l Rectangle:

    \snippet qml/anchoranimation.qml 0

    When an AnchorAnimation is used in a \l Transition, it will
    animate any AnchorChanges that have occurred during the state change.
    This can be overridden by setting a specific target item using the
    \l {AnchorChanges::target}{AnchorChanges.target} property.

    \note AnchorAnimation can only be used in a \l Transition and in
    conjunction with an AnchorChange. It cannot be used in behaviors and
    other types of animations.

    \sa {Animation and Transitions in Qt Quick}, AnchorChanges
*/
QQuickAnchorAnimation::QQuickAnchorAnimation(QObject *parent)
: QQuickAbstractAnimation(*(new QQuickAnchorAnimationPrivate), parent)
{
}

QQuickAnchorAnimation::~QQuickAnchorAnimation()
{
}

/*!
    \qmlproperty list<Item> QtQuick::AnchorAnimation::targets
    The items to reanchor.

    If no targets are specified all AnchorChanges will be
    animated by the AnchorAnimation.
*/
QQmlListProperty<QQuickItem> QQuickAnchorAnimation::targets()
{
    Q_D(QQuickAnchorAnimation);
    return QQmlListProperty<QQuickItem>(this, &(d->targets));
}

/*!
    \qmlproperty int QtQuick::AnchorAnimation::duration
    This property holds the duration of the animation, in milliseconds.

    The default value is 250.
*/
int QQuickAnchorAnimation::duration() const
{
    Q_D(const QQuickAnchorAnimation);
    return d->duration;
}

void QQuickAnchorAnimation::setDuration(int duration)
{
    if (duration < 0) {
        qmlWarning(this) << tr("Cannot set a duration of < 0");
        return;
    }

    Q_D(QQuickAnchorAnimation);
    if (d->duration == duration)
        return;
    d->duration = duration;
    emit durationChanged(duration);
}

/*!
    \qmlpropertygroup QtQuick::AnchorAnimation::easing
    \qmlproperty enumeration QtQuick::AnchorAnimation::easing.type
    \qmlproperty real QtQuick::AnchorAnimation::easing.amplitude
    \qmlproperty real QtQuick::AnchorAnimation::easing.overshoot
    \qmlproperty real QtQuick::AnchorAnimation::easing.period
    \brief Specifies the easing curve used for the animation

    To specify an easing curve you need to specify at least the type. For some curves you can also specify
    amplitude, period and/or overshoot. The default easing curve is
    Linear.

    \qml
    AnchorAnimation { easing.type: Easing.InOutQuad }
    \endqml

    See the \l{PropertyAnimation::easing.type} documentation for information
    about the different types of easing curves.
*/
QEasingCurve QQuickAnchorAnimation::easing() const
{
    Q_D(const QQuickAnchorAnimation);
    return d->easing;
}

void QQuickAnchorAnimation::setEasing(const QEasingCurve &e)
{
    Q_D(QQuickAnchorAnimation);
    if (d->easing == e)
        return;

    d->easing = e;
    emit easingChanged(e);
}

QAbstractAnimationJob* QQuickAnchorAnimation::transition(QQuickStateActions &actions,
                        QQmlProperties &modified,
                        TransitionDirection direction,
                        QObject *defaultTarget)
{
    Q_UNUSED(modified);
    Q_UNUSED(defaultTarget);
    Q_D(QQuickAnchorAnimation);
    QQuickAnimationPropertyUpdater *data = new QQuickAnimationPropertyUpdater;
    data->interpolatorType = QMetaType::QReal;
    data->interpolator = d->interpolator;
    data->reverse = direction == Backward ? true : false;
    data->fromSourced = false;
    data->fromDefined = false;

    for (int ii = 0; ii < actions.count(); ++ii) {
        QQuickStateAction &action = actions[ii];
        if (action.event && action.event->type() == QQuickStateActionEvent::AnchorChanges
            && (d->targets.isEmpty() || d->targets.contains(static_cast<QQuickAnchorChanges*>(action.event)->object()))) {
            data->actions << static_cast<QQuickAnchorChanges*>(action.event)->additionalActions();
        }
    }

    QQuickBulkValueAnimator *animator = new QQuickBulkValueAnimator;
    if (data->actions.count()) {
        animator->setAnimValue(data);
        animator->setFromSourcedValue(&data->fromSourced);
    } else {
        delete data;
    }

    animator->setDuration(d->duration);
    animator->setEasingCurve(d->easing);
    return initInstance(animator);
}


#if QT_CONFIG(quick_path)
/*!
    \qmltype PathAnimation
    \instantiates QQuickPathAnimation
    \inqmlmodule QtQuick
    \ingroup qtquick-animation-properties
    \inherits Animation
    \since 5.0
    \brief Animates an item along a path.

    When used in a transition, the path can be specified without start
    or end points, for example:
    \qml
    PathAnimation {
        path: Path {
            //no startX, startY
            PathCurve { x: 100; y: 100}
            PathCurve {}    //last element is empty with no end point specified
        }
    }
    \endqml

    In the above case, the path start will be the item's current position, and the
    path end will be the item's target position in the target state.

    \sa {Animation and Transitions in Qt Quick}, PathInterpolator
*/
QQuickPathAnimation::QQuickPathAnimation(QObject *parent)
: QQuickAbstractAnimation(*(new QQuickPathAnimationPrivate), parent)
{
}

QQuickPathAnimation::~QQuickPathAnimation()
{
    typedef QHash<QQuickItem*, QQuickPathAnimationAnimator* >::iterator ActiveAnimationsIt;

    Q_D(QQuickPathAnimation);
    for (ActiveAnimationsIt it = d->activeAnimations.begin(), end = d->activeAnimations.end(); it != end; ++it)
        it.value()->clearTemplate();
}

/*!
    \qmlproperty int QtQuick::PathAnimation::duration
    This property holds the duration of the animation, in milliseconds.

    The default value is 250.
*/
int QQuickPathAnimation::duration() const
{
    Q_D(const QQuickPathAnimation);
    return d->duration;
}

void QQuickPathAnimation::setDuration(int duration)
{
    if (duration < 0) {
        qmlWarning(this) << tr("Cannot set a duration of < 0");
        return;
    }

    Q_D(QQuickPathAnimation);
    if (d->duration == duration)
        return;
    d->duration = duration;
    emit durationChanged(duration);
}

/*!
    \qmlpropertygroup QtQuick::PathAnimation::easing
    \qmlproperty enumeration QtQuick::PathAnimation::easing.type
    \qmlproperty real QtQuick::PathAnimation::easing.amplitude
    \qmlproperty list<real> QtQuick::PathAnimation::easing.bezierCurve
    \qmlproperty real QtQuick::PathAnimation::easing.overshoot
    \qmlproperty real QtQuick::PathAnimation::easing.period
    \brief the easing curve used for the animation.

    To specify an easing curve you need to specify at least the type. For some curves you can also specify
    amplitude, period, overshoot or custom bezierCurve data. The default easing curve is \c Easing.Linear.

    See the \l{PropertyAnimation::easing.type} documentation for information
    about the different types of easing curves.
*/
QEasingCurve QQuickPathAnimation::easing() const
{
    Q_D(const QQuickPathAnimation);
    return d->easingCurve;
}

void QQuickPathAnimation::setEasing(const QEasingCurve &e)
{
    Q_D(QQuickPathAnimation);
    if (d->easingCurve == e)
        return;

    d->easingCurve = e;
    emit easingChanged(e);
}

/*!
    \qmlproperty Path QtQuick::PathAnimation::path
    This property holds the path to animate along.

    For more information on defining a path see the \l Path documentation.
*/
QQuickPath *QQuickPathAnimation::path() const
{
    Q_D(const QQuickPathAnimation);
    return d->path;
}

void QQuickPathAnimation::setPath(QQuickPath *path)
{
    Q_D(QQuickPathAnimation);
    if (d->path == path)
        return;

    d->path = path;
    emit pathChanged();
}

/*!
    \qmlproperty Item QtQuick::PathAnimation::target
    This property holds the item to animate.
*/
QQuickItem *QQuickPathAnimation::target() const
{
    Q_D(const QQuickPathAnimation);
    return d->target;
}

void QQuickPathAnimation::setTargetObject(QQuickItem *target)
{
    Q_D(QQuickPathAnimation);
    if (d->target == target)
        return;

    d->target = target;
    emit targetChanged();
}

/*!
    \qmlproperty enumeration QtQuick::PathAnimation::orientation
    This property controls the rotation of the item as it animates along the path.

    If a value other than \c Fixed is specified, the PathAnimation will rotate the
    item to achieve the specified orientation as it travels along the path.

    \list
    \li PathAnimation.Fixed (default) - the PathAnimation will not control
       the rotation of the item.
    \li PathAnimation.RightFirst - The right side of the item will lead along the path.
    \li PathAnimation.LeftFirst - The left side of the item will lead along the path.
    \li PathAnimation.BottomFirst - The bottom of the item will lead along the path.
    \li PathAnimation.TopFirst - The top of the item will lead along the path.
    \endlist
*/
QQuickPathAnimation::Orientation QQuickPathAnimation::orientation() const
{
    Q_D(const QQuickPathAnimation);
    return d->orientation;
}

void QQuickPathAnimation::setOrientation(Orientation orientation)
{
    Q_D(QQuickPathAnimation);
    if (d->orientation == orientation)
        return;

    d->orientation = orientation;
    emit orientationChanged(d->orientation);
}

/*!
    \qmlproperty point QtQuick::PathAnimation::anchorPoint
    This property holds the anchor point for the item being animated.

    By default, the upper-left corner of the target (its 0,0 point)
    will be anchored to (or follow) the path. The anchorPoint property can be used to
    specify a different point for anchoring. For example, specifying an anchorPoint of
    5,5 for a 10x10 item means the center of the item will follow the path.
*/
QPointF QQuickPathAnimation::anchorPoint() const
{
    Q_D(const QQuickPathAnimation);
    return d->anchorPoint;
}

void QQuickPathAnimation::setAnchorPoint(const QPointF &point)
{
    Q_D(QQuickPathAnimation);
    if (d->anchorPoint == point)
        return;

    d->anchorPoint = point;
    emit anchorPointChanged(point);
}

/*!
    \qmlproperty real QtQuick::PathAnimation::orientationEntryDuration
    This property holds the duration (in milliseconds) of the transition in to the orientation.

    If an orientation has been specified for the PathAnimation, and the starting
    rotation of the item does not match that given by the orientation,
    orientationEntryDuration can be used to smoothly transition from the item's
    starting rotation to the rotation given by the path orientation.
*/
int QQuickPathAnimation::orientationEntryDuration() const
{
    Q_D(const QQuickPathAnimation);
    return d->entryDuration;
}

void QQuickPathAnimation::setOrientationEntryDuration(int duration)
{
    Q_D(QQuickPathAnimation);
    if (d->entryDuration == duration)
        return;
    d->entryDuration = duration;
    emit orientationEntryDurationChanged(duration);
}

/*!
    \qmlproperty real QtQuick::PathAnimation::orientationExitDuration
    This property holds the duration (in milliseconds) of the transition out of the orientation.

    If an orientation and endRotation have been specified for the PathAnimation,
    orientationExitDuration can be used to smoothly transition from the rotation given
    by the path orientation to the specified endRotation.
*/
int QQuickPathAnimation::orientationExitDuration() const
{
    Q_D(const QQuickPathAnimation);
    return d->exitDuration;
}

void QQuickPathAnimation::setOrientationExitDuration(int duration)
{
    Q_D(QQuickPathAnimation);
    if (d->exitDuration == duration)
        return;
    d->exitDuration = duration;
    emit orientationExitDurationChanged(duration);
}

/*!
    \qmlproperty real QtQuick::PathAnimation::endRotation
    This property holds the ending rotation for the target.

    If an orientation has been specified for the PathAnimation,
    and the path doesn't end with the item at the desired rotation,
    the endRotation property can be used to manually specify an end
    rotation.

    This property is typically used with orientationExitDuration, as specifying
    an endRotation without an orientationExitDuration may cause a jump to
    the final rotation, rather than a smooth transition.
*/
qreal QQuickPathAnimation::endRotation() const
{
    Q_D(const QQuickPathAnimation);
    return d->endRotation.isNull ? qreal(0) : d->endRotation.value;
}

void QQuickPathAnimation::setEndRotation(qreal rotation)
{
    Q_D(QQuickPathAnimation);
    if (!d->endRotation.isNull && d->endRotation == rotation)
        return;

    d->endRotation = rotation;
    emit endRotationChanged(d->endRotation);
}

QAbstractAnimationJob* QQuickPathAnimation::transition(QQuickStateActions &actions,
                                           QQmlProperties &modified,
                                           TransitionDirection direction,
                                           QObject *defaultTarget)
{
    Q_D(QQuickPathAnimation);

    QQuickItem *target = d->target ? d->target : qobject_cast<QQuickItem*>(defaultTarget);

    QQuickPathAnimationUpdater prevData;
    bool havePrevData = false;
    if (d->activeAnimations.contains(target)) {
        havePrevData = true;
        prevData = *d->activeAnimations[target]->pathUpdater();
    }

    for (auto it = d->activeAnimations.begin(); it != d->activeAnimations.end();) {
        QQuickPathAnimationAnimator *anim = it.value();
        if (anim->state() == QAbstractAnimationJob::Stopped) {
            anim->clearTemplate();
            it = d->activeAnimations.erase(it);
        } else {
            ++it;
        }
    }

    QQuickPathAnimationUpdater *data = new QQuickPathAnimationUpdater();
    QQuickPathAnimationAnimator *pa = new QQuickPathAnimationAnimator(d);

    d->activeAnimations[target] = pa;

    data->orientation = d->orientation;
    data->anchorPoint = d->anchorPoint;
    data->entryInterval = d->duration ? qreal(d->entryDuration) / d->duration : qreal(0);
    data->exitInterval = d->duration ? qreal(d->exitDuration) / d->duration : qreal(0);
    data->endRotation = d->endRotation;
    data->reverse = direction == Backward ? true : false;
    data->fromSourced = false;
    data->fromDefined = (d->path && d->path->hasStartX() && d->path->hasStartY()) ? true : false;
    data->toDefined = d->path ? true : false;
    int origModifiedSize = modified.count();

    for (int i = 0; i < actions.count(); ++i) {
        QQuickStateAction &action = actions[i];
        if (action.event)
            continue;
        if (action.specifiedObject == target && action.property.name() == QLatin1String("x")) {
            data->toX = action.toValue.toReal();
            modified << action.property;
            action.fromValue = action.toValue;
        }
        if (action.specifiedObject == target && action.property.name() == QLatin1String("y")) {
            data->toY = action.toValue.toReal();
            modified << action.property;
            action.fromValue = action.toValue;
        }
    }

    if (target && d->path &&
        (modified.count() > origModifiedSize || data->toDefined)) {
        data->target = target;
        data->path = d->path;
        data->path->invalidateSequentialHistory();

        if (havePrevData) {
            // get the original start angle that was used (so we can exactly reverse).
            data->startRotation = prevData.startRotation;

            // treat interruptions specially, otherwise we end up with strange paths
            if ((data->reverse || prevData.reverse) && prevData.currentV > 0 && prevData.currentV < 1) {
                if (!data->fromDefined && !data->toDefined && !prevData.painterPath.isEmpty()) {
                    QPointF pathPos = QQuickPath::sequentialPointAt(prevData.painterPath, prevData.pathLength, prevData.attributePoints, prevData.prevBez, prevData.currentV);
                    if (!prevData.anchorPoint.isNull())
                        pathPos -= prevData.anchorPoint;
                    if (pathPos == data->target->position()) {   //only treat as interruption if we interrupted ourself
                        data->painterPath = prevData.painterPath;
                        data->toDefined = data->fromDefined = data->fromSourced = true;
                        data->prevBez.isValid = false;
                        data->interruptStart = prevData.currentV;
                        data->startRotation = prevData.startRotation;
                        data->pathLength = prevData.pathLength;
                        data->attributePoints = prevData.attributePoints;
                    }
                }
            }
        }
        pa->setFromSourcedValue(&data->fromSourced);
        pa->setAnimValue(data);
        pa->setDuration(d->duration);
        pa->setEasingCurve(d->easingCurve);
        return initInstance(pa);
    } else {
        pa->setFromSourcedValue(nullptr);
        pa->setAnimValue(nullptr);
        delete pa;
        delete data;
    }
    return nullptr;
}

void QQuickPathAnimationUpdater::setValue(qreal v)
{
    v = qMin(qMax(v, (qreal)0.0), (qreal)1.0);;

    if (interruptStart.isValid()) {
        if (reverse)
            v = 1 - v;
        qreal end = reverse ? 0.0 : 1.0;
        v = interruptStart + v * (end-interruptStart);
    }
    currentV = v;
    bool atStart = ((reverse && v == 1.0) || (!reverse && v == 0.0));
    if (!fromSourced && (!fromDefined || !toDefined)) {
        qreal startX = reverse ? toX + anchorPoint.x() : target->x() + anchorPoint.x();
        qreal startY = reverse ? toY + anchorPoint.y() : target->y() + anchorPoint.y();
        qreal endX = reverse ? target->x() + anchorPoint.x() : toX + anchorPoint.x();
        qreal endY = reverse ? target->y() + anchorPoint.y() : toY + anchorPoint.y();

        prevBez.isValid = false;
        painterPath = path->createPath(QPointF(startX, startY), QPointF(endX, endY), QStringList(), pathLength, attributePoints);
        fromSourced = true;
    }

    qreal angle;
    bool fixed = orientation == QQuickPathAnimation::Fixed;
    QPointF currentPos = !painterPath.isEmpty() ? path->sequentialPointAt(painterPath, pathLength, attributePoints, prevBez, v, fixed ? nullptr : &angle) : path->sequentialPointAt(v, fixed ? nullptr : &angle);

    //adjust position according to anchor point
    if (!anchorPoint.isNull()) {
        currentPos -= anchorPoint;
        if (atStart) {
            if (!anchorPoint.isNull() && !fixed)
                target->setTransformOriginPoint(anchorPoint);
        }
    }

    target->setPosition(currentPos);

    //adjust angle according to orientation
    if (!fixed) {
        switch (orientation) {
            case QQuickPathAnimation::RightFirst:
                angle = -angle;
                break;
            case QQuickPathAnimation::TopFirst:
                angle = -angle + 90;
                break;
            case QQuickPathAnimation::LeftFirst:
                angle = -angle + 180;
                break;
            case QQuickPathAnimation::BottomFirst:
                angle = -angle + 270;
                break;
            default:
                angle = 0;
                break;
        }

        if (atStart && !reverse) {
            startRotation = target->rotation();

            //shortest distance to correct orientation
            qreal diff = angle - startRotation;
            while (diff > 180.0) {
                startRotation.value += 360.0;
                diff -= 360.0;
            }
            while (diff < -180.0) {
                startRotation.value -= 360.0;
                diff += 360.0;
            }
        }

        //smoothly transition to the desired orientation
        //TODO: shortest distance calculations
        if (startRotation.isValid()) {
            if (reverse && v == 0.0)
                angle = startRotation;
            else if (v < entryInterval)
                angle = angle * v / entryInterval + startRotation * (entryInterval - v) / entryInterval;
        }
        if (endRotation.isValid()) {
            qreal exitStart = 1 - entryInterval;
            if (!reverse && v == 1.0)
                angle = endRotation;
            else if (v > exitStart)
                angle = endRotation * (v - exitStart) / exitInterval + angle * (exitInterval - (v - exitStart)) / exitInterval;
        }
        target->setRotation(angle);
    }

    /*
        NOTE: we don't always reset the transform origin, as it can cause a
        visual jump if ending on an angle. This means that in some cases
        (anchor point and orientation both specified, and ending at an angle)
        the transform origin will always be set after running the path animation.
     */
    if ((reverse && v == 0.0) || (!reverse && v == 1.0)) {
        if (!anchorPoint.isNull() && !fixed && qFuzzyIsNull(angle))
            target->setTransformOriginPoint(QPointF());
    }
}

QQuickPathAnimationAnimator::QQuickPathAnimationAnimator(QQuickPathAnimationPrivate *priv)
    : animationTemplate(priv)
{
}

QQuickPathAnimationAnimator::~QQuickPathAnimationAnimator()
{
    if (animationTemplate && pathUpdater()) {
        QHash<QQuickItem*, QQuickPathAnimationAnimator* >::iterator it =
                animationTemplate->activeAnimations.find(pathUpdater()->target);
        if (it != animationTemplate->activeAnimations.end() && it.value() == this)
            animationTemplate->activeAnimations.erase(it);
    }
}

#endif // quick_path

QT_END_NAMESPACE

#include "moc_qquickitemanimation_p.cpp"