summaryrefslogtreecommitdiffstats
path: root/src/declarative/util/qmlanimation.cpp
blob: f2a91eb88329b47762e45381b21f317656fd736c (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
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmlanimation.h"
#include "qvariant.h"
#include "qcolor.h"
#include "qfile.h"
#include "qmlpropertyvaluesource.h"
#include "qml.h"
#include "qmlanimation_p.h"
#include "qmlbehaviour.h"
#include <QParallelAnimationGroup>
#include <QSequentialAnimationGroup>
#include <QtCore/qset.h>
#include <QtCore/qrect.h>
#include <QtCore/qpoint.h>
#include <QtCore/qsize.h>
#include <QtDeclarative/qmlexpression.h>
#include <private/qmlstringconverters_p.h>
#include <private/qvariantanimation_p.h>

/* TODO:
    Check for any memory leaks
    easing should be a QEasingCurve-type property
    All other XXXs
*/

QT_BEGIN_NAMESPACE

QEasingCurve stringToCurve(const QString &curve)
{
    QEasingCurve easingCurve;

    QString normalizedCurve = curve;
    bool hasParams = curve.contains(QLatin1Char('('));
    QStringList props;

    if (hasParams) {
        QString easeName = curve.trimmed();
        if (!easeName.endsWith(QLatin1Char(')'))) {
            qWarning("QEasingCurve: Unmatched perenthesis in easing function '%s'",
                     curve.toLatin1().constData());
            return easingCurve;
        }

        int idx = easeName.indexOf(QLatin1Char('('));
        QString prop_str =
            easeName.mid(idx + 1, easeName.length() - 1 - idx - 1);
        normalizedCurve = easeName.left(idx);

        props = prop_str.split(QLatin1Char(','));
    }

    normalizedCurve = normalizedCurve.mid(4);
    //XXX optimize?
    int index = QEasingCurve::staticMetaObject.indexOfEnumerator("Type");
    QMetaEnum me = QEasingCurve::staticMetaObject.enumerator(index);

    int value = me.keyToValue(normalizedCurve.toLatin1().constData());
    if (value < 0) {
        //XXX print line number
        qWarning("QEasingCurve: Unknown easing curve '%s'",
                 curve.toLatin1().constData());
        value = 0;
    }
    easingCurve.setType((QEasingCurve::Type)value);

    if (hasParams) {
        foreach(const QString &str, props) {
            int sep = str.indexOf(QLatin1Char(':'));

            if (sep == -1) {
                qWarning("QEasingCurve: Improperly specified property in easing function '%s'",
                        curve.toLatin1().constData());
                return easingCurve;
            }

            QString propName = str.left(sep).trimmed();
            bool isOk;
            qreal propValue = str.mid(sep + 1).trimmed().toDouble(&isOk);

            if (propName.isEmpty() || !isOk) {
                qWarning("QEasingCurve: Improperly specified property in easing function '%s'",
                        curve.toLatin1().constData());
                return easingCurve;
            }

            //XXX optimize
            if (propName == QLatin1String("amplitude")) {
                easingCurve.setAmplitude(propValue);
            } else if (propName == QLatin1String("period")) {
                easingCurve.setPeriod(propValue);
            } else if (propName == QLatin1String("overshoot")) {
                easingCurve.setOvershoot(propValue);
            }
        }
    }

    return easingCurve;
}

QML_DEFINE_NOCREATE_TYPE(QmlAbstractAnimation)

/*!
    \qmlclass Animation
    \brief The Animation element is the base of all QML animations.

    The Animation element cannot be used directly in a QML file.  It exists
    to provide a set of common properties and methods, available across all the
    other animation types that inherit from it.  Attempting to use the Animation
    element directly will result in an error.
*/

/*!
    \class QmlAbstractAnimation
    \internal
*/

QmlAbstractAnimation::QmlAbstractAnimation(QObject *parent)
: QmlPropertyValueSource(*(new QmlAbstractAnimationPrivate), parent)
{
}

QmlAbstractAnimation::~QmlAbstractAnimation()
{
}

QmlAbstractAnimation::QmlAbstractAnimation(QmlAbstractAnimationPrivate &dd, QObject *parent)
: QmlPropertyValueSource(dd, parent)
{
}

/*!
    \qmlproperty bool Animation::running
    This property holds whether the animation is currently running.

    The \c running property can be set to declaratively control whether or not
    an animation is running.  The following example will animate a rectangle
    whenever the \l MouseRegion is pressed.

    \code
    Rect {
        width: 100; height: 100
        x: NumericAnimation {
            running: MyMouse.pressed
            from: 0; to: 100
        }
        MouseRegion { id: MyMouse }
    }
    \endcode

    Likewise, the \c running property can be read to determine if the animation
    is running.  In the following example the text element will indicate whether
    or not the animation is running.

    \code
    NumericAnimation { id: MyAnimation }
    Text { text: MyAnimation.running ? "Animation is running" : "Animation is not running" }
    \endcode

    Animations can also be started and stopped imperatively from JavaScript
    using the \c start() and \c stop() methods.

    By default, animations are not running.
*/
bool QmlAbstractAnimation::isRunning() const
{
    Q_D(const QmlAbstractAnimation);
    return d->running;
}

void QmlAbstractAnimationPrivate::commence()
{
    Q_Q(QmlAbstractAnimation);

    q->prepare(userProperty.value);
    q->qtAnimation()->start();
    if (q->qtAnimation()->state() != QAbstractAnimation::Running) {
        running = false;
        emit q->completed();
    }
}

void QmlAbstractAnimation::setRunning(bool r)
{
    Q_D(QmlAbstractAnimation);
    if (d->running == r)
        return;

    if (d->group) {
        qWarning("QmlAbstractAnimation: setRunning() cannot be used on non-root animation nodes");
        return;
    }

    d->running = r;
    if (d->running) {
        if (!d->connectedTimeLine) {
            QObject::connect(qtAnimation(), SIGNAL(finished()),
                             this, SLOT(timelineComplete()));
            d->connectedTimeLine = true;
        }
        if (d->componentComplete)
            d->commence();
        else
            d->startOnCompletion = true;
        emit started();
    } else {
        if (d->finishPlaying) {
            if (d->repeat)
                qtAnimation()->setLoopCount(qtAnimation()->currentLoop()+1);
        } else
            qtAnimation()->stop();

        emit completed();
    }

    emit runningChanged(d->running);
}

/*!
    \qmlproperty bool Animation::paused
    This property holds whether the animation is currently paused.

    The \c paused property can be set to declaratively control whether or not
    an animation is paused.

    Animations can also be paused and resumed imperatively from JavaScript
    using the \c pause() and \c resume() methods.

    By default, animations are not paused.
*/
bool QmlAbstractAnimation::isPaused() const
{
    Q_D(const QmlAbstractAnimation);
    return d->paused;
}

void QmlAbstractAnimation::setPaused(bool p)
{
    Q_D(QmlAbstractAnimation);
    if (d->paused == p)
        return;

    if (d->group) {
        qWarning("QmlAbstractAnimation: setPaused() cannot be used on non-root animation nodes");
        return;
    }

    d->paused = p;
    if (d->paused)
        qtAnimation()->pause();
    else
        qtAnimation()->resume();

    emit pausedChanged(d->running);
}

void QmlAbstractAnimation::classBegin()
{
    Q_D(QmlAbstractAnimation);
    d->componentComplete = false;
}

void QmlAbstractAnimation::componentComplete()
{
    Q_D(QmlAbstractAnimation);
    if (d->startOnCompletion)
        d->commence();
    d->componentComplete = true;
}

/*!
    \qmlproperty bool Animation::finishPlaying
    This property holds whether the animation should finish playing when it is stopped.

    If this true the animation will complete its current iteration when it
    is stopped - either by setting the \c running property to false, or by
    calling the \c stop() method.  The \c complete() method is not effected
    by this value.

    This behaviour is most useful when the \c repeat property is set, as the
    animation will finish playing normally but not restart.

    By default, the finishPlaying property is not set.
*/
bool QmlAbstractAnimation::finishPlaying() const
{
    Q_D(const QmlAbstractAnimation);
    return d->finishPlaying;
}

void QmlAbstractAnimation::setFinishPlaying(bool f)
{
    Q_D(QmlAbstractAnimation);
    if (d->finishPlaying == f)
        return;

    d->finishPlaying = f;
    emit finishPlayingChanged(f);
}

/*!
    \qmlproperty bool Animation::repeat
    This property holds whether the animation should repeat.

    If set, the animation will continuously repeat until it is explicitly
    stopped - either by setting the \c running property to false, or by calling
    the \c stop() method.

    In the following example, the rectangle will spin indefinately.

    \code
    Rect {
        rotation: NumericAnimation { running: true; repeat: true; from: 0 to: 360 }
    }
    \endcode
*/
bool QmlAbstractAnimation::repeat() const
{
    Q_D(const QmlAbstractAnimation);
    return d->repeat;
}

void QmlAbstractAnimation::setRepeat(bool r)
{
    Q_D(QmlAbstractAnimation);
    if (r == d->repeat)
        return;

    d->repeat = r;
    int lc = r ? -1 : 1;
    qtAnimation()->setLoopCount(lc);
    emit repeatChanged(r);
}

int QmlAbstractAnimation::currentTime()
{
    return qtAnimation()->currentTime();
}

void QmlAbstractAnimation::setCurrentTime(int time)
{
    qtAnimation()->setCurrentTime(time);
}

QmlAnimationGroup *QmlAbstractAnimation::group() const
{
    Q_D(const QmlAbstractAnimation);
    return d->group;
}

void QmlAbstractAnimation::setGroup(QmlAnimationGroup *g)
{
    Q_D(QmlAbstractAnimation);
    if (d->group == g)
        return;
    if (d->group)
        static_cast<QmlAnimationGroupPrivate *>(d->group->d_ptr)->animations.removeAll(this);

    d->group = g;

    if (d->group && !static_cast<QmlAnimationGroupPrivate *>(d->group->d_ptr)->animations.contains(this))
        static_cast<QmlAnimationGroupPrivate *>(d->group->d_ptr)->animations.append(this);

    if (d->group)
        ((QAnimationGroup*)d->group->qtAnimation())->addAnimation(qtAnimation());

    //if (g) //if removed from a group, then the group should no longer be the parent
        setParent(g);
}

/*!
    \qmlproperty Object Animation::target
    This property holds an explicit target object to animate.

    The exact effect of the \c target property depends on how the animation
    is being used.  Refer to the \l animation documentation for details.
*/
QObject *QmlAbstractAnimation::target() const
{
    Q_D(const QmlAbstractAnimation);
    return d->target;
}

void QmlAbstractAnimation::setTarget(QObject *o)
{
    Q_D(QmlAbstractAnimation);
    if (d->target == o)
        return;

    d->target = o;
    if (d->target && !d->propertyName.isEmpty()) {
        d->userProperty = QmlMetaProperty(d->target, d->propertyName);
    } else {
        d->userProperty.invalidate();
    }

    emit targetChanged(d->target, d->propertyName);
}

/*!
    \qmlproperty string Animation::property
    This property holds an explicit property to animated.

    The exact effect of the \c property property depends on how the animation
    is being used.  Refer to the \l animation documentation for details.
*/
QString QmlAbstractAnimation::property() const
{
    Q_D(const QmlAbstractAnimation);
    return d->propertyName;
}

void QmlAbstractAnimation::setProperty(const QString &n)
{
    Q_D(QmlAbstractAnimation);
    if (d->propertyName == n)
        return;

    d->propertyName = n;
    if (d->target && !d->propertyName.isEmpty()) {
        d->userProperty = QmlMetaProperty(d->target, d->propertyName);
    } else {
        d->userProperty.invalidate();
    }

    emit targetChanged(d->target, d->propertyName);
}

/*!
    \qmlmethod Animation::start()
    \brief Starts the animation.

    If the animation is already running, calling this method has no effect.  The
    \c running property will be true following a call to \c start().
*/
void QmlAbstractAnimation::start()
{
    setRunning(true);
}

/*!
    \qmlmethod Animation::pause()
    \brief Pauses the animation.

    If the animation is already paused, calling this method has no effect.  The
    \c paused property will be true following a call to \c pause().
*/
void QmlAbstractAnimation::pause()
{
    setPaused(true);
}

/*!
    \qmlmethod Animation::resume()
    \brief Resumes a paused animation.

    If the animation is not paused, calling this method has no effect.  The
    \c paused property will be false following a call to \c resume().
*/
void QmlAbstractAnimation::resume()
{
    setPaused(false);
}

/*!
    \qmlmethod Animation::stop()
    \brief Stops the animation.

    If the animation is not running, calling this method has no effect.  The
    \c running property will be false following a call to \c stop().

    Normally \c stop() stops the animation immediately, and the animation has
    no further influence on property values.  In this example animation
    \code
    Rect {
        x: NumericAnimation { from: 0; to: 100; duration: 500 }
    }
    \endcode
    was stopped at time 250ms, the \c x property will have a value of 50.

    However, if the \c finishPlaying property is set, the animation will
    continue running until it completes and then stop.  The \c running property
    will still become false immediately.
*/
void QmlAbstractAnimation::stop()
{
    setRunning(false);
}

/*!
    \qmlmethod Animation::restart()
    \brief Restarts the animation.

    This is a convenience method, and is equivalent to calling \c stop() and
    then \c start().
*/
void QmlAbstractAnimation::restart()
{
    stop();
    start();
}

/*!
    \qmlmethod Animation::complete()
    \brief Stops the animation, jumping to the final property values.

    If the animation is not running, calling this method has no effect.  The
    \c running property will be false following a call to \c complete().

    Unlike \c stop(), \c complete() immediately fast-forwards the animation to
    its end.  In the following example,
    \code
    Rect {
        x: NumericAnimation { from: 0; to: 100; duration: 500 }
    }
    \endcode
    calling \c stop() at time 250ms will result in the \c x property having
    a value of 50, while calling \c complete() will set the \c x property to
    100, exactly as though the animation had played the whole way through.
*/
void QmlAbstractAnimation::complete()
{
    if (isRunning()) {
         qtAnimation()->setCurrentTime(qtAnimation()->duration());
    }
}

void QmlAbstractAnimation::setTarget(const QmlMetaProperty &p)
{
    Q_D(QmlAbstractAnimation);
    if (d->userProperty.isNull)
        d->userProperty = p;
}

//prepare is called before an animation begins
//(when an animation is used as a simple animation, and not as part of a transition)
void QmlAbstractAnimation::prepare(QmlMetaProperty &)
{
}

void QmlAbstractAnimation::transition(QmlStateActions &actions,
                                      QmlMetaProperties &modified,
                                      TransitionDirection direction)
{
    Q_UNUSED(actions);
    Q_UNUSED(modified);
    Q_UNUSED(direction);
}

void QmlAbstractAnimation::timelineComplete()
{
    Q_D(QmlAbstractAnimation);
    setRunning(false);
    if (d->finishPlaying && d->repeat) {
        qtAnimation()->setLoopCount(-1);
    }
}

/*!
    \qmlclass PauseAnimation QmlPauseAnimation
    \inherits Animation
    \brief The PauseAnimation provides a pause for an animation.

    When used in a SequentialAnimation, PauseAnimation is a step when
    nothing happens, for a specified duration.

    A 500ms animation sequence, with a 100ms pause between two animations:
    \code
    SequentialAnimation {
        NumericAnimation { ... duration: 200 }
        PauseAnimation { duration: 100 }
        NumericAnimation { ... duration: 200 }
    }
    \endcode
*/
/*!
    \internal
    \class QmlPauseAnimation
    \ingroup group_animation
    \ingroup group_states
    \brief The QmlPauseAnimation class provides a pause for an animation.

    When used in a QmlSequentialAnimation, QmlPauseAnimation is a step when
    nothing happens, for a specified duration.

    A QmlPauseAnimation object can be instantiated in Qml using the tag
    \l{xmlPauseAnimation} {&lt;PauseAnimation&gt;}.
*/

QML_DEFINE_TYPE(QmlPauseAnimation,PauseAnimation)
QmlPauseAnimation::QmlPauseAnimation(QObject *parent)
: QmlAbstractAnimation(*(new QmlPauseAnimationPrivate), parent)
{
    Q_D(QmlPauseAnimation);
    d->init();
}

QmlPauseAnimation::~QmlPauseAnimation()
{
}

void QmlPauseAnimationPrivate::init()
{
    Q_Q(QmlPauseAnimation);
    pa = new QPauseAnimation(q);
}

/*!
    \qmlproperty int PauseAnimation::duration
    This property holds the duration of the pause in milliseconds

    The default value is 250.
*/
/*!
    \property QmlPauseAnimation::duration
    \brief the duration of the pause in milliseconds

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

void QmlPauseAnimation::setDuration(int duration)
{
    if (duration < 0) {
        qWarning("QmlPauseAnimation: Cannot set a duration of < 0");
        return;
    }

    Q_D(QmlPauseAnimation);
    if (d->pa->duration() == duration)
        return;
    d->pa->setDuration(duration);
    emit durationChanged(duration);
}

void QmlPauseAnimation::prepare(QmlMetaProperty &p)
{
    Q_D(QmlPauseAnimation);
    if (d->userProperty.isNull)
        d->property = p;
    else
        d->property = d->userProperty;
}

QAbstractAnimation *QmlPauseAnimation::qtAnimation()
{
    Q_D(QmlPauseAnimation);
    return d->pa;
}

/*!
    \qmlclass ColorAnimation QmlColorAnimation
    \inherits Animation
    \brief The ColorAnimation allows you to animate color changes.

    \code
    ColorAnimation { from: "white"; to: "#c0c0c0"; duration: 100 }
    \endcode

    The default property animated is \c color, but like other animations,
    this can be changed by setting \c property. The \c color property will
    still animate. XXX is this a bug?
*/
/*!
    \internal
    \class QmlColorAnimation
    \ingroup group_animation
    \ingroup group_states
    \brief The QmlColorAnimation class allows you to animate color changes.

    A QmlColorAnimation object can be instantiated in Qml using the tag
    \l{xmlColorAnimation} {&lt;ColorAnimation&gt;}.
*/

QmlColorAnimation::QmlColorAnimation(QObject *parent)
: QmlVariantAnimation(parent)
{
    Q_D(QmlVariantAnimation);
    d->init();
}

QmlColorAnimation::~QmlColorAnimation()
{
}

/*!
    \qmlproperty color ColorAnimation::from
    This property holds the starting color.
*/
/*!
    \property QmlColorAnimation::from
    \brief the starting color.
*/
QColor QmlColorAnimation::from() const
{
    Q_D(const QmlVariantAnimation);
    return d->from.value<QColor>();
}

void QmlColorAnimation::setFrom(const QColor &f)
{
    QmlVariantAnimation::setFrom(f);
}

/*!
    \qmlproperty color ColorAnimation::from
    This property holds the ending color.
*/
/*!
    \property QmlColorAnimation::to
    \brief the ending color.
*/
QColor QmlColorAnimation::to() const
{
    Q_D(const QmlVariantAnimation);
    return d->to.value<QColor>();
}

void QmlColorAnimation::setTo(const QColor &t)
{
    QmlVariantAnimation::setTo(t);
}

QML_DEFINE_TYPE(QmlColorAnimation,ColorAnimation)

/*!
    \qmlclass RunScriptAction QmlRunScriptAction
    \inherits Animation
    \brief The RunScripAction allows scripts to be run during transitions.

*/
/*!
    \internal
    \class QmlRunScriptAction
    \brief The QmlRunScriptAction class allows scripts to be run during transitions

    \sa xmlRunScriptAction
*/
QmlRunScriptAction::QmlRunScriptAction(QObject *parent)
    :QmlAbstractAnimation(*(new QmlRunScriptActionPrivate), parent)
{
    Q_D(QmlRunScriptAction);
    d->init();
}

QmlRunScriptAction::~QmlRunScriptAction()
{
}

void QmlRunScriptActionPrivate::init()
{
    Q_Q(QmlRunScriptAction);
    rsa = new QActionAnimation(&proxy, q);
}

/*!
    \qmlproperty QString RunScript::script
    This property holds the script to run.
*/
QString QmlRunScriptAction::script() const
{
    Q_D(const QmlRunScriptAction);
    return d->script;
}

void QmlRunScriptAction::setScript(const QString &script)
{
    Q_D(QmlRunScriptAction);
    if (script == d->script)
        return;
    d->script = script;
    emit scriptChanged(script);
}

/*!
    \qmlproperty QString RunScript::script
    This property holds the file containing the script to run.
*/
QString QmlRunScriptAction::file() const
{
    Q_D(const QmlRunScriptAction);
    return d->file;
}

void QmlRunScriptAction::setFile(const QString &file)
{
    Q_D(QmlRunScriptAction);
    if (file == d->file)
        return;
    d->file = file;
    emit fileChanged(file);
}

void QmlRunScriptActionPrivate::execute()
{
    Q_Q(QmlRunScriptAction);
    QString scriptStr = script;
    if (!file.isEmpty()){
        QFile scriptFile(file);
        if (scriptFile.open(QIODevice::ReadOnly | QIODevice::Text)){
            scriptStr = QString::fromUtf8(scriptFile.readAll());
        }
    }

    if (!scriptStr.isEmpty()) {
        QmlExpression expr(qmlContext(q), scriptStr, q);
        expr.setTrackChange(false);
        expr.value();
    }
}

QAbstractAnimation *QmlRunScriptAction::qtAnimation()
{
    Q_D(QmlRunScriptAction);
    return d->rsa;
}

QML_DEFINE_TYPE(QmlRunScriptAction, RunScriptAction)

/*!
    \qmlclass SetPropertyAction QmlSetPropertyAction
    \inherits Animation
    \brief The SetPropertyAction allows property changes during transitions.

    Explicitly set \c theimage.smooth=true during a transition:
    \code
    SetPropertyAction { target: theimage; property: "smooth"; value: true }
    \endcode

    Set \c thewebview.url to the value set for the destination state:
    \code
    SetPropertyAction { target: thewebview; property: "url" }
    \endcode

    The SetPropertyAction is immediate -
    the target property is not animated to the selected value in any way.
*/
/*!
    \internal
    \class QmlSetPropertyAction
    \brief The QmlSetPropertyAction class allows property changes during transitions.

    A QmlSetPropertyAction object can be instantiated in Qml using the tag
    \l{xmlSetPropertyAction} {&lt;SetPropertyAction&gt;}.
*/
QmlSetPropertyAction::QmlSetPropertyAction(QObject *parent)
: QmlAbstractAnimation(*(new QmlSetPropertyActionPrivate), parent)
{
    Q_D(QmlSetPropertyAction);
    d->init();
}

QmlSetPropertyAction::~QmlSetPropertyAction()
{
}

void QmlSetPropertyActionPrivate::init()
{
    Q_Q(QmlSetPropertyAction);
    spa = new QActionAnimation(q);
}

/*!
    \qmlproperty string SetPropertyAction::properties
    This property holds the properties to be immediately set, comma-separated.
*/
QString QmlSetPropertyAction::properties() const
{
    Q_D(const QmlSetPropertyAction);
    return d->properties;
}

void QmlSetPropertyAction::setProperties(const QString &p)
{
    Q_D(QmlSetPropertyAction);
    if (d->properties == p)
        return;
    d->properties = p;
    emit propertiesChanged(p);
}

/*!
    \qmlproperty list<Item> SetPropertyAction::filter
    This property holds the items selected to be affected by this animation (all if not set).
    \sa exclude
*/
QList<QObject *> *QmlSetPropertyAction::filter()
{
    Q_D(QmlSetPropertyAction);
    return &d->filter;
}

/*!
    \qmlproperty list<Item> SetPropertyAction::exclude
    This property holds the items not to be affected by this animation.
    \sa filter
*/
QList<QObject *> *QmlSetPropertyAction::exclude()
{
    Q_D(QmlSetPropertyAction);
    return &d->exclude;
}

/*!
    \qmlproperty any SetPropertyAction::value
    This property holds the value to be set on the property.
    If not set, then the value defined for the end state of the transition.
*/
QVariant QmlSetPropertyAction::value() const
{
    Q_D(const QmlSetPropertyAction);
    return d->value;
}

void QmlSetPropertyAction::setValue(const QVariant &v)
{
    Q_D(QmlSetPropertyAction);
    if (d->value.isNull || d->value != v) {
        d->value = v;
        emit valueChanged(v);
    }
}

void QmlSetPropertyActionPrivate::doAction()
{
    property.write(value);
}

QAbstractAnimation *QmlSetPropertyAction::qtAnimation()
{
    Q_D(QmlSetPropertyAction);
    return d->spa;
}

void QmlSetPropertyAction::prepare(QmlMetaProperty &p)
{
    Q_D(QmlSetPropertyAction);

    if (d->userProperty.isNull)
        d->property = p;
    else
        d->property = d->userProperty;

    d->spa->setAnimAction(&d->proxy, QAbstractAnimation::KeepWhenStopped);
}

void QmlSetPropertyAction::transition(QmlStateActions &actions,
                                      QmlMetaProperties &modified,
                                      TransitionDirection direction)
{
    Q_D(QmlSetPropertyAction);
    Q_UNUSED(direction);

    struct QmlSetPropertyAnimationAction : public QAbstractAnimationAction
    {
        QmlStateActions actions;
        virtual void doAction()
        {
            for (int ii = 0; ii < actions.count(); ++ii) {
                const Action &action = actions.at(ii);
                QmlBehaviour::_ignore = true;
                action.property.write(action.toValue);
                QmlBehaviour::_ignore = false;
            }
        }
    };

    QStringList props = d->properties.isEmpty() ? QStringList() : d->properties.split(QLatin1Char(','));
    for (int ii = 0; ii < props.count(); ++ii)
        props[ii] = props.at(ii).trimmed();
    if (!d->propertyName.isEmpty() && !props.contains(d->propertyName))
        props.append(d->propertyName);

    if (d->userProperty.isValid() && props.isEmpty() && !target()) {
        props.append(d->userProperty.value.name());
        d->target = d->userProperty.value.object();
   }

    QmlSetPropertyAnimationAction *data = new QmlSetPropertyAnimationAction;

    QSet<QObject *> objs;
    for (int ii = 0; ii < actions.count(); ++ii) {
        Action &action = actions[ii];

        QObject *obj = action.property.object();
        QString propertyName = action.property.name();

        if ((d->filter.isEmpty() || d->filter.contains(obj)) &&
           (!d->exclude.contains(obj)) && props.contains(propertyName) &&
           (!target() || target() == obj)) {
            objs.insert(obj);
            Action myAction = action;

            if (d->value.isValid())
                myAction.toValue = d->value;

            modified << action.property;
            data->actions << myAction;
            action.fromValue = myAction.toValue;
        }
    }

    if (d->value.isValid() && target() && !objs.contains(target())) {
        QObject *obj = target();
        for (int jj = 0; jj < props.count(); ++jj) {
            Action myAction;
            myAction.property = QmlMetaProperty(obj, props.at(jj));
            myAction.toValue = d->value;
            data->actions << myAction;
        }
    }

    if (data->actions.count()) {
        d->spa->setAnimAction(data, QAbstractAnimation::DeleteWhenStopped);
    } else {
        delete data;
    }
}

QML_DEFINE_TYPE(QmlSetPropertyAction,SetPropertyAction)

/*!
    \qmlclass ParentChangeAction QmlParentChangeAction
    \inherits Animation
    \brief The ParentChangeAction allows parent changes during transitions.

    The ParentChangeAction is immediate - it is not animated in any way.
*/

QmlParentChangeAction::QmlParentChangeAction(QObject *parent)
: QmlAbstractAnimation(*(new QmlParentChangeActionPrivate), parent)
{
    Q_D(QmlParentChangeAction);
    d->init();
}

QmlParentChangeAction::~QmlParentChangeAction()
{
}

void QmlParentChangeActionPrivate::init()
{
    Q_Q(QmlParentChangeAction);
    cpa = new QActionAnimation(q);
}

void QmlParentChangeActionPrivate::doAction()
{
    //XXX property.write(value);
}

void QmlParentChangeAction::prepare(QmlMetaProperty &p)
{
    Q_D(QmlParentChangeAction);

    if (d->userProperty.isNull)
        d->property = p;
    else
        d->property = d->userProperty;

    //XXX
}

QAbstractAnimation *QmlParentChangeAction::qtAnimation()
{
    Q_D(QmlParentChangeAction);
    return d->cpa;
}

void QmlParentChangeAction::transition(QmlStateActions &actions,
                                       QmlMetaProperties &modified,
                                       TransitionDirection direction)
{
    Q_D(QmlParentChangeAction);
    Q_UNUSED(direction);

    struct QmlParentChangeActionData : public QAbstractAnimationAction
    {
        QmlStateActions actions;
        virtual void doAction()
        {
            for (int ii = 0; ii < actions.count(); ++ii) {
                const Action &action = actions.at(ii);
                QmlBehaviour::_ignore = true;
                action.property.write(action.toValue);
                QmlBehaviour::_ignore = false;
            }
        }
    };

    QmlParentChangeActionData *data = new QmlParentChangeActionData;

    QSet<QObject *> objs;
    for (int ii = 0; ii < actions.count(); ++ii) {
        Action &action = actions[ii];

        QObject *obj = action.property.object();
        QString propertyName = action.property.name();

        if ((!target() || target() == obj) && propertyName == QString(QLatin1String("moveToParent"))) {
            objs.insert(obj);
            Action myAction = action;

            /*if (d->value.isValid())
                myAction.toValue = d->value;*/

            modified << action.property;
            data->actions << myAction;
            action.fromValue = myAction.toValue;
        }
    }

    /*if (d->value.isValid() && target() && !objs.contains(target())) {
        QObject *obj = target();
        for (int jj = 0; jj < props.count(); ++jj) {
            Action myAction;
            myAction.property = QmlMetaProperty(obj, props.at(jj));
            myAction.toValue = d->value;
            data->actions << myAction;
        }
    }*/

    if (data->actions.count()) {
        d->cpa->setAnimAction(data, QAbstractAnimation::DeleteWhenStopped);
    } else {
        delete data;
    }
}

QML_DEFINE_TYPE(QmlParentChangeAction,ParentChangeAction)

/*!
    \qmlclass NumericAnimation QmlNumericAnimation
    \inherits Animation
    \brief The NumericAnimation allows you to animate changes in properties of type qreal.

    Animate a set of properties over 200ms, from their values in the start state to
    their values in the end state of the transition:
    \code
    NumericAnimation { properties: "x,y,scale"; duration: 200 }
    \endcode
*/

/*!
    \internal
    \class QmlNumericAnimation
    \ingroup group_animation
    \ingroup group_states
    \brief The QmlNumericAnimation class allows you to animate changes in properties of type qreal.

    A QmlNumericAnimation object can be instantiated in Qml using the tag
    \l{xmlNumericAnimation} {&lt;NumericAnimation&gt;}.
*/

QmlNumericAnimation::QmlNumericAnimation(QObject *parent)
: QmlVariantAnimation(parent)
{
    Q_D(QmlVariantAnimation);
    d->init();
}

QmlNumericAnimation::~QmlNumericAnimation()
{
}

/*!
    \qmlproperty real NumericAnimation::from
    This property holds the starting value.
    If not set, then the value defined in the start state of the transition.
*/
/*!
    \property QmlNumericAnimation::from
    \brief the starting value.
*/
qreal QmlNumericAnimation::from() const
{
    Q_D(const QmlVariantAnimation);
    return d->from.toDouble();    //### toFloat?
}

void QmlNumericAnimation::setFrom(qreal f)
{
    QmlVariantAnimation::setFrom(f);
}

/*!
    \qmlproperty real NumericAnimation::to
    This property holds the ending value.
    If not set, then the value defined in the end state of the transition.
*/
/*!
    \property QmlNumericAnimation::to
    \brief the ending value.
*/
qreal QmlNumericAnimation::to() const
{
    Q_D(const QmlVariantAnimation);
    return d->to.toDouble();    //### toFloat?
}

void QmlNumericAnimation::setTo(qreal t)
{
    QmlVariantAnimation::setTo(t);
}

QML_DEFINE_TYPE(QmlNumericAnimation,NumericAnimation)

QmlAnimationGroup::QmlAnimationGroup(QObject *parent)
: QmlAbstractAnimation(*(new QmlAnimationGroupPrivate), parent)
{
}

QmlAnimationGroup::~QmlAnimationGroup()
{
}

QmlList<QmlAbstractAnimation *> *QmlAnimationGroup::animations()
{
    Q_D(QmlAnimationGroup);
    return &d->animations;
}

/*!
    \qmlclass SequentialAnimation QmlSequentialAnimation
    \inherits Animation
    \brief The SequentialAnimation allows you to run animations sequentially.

    Animations controlled in SequentialAnimation will be run one after the other.

    The following example chains two numeric animations together.  The \c MyItem
    object will animate from its current x position to 100, and then back to 0.

    \code
    SequentialAnimation {
        NumericAnimation { target: MyItem; property: "x"; to: 100 }
        NumericAnimation { target: MyItem; property: "x"; to: 0 }
    }
    \endcode

    \sa ParallelAnimation
*/

QmlSequentialAnimation::QmlSequentialAnimation(QObject *parent) :
    QmlAnimationGroup(parent)
{
    Q_D(QmlAnimationGroup);
    d->ag = new QSequentialAnimationGroup(this);
}

QmlSequentialAnimation::~QmlSequentialAnimation()
{
}

void QmlSequentialAnimation::prepare(QmlMetaProperty &p)
{
    Q_D(QmlAnimationGroup);
    if (d->userProperty.isNull)
        d->property = p;
    else
        d->property = d->userProperty;

    for (int i = 0; i < d->animations.size(); ++i)
        d->animations.at(i)->prepare(d->property);
}

QAbstractAnimation *QmlSequentialAnimation::qtAnimation()
{
    Q_D(QmlAnimationGroup);
    return d->ag;
}

void QmlSequentialAnimation::transition(QmlStateActions &actions,
                                    QmlMetaProperties &modified,
                                    TransitionDirection direction)
{
    Q_D(QmlAnimationGroup);

    int inc = 1;
    int from = 0;
    if (direction == Backward) {
        inc = -1;
        from = d->animations.count() - 1;
    }
    
    //### needed for Behavior
    if (d->userProperty.isValid() && d->propertyName.isEmpty() && !target()) {
        for (int i = 0; i < d->animations.count(); ++i)
            d->animations.at(i)->setTarget(d->userProperty);
   }

    //XXX removing and readding isn't ideal; we do it to get around the problem mentioned below.
    for (int i = d->ag->animationCount()-1; i >= 0; --i)
        d->ag->takeAnimationAt(i);

    for (int ii = from; ii < d->animations.count() && ii >= 0; ii += inc) {
        d->animations.at(ii)->transition(actions, modified, direction);
        d->ag->addAnimation(d->animations.at(ii)->qtAnimation());
    }

    //XXX changing direction means all the animations play in reverse, while we only want the ordering reversed.
    //d->ag->setDirection(direction == Backward ? QAbstractAnimation::Backward : QAbstractAnimation::Forward);
}

QML_DEFINE_TYPE(QmlSequentialAnimation,SequentialAnimation)

/*!
    \qmlclass ParallelAnimation QmlParallelAnimation
    \inherits Animation
    \brief The ParallelAnimation allows you to run animations in parallel.

    Animations contained in ParallelAnimation will be run at the same time.

    The following animation demonstrates animating the \c MyItem item
    to (100,100) by animating the x and y properties in parallel.

    \code
    ParallelAnimation {
        NumericAnimation { target: MyItem; property: "x"; to: 100 }
        NumericAnimation { target: MyItem; property: "y"; to: 100 }
    }
    \endcode

    \sa SequentialAnimation
*/
/*!
    \internal
    \class QmlParallelAnimation
    \ingroup group_animation
    \ingroup group_states
    \brief The QmlParallelAnimation class allows you to run animations in parallel.

    Animations controlled by QmlParallelAnimation will be run at the same time.

    \sa QmlSequentialAnimation

    A QmlParallelAnimation object can be instantiated in Qml using the tag
    \l{xmlParallelAnimation} {&lt;ParallelAnimation&gt;}.
*/

QmlParallelAnimation::QmlParallelAnimation(QObject *parent) :
    QmlAnimationGroup(parent)
{
    Q_D(QmlAnimationGroup);
    d->ag = new QParallelAnimationGroup(this);
}

QmlParallelAnimation::~QmlParallelAnimation()
{
}

void QmlParallelAnimation::prepare(QmlMetaProperty &p)
{
    Q_D(QmlAnimationGroup);
    if (d->userProperty.isNull)
        d->property = p;
    else
        d->property = d->userProperty;

    for (int i = 0; i < d->animations.size(); ++i)
        d->animations.at(i)->prepare(d->property);
}

QAbstractAnimation *QmlParallelAnimation::qtAnimation()
{
    Q_D(QmlAnimationGroup);
    return d->ag;
}

void QmlParallelAnimation::transition(QmlStateActions &actions,
                                      QmlMetaProperties &modified,
                                      TransitionDirection direction)
{
    Q_D(QmlAnimationGroup);

     //### needed for Behavior
    if (d->userProperty.isValid() && d->propertyName.isEmpty() && !target()) {
        for (int i = 0; i < d->animations.count(); ++i)
            d->animations.at(i)->setTarget(d->userProperty);
   }

    for (int ii = 0; ii < d->animations.count(); ++ii) {
        d->animations.at(ii)->transition(actions, modified, direction);
    }
}

QML_DEFINE_TYPE(QmlParallelAnimation,ParallelAnimation)

//### profile and optimize
QVariant QmlVariantAnimationPrivate::interpolateVariant(const QVariant &from, const QVariant &to, qreal progress)
{
    if (from.userType() != to.userType())
        return QVariant();

    QVariantAnimation::Interpolator interpolator = QVariantAnimationPrivate::getInterpolator(from.userType());
    return interpolator(from.constData(), to.constData(), progress);
}

//convert a variant from string type to another animatable type
//### should use any registered string convertor
//### profile and optimize
void QmlVariantAnimationPrivate::convertVariant(QVariant &variant, QVariant::Type type)
{
    if (variant.type() != QVariant::String) {
        variant.convert(type);
        return;
    }

    switch (type) {
    case QVariant::Rect: {
        variant.setValue(QmlStringConverters::rectFFromString(variant.toString()).toRect());
        break;
    }
    case QVariant::RectF: {
        variant.setValue(QmlStringConverters::rectFFromString(variant.toString()));
        break;
    }
    case QVariant::Point: {
        variant.setValue(QmlStringConverters::pointFFromString(variant.toString()).toPoint());
        break;
    }
    case QVariant::PointF: {
        variant.setValue(QmlStringConverters::pointFFromString(variant.toString()));
        break;
    }
    case QVariant::Size: {
        variant.setValue(QmlStringConverters::sizeFFromString(variant.toString()).toSize());
        break;
    }
    case QVariant::SizeF: {
        variant.setValue(QmlStringConverters::sizeFFromString(variant.toString()));
        break;
    }
    case QVariant::Color: {
        variant.setValue(QmlStringConverters::colorFromString(variant.toString()));
        break;
    }
    default:
        variant.convert(type);
        break;
    }
}

/*!
    \qmlclass VariantAnimation QmlVariantAnimation
    \inherits Animation
    \brief The VariantAnimation allows you to animate changes in properties of type QVariant.

    Animate a size property over 200ms, from its current size to 20-by-20:
    \code
    VariantAnimation { property: "size"; to: "20x20"; duration: 200 }
    \endcode
*/

QmlVariantAnimation::QmlVariantAnimation(QObject *parent)
: QmlAbstractAnimation(*(new QmlVariantAnimationPrivate), parent)
{
    Q_D(QmlVariantAnimation);
    d->init();
}

QmlVariantAnimation::~QmlVariantAnimation()
{
}

void QmlVariantAnimationPrivate::init()
{
    Q_Q(QmlVariantAnimation);
    va = new QmlTimeLineValueAnimator(q);
    va->setStartValue(QVariant(0.0f));
    va->setEndValue(QVariant(1.0f));
}

/*!
    \qmlproperty int VariantAnimation::duration
    This property holds the duration of the transition, in milliseconds.

    The default value is 250.
*/
/*!
    \property QmlVariantAnimation::duration
    \brief the duration of the transition, in milliseconds.

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

void QmlVariantAnimation::setDuration(int duration)
{
    if (duration < 0) {
        qWarning("QmlVariantAnimation: Cannot set a duration of < 0");
        return;
    }

    Q_D(QmlVariantAnimation);
    if (d->va->duration() == duration)
        return;
    d->va->setDuration(duration);
    emit durationChanged(duration);
}

/*!
    \qmlproperty real VariantAnimation::from
    This property holds the starting value.
    If not set, then the value defined in the start state of the transition.
*/
/*!
    \property QmlVariantAnimation::from
    \brief the starting value.
*/
QVariant QmlVariantAnimation::from() const
{
    Q_D(const QmlVariantAnimation);
    return d->from;
}

void QmlVariantAnimation::setFrom(const QVariant &f)
{
    Q_D(QmlVariantAnimation);
    if (d->fromIsDefined && f == d->from)
        return;
    d->from = f;
    d->fromIsDefined = f.isValid();
    emit fromChanged(f);
}

/*!
    \qmlproperty real VariantAnimation::to
    This property holds the ending value.
    If not set, then the value defined in the end state of the transition.
*/
/*!
    \property QmlVariantAnimation::to
    \brief the ending value.
*/
QVariant QmlVariantAnimation::to() const
{
    Q_D(const QmlVariantAnimation);
    return d->to;
}

void QmlVariantAnimation::setTo(const QVariant &t)
{
    Q_D(QmlVariantAnimation);
    if (d->toIsDefined && t == d->to)
        return;
    d->to = t;
    d->toIsDefined = t.isValid();
    emit toChanged(t);
}

/*!
    \qmlproperty string VariantAnimation::easing
    \brief the easing curve used for the transition.

    Available values are:

    \list
    \i \e easeNone - Easing equation function for a simple linear tweening, with no easing.
    \i \e easeInQuad - Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
    \i \e easeOutQuad - Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
    \i \e easeInOutQuad - Easing equation function for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInQuad - Easing equation function for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInCubic - Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
    \i \e easeOutCubic - Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
    \i \e easeInOutCubic - Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInCubic - Easing equation function for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInQuart - Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
    \i \e easeOutQuart - Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
    \i \e easeInOutQuart - Easing equation function for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInQuart - Easing equation function for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInQuint - Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
    \i \e easeOutQuint - Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
    \i \e easeInOutQuint - Easing equation function for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInQuint - Easing equation function for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInSine - Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
    \i \e easeOutSine - Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
    \i \e easeInOutSine - Easing equation function for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInSine - Easing equation function for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInExpo - Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
    \i \e easeOutExpo - Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
    \i \e easeInOutExpo - Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInExpo - Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInCirc - Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
    \i \e easeOutCirc - Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
    \i \e easeInOutCirc - Easing equation function for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInCirc - Easing equation function for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity.  The peak amplitude can be set with the \e amplitude parameter, and the period of decay by the \e period parameter.
    \i \e easeOutElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity.  The peak amplitude can be set with the \e amplitude parameter, and the period of decay by the \e period parameter.
    \i \e easeInOutElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeInBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
    \i \e easeOutBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
    \i \e easeInOutBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
    \i \e easeOutBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
    \i \e easeInBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
    \i \e easeInOutBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
    \i \e easeOutInBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
    \endlist
*/

/*!
    \property QmlVariantAnimation::easing
    \brief the easing curve to use.

    \sa QEasingCurve
*/
QString QmlVariantAnimation::easing() const
{
    Q_D(const QmlVariantAnimation);
    return d->easing;
}

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

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

/*!
    \qmlproperty string VariantAnimation::properties
    This property holds the properties this animation should be applied to.

    This is a comma-separated list of properties that should use
    this animation when they change.
*/
/*!
    \property QmlVariantAnimation::properties
    \brief the properties this animation should be applied to

    properties holds a copy separated list of properties that should use
    this animation when they change.
*/
QString QmlVariantAnimation::properties() const
{
    Q_D(const QmlVariantAnimation);
    return d->properties;
}

void QmlVariantAnimation::setProperties(const QString &prop)
{
    Q_D(QmlVariantAnimation);
    if (d->properties == prop)
        return;

    d->properties = prop;
    emit propertiesChanged(prop);
}

/*!
    \qmlproperty list<Item> VariantAnimation::filter
    This property holds the items selected to be affected by this animation (all if not set).
    \sa exclude
*/
QList<QObject *> *QmlVariantAnimation::filter()
{
    Q_D(QmlVariantAnimation);
    return &d->filter;
}

/*!
    \qmlproperty list<Item> VariantAnimation::exclude
    This property holds the items not to be affected by this animation.
    \sa filter
*/
QList<QObject *> *QmlVariantAnimation::exclude()
{
    Q_D(QmlVariantAnimation);
    return &d->exclude;
}

void QmlVariantAnimationPrivate::valueChanged(qreal r)
{
    if (!fromSourced) {
        if (!fromIsDefined) {
            from = property.read();
        }
        fromSourced = true;
    }

    if (r == 1.) {
        property.write(to);
    } else {
        property.write(interpolateVariant(from, to, r));
    }
}

QAbstractAnimation *QmlVariantAnimation::qtAnimation()
{
    Q_D(QmlVariantAnimation);
    return d->va;
}

void QmlVariantAnimation::prepare(QmlMetaProperty &p)
{
    Q_D(QmlVariantAnimation);
    if (d->userProperty.isNull)
        d->property = p;
    else
        d->property = d->userProperty;

    d->convertVariant(d->to, (QVariant::Type)d->property.propertyType());
    if (d->fromIsDefined)
        d->convertVariant(d->from, (QVariant::Type)d->property.propertyType());

    d->fromSourced = false;
    d->value.QmlTimeLineValue::setValue(0.);
    d->va->setAnimValue(&d->value, QAbstractAnimation::KeepWhenStopped);
    d->va->setFromSourcedValue(&d->fromSourced);
}

void QmlVariantAnimation::transition(QmlStateActions &actions,
                                     QmlMetaProperties &modified,
                                     TransitionDirection direction)
{
    Q_D(QmlVariantAnimation);
    Q_UNUSED(direction);

    struct PropertyUpdater : public QmlTimeLineValue
    {
        QmlStateActions actions;
        void setValue(qreal v)
        {
            QmlTimeLineValue::setValue(v);
            for (int ii = 0; ii < actions.count(); ++ii) {
                Action &action = actions[ii];

                QmlBehaviour::_ignore = true;
                if (v == 1.)
                    action.property.write(action.toValue);
                else {
                    if (action.fromValue.isNull()) {
                        action.fromValue = action.property.read();
                        /*if (action.fromValue.isNull())
                            action.fromValue = QVariant(0.);*/    //### can/should we give a default value for any type?
                    }
                    QVariant val = QmlVariantAnimationPrivate::interpolateVariant(action.fromValue, action.toValue, v);
                    action.property.write(val);
                }
                QmlBehaviour::_ignore = false;
            }
        }
    };

    QStringList props = d->properties.isEmpty() ? QStringList() : d->properties.split(QLatin1Char(','));
    for (int ii = 0; ii < props.count(); ++ii)
        props[ii] = props.at(ii).trimmed();
    if (!d->propertyName.isEmpty() && !props.contains(d->propertyName))
        props.append(d->propertyName);

    /* ### we used to select properties of name 'color' by default for color animations
    props << QLatin1String("color");*/

    if (d->userProperty.isValid() && props.isEmpty() && !target()) {
        props.append(d->userProperty.value.name());
        d->target = d->userProperty.value.object();
    }

    PropertyUpdater *data = new PropertyUpdater;

    QSet<QObject *> objs;
    for (int ii = 0; ii < actions.count(); ++ii) {
        Action &action = actions[ii];

        QObject *obj = action.property.object();
        QString propertyName = action.property.name();

        if ((d->filter.isEmpty() || d->filter.contains(obj)) &&
           (!d->exclude.contains(obj)) && props.contains(propertyName) &&
           (!target() || target() == obj)) {
            objs.insert(obj);
            Action myAction = action;

            if (d->fromIsDefined) {
                myAction.fromValue = d->from;
            } else {
                myAction.fromValue = QVariant();
            }
            if (d->toIsDefined)
                myAction.toValue = d->to;

            d->convertVariant(myAction.fromValue, (QVariant::Type)myAction.property.propertyType());
            d->convertVariant(myAction.toValue, (QVariant::Type)myAction.property.propertyType());

            modified << action.property;

            data->actions << myAction;
            action.fromValue = myAction.toValue;
        }
    }

    if (d->toIsDefined && target() && !objs.contains(target())) {
        QObject *obj = target();
        for (int jj = 0; jj < props.count(); ++jj) {
            Action myAction;
            myAction.property = QmlMetaProperty(obj, props.at(jj));

            if (d->fromIsDefined) {
                d->convertVariant(d->from, (QVariant::Type)myAction.property.propertyType());
                myAction.fromValue = d->from;
            }
            d->convertVariant(d->to, (QVariant::Type)myAction.property.propertyType());
            myAction.toValue = d->to;
            myAction.bv = 0;
            myAction.event = 0;
            data->actions << myAction;
        }
    }

    if (data->actions.count()) {
        d->va->setAnimValue(data, QAbstractAnimation::DeleteWhenStopped);
    } else {
        delete data;
    }
}

//XXX whats the best name for this? (just Animation?)
QML_DEFINE_TYPE(QmlVariantAnimation,VariantAnimation)

QT_END_NAMESPACE