aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquickpathitem.cpp
blob: fff666c205473ab2eafb3c79835d77afd43499e7 (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
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
/****************************************************************************
**
** 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 "qquickpathitem_p.h"
#include "qquickpathitem_p_p.h"
#include "qquickpathitemgenericrenderer_p.h"
#include "qquickpathitemnvprrenderer_p.h"
#include "qquickpathitemsoftwarerenderer_p.h"
#include <private/qsgtexture_p.h>
#include <private/qquicksvgparser_p.h>
#include <QtGui/private/qdrawhelper_p.h>
#include <QOpenGLFunctions>

#include <private/qv4engine_p.h>
#include <private/qv4object_p.h>
#include <private/qv4qobjectwrapper_p.h>
#include <private/qv4mm_p.h>
#include <private/qqmlengine_p.h>

QT_BEGIN_NAMESPACE

QQuickPathItemStrokeFillParams::QQuickPathItemStrokeFillParams()
    : strokeColor(Qt::white),
      strokeWidth(1),
      fillColor(Qt::white),
      fillRule(QQuickVisualPath::OddEvenFill),
      joinStyle(QQuickVisualPath::BevelJoin),
      miterLimit(2),
      capStyle(QQuickVisualPath::SquareCap),
      strokeStyle(QQuickVisualPath::SolidLine),
      dashOffset(0),
      fillGradient(nullptr)
{
    dashPattern << 4 << 2; // 4 * strokeWidth dash followed by 2 * strokeWidth space
}

QPainterPath QQuickPathItemPath::toPainterPath() const
{
    QPainterPath p;
    int coordIdx = 0;
    for (int i = 0; i < cmd.count(); ++i) {
        switch (cmd[i]) {
        case QQuickPathItemPath::MoveTo:
            p.moveTo(coords[coordIdx], coords[coordIdx + 1]);
            coordIdx += 2;
            break;
        case QQuickPathItemPath::LineTo:
            p.lineTo(coords[coordIdx], coords[coordIdx + 1]);
            coordIdx += 2;
            break;
        case QQuickPathItemPath::QuadTo:
            p.quadTo(coords[coordIdx], coords[coordIdx + 1],
                    coords[coordIdx + 2], coords[coordIdx + 3]);
            coordIdx += 4;
            break;
        case QQuickPathItemPath::CubicTo:
            p.cubicTo(coords[coordIdx], coords[coordIdx + 1],
                    coords[coordIdx + 2], coords[coordIdx + 3],
                    coords[coordIdx + 4], coords[coordIdx + 5]);
            coordIdx += 6;
            break;
        case QQuickPathItemPath::ArcTo:
            // does not map to the QPainterPath API; reuse the helper code from QQuickSvgParser
            QQuickSvgParser::pathArc(p,
                                     coords[coordIdx], coords[coordIdx + 1], // radius
                                     coords[coordIdx + 2], // xAxisRotation
                                     !qFuzzyIsNull(coords[coordIdx + 6]), // useLargeArc
                                     !qFuzzyIsNull(coords[coordIdx + 5]), // sweep flag
                                     coords[coordIdx + 3], coords[coordIdx + 4], // end
                                     p.currentPosition().x(), p.currentPosition().y());
            coordIdx += 7;
            break;
        default:
            qWarning("Unknown JS path command: %d", cmd[i]);
            break;
        }
    }
    return p;
}

/*!
    \qmltype VisualPath
    \instantiates QQuickVisualPath
    \inqmlmodule QtQuick
    \ingroup qtquick-paths
    \ingroup qtquick-views
    \inherits Object
    \brief Describes a Path and associated properties for stroking and filling
    \since 5.10

    A PathItem contains one or more VisualPath elements. At least one
    VisualPath is necessary in order to have a PathItem output anything
    visible. A VisualPath in turn contains a Path and properties describing the
    stroking and filling parameters, such as the stroke width and color, the
    fill color or gradient, join and cap styles, and so on. Finally, the Path
    object contains a list of path elements like PathMove, PathLine, PathCubic,
    PathQuad, PathArc.

    Any property changes in these data sets will be bubble up and change the
    output of the PathItem. This means that it is simple and easy to change, or
    even animate, the starting and ending position, control points, or any
    stroke or fill parameters using the usual QML bindings and animation types
    like NumberAnimation.

    In the following example the line join style changes automatically based on
    the value of joinStyleIndex:

    \code
    VisualPath {
        strokeColor: "black"
        strokeWidth: 16
        fillColor: "transparent"
        capStyle: VisualPath.RoundCap

        property int joinStyleIndex: 0
        property variant styles: [ VisualPath.BevelJoin, VisualPath.MiterJoin, VisualPath.RoundJoin ]

        joinStyle: styles[joinStyleIndex]

        Path {
            startX: 30
            startY: 30
            PathLine { x: 100; y: 100 }
            PathLine { x: 30; y: 100 }
        }
    }
    \endcode

    Once associated with a PathItem, here is the output with a joinStyleIndex
    of 2 (VisualPath.RoundJoin):

    \image visualpath-code-example.png
 */

QQuickVisualPathPrivate::QQuickVisualPathPrivate()
    : path(nullptr),
      dirty(DirtyAll)
{
}

QQuickVisualPath::QQuickVisualPath(QObject *parent)
    : QObject(*(new QQuickVisualPathPrivate), parent)
{
}

QQuickVisualPath::~QQuickVisualPath()
{
}

/*!
    \qmlproperty Path QtQuick::VisualPath::path

    This property holds the Path object.

    \default
 */

QQuickPath *QQuickVisualPath::path() const
{
    Q_D(const QQuickVisualPath);
    return d->path;
}

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

    if (d->path)
        qmlobject_disconnect(d->path, QQuickPath, SIGNAL(changed()),
                             this, QQuickVisualPath, SLOT(_q_pathChanged()));
    d->path = path;
    qmlobject_connect(d->path, QQuickPath, SIGNAL(changed()),
                      this, QQuickVisualPath, SLOT(_q_pathChanged()));

    d->dirty |= QQuickVisualPathPrivate::DirtyPath;
    emit pathChanged();
    emit changed();
}

void QQuickVisualPathPrivate::_q_pathChanged()
{
    Q_Q(QQuickVisualPath);
    dirty |= DirtyPath;
    emit q->changed();
}

/*!
    \qmlproperty color QtQuick::VisualPath::strokeColor

    This property holds the stroking color.

    When set to \c transparent, no stroking occurs.

    The default value is \c white.
 */

QColor QQuickVisualPath::strokeColor() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.strokeColor;
}

void QQuickVisualPath::setStrokeColor(const QColor &color)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.strokeColor != color) {
        d->sfp.strokeColor = color;
        d->dirty |= QQuickVisualPathPrivate::DirtyStrokeColor;
        emit strokeColorChanged();
        emit changed();
    }
}

/*!
    \qmlproperty color QtQuick::VisualPath::strokeWidth

    This property holds the stroke width.

    When set to a negative value, no stroking occurs.

    The default value is 1.
 */

qreal QQuickVisualPath::strokeWidth() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.strokeWidth;
}

void QQuickVisualPath::setStrokeWidth(qreal w)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.strokeWidth != w) {
        d->sfp.strokeWidth = w;
        d->dirty |= QQuickVisualPathPrivate::DirtyStrokeWidth;
        emit strokeWidthChanged();
        emit changed();
    }
}

/*!
    \qmlproperty color QtQuick::VisualPath::fillColor

    This property holds the fill color.

    When set to \c transparent, no filling occurs.

    The default value is \c white.
 */

QColor QQuickVisualPath::fillColor() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.fillColor;
}

void QQuickVisualPath::setFillColor(const QColor &color)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.fillColor != color) {
        d->sfp.fillColor = color;
        d->dirty |= QQuickVisualPathPrivate::DirtyFillColor;
        emit fillColorChanged();
        emit changed();
    }
}

/*!
    \qmlproperty enumeration QtQuick::VisualPath::fillRule

    This property holds the fill rule. The default value is
    VisualPath.OddEvenFill. For an example on fill rules, see
    QPainterPath::setFillRule().

    \list
    \li VisualPath.OddEvenFill
    \li VisualPath.WindingFill
    \endlist
 */

QQuickVisualPath::FillRule QQuickVisualPath::fillRule() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.fillRule;
}

void QQuickVisualPath::setFillRule(FillRule fillRule)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.fillRule != fillRule) {
        d->sfp.fillRule = fillRule;
        d->dirty |= QQuickVisualPathPrivate::DirtyFillRule;
        emit fillRuleChanged();
        emit changed();
    }
}

/*!
    \qmlproperty enumeration QtQuick::VisualPath::joinStyle

    This property defines how joins between two connected lines are drawn. The
    default value is VisualPath.BevelJoin.

    \list
    \li VisualPath.MiterJoin - The outer edges of the lines are extended to meet at an angle, and this area is filled.
    \li VisualPath.BevelJoin - The triangular notch between the two lines is filled.
    \li VisualPath.RoundJoin - A circular arc between the two lines is filled.
    \endlist
 */

QQuickVisualPath::JoinStyle QQuickVisualPath::joinStyle() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.joinStyle;
}

void QQuickVisualPath::setJoinStyle(JoinStyle style)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.joinStyle != style) {
        d->sfp.joinStyle = style;
        d->dirty |= QQuickVisualPathPrivate::DirtyStyle;
        emit joinStyleChanged();
        emit changed();
    }
}

/*!
    \qmlproperty int QtQuick::VisualPath::miterLimit

    When VisualPath.joinStyle is set to VisualPath.MiterJoin, this property
    specifies how far the miter join can extend from the join point.

    The default value is 2.
 */

int QQuickVisualPath::miterLimit() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.miterLimit;
}

void QQuickVisualPath::setMiterLimit(int limit)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.miterLimit != limit) {
        d->sfp.miterLimit = limit;
        d->dirty |= QQuickVisualPathPrivate::DirtyStyle;
        emit miterLimitChanged();
        emit changed();
    }
}

/*!
    \qmlproperty enumeration QtQuick::VisualPath::capStyle

    This property defines how the end points of lines are drawn. The
    default value is VisualPath.SquareCap.

    \list
    \li VisualPath.FlatCap - A square line end that does not cover the end point of the line.
    \li VisualPath.SquareCap - A square line end that covers the end point and extends beyond it by half the line width.
    \li VisualPath.RoundCap - A rounded line end.
    \endlist
 */

QQuickVisualPath::CapStyle QQuickVisualPath::capStyle() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.capStyle;
}

void QQuickVisualPath::setCapStyle(CapStyle style)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.capStyle != style) {
        d->sfp.capStyle = style;
        d->dirty |= QQuickVisualPathPrivate::DirtyStyle;
        emit capStyleChanged();
        emit changed();
    }
}

/*!
    \qmlproperty enumeration QtQuick::VisualPath::strokeStyle

    This property defines the style of stroking. The default value is
    VisualPath.SolidLine.

    \list
    \li VisualPath.SolidLine - A plain line.
    \li VisualPath.DashLine - Dashes separated by a few pixels.
    \endlist
 */

QQuickVisualPath::StrokeStyle QQuickVisualPath::strokeStyle() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.strokeStyle;
}

void QQuickVisualPath::setStrokeStyle(StrokeStyle style)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.strokeStyle != style) {
        d->sfp.strokeStyle = style;
        d->dirty |= QQuickVisualPathPrivate::DirtyDash;
        emit strokeStyleChanged();
        emit changed();
    }
}

/*!
    \qmlproperty real QtQuick::VisualPath::dashOffset

    This property defines the starting point on the dash pattern, measured in
    units used to specify the dash pattern.

    The default value is 0.

    \sa QPen::setDashOffset()
 */

qreal QQuickVisualPath::dashOffset() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.dashOffset;
}

void QQuickVisualPath::setDashOffset(qreal offset)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.dashOffset != offset) {
        d->sfp.dashOffset = offset;
        d->dirty |= QQuickVisualPathPrivate::DirtyDash;
        emit dashOffsetChanged();
        emit changed();
    }
}

/*!
    \qmlproperty list<real> QtQuick::VisualPath::dashPattern

    This property defines the dash pattern when VisualPath.strokeStyle is set
    to VisualPath.DashLine. The pattern must be specified as an even number of
    positive entries where the entries 1, 3, 5... are the dashes and 2, 4, 6...
    are the spaces. The pattern is specified in units of the pen's width.

    The default value is (4, 2), meaning a dash of 4 * VisualPath.strokeWidth
    pixels followed by a space of 2 * VisualPath.strokeWidth pixels.

    \sa QPen::setDashPattern()
 */

QVector<qreal> QQuickVisualPath::dashPattern() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.dashPattern;
}

void QQuickVisualPath::setDashPattern(const QVector<qreal> &array)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.dashPattern != array) {
        d->sfp.dashPattern = array;
        d->dirty |= QQuickVisualPathPrivate::DirtyDash;
        emit dashPatternChanged();
        emit changed();
    }
}

/*!
    \qmlproperty PathGradient QtQuick::VisualPath::fillGradient

    This property defines the fill gradient. By default no gradient is enabled
    and the value is \c null. In this case the fill uses a solid color based on
    the value of VisuaLPath.fillColor.

    When set, VisualPath.fillColor is ignored and filling is done using one of
    the PathGradient subtypes.
 */

QQuickPathGradient *QQuickVisualPath::fillGradient() const
{
    Q_D(const QQuickVisualPath);
    return d->sfp.fillGradient;
}

void QQuickVisualPath::setFillGradient(QQuickPathGradient *gradient)
{
    Q_D(QQuickVisualPath);
    if (d->sfp.fillGradient != gradient) {
        if (d->sfp.fillGradient)
            qmlobject_disconnect(d->sfp.fillGradient, QQuickPathGradient, SIGNAL(updated()),
                                 this, QQuickVisualPath, SLOT(_q_fillGradientChanged()));
        d->sfp.fillGradient = gradient;
        if (d->sfp.fillGradient)
            qmlobject_connect(d->sfp.fillGradient, QQuickPathGradient, SIGNAL(updated()),
                              this, QQuickVisualPath, SLOT(_q_fillGradientChanged()));
        d->dirty |= QQuickVisualPathPrivate::DirtyFillGradient;
        emit changed();
    }
}

void QQuickVisualPathPrivate::_q_fillGradientChanged()
{
    Q_Q(QQuickVisualPath);
    dirty |= DirtyFillGradient;
    emit q->changed();
}

void QQuickVisualPath::resetFillGradient()
{
    setFillGradient(nullptr);
}

/*!
    \qmltype PathItem
    \instantiates QQuickPathItem
    \inqmlmodule QtQuick
    \ingroup qtquick-paths
    \ingroup qtquick-views
    \inherits Item
    \brief Renders a path
    \since 5.10

    Renders a path either by generating geometry via QPainterPath and manual
    triangulation or by using a GPU vendor extension like \c{GL_NV_path_rendering}.

    This approach is different from rendering shapes via QQuickPaintedItem or
    the 2D Canvas because the path never gets rasterized in software. Therefore
    PathItem is suitable for creating shapes spreading over larger areas of the
    screen, avoiding the performance penalty for texture uploads or framebuffer
    blits. In addition, the declarative API allows manipulating, binding to,
    and even animating the path element properties like starting and ending
    position, the control points, etc.

    The types for specifying path elements are shared between \l PathView and
    PathItem. However, not all PathItem implementations support all path
    element types, while some may not make sense for PathView. PathItem's
    currently supported subset is: PathMove, PathLine, PathQuad, PathCubic,
    PathArc, PathSvg.

    See \l Path for a detailed overview of the supported path elements.

    \code
    PathItem {
        width: 200
        height: 150
        anchors.centerIn: parent
        VisualPath {
            strokeWidth: 4
            strokeColor: "red"
            fillGradient: PathLinearGradient {
                x1: 20; y1: 20
                x2: 180; y2: 130
                PathGradientStop { position: 0; color: "blue" }
                PathGradientStop { position: 0.2; color: "green" }
                PathGradientStop { position: 0.4; color: "red" }
                PathGradientStop { position: 0.6; color: "yellow" }
                PathGradientStop { position: 1; color: "cyan" }
            }
            strokeStyle: VisualPath.DashLine
            dashPattern: [ 1, 4 ]
            Path {
                startX: 20; startY: 20
                PathLine { x: 180; y: 130 }
                PathLine { x: 20; y: 130 }
                PathLine { x: 20; y: 20 }
            }
        }
    }
    \endcode

    \image pathitem-code-example.png

    \note It is important to be aware of performance implications, in
    particular when the application is running on the generic PathItem
    implementation due to not having support for accelerated path rendering.
    The geometry generation happens entirely on the CPU in this case, and this
    is potentially expensive. Changing the set of path elements, changing the
    properties of these elements, or changing certain properties of the
    PathItem itself all lead to retriangulation on every change. Therefore,
    applying animation to such properties can heavily affect performance on
    less powerful systems. If animating properties other than stroke and fill
    colors is a must, it is recommended to target systems providing
    \c{GL_NV_path_rendering} where the cost of path property changes is much
    smaller.

    The following list summarizes the available PathItem rendering approaches:

    \list

    \li When running with the default, OpenGL backend of Qt Quick, both the
    generic, triangulation-based and the NVIDIA-specific
    \c{GL_NV_path_rendering} methods are available. The choice is made at
    runtime, depending on the graphics driver's capabilities. When this is not
    desired, applications can force using the generic method by setting the
    PathItem.enableVendorExtensions property to \c false.

    \li The \c software backend is fully supported. The path is rendered via
    QPainter::strokePath() and QPainter::fillPath() in this case.

    \li The Direct 3D 12 backend is not currently supported.

    \li The OpenVG backend is not currently supported.

    \endlist

    \sa Path, PathMove, PathLine, PathQuad, PathCubic, PathArc, PathSvg
*/

QQuickPathItemPrivate::QQuickPathItemPrivate()
    : componentComplete(true),
      vpChanged(false),
      rendererType(QQuickPathItem::UnknownRenderer),
      async(false),
      status(QQuickPathItem::Null),
      renderer(nullptr),
      enableVendorExts(true)
{
}

QQuickPathItemPrivate::~QQuickPathItemPrivate()
{
    delete renderer;
}

void QQuickPathItemPrivate::_q_visualPathChanged()
{
    Q_Q(QQuickPathItem);
    vpChanged = true;
    q->polish();
}

void QQuickPathItemPrivate::setStatus(QQuickPathItem::Status newStatus)
{
    Q_Q(QQuickPathItem);
    if (status != newStatus) {
        status = newStatus;
        emit q->statusChanged();
    }
}

QQuickPathItem::QQuickPathItem(QQuickItem *parent)
  : QQuickItem(*(new QQuickPathItemPrivate), parent)
{
    setFlag(ItemHasContents);
}

QQuickPathItem::~QQuickPathItem()
{
}

/*!
    \qmlproperty enumeration QtQuick::PathItem::rendererType

    This property determines which path rendering backend is active.

    \list

    \li PathItem.UnknownRenderer - The renderer is unknown.

    \li PathItem.GeometryRenderer - The generic, driver independent solution
    for OpenGL. Uses the same CPU-based triangulation approach as QPainter's
    OpenGL 2 paint engine. This is the default on non-NVIDIA hardware when the
    default, OpenGL Qt Quick scenegraph backend is in use.

    \li PathItem.NvprRenderer - Path items are rendered by performing OpenGL
    calls using the \c{GL_NV_path_rendering} extension. This is the default on
    NVIDIA hardware when the default, OpenGL Qt Quick scenegraph backend is in
    use.

    \li PathItem.SoftwareRenderer - Pure QPainter drawing using the raster
    paint engine. This is the default, and only, option when the Qt Quick
    scenegraph is running with the \c software backend.

    \endlist
*/

QQuickPathItem::RendererType QQuickPathItem::rendererType() const
{
    Q_D(const QQuickPathItem);
    return d->rendererType;
}

/*!
    \qmlproperty bool QtQuick::PathItem::asynchronous

    When PathItem.rendererType is PathItem.GeometryRenderer, the input path is
    triangulated on the CPU during the polishing phase of the PathItem. This is
    potentially expensive. To offload this work to separate worker threads, set
    this property to \c true.

    When enabled, making a PathItem visible will not wait for the content to
    become available. Instead, the gui/main thread is not blocked and the
    results of the path rendering are shown only when all the asynchronous work
    has been finished.

    The default value is \c false.
 */

bool QQuickPathItem::asynchronous() const
{
    Q_D(const QQuickPathItem);
    return d->async;
}

void QQuickPathItem::setAsynchronous(bool async)
{
    Q_D(QQuickPathItem);
    if (d->async != async) {
        d->async = async;
        emit asynchronousChanged();
        if (d->componentComplete)
            d->_q_visualPathChanged();
    }
}

/*!
    \qmlproperty bool QtQuick::PathItem::enableVendorExtensions

    This property controls the usage of non-standard OpenGL extensions like
    GL_NV_path_rendering. To disable PathItem.NvprRenderer and force a uniform
    behavior regardless of the graphics card and drivers, set this property to
    \c false.

    The default value is \c true.
 */

bool QQuickPathItem::enableVendorExtensions() const
{
    Q_D(const QQuickPathItem);
    return d->enableVendorExts;
}

void QQuickPathItem::setEnableVendorExtensions(bool enable)
{
    Q_D(QQuickPathItem);
    if (d->enableVendorExts != enable) {
        d->enableVendorExts = enable;
        emit enableVendorExtensionsChanged();
    }
}

/*!
    \qmlproperty enumeration QtQuick::PathItem::status

    This property determines the status of the PathItem and is relevant when
    PathItem.asynchronous is set to \c true.

    \list

    \li PathItem.Null - Not yet initialized.

    \li PathItem.Ready - The PathItem has finished processing.

    \li PathItem.Processing - The path is being processed.

    \endlist
 */

QQuickPathItem::Status QQuickPathItem::status() const
{
    Q_D(const QQuickPathItem);
    return d->status;
}

static QQuickVisualPath *vpe_at(QQmlListProperty<QQuickVisualPath> *property, int index)
{
    QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast<QQuickPathItem *>(property->object));
    return d->qmlData.vp.at(index);
}

static void vpe_append(QQmlListProperty<QQuickVisualPath> *property, QQuickVisualPath *obj)
{
    QQuickPathItem *item = static_cast<QQuickPathItem *>(property->object);
    QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item);
    d->qmlData.vp.append(obj);

    if (d->componentComplete) {
        QObject::connect(obj, SIGNAL(changed()), item, SLOT(_q_visualPathChanged()));
        d->_q_visualPathChanged();
    }
}

static int vpe_count(QQmlListProperty<QQuickVisualPath> *property)
{
    QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(static_cast<QQuickPathItem *>(property->object));
    return d->qmlData.vp.count();
}

static void vpe_clear(QQmlListProperty<QQuickVisualPath> *property)
{
    QQuickPathItem *item = static_cast<QQuickPathItem *>(property->object);
    QQuickPathItemPrivate *d = QQuickPathItemPrivate::get(item);

    for (QQuickVisualPath *p : d->qmlData.vp)
        QObject::disconnect(p, SIGNAL(changed()), item, SLOT(_q_visualPathChanged()));

    d->qmlData.vp.clear();

    if (d->componentComplete)
        d->_q_visualPathChanged();
}

/*!
    \qmlproperty list<VisualPath> QtQuick::PathItem::elements

    This property holds the VisualPath objects that define the contents of the
    PathItem.

    \default
 */

QQmlListProperty<QQuickVisualPath> QQuickPathItem::elements()
{
    return QQmlListProperty<QQuickVisualPath>(this,
                                              nullptr,
                                              vpe_append,
                                              vpe_count,
                                              vpe_at,
                                              vpe_clear);
}

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

void QQuickPathItem::componentComplete()
{
    Q_D(QQuickPathItem);
    d->componentComplete = true;

    for (QQuickVisualPath *p : d->qmlData.vp)
        connect(p, SIGNAL(changed()), this, SLOT(_q_visualPathChanged()));

    d->_q_visualPathChanged();
}

void QQuickPathItem::updatePolish()
{
    Q_D(QQuickPathItem);

    if (!d->vpChanged)
        return;

    d->vpChanged = false;

    if (!d->renderer) {
        d->createRenderer();
        if (!d->renderer)
            return;
        emit rendererChanged();
    }

    // endSync() is where expensive calculations may happen (or get kicked off
    // on worker threads), depending on the backend. Therefore do this only
    // when the item is visible.
    if (isVisible())
        d->sync();

    update();
}

void QQuickPathItem::itemChange(ItemChange change, const ItemChangeData &data)
{
    Q_D(QQuickPathItem);

    // sync may have been deferred; do it now if the item became visible
    if (change == ItemVisibleHasChanged && data.boolValue)
        d->_q_visualPathChanged();

    QQuickItem::itemChange(change, data);
}

QSGNode *QQuickPathItem::updatePaintNode(QSGNode *node, UpdatePaintNodeData *)
{
    // Called on the render thread, with the gui thread blocked. We can now
    // safely access gui thread data.

    Q_D(QQuickPathItem);
    if (d->renderer) {
        if (!node)
            node = d->createNode();
        d->renderer->updateNode();
    }
    return node;
}

// the renderer object lives on the gui thread
void QQuickPathItemPrivate::createRenderer()
{
    Q_Q(QQuickPathItem);
    QSGRendererInterface *ri = q->window()->rendererInterface();
    if (!ri)
        return;

    switch (ri->graphicsApi()) {
#ifndef QT_NO_OPENGL
    case QSGRendererInterface::OpenGL:
        if (enableVendorExts && QQuickPathItemNvprRenderNode::isSupported()) {
            rendererType = QQuickPathItem::NvprRenderer;
            renderer = new QQuickPathItemNvprRenderer;
        } else {
            rendererType = QQuickPathItem::GeometryRenderer;
            renderer = new QQuickPathItemGenericRenderer(q);
        }
        break;
#endif
    case QSGRendererInterface::Software:
        rendererType = QQuickPathItem::SoftwareRenderer;
        renderer = new QQuickPathItemSoftwareRenderer;
        break;
    default:
        qWarning("No path backend for this graphics API yet");
        break;
    }
}

// the node lives on the render thread
QSGNode *QQuickPathItemPrivate::createNode()
{
    Q_Q(QQuickPathItem);
    QSGNode *node = nullptr;
    if (!q->window())
        return node;
    QSGRendererInterface *ri = q->window()->rendererInterface();
    if (!ri)
        return node;

    switch (ri->graphicsApi()) {
#ifndef QT_NO_OPENGL
    case QSGRendererInterface::OpenGL:
        if (enableVendorExts && QQuickPathItemNvprRenderNode::isSupported()) {
            node = new QQuickPathItemNvprRenderNode;
            static_cast<QQuickPathItemNvprRenderer *>(renderer)->setNode(
                static_cast<QQuickPathItemNvprRenderNode *>(node));
        } else {
            node = new QQuickPathItemGenericNode;
            static_cast<QQuickPathItemGenericRenderer *>(renderer)->setRootNode(
                static_cast<QQuickPathItemGenericNode *>(node));
        }
        break;
#endif
    case QSGRendererInterface::Software:
        node = new QQuickPathItemSoftwareRenderNode(q);
        static_cast<QQuickPathItemSoftwareRenderer *>(renderer)->setNode(
                    static_cast<QQuickPathItemSoftwareRenderNode *>(node));
        break;
    default:
        qWarning("No path backend for this graphics API yet");
        break;
    }

    return node;
}

static void q_asyncPathItemReady(void *data)
{
    QQuickPathItemPrivate *self = static_cast<QQuickPathItemPrivate *>(data);
    self->setStatus(QQuickPathItem::Ready);
}

void QQuickPathItemPrivate::sync()
{
    const bool useAsync = async && renderer->flags().testFlag(QQuickAbstractPathRenderer::SupportsAsync);
    if (useAsync) {
        setStatus(QQuickPathItem::Processing);
        renderer->setAsyncCallback(q_asyncPathItemReady, this);
    }

    if (!jsData.isValid()) {
        // Standard route: The path and stroke/fill parameters are provided via
        // VisualPath and Path.
        const int count = qmlData.vp.count();
        renderer->beginSync(count);

        for (int i = 0; i < count; ++i) {
            QQuickVisualPath *p = qmlData.vp[i];
            int &dirty(QQuickVisualPathPrivate::get(p)->dirty);

            if (dirty & QQuickVisualPathPrivate::DirtyPath)
                renderer->setPath(i, p->path());
            if (dirty & QQuickVisualPathPrivate::DirtyStrokeColor)
                renderer->setStrokeColor(i, p->strokeColor());
            if (dirty & QQuickVisualPathPrivate::DirtyStrokeWidth)
                renderer->setStrokeWidth(i, p->strokeWidth());
            if (dirty & QQuickVisualPathPrivate::DirtyFillColor)
                renderer->setFillColor(i, p->fillColor());
            if (dirty & QQuickVisualPathPrivate::DirtyFillRule)
                renderer->setFillRule(i, p->fillRule());
            if (dirty & QQuickVisualPathPrivate::DirtyStyle) {
                renderer->setJoinStyle(i, p->joinStyle(), p->miterLimit());
                renderer->setCapStyle(i, p->capStyle());
            }
            if (dirty & QQuickVisualPathPrivate::DirtyDash)
                renderer->setStrokeStyle(i, p->strokeStyle(), p->dashOffset(), p->dashPattern());
            if (dirty & QQuickVisualPathPrivate::DirtyFillGradient)
                renderer->setFillGradient(i, p->fillGradient());

            dirty = 0;
        }

        renderer->endSync(useAsync);
    } else {
        // Path and stroke/fill params provided from JavaScript. This avoids
        // QObjects at the expense of not supporting changes afterwards.
        const int count = jsData.paths.count();
        renderer->beginSync(count);

        for (int i = 0; i < count; ++i) {
            renderer->setJSPath(i, jsData.paths[i]);
            const QQuickPathItemStrokeFillParams sfp(jsData.sfp[i]);
            renderer->setStrokeColor(i, sfp.strokeColor);
            renderer->setStrokeWidth(i, sfp.strokeWidth);
            renderer->setFillColor(i, sfp.fillColor);
            renderer->setFillRule(i, sfp.fillRule);
            renderer->setJoinStyle(i, sfp.joinStyle, sfp.miterLimit);
            renderer->setCapStyle(i, sfp.capStyle);
            renderer->setStrokeStyle(i, sfp.strokeStyle, sfp.dashOffset, sfp.dashPattern);
            renderer->setFillGradient(i, sfp.fillGradient);
        }

        renderer->endSync(useAsync);
    }

    if (!useAsync)
        setStatus(QQuickPathItem::Ready);
}

// ***** gradient support *****

/*!
    \qmltype PathGradientStop
    \instantiates QQuickPathGradientStop
    \inqmlmodule QtQuick
    \ingroup qtquick-paths
    \ingroup qtquick-views
    \inherits Object
    \brief Defines a color at a position in a gradient
    \since 5.10
 */

QQuickPathGradientStop::QQuickPathGradientStop(QObject *parent)
    : QObject(parent),
      m_position(0),
      m_color(Qt::black)
{
}

/*!
    \qmlproperty real QtQuick::PathGradientStop::position

    The position and color properties describe the color used at a given
    position in a gradient, as represented by a gradient stop.

    The default value is 0.
 */

qreal QQuickPathGradientStop::position() const
{
    return m_position;
}

void QQuickPathGradientStop::setPosition(qreal position)
{
    if (m_position != position) {
        m_position = position;
        if (QQuickPathGradient *grad = qobject_cast<QQuickPathGradient *>(parent()))
            emit grad->updated();
    }
}

/*!
    \qmlproperty real QtQuick::PathGradientStop::color

    The position and color properties describe the color used at a given
    position in a gradient, as represented by a gradient stop.

    The default value is \c black.
 */

QColor QQuickPathGradientStop::color() const
{
    return m_color;
}

void QQuickPathGradientStop::setColor(const QColor &color)
{
    if (m_color != color) {
        m_color = color;
        if (QQuickPathGradient *grad = qobject_cast<QQuickPathGradient *>(parent()))
            emit grad->updated();
    }
}

/*!
    \qmltype PathGradient
    \instantiates QQuickPathGradient
    \inqmlmodule QtQuick
    \ingroup qtquick-paths
    \ingroup qtquick-views
    \inherits Object
    \brief Base type of PathItem fill gradients
    \since 5.10

    This is an abstract base class for gradients like PathLinearGradient and
    cannot be created directly.
 */

QQuickPathGradient::QQuickPathGradient(QObject *parent)
    : QObject(parent),
      m_spread(PadSpread)
{
}

int QQuickPathGradient::countStops(QQmlListProperty<QObject> *list)
{
    QQuickPathGradient *grad = qobject_cast<QQuickPathGradient *>(list->object);
    Q_ASSERT(grad);
    return grad->m_stops.count();
}

QObject *QQuickPathGradient::atStop(QQmlListProperty<QObject> *list, int index)
{
    QQuickPathGradient *grad = qobject_cast<QQuickPathGradient *>(list->object);
    Q_ASSERT(grad);
    return grad->m_stops.at(index);
}

void QQuickPathGradient::appendStop(QQmlListProperty<QObject> *list, QObject *stop)
{
    QQuickPathGradientStop *sstop = qobject_cast<QQuickPathGradientStop *>(stop);
    if (!sstop) {
        qWarning("Gradient stop list only supports QQuickPathGradientStop elements");
        return;
    }
    QQuickPathGradient *grad = qobject_cast<QQuickPathGradient *>(list->object);
    Q_ASSERT(grad);
    sstop->setParent(grad);
    grad->m_stops.append(sstop);
}

/*!
    \qmlproperty list<Object> QtQuick::PathGradient::stops
    \default

    The list of PathGradientStop objects defining the colors at given positions
    in the gradient.
 */

QQmlListProperty<QObject> QQuickPathGradient::stops()
{
    return QQmlListProperty<QObject>(this, nullptr,
                                     &QQuickPathGradient::appendStop,
                                     &QQuickPathGradient::countStops,
                                     &QQuickPathGradient::atStop,
                                     nullptr);
}

QGradientStops QQuickPathGradient::sortedGradientStops() const
{
    QGradientStops result;
    for (int i = 0; i < m_stops.count(); ++i) {
        QQuickPathGradientStop *s = static_cast<QQuickPathGradientStop *>(m_stops[i]);
        int j = 0;
        while (j < result.count() && result[j].first < s->position())
            ++j;
        result.insert(j, QGradientStop(s->position(), s->color()));
    }
    return result;
}

/*!
    \qmlproperty enumeration QtQuick::PathGradient::spred

    Specifies how the area outside the gradient area should be filled. The
    default value is PathGradient.PadSpread.

    \list
    \li PathGradient.PadSpread - The area is filled with the closest stop color.
    \li PathGradient.RepeatSpread - The gradient is repeated outside the gradient area.
    \li PathGradient.ReflectSpread - The gradient is reflected outside the gradient area.
    \endlist
 */

QQuickPathGradient::SpreadMode QQuickPathGradient::spread() const
{
    return m_spread;
}

void QQuickPathGradient::setSpread(SpreadMode mode)
{
    if (m_spread != mode) {
        m_spread = mode;
        emit spreadChanged();
        emit updated();
    }
}

/*!
    \qmltype PathLinearGradient
    \instantiates QQuickPathLinearGradient
    \inqmlmodule QtQuick
    \ingroup qtquick-paths
    \ingroup qtquick-views
    \inherits PathGradient
    \brief Linear gradient
    \since 5.10

    Linear gradients interpolate colors between start and end points. Outside
    these points the gradient is either padded, reflected or repeated depending
    on the spread type.

    \sa QLinearGradient
 */

QQuickPathLinearGradient::QQuickPathLinearGradient(QObject *parent)
    : QQuickPathGradient(parent)
{
}

/*!
    \qmlproperty real QtQuick::PathLinearGradient::x1
    \qmlproperty real QtQuick::PathLinearGradient::y1
    \qmlproperty real QtQuick::PathLinearGradient::x2
    \qmlproperty real QtQuick::PathLinearGradient::y2

    These properties define the start and end points between which color
    interpolation occurs. By default both the stard and end points are set to
    (0, 0).
 */

qreal QQuickPathLinearGradient::x1() const
{
    return m_start.x();
}

void QQuickPathLinearGradient::setX1(qreal v)
{
    if (m_start.x() != v) {
        m_start.setX(v);
        emit x1Changed();
        emit updated();
    }
}

qreal QQuickPathLinearGradient::y1() const
{
    return m_start.y();
}

void QQuickPathLinearGradient::setY1(qreal v)
{
    if (m_start.y() != v) {
        m_start.setY(v);
        emit y1Changed();
        emit updated();
    }
}

qreal QQuickPathLinearGradient::x2() const
{
    return m_end.x();
}

void QQuickPathLinearGradient::setX2(qreal v)
{
    if (m_end.x() != v) {
        m_end.setX(v);
        emit x2Changed();
        emit updated();
    }
}

qreal QQuickPathLinearGradient::y2() const
{
    return m_end.y();
}

void QQuickPathLinearGradient::setY2(qreal v)
{
    if (m_end.y() != v) {
        m_end.setY(v);
        emit y2Changed();
        emit updated();
    }
}

#ifndef QT_NO_OPENGL

// contexts sharing with each other get the same cache instance
class QQuickPathItemGradientCacheWrapper
{
public:
    QQuickPathItemGradientCache *get(QOpenGLContext *context)
    {
        return m_resource.value<QQuickPathItemGradientCache>(context);
    }

private:
    QOpenGLMultiGroupSharedResource m_resource;
};

QQuickPathItemGradientCache *QQuickPathItemGradientCache::currentCache()
{
    static QQuickPathItemGradientCacheWrapper qt_path_gradient_caches;
    return qt_path_gradient_caches.get(QOpenGLContext::currentContext());
}

// let QOpenGLContext manage the lifetime of the cached textures
QQuickPathItemGradientCache::~QQuickPathItemGradientCache()
{
    m_cache.clear();
}

void QQuickPathItemGradientCache::invalidateResource()
{
    m_cache.clear();
}

void QQuickPathItemGradientCache::freeResource(QOpenGLContext *)
{
    qDeleteAll(m_cache);
    m_cache.clear();
}

static void generateGradientColorTable(const QQuickPathItemGradientCache::GradientDesc &gradient,
                                       uint *colorTable, int size, float opacity)
{
    int pos = 0;
    const QGradientStops &s = gradient.stops;
    const bool colorInterpolation = true;

    uint alpha = qRound(opacity * 256);
    uint current_color = ARGB_COMBINE_ALPHA(s[0].second.rgba(), alpha);
    qreal incr = 1.0 / qreal(size);
    qreal fpos = 1.5 * incr;
    colorTable[pos++] = ARGB2RGBA(qPremultiply(current_color));

    while (fpos <= s.first().first) {
        colorTable[pos] = colorTable[pos - 1];
        pos++;
        fpos += incr;
    }

    if (colorInterpolation)
        current_color = qPremultiply(current_color);

    const int sLast = s.size() - 1;
    for (int i = 0; i < sLast; ++i) {
        qreal delta = 1/(s[i+1].first - s[i].first);
        uint next_color = ARGB_COMBINE_ALPHA(s[i + 1].second.rgba(), alpha);
        if (colorInterpolation)
            next_color = qPremultiply(next_color);

        while (fpos < s[i+1].first && pos < size) {
            int dist = int(256 * ((fpos - s[i].first) * delta));
            int idist = 256 - dist;
            if (colorInterpolation)
                colorTable[pos] = ARGB2RGBA(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist));
            else
                colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)));
            ++pos;
            fpos += incr;
        }
        current_color = next_color;
    }

    Q_ASSERT(s.size() > 0);

    uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha)));
    for ( ; pos < size; ++pos)
        colorTable[pos] = last_color;

    colorTable[size-1] = last_color;
}

QSGTexture *QQuickPathItemGradientCache::get(const GradientDesc &grad)
{
    QSGPlainTexture *tx = m_cache[grad];
    if (!tx) {
        QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
        GLuint id;
        f->glGenTextures(1, &id);
        f->glBindTexture(GL_TEXTURE_2D, id);
        static const uint W = 1024; // texture size is 1024x1
        uint buf[W];
        generateGradientColorTable(grad, buf, W, 1.0f);
        f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
        tx = new QSGPlainTexture;
        tx->setTextureId(id);
        switch (grad.spread) {
        case QQuickPathGradient::PadSpread:
            tx->setHorizontalWrapMode(QSGTexture::ClampToEdge);
            tx->setVerticalWrapMode(QSGTexture::ClampToEdge);
            break;
        case QQuickPathGradient::RepeatSpread:
            tx->setHorizontalWrapMode(QSGTexture::Repeat);
            tx->setVerticalWrapMode(QSGTexture::Repeat);
            break;
        case QQuickPathGradient::ReflectSpread:
            tx->setHorizontalWrapMode(QSGTexture::MirroredRepeat);
            tx->setVerticalWrapMode(QSGTexture::MirroredRepeat);
            break;
        default:
            qWarning("Unknown gradient spread mode %d", grad.spread);
            break;
        }
        m_cache[grad] = tx;
    }
    return tx;
}

#endif // QT_NO_OPENGL

// ***** JS-based alternative for creating static paths, (mostly) without QObjects *****

class QQuickPathItemJSEngineData : public QV8Engine::Deletable
{
public:
    QQuickPathItemJSEngineData(QV4::ExecutionEngine *engine);

    QV4::PersistentValue pathProto;
    QV4::PersistentValue strokeFillParamsProto;
};

V4_DEFINE_EXTENSION(QQuickPathItemJSEngineData, engineData)

namespace QV4 {
namespace Heap {

struct QQuickPathItemJSPathPrototype : Object {
    void init() { Object::init(); }
};

struct QQuickPathItemJSPath : Object {
    void init() { Object::init(); }
    QQuickPathItemPathObject *obj;
};

struct QQuickPathItemJSStrokeFillParamsPrototype : Object {
    void init() { Object::init(); }
};

struct QQuickPathItemJSStrokeFillParams : Object {
    void init() { Object::init(); }
    QQuickPathItemStrokeFillParamsObject *obj;
};

} // namespace Heap
} // namespace QV4

struct QQuickPathItemJSPathPrototype : public QV4::Object
{
    V4_OBJECT2(QQuickPathItemJSPathPrototype, QV4::Object)
public:
    static QV4::Heap::QQuickPathItemJSPathPrototype *create(QV4::ExecutionEngine *engine)
    {
        QV4::Scope scope(engine);
        auto obj = engine->memoryManager->allocObject<QQuickPathItemJSPathPrototype>();
        QV4::Scoped<QQuickPathItemJSPathPrototype> o(scope, obj);

        o->defineDefaultProperty(QStringLiteral("clear"), method_clear, 0);
        o->defineDefaultProperty(QStringLiteral("moveTo"), method_moveTo, 0);
        o->defineDefaultProperty(QStringLiteral("lineTo"), method_lineTo, 0);
        o->defineDefaultProperty(QStringLiteral("quadTo"), method_quadTo, 0);
        o->defineDefaultProperty(QStringLiteral("cubicTo"), method_cubicTo, 0);
        o->defineDefaultProperty(QStringLiteral("arcTo"), method_arcTo, 0);

        return o->d();
    }

    static void method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_moveTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_lineTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_quadTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_cubicTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_arcTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
};

DEFINE_OBJECT_VTABLE(QQuickPathItemJSPathPrototype);

struct QQuickPathItemJSStrokeFillParamsPrototype : public QV4::Object
{
    V4_OBJECT2(QQuickPathItemJSStrokeFillParamsPrototype, QV4::Object)
public:
    static QV4::Heap::QQuickPathItemJSStrokeFillParamsPrototype *create(QV4::ExecutionEngine *engine)
    {
        QV4::Scope scope(engine);
        auto obj = engine->memoryManager->allocObject<QQuickPathItemJSStrokeFillParamsPrototype>();
        QV4::Scoped<QQuickPathItemJSStrokeFillParamsPrototype> o(scope, obj);

        o->defineDefaultProperty(QStringLiteral("clear"), method_clear, 0);

        return o->d();
    }

    static void method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
};

DEFINE_OBJECT_VTABLE(QQuickPathItemJSStrokeFillParamsPrototype);

struct QQuickPathItemJSPath : public QV4::Object
{
    V4_OBJECT2(QQuickPathItemJSPath, QV4::Object)
};

DEFINE_OBJECT_VTABLE(QQuickPathItemJSPath);

struct QQuickPathItemJSStrokeFillParams : public QV4::Object
{
    V4_OBJECT2(QQuickPathItemJSStrokeFillParams, QV4::Object)

    static void method_get_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_get_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
    static void method_set_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData);
};

DEFINE_OBJECT_VTABLE(QQuickPathItemJSStrokeFillParams);

void QQuickPathItemJSPathPrototype::method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSPath> r(scope, callData->thisObject.as<QQuickPathItemJSPath>());

    r->d()->obj->clear();

    scope.result = callData->thisObject.asReturnedValue();
}

void QQuickPathItemJSPathPrototype::method_moveTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSPath> r(scope, callData->thisObject.as<QQuickPathItemJSPath>());

    if (callData->argc >= 2) {
        QQuickPathItemPathObject *p = r->d()->obj;
        p->path.cmd.append(QQuickPathItemPath::MoveTo);
        p->path.coords.append(callData->args[0].toNumber());
        p->path.coords.append(callData->args[1].toNumber());
    }

    scope.result = callData->thisObject.asReturnedValue();
}

void QQuickPathItemJSPathPrototype::method_lineTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSPath> r(scope, callData->thisObject.as<QQuickPathItemJSPath>());

    if (callData->argc >= 2) {
        QQuickPathItemPathObject *p = r->d()->obj;
        p->path.cmd.append(QQuickPathItemPath::LineTo);
        p->path.coords.append(callData->args[0].toNumber());
        p->path.coords.append(callData->args[1].toNumber());
    }

    scope.result = callData->thisObject.asReturnedValue();
}

void QQuickPathItemJSPathPrototype::method_quadTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSPath> r(scope, callData->thisObject.as<QQuickPathItemJSPath>());

    if (callData->argc >= 4) {
        QQuickPathItemPathObject *p = r->d()->obj;
        p->path.cmd.append(QQuickPathItemPath::QuadTo);
        const QV4::Value *v = callData->args;
        p->path.coords.append(v[0].toNumber()); // cx
        p->path.coords.append(v[1].toNumber()); // cy
        p->path.coords.append(v[2].toNumber()); // x
        p->path.coords.append(v[3].toNumber()); // y
    }

    scope.result = callData->thisObject.asReturnedValue();
}

void QQuickPathItemJSPathPrototype::method_cubicTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSPath> r(scope, callData->thisObject.as<QQuickPathItemJSPath>());

    if (callData->argc >= 6) {
        QQuickPathItemPathObject *p = r->d()->obj;
        p->path.cmd.append(QQuickPathItemPath::CubicTo);
        const QV4::Value *v = callData->args;
        p->path.coords.append(v[0].toNumber()); // c1x
        p->path.coords.append(v[1].toNumber()); // c1y
        p->path.coords.append(v[2].toNumber()); // c2x
        p->path.coords.append(v[3].toNumber()); // c2y
        p->path.coords.append(v[4].toNumber()); // x
        p->path.coords.append(v[5].toNumber()); // y
    }

    scope.result = callData->thisObject.asReturnedValue();
}

void QQuickPathItemJSPathPrototype::method_arcTo(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSPath> r(scope, callData->thisObject.as<QQuickPathItemJSPath>());

    if (callData->argc >= 7) {
        QQuickPathItemPathObject *p = r->d()->obj;
        p->path.cmd.append(QQuickPathItemPath::ArcTo);
        const QV4::Value *v = callData->args;
        p->path.coords.append(v[0].toNumber()); // radiusX
        p->path.coords.append(v[1].toNumber()); // radiusY
        p->path.coords.append(v[2].toNumber()); // xAxisRotation
        p->path.coords.append(v[3].toNumber()); // x
        p->path.coords.append(v[4].toNumber()); // y
        p->path.coords.append(v[5].toNumber()); // sweepFlag
        p->path.coords.append(v[6].toNumber()); // largeArc
    }

    scope.result = callData->thisObject.asReturnedValue();
}

void QQuickPathItemJSStrokeFillParamsPrototype::method_clear(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    r->d()->obj->clear();

    scope.result = callData->thisObject.asReturnedValue();
}

extern QColor qt_color_from_string(const QV4::Value &name); // qquickcontext2d.cpp

static inline QString qt_color_string(const QColor &color)
{
    if (color.alpha() == 255)
        return color.name();
    QString alphaString = QString::number(color.alphaF(), 'f');
    while (alphaString.endsWith(QLatin1Char('0')))
        alphaString.chop(1);
    if (alphaString.endsWith(QLatin1Char('.')))
        alphaString += QLatin1Char('0');
    return QString::fromLatin1("rgba(%1, %2, %3, %4)").arg(color.red()).arg(color.green()).arg(color.blue()).arg(alphaString);
}

void QQuickPathItemJSStrokeFillParams::method_get_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = QV4::Encode(scope.engine->newString(qt_color_string(r->d()->obj->sfp.strokeColor)));
}

void QQuickPathItemJSStrokeFillParams::method_set_strokeColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isString())
        r->d()->obj->sfp.strokeColor = qt_color_from_string(value);

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.strokeWidth);
}

void QQuickPathItemJSStrokeFillParams::method_set_strokeWidth(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    r->d()->obj->sfp.strokeWidth = value->toNumber();

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = QV4::Encode(scope.engine->newString(qt_color_string(r->d()->obj->sfp.fillColor)));
}

void QQuickPathItemJSStrokeFillParams::method_set_fillColor(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isString())
        r->d()->obj->sfp.fillColor = qt_color_from_string(value);

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.fillRule);
}

void QQuickPathItemJSStrokeFillParams::method_set_fillRule(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isInt32())
        r->d()->obj->sfp.fillRule = QQuickVisualPath::FillRule(value->integerValue());

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.joinStyle);
}

void QQuickPathItemJSStrokeFillParams::method_set_joinStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isInt32())
        r->d()->obj->sfp.joinStyle = QQuickVisualPath::JoinStyle(value->integerValue());

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.miterLimit);
}

void QQuickPathItemJSStrokeFillParams::method_set_miterLimit(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    r->d()->obj->sfp.miterLimit = value->toNumber();

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.capStyle);
}

void QQuickPathItemJSStrokeFillParams::method_set_capStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isInt32())
        r->d()->obj->sfp.capStyle = QQuickVisualPath::CapStyle(value->integerValue());

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.strokeStyle);
}

void QQuickPathItemJSStrokeFillParams::method_set_strokeStyle(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isInt32())
        r->d()->obj->sfp.strokeStyle = QQuickVisualPath::StrokeStyle(value->integerValue());

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = scope.engine->fromVariant(r->d()->obj->sfp.dashOffset);
}

void QQuickPathItemJSStrokeFillParams::method_set_dashOffset(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    r->d()->obj->sfp.dashOffset = value->toNumber();

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedArrayObject a(scope, scope.engine->newArrayObject());
    QQuickPathItemStrokeFillParamsObject *p = r->d()->obj;
    a->arrayReserve(p->sfp.dashPattern.count());
    QV4::ScopedValue v(scope);
    for (int i = 0; i < p->sfp.dashPattern.count(); ++i)
        a->arrayPut(i, (v = scope.engine->fromVariant(p->sfp.dashPattern[i])));
    a->setArrayLengthUnchecked(p->sfp.dashPattern.count());

    scope.result = a.asReturnedValue();
}

void QQuickPathItemJSStrokeFillParams::method_set_dashPattern(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    if (value->isObject()) {
        QV4::Scoped<QV4::ArrayObject> ao(scope, value);
       if (!!ao) {
            QQuickPathItemStrokeFillParamsObject *p = r->d()->obj;
            p->sfp.dashPattern.resize(ao->getLength());
            QV4::ScopedValue val(scope);
            for (int i = 0; i < p->sfp.dashPattern.count(); ++i) {
                val = ao->getIndexed(i);
                p->sfp.dashPattern[i] = val->toNumber();
            }
        }
    }

    scope.result = QV4::Encode::undefined();
}

void QQuickPathItemJSStrokeFillParams::method_get_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    scope.result = r->d()->obj->v4fillGradient.value();
}

void QQuickPathItemJSStrokeFillParams::method_set_fillGradient(const QV4::BuiltinFunction *, QV4::Scope &scope, QV4::CallData *callData)
{
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> r(scope, callData->thisObject.as<QQuickPathItemJSStrokeFillParams>());

    QV4::ScopedValue value(scope, callData->argument(0));
    QV4::Scoped<QV4::QObjectWrapper> qobjectWrapper(scope, value);
    if (!!qobjectWrapper) {
        if (QQuickPathGradient *grad = qobject_cast<QQuickPathGradient *>(qobjectWrapper->object())) {
            r->d()->obj->v4fillGradient.set(scope.engine, value);
            r->d()->obj->sfp.fillGradient = grad;
        }
    } else {
        r->d()->obj->v4fillGradient.set(scope.engine, nullptr);
        r->d()->obj->sfp.fillGradient = nullptr;
    }

    scope.result = QV4::Encode::undefined();
}

QQuickPathItemJSEngineData::QQuickPathItemJSEngineData(QV4::ExecutionEngine *v4)
{
    QV4::Scope scope(v4);

    QV4::ScopedObject proto(scope, QQuickPathItemJSPathPrototype::create(v4));
    pathProto = proto;

    proto = QV4::ScopedObject(scope, QQuickPathItemJSStrokeFillParamsPrototype::create(v4));

    proto->defineAccessorProperty(QStringLiteral("strokeColor"),
                                  QQuickPathItemJSStrokeFillParams::method_get_strokeColor,
                                  QQuickPathItemJSStrokeFillParams::method_set_strokeColor);
    proto->defineAccessorProperty(QStringLiteral("strokeWidth"),
                                  QQuickPathItemJSStrokeFillParams::method_get_strokeWidth,
                                  QQuickPathItemJSStrokeFillParams::method_set_strokeWidth);
    proto->defineAccessorProperty(QStringLiteral("fillColor"),
                                  QQuickPathItemJSStrokeFillParams::method_get_fillColor,
                                  QQuickPathItemJSStrokeFillParams::method_set_fillColor);
    proto->defineAccessorProperty(QStringLiteral("fillRule"),
                                  QQuickPathItemJSStrokeFillParams::method_get_fillRule,
                                  QQuickPathItemJSStrokeFillParams::method_set_fillRule);
    proto->defineAccessorProperty(QStringLiteral("joinStyle"),
                                  QQuickPathItemJSStrokeFillParams::method_get_joinStyle,
                                  QQuickPathItemJSStrokeFillParams::method_set_joinStyle);
    proto->defineAccessorProperty(QStringLiteral("miterLimit"),
                                  QQuickPathItemJSStrokeFillParams::method_get_miterLimit,
                                  QQuickPathItemJSStrokeFillParams::method_set_miterLimit);
    proto->defineAccessorProperty(QStringLiteral("capStyle"),
                                  QQuickPathItemJSStrokeFillParams::method_get_capStyle,
                                  QQuickPathItemJSStrokeFillParams::method_set_capStyle);
    proto->defineAccessorProperty(QStringLiteral("strokeStyle"),
                                  QQuickPathItemJSStrokeFillParams::method_get_strokeStyle,
                                  QQuickPathItemJSStrokeFillParams::method_set_strokeStyle);
    proto->defineAccessorProperty(QStringLiteral("dashOffset"),
                                  QQuickPathItemJSStrokeFillParams::method_get_dashOffset,
                                  QQuickPathItemJSStrokeFillParams::method_set_dashOffset);
    proto->defineAccessorProperty(QStringLiteral("dashPattern"),
                                  QQuickPathItemJSStrokeFillParams::method_get_dashPattern,
                                  QQuickPathItemJSStrokeFillParams::method_set_dashPattern);
    proto->defineAccessorProperty(QStringLiteral("fillGradient"),
                                  QQuickPathItemJSStrokeFillParams::method_get_fillGradient,
                                  QQuickPathItemJSStrokeFillParams::method_set_fillGradient);

    strokeFillParamsProto = proto;
}

void QQuickPathItemPathObject::setV4Engine(QV4::ExecutionEngine *engine)
{
    QQuickPathItemJSEngineData *ed = engineData(engine);
    QV4::Scope scope(engine);
    QV4::Scoped<QQuickPathItemJSPath> wrapper(scope, engine->memoryManager->allocObject<QQuickPathItemJSPath>());
    QV4::ScopedObject p(scope, ed->pathProto.value());
    wrapper->setPrototype(p);
    wrapper->d()->obj = this;
    m_v4value = wrapper;
}

void QQuickPathItemPathObject::clear()
{
    path = QQuickPathItemPath();
}

void QQuickPathItemStrokeFillParamsObject::clear()
{
    sfp = QQuickPathItemStrokeFillParams();
    if (!v4fillGradient.isNullOrUndefined())
        v4fillGradient.set(v4fillGradient.engine(), nullptr);
}

void QQuickPathItemStrokeFillParamsObject::setV4Engine(QV4::ExecutionEngine *engine)
{
    QQuickPathItemJSEngineData *ed = engineData(engine);
    QV4::Scope scope(engine);
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> wrapper(scope, engine->memoryManager->allocObject<QQuickPathItemJSStrokeFillParams>());
    QV4::ScopedObject p(scope, ed->strokeFillParamsProto.value());
    wrapper->setPrototype(p);
    wrapper->d()->obj = this;
    m_v4value = wrapper;
}

void QQuickPathItem::newPath(QQmlV4Function *args)
{
    QQuickPathItemPathObject *obj = new QQuickPathItemPathObject(this);
    obj->setV4Engine(QQmlEnginePrivate::get(qmlEngine(this))->v4engine());
    args->setReturnValue(obj->v4value());
}

void QQuickPathItem::newStrokeFillParams(QQmlV4Function *args)
{
    QQuickPathItemStrokeFillParamsObject *obj = new QQuickPathItemStrokeFillParamsObject(this);
    obj->setV4Engine(QQmlEnginePrivate::get(qmlEngine(this))->v4engine());
    args->setReturnValue(obj->v4value());
}

void QQuickPathItem::clearVisualPaths(QQmlV4Function *args)
{
    Q_UNUSED(args);
    Q_D(QQuickPathItem);
    d->jsData.paths.clear();
    d->jsData.sfp.clear();
}

void QQuickPathItem::commitVisualPaths(QQmlV4Function *args)
{
    Q_UNUSED(args);
    Q_D(QQuickPathItem);
    d->_q_visualPathChanged();
}

void QQuickPathItem::appendVisualPath(QQmlV4Function *args)
{
    if (args->length() < 2)
        return;

    Q_D(QQuickPathItem);
    QV4::Scope scope(args->v4engine());
    QV4::Scoped<QQuickPathItemJSPath> jsp(scope, (*args)[0]);
    QV4::Scoped<QQuickPathItemJSStrokeFillParams> jssfp(scope, (*args)[1]);

    const QQuickPathItemPath &path(jsp->d()->obj->path);
    const QQuickPathItemStrokeFillParams &sfp(jssfp->d()->obj->sfp);

    d->jsData.paths.append(path);
    d->jsData.sfp.append(sfp);
}

QT_END_NAMESPACE

#include "moc_qquickpathitem_p.cpp"