File: vibesgraphicsitem.cpp

package info (click to toggle)
vibes 0.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,684 kB
  • sloc: cpp: 6,120; python: 412; makefile: 214; sh: 13
file content (1923 lines) | stat: -rw-r--r-- 64,085 bytes parent folder | download
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
#include "vibesgraphicsitem.h"

#include <QtCore>
#include <QVector>

#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsItemGroup>
#include <QGraphicsPathItem>
#include <QPainterPath>
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QBitmap>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>

#include <QDebug>
#include <cmath>
using namespace std;

// The only instance of VibesDefaults
VibesDefaults VibesDefaults::_instance;

VibesDefaults::VibesDefaults()
{
    initDefaultBrushesAndPens();
}

/// Initializes brushes and pens for default color names

void VibesDefaults::initDefaultBrushesAndPens()
{
#define ADD_DEFAULT_BRUSH(full_name) \
    _brushes[ #full_name ]  = QBrush(Qt::full_name);

#define ADD_DEFAULT_BRUSH2(full_name, short_name) \
    _brushes[ #full_name ]  = QBrush(Qt::full_name); \
    _brushes[ #short_name ] = QBrush(Qt::full_name);

#define ADD_DEFAULT_PEN(full_name) \
    _pens[ #full_name ]  = QPen(Qt::full_name, 0);

#define ADD_DEFAULT_PEN2(full_name, short_name) \
    _pens[ #full_name ]  = QPen(Qt::full_name, 0); \
    _pens[ #short_name ] = QPen(Qt::full_name, 0);

#define ADD_DEFAULT_BRUSH_AND_PEN(full_name) \
    ADD_DEFAULT_BRUSH(full_name) \
    ADD_DEFAULT_PEN(full_name)

#define ADD_DEFAULT_BRUSH_AND_PEN2(full_name, short_name) \
    ADD_DEFAULT_BRUSH2(full_name, short_name) \
    ADD_DEFAULT_PEN2(full_name, short_name)

    // Default brush and pen
    _brushes[QString()] = QBrush();
    _pens[QString()] = QPen(Qt::black, 0);

    // Named brushes and pens
    ADD_DEFAULT_BRUSH_AND_PEN2(transparent, none);

    ADD_DEFAULT_BRUSH_AND_PEN2(cyan, c);
    ADD_DEFAULT_BRUSH_AND_PEN2(yellow, y);
    ADD_DEFAULT_BRUSH_AND_PEN2(magenta, m);
    ADD_DEFAULT_BRUSH_AND_PEN2(red, r);
    ADD_DEFAULT_BRUSH_AND_PEN2(green, g);
    ADD_DEFAULT_BRUSH_AND_PEN2(blue, b);
    ADD_DEFAULT_BRUSH_AND_PEN2(black, k);
    ADD_DEFAULT_BRUSH_AND_PEN2(white, w);
    ADD_DEFAULT_BRUSH_AND_PEN(darkGray);
    ADD_DEFAULT_BRUSH_AND_PEN(gray);
    ADD_DEFAULT_BRUSH_AND_PEN(lightGray);
    ADD_DEFAULT_BRUSH_AND_PEN(darkCyan);
    ADD_DEFAULT_BRUSH_AND_PEN(darkYellow);
    ADD_DEFAULT_BRUSH_AND_PEN(darkMagenta);
    ADD_DEFAULT_BRUSH_AND_PEN(darkRed);
    ADD_DEFAULT_BRUSH_AND_PEN(darkGreen);
    ADD_DEFAULT_BRUSH_AND_PEN(darkBlue);
}

VibesGraphicsItem::VibesGraphicsItem(QGraphicsItem *qGraphicsItem)
: _qGraphicsItem(qGraphicsItem), _nbDim(0), _dimX(-1), _dimY(-1)
{
}

bool VibesGraphicsItem::setJson(QJsonObject json, int dimX, int dimY)
{
    if (!parseJson(json)) return false;
    _json = json;

    setProj(dimX, dimY);
    return true;
}

QJsonValue VibesGraphicsItem::jsonValue(const QString& key) const
{
    // If object has the requested property, return it
    if (json().contains(key))
    {
        return json()[key];
    }
        // Else, return the property from its parent group
    else
    {
        const VibesGraphicsGroup* group = 0;
        if (_qGraphicsItem)
            group = qgraphicsitem_cast<const VibesGraphicsGroup*>(_qGraphicsItem->parentItem());
        // Item is member of a group, ask the group for the requested property
        if (group)
        {
            return group->jsonValue(key);
        }
            // Item has no parent group, then return an empty value
        else
        {
            return QJsonValue();
        }
    }
}

void VibesGraphicsItem::setJsonValue(const QString &key, const QJsonValue &value)
{
    // Wrap key-value pair in a QJsonObject...
    QJsonObject jsonObject;
    jsonObject[key] = value;
    // ...and call setJsonValues
    setJsonValues(jsonObject);
}

void VibesGraphicsItem::setJsonValues(const QJsonObject &values)
{
    bool bNeedProjection = false;

    for (QJsonObject::const_iterator prop = values.constBegin(); prop != values.constEnd(); prop++)
    {
        // Cannot update read-only properties
        if (propertyIsReadOnly(prop.key()))
            continue;
        // Set or update property value
        _json[prop.key()] = prop.value();

        // Check if we need to update projection
        if (propertyChangesGeometry(prop.key()))
            bNeedProjection = true;
    }

    // Update graphics with new Json
    parseJson(_json);

    if (bNeedProjection)
        computeProjection(_dimX, _dimY);

    // Apply property change
    if (this->_qGraphicsItem->type() != VibesGraphicsGroupType)
    {
        this->updateProj();
    }
}

bool VibesGraphicsItem::setProj(int dimX, int dimY)
{
    _dimX = dimX;
    _dimY = dimY;
    if (existsInProj(_dimX, _dimY))
    {
        return computeProjection(_dimX, _dimY);
    }
    else
    {
        /// \todo Change visibility to none when projection is not available
        return false;
    }
}

VibesGraphicsItem * VibesGraphicsItem::newWithType(const QString type)
{
    if (type == "group")
    {
        return new VibesGraphicsGroup();
    }
    else if (type == "box")
    {
        return new VibesGraphicsBox();
    }
    else if (type == "boxes")
    {
        return new VibesGraphicsBoxes();
    }
    else if (type == "boxes union")
    {
        return new VibesGraphicsBoxesUnion();
    }
    else if (type == "ellipse")
    {
        return new VibesGraphicsEllipse();
    }
    else if (type == "pie")
    {
        return new VibesGraphicsPie();
    }
    else if (type == "ring")
    {
        return new VibesGraphicsRing();
    }
    else if (type == "point")
    {
        return new VibesGraphicsPoint();
    }
    else if (type == "points")
    {
        return new VibesGraphicsPoints();
    }
    else if (type == "line")
    {
        return new VibesGraphicsLine();
    }
    else if (type == "arrow")
    {
        return new VibesGraphicsArrow();
    }
    else if (type == "polygon")
    {
        return new VibesGraphicsPolygon();
    }
    else if (type == "vehicle")
    {
        return new VibesGraphicsVehicle();
    }
    else if (type == "vehicle_auv")
    {
        return new VibesGraphicsVehicleAUV();
    }
    else if (type == "vehicle_tank")
    {
        return new VibesGraphicsVehicleTank();
    }
    else if (type == "raster")
    {
        return new VibesGraphicsRaster();
    }
    return 0;
}

bool VibesGraphicsItem::parseJson(QJsonObject &json)
{
    // Process format string
    if (json.contains("format") && json["format"].isString())
    {
        QString format = json["format"].toString();
        format = format.trimmed();
        // Extract FaceColor
        int fcStart = format.indexOf('[');
        int fcEnd = format.lastIndexOf(']');
        if (fcStart < fcEnd)
        {
            json["FaceColor"] = QJsonValue(format.mid(fcStart + 1, fcEnd - fcStart - 1).trimmed());
            format.remove(fcStart, fcEnd - fcStart + 1);
        }
        
        // Extract EdgeColor
        format = format.trimmed();
        if (!format.isEmpty())
        {
            json["EdgeColor"] = QJsonValue(format);
        }
        // Remove "format" from Json
        json.remove("format");
    }

    // Process object name
    if (json.contains("name") && json["name"].isString())
    {
        this->setName(json["name"].toString());
    }

    // LineStyle and LineWidth need no processing

    // Process object specific JSON
    return parseJsonGraphics(json);
}

bool VibesGraphicsItem::isJsonMatrix(const QJsonValue value, int &nbRows, int &nbCols)
{
    nbRows = 0;
    nbCols = 0;
    // Check number of rows
    if (!value.isArray()) return false;
    QJsonArray lines = value.toArray();
    nbRows = lines.size();
    if (nbRows == 0) return false;
    // Check number of columns and consistency among rows
    QJsonArray::const_iterator line = lines.begin();
    if (!(*line).isArray()) return false;
    nbCols = (*line).toArray().size();
    if (nbCols == 0) return false;
    while (++line != lines.end())
    {
        if (!(*line).isArray()) return false;
        if ((*line).toArray().size() != nbCols) return false;
    }
    return true;
}

void VibesGraphicsGroup::addToGroup(VibesGraphicsItem *item)
{
    // Cannot add nullptr to the group
    if (!item) return;
    // Add to the group
    QGraphicsItemGroup::addToGroup(vibesgraphicsitem_cast<QGraphicsItem*>(item));
    // Update dimension
    this->_nbDim = qMax(_nbDim, item->dimension());
    // Update item with group properties
    if (VibesGraphicsItem::scene())
    {
        item->setProj(VibesGraphicsItem::scene()->dimX(), VibesGraphicsItem::scene()->dimY());
    }
}

void VibesGraphicsGroup::clear()
{
    // Remove all children
    QList<QGraphicsItem*> children = this->childItems();

    foreach(QGraphicsItem* child, children)
    {
        VibesGraphicsItem * item = qgraphicsitem_cast<VibesGraphicsItem *>(child);
        if (item) delete item;
    }
}

bool VibesGraphicsGroup::parseJsonGraphics(const QJsonObject &json)
{
    // Set graphical properties
    QList<QGraphicsItem*> children = this->childItems();

    foreach(QGraphicsItem* child, children)
    {
        VibesGraphicsItem * item = qgraphicsitem_cast<VibesGraphicsItem *>(child);
        // Each item gets the group properties if needed
        if (item)
        {
            if (json["EdgeColor"].toString()!=QString())
            {
                item->setJsonValue("EdgeColor",json["EdgeColor"]);
            }
            if (json["FaceColor"].toString()!=QString())
            {
                item->setJsonValue("FaceColor",json["FaceColor"]);
            }
            if (json["LineStyle"].toString()!=QString())
            {
                item->setJsonValue("LineStyle",json["LineStyle"]);
            }
            if (json["LineWidth"].toString()!=QString())
            {
                item->setJsonValue("LineWidth",json["LineWidth"]);
            }
            // Update item display
            item->updateProj();
        }
            
    }
    return true;
}

bool VibesGraphicsBox::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsBox has JSON type "box"
        if (type == "box")
        {
            QJsonArray bounds = json["bounds"].toArray();
            // We need at least a 2-D box. Number of bounds has to be even.
            if (bounds.size() < 4 || bounds.size() % 2 != 0)
                return false;

            // Compute dimension
            this->_nbDim = bounds.size() / 2;

            // Set graphical properties
            this->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));
            this->setBrush(vibesDefaults.brush(jsonValue("FaceColor").toString()));

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsBox::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    Q_ASSERT(json.contains("type"));
    Q_ASSERT(json["type"].toString() == "box");

    QJsonArray bounds = json["bounds"].toArray();
    // Compute dimension
    Q_ASSERT(this->_nbDim == bounds.size() / 2);
    Q_ASSERT(bounds.size() >= (2 * (qMax(dimX, dimY) + 1)));

    // Read bounds
    double lb_x = bounds[2 * dimX].toDouble();
    double ub_x = bounds[2 * dimX + 1].toDouble();
    double lb_y = bounds[2 * dimY].toDouble();
    double ub_y = bounds[2 * dimY + 1].toDouble();

    // Update rectangle
    this->setRect(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);

    // Update rectangle color
    this->setPen(pen);
    this->setBrush(brush);
    QString msg;// = QString("[%1, %2]x[%3, %4]").arg(lb_x).arg(ub_x).arg(lb_y).arg(ub_y);
#if QT_VERSION >= 0x050600 // Qt 5.6 or over
    msg = QString::asprintf("x: [%f ; %f]\ny: [%f ; %f]\n %f x %f", lb_x, ub_x, lb_y, ub_y, ub_x - lb_x, ub_y - lb_y);
#else
    msg.sprintf("x: [%f ; %f]\ny: [%f ; %f]\n %f x %f", lb_x, ub_x, lb_y, ub_y, ub_x - lb_x, ub_y - lb_y);
#endif
    this->setToolTip(msg);

    // Update successful
    return true;
}

//
// VibesGraphicsBoxes
//

bool VibesGraphicsBoxes::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsBoxes has JSON type "boxes"
        if (type == "boxes")
        {
            // Check that the "bounds" fields is a matrix
            int nbCols, nbRows;
            if (!isJsonMatrix(json["bounds"], nbRows, nbCols))
                return false;
            // Number of bounds has to be even
            if (nbCols % 2 != 0)
                return false;
            // Number of bounds has to be at least 4 (to draw boxes)
            if (nbCols < 4)
                return 0;

            //            bool colors = json.contains("colors");
            //            bool enoughColors = false;
            //            if (colors)
            //                enoughColors = json["colors"].toArray().size() == nbRows;

            // Compute dimension
            this->_nbDim = nbCols / 2;

            // Set graphical properties
            QList<QGraphicsItem*> rects = this->childItems();

            foreach(QGraphicsItem* item, rects)
            {
                QGraphicsRectItem * rect = qgraphicsitem_cast<QGraphicsRectItem *>(item);
                if (!rect) continue;
                rect->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));
                rect->setBrush(vibesDefaults.brush(jsonValue("FaceColor").toString()));
            }

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsBoxes::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsBoxes has JSON type "boxes"
    Q_ASSERT(json["type"].toString() == "boxes");

    // VibesGraphicsBoxes is basically a QGraphicsItemGroup containing QGraphicsRectItems
    // Before update, we first remove all existing rectangles
    {
        QList<QGraphicsItem*> rects = this->childItems();
        foreach(QGraphicsItem* rect, rects) delete rect;
    }
    // "bounds" is a matrix
    Q_ASSERT(isJsonMatrix(json["bounds"]));

    // Fill group with projected boxes

    foreach(const QJsonValue value, json["bounds"].toArray())
    {
        const QJsonArray box = value.toArray();
        // Read bounds
        double lb_x = box[2 * dimX].toDouble();
        double ub_x = box[2 * dimX + 1].toDouble();
        double lb_y = box[2 * dimY].toDouble();
        double ub_y = box[2 * dimY + 1].toDouble();
        // Create a new rect
        QGraphicsRectItem * rect = new QGraphicsRectItem(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);
        rect->setPen(pen);
        rect->setBrush(brush);
        // Add it to the group
        this->addToGroup(rect);
    }

    /*
        QJsonArray boundsX_lb = json["boundsX_lb"].toArray();
        QJsonArray boundsX_ub = json["boundsX_ub"].toArray();
        QJsonArray boundsY_lb = json["boundsY_lb"].toArray();
        QJsonArray boundsY_ub = json["boundsY_ub"].toArray();

        if (boundsX_lb.size() == boundsX_ub.size() &&
                boundsX_ub.size() == boundsY_lb.size() &&
                boundsY_lb.size() == boundsY_ub.size())
        {
            bool colors = json.contains("colors");
            bool enoughColors = false;
            if (colors)
                enoughColors = json["colors"].toArray().size() == boundsX_lb.size();
            for (int i = 0; i < boundsX_lb.size(); i++)
            {
                // Read bounds
                double lb_x = boundsX_lb[i].toDouble();
                double ub_x = boundsX_ub[i].toDouble();
                double lb_y = boundsY_lb[i].toDouble();
                double ub_y = boundsY_ub[i].toDouble();
                // Create a new rect
                QGraphicsRectItem * rect = new QGraphicsRectItem(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);
                rect->setPen(pen);
                rect->setBrush(brush);
                // Add it to the group
                this->addToGroup( rect );
            }
     */

    // Update successful
    return true;
}


//
// VibesGraphicsBoxesUnion
//

bool VibesGraphicsBoxesUnion::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsBoxesUnion has JSON type "boxes union"
        if (type == "boxes union")
        {
            // Check that the "bounds" fields is a matrix
            int nbCols, nbRows;
            if (!isJsonMatrix(json["bounds"], nbRows, nbCols))
                return false;
            // Number of bounds has to be even
            if (nbCols % 2 != 0)
                return false;
            // Number of bounds has to be at least 4 (to draw boxes)
            if (nbCols < 4)
                return 0;

            //            bool colors = json.contains("colors");
            //            bool enoughColors = false;
            //            if (colors)
            //                enoughColors = json["colors"].toArray().size() == nbRows;

            // Compute dimension
            this->_nbDim = nbCols / 2;

            // Set graphical properties
            this->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));
            this->setBrush(vibesDefaults.brush(jsonValue("FaceColor").toString()));

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsBoxesUnion::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsBoxes has JSON type "boxes union"
    Q_ASSERT(json["type"].toString() == "boxes union");
    // "bounds" is a matrix
    Q_ASSERT(isJsonMatrix(json["bounds"]));

    // Update path with projected boxes
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);

    foreach(const QJsonValue value, json["bounds"].toArray())
    {
        const QJsonArray box = value.toArray();
        // Read bounds
        double lb_x = box[2 * dimX].toDouble();
        double ub_x = box[2 * dimX + 1].toDouble();
        double lb_y = box[2 * dimY].toDouble();
        double ub_y = box[2 * dimY + 1].toDouble();
        // Create a new rect path
        QPainterPath rect_path;
        rect_path.addRect(lb_x, lb_y, ub_x - lb_x, ub_y - lb_y);
        // Unite with current path
        path |= rect_path;
    }
    this->setPath(path);
    // Set graphics properties
    this->setPen(pen);
    this->setBrush(brush);

    // Update successful
    return true;
}


//
// VibesGraphicsEllipse
//

bool VibesGraphicsEllipse::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // JSON type for VibesGraphicsEllipse is "ellipse" (Ellipses and Confidence ellipses)
        if (type == "ellipse")
        {
            QJsonArray center = json["center"].toArray();
            if (center.size() >= 2)
            {
                if (json.contains("axis") && json.contains("orientation"))
                {
                    // Axis-orientation only supported for 2D ellipses
                    if (center.size() != 2) return false;
                    if (json["axis"].toArray().size() != 2) return false;

                }
                else if (json.contains("angles"))
                {
                    if (json["angles"].toArray().size() != 2) return false;
                }
                else if (json.contains("covariance"))
                {
                    // A covariance matrix has been provided (Confidence ellipse)
                    double k = json.contains("sigma") ? json["sigma"].toDouble() : 5;
                    QJsonArray covariance = json["covariance"].toArray();
                    if (covariance.size() != pow(center.size(), 2)) return false;
                }
                else
                {
                    // Should not be here, ellipse parameters have not been provided
                    return false;
                }
                // Set dimension
                this->_nbDim = center.size();

                // Set graphical properties
                this->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));
                this->setBrush(vibesDefaults.brush(jsonValue("FaceColor").toString()));

                // Update successful
                return true;
            }
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsEllipse::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    Q_ASSERT(json.contains("type"));
    Q_ASSERT(json["type"].toString() == "ellipse");

    QJsonArray center = json["center"].toArray();

    Q_ASSERT(center.size() >= 2);
    Q_ASSERT(this->_nbDim == center.size());
    Q_ASSERT(center.size() >= (qMax(dimX, dimY) + 1));

    // Get center
    double x = center[dimX].toDouble();
    double y = center[dimY].toDouble();

    // Semi-major and semi-minor axes, and rotation
    double wx, wy, angle;
    // Intialize with full-ellipse
    int angle_min = 0;
    int angle_max = 5760;

    Q_ASSERT((json.contains("axis") && json.contains("orientation")) || json.contains("covariance"));
    if (json.contains("axis") && json.contains("orientation"))
    {
        // Semi-major and semi-minor axes are directly provided (Ellipse or circle)
        Q_ASSERT((dimX == 0 && dimY == 1) || (dimX == 1 && dimY == 0));

        QJsonArray axis = json["axis"].toArray();
        wx = axis[0].toDouble();
        wy = axis[1].toDouble();
        if (dimX == 0 && dimY == 1)
            angle = json.value("orientation").toDouble();
        else if (dimX == 1 && dimY == 0)
            angle = 90.0 - json.value("orientation").toDouble();
        else
            return false;
    }
    else if (json.contains("covariance"))
    {
        // A covariance matrix has been provided (Confidence ellipse)
        double k = json.contains("sigma") ? json["sigma"].toDouble() : 5;
        QJsonArray covariance = json["covariance"].toArray();
        Q_ASSERT(covariance.size() == center.size() * center.size());
        double sxx = covariance[dimX + center.size() * dimX].toDouble();
        double sxy = covariance[dimX + center.size() * dimY].toDouble();
        double syy = covariance[dimY + center.size() * dimY].toDouble();
        // Compute the semi-major and semi-minor axes and rotation angle.
        axisAngleFromCovarianceK(sxx, syy, sxy, k, wx, wy, angle);
    }
    else
    {
        // Should not be here, ellipse parameters have not been provided
        return false;
    }

    if (json.contains("angles"))
    {
        QJsonArray bounds = json["angles"].toArray();
        angle_min = static_cast<int> (bounds[0].toDouble()*16);
        angle_max = static_cast<int> (bounds[1].toDouble()*16);
    }
    // Update ellipse
    this->setRect(-wx, -wy, 2 * wx, 2 * wy);
    this->setRotation(angle);
    this->setPos(x, y);
    this->setStartAngle(angle_min);
    this->setSpanAngle(angle_max - angle_min);
    // Update ellipse properties
    this->setPen(pen);
    this->setBrush(brush);
    // Update successful
    return true;
}

void VibesGraphicsEllipse::axisAngleFromCovarianceK(const double &sxx, const double &syy,
                                                    const double &sxy, const double &k,
                                                    double &wx, double &wy, double &angle)
{
    double eval1, eval2, det, trace, rightTerm;
    double evect1[2], evect2[2];

    if (sxy == 0)
    {
        // Uncorrelated case
        eval1 = sxx;
        eval2 = syy;
        evect1[0] = 1;
        evect1[1] = 0;
        evect2[0] = 0;
        evect2[1] = 1;
    }
    else
    {
        // Correlated case
        det = sxx * syy - pow(sxy, 2);
        trace = sxx + syy;
        rightTerm = sqrt(pow(sxx + syy, 2) / 4 - det);
        eval1 = trace / 2 + rightTerm;
        eval2 = trace / 2 - rightTerm;

        evect1[0] = evect2[0] = 1; // We set the x-component of the eigenvectors to 1
        evect1[1] = (eval1 - sxy - sxx) / (sxy + syy - eval1);
        evect2[1] = (eval2 - sxy - sxx) / (sxy + syy - eval2);
    }

    // (evect1; evect2) give us the rotation matrix
    // k*sqrt(eval1) k*sqrt(eval2) give us the main axis-sizes of the ellipse
    angle = (evect1[0] != evect1[0]) || (evect1[1] != evect1[1]) ? (atan2(evect2[1], evect2[0])*180 * M_1_PI - 90) : atan2(evect1[1], evect1[0])*180 * M_1_PI;
    wx = k * sqrt(eval1);
    wy = k * sqrt(eval2);
}

//
// VibesGraphicsBoxesUnion
//

bool VibesGraphicsLine::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsLine has JSON type "line"
        if (type == "line")
        {
            // Check that the "points" field is a matrix
            int nbCols, nbRows;
            if (!isJsonMatrix(json["points"], nbRows, nbCols))
                return false;
            // Number of coordinates has to be at least 2 (to draw in the plane)
            if (nbCols < 2)
                return 0;

            // Compute dimension
            this->_nbDim = nbCols;

            // Set pen
            this->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsLine::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsLine has JSON type "line"
    Q_ASSERT(json["type"].toString() == "line");
    // "bounds" is a matrix
    Q_ASSERT(isJsonMatrix(json["points"]));

    // Update line with projected points
    QPainterPath path;
    QPolygonF polygon;

    foreach(const QJsonValue value, json["points"].toArray())
    {
        // Read coordinates and append them to the list of points
        const QJsonArray coords = value.toArray();
        polygon << QPointF(coords[dimX].toDouble(), coords[dimY].toDouble());
    }
    // Update polygon with the list of vertices
    path.addPolygon(polygon);
    this->setPath(path);
    // Set graphics properties
    this->setPen(pen);
    //this->setBrush(brush);

    // Update successful
    return true;
}

bool VibesGraphicsPolygon::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsBox has JSON type "polygon"
        if (type == "polygon")
        {
            // Check that the "points" field is a matrix
            int nbCols, nbRows;
            if (!isJsonMatrix(json["bounds"], nbRows, nbCols))
                return false;
            // Number of coordinates has to be at least 2 (to draw in the plane)
            if (nbCols < 2)
                return 0;

            // Compute dimension
            this->_nbDim = nbCols;

            // Set pen
            this->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));
            this->setBrush(vibesDefaults.brush(jsonValue("FaceColor").toString()));

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsPolygon::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    Q_ASSERT(json.contains("type"));
    Q_ASSERT(json["type"].toString() == "polygon");

    // Update polygon
    QPolygonF polygon;

    foreach(const QJsonValue value, json["bounds"].toArray())
    {
        // Read coordinates and append them to the list of points
        const QJsonArray coords = value.toArray();
        polygon << QPointF(coords[dimX].toDouble(), coords[dimY].toDouble());
    }
    this->setPolygon(polygon);

    // Update polygon color
    this->setPen(pen);
    this->setBrush(brush);

    // Update successful
    return true;
}


//
// VibesGraphicsVehicle
//

bool VibesGraphicsVehicle::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // JSON type for VibesGraphicsVehicle is "vehicle"
        if (type == "vehicle")
        {
            if (json.contains("center") && json.contains("length") && json.contains("orientation"))
            {
                int center_size = json["center"].toArray().size();
                if (center_size == 2 && json["length"].toDouble() > 0.)
                {
                    // Set dimension
                    this->_nbDim = center_size;

                    // Update successful
                    return true;
                }
            }
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsVehicle::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    Q_ASSERT(json.contains("type"));
    Q_ASSERT(json["type"].toString() == "vehicle");

    QJsonArray center = json["center"].toArray();
    double length = json["length"].toDouble();
    double orientation = json["orientation"].toDouble();

    Q_ASSERT(center.size() == 2);
    Q_ASSERT(this->_nbDim == center.size());
    Q_ASSERT(length > 0.);

    // Get center
    const QPointF & centerPoint = QPointF(center[dimX].toDouble(), center[dimY].toDouble());

    // If the shape has already been drawn, it has at least one child
    // Update child items if they exist
    if (this->childItems().size() > 0)
    {
        foreach(QGraphicsItem * item, this->childItems())
        {
            //to vibes graphics item
            QGraphicsPolygonItem *graphics_polygon = qgraphicsitem_cast<QGraphicsPolygonItem *>(item);
            graphics_polygon->setPen(pen);
            graphics_polygon->setBrush(brush);
            graphics_polygon->setTransformOriginPoint(centerPoint);
            graphics_polygon->setRotation(orientation);
            graphics_polygon->setScale(length / 4.); // initial vehicle's length is 4
        }
    }
    // Else draw the shape for the first time
    else{

        // Set polygon shape
        QPolygonF polygon;
        polygon << QPointF(-1. * length, 1. * length) + centerPoint;
        polygon << QPointF(+3. * length, 0. * length) + centerPoint;
        polygon << QPointF(-1. * length, -1. * length) + centerPoint;


        // Draw with the new properties
        QGraphicsPolygonItem *graphics_polygon = new QGraphicsPolygonItem(polygon);
        graphics_polygon->setPen(pen);
        graphics_polygon->setBrush(brush);
        graphics_polygon->setTransformOriginPoint(centerPoint);
        graphics_polygon->setRotation(orientation);
        graphics_polygon->setScale(length / 4.); // initial vehicle's length is 4
        this->addToGroup(graphics_polygon);
    }

    // Update successful
    return true;
}


//
// VibesGraphicsVehicleAUV
//

bool VibesGraphicsVehicleAUV::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // JSON type for VibesGraphicsVehicleAUV is "vehicle_auv"
        if (type == "vehicle_auv")
        {
            if (json.contains("center") && json.contains("length") && json.contains("orientation"))
            {
                int center_size = json["center"].toArray().size();
                if (center_size == 2 && json["length"].toDouble() > 0.)
                {
                    // Set dimension
                    this->_nbDim = center_size;

                    // Update successful
                    return true;
                }
            }
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsVehicleAUV::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    Q_ASSERT(json.contains("type"));
    Q_ASSERT(json["type"].toString() == "vehicle_auv");

    QJsonArray center = json["center"].toArray();
    double length = json["length"].toDouble();
    double orientation = json["orientation"].toDouble();

    Q_ASSERT(center.size() == 2);
    Q_ASSERT(this->_nbDim == center.size());
    Q_ASSERT(length > 0.);

    // Get center
    const QPointF & centerPoint = QPointF(center[dimX].toDouble(), center[dimY].toDouble());

    /*  This shape is inspired by the MOOS middleware GUI (see pMarineViewer)   */

    // If the shape has already been drawn, it has at least one child
    // Update child items if they exist
    if (this->childItems().size() > 0)
    {
        foreach(QGraphicsItem * item, this->childItems())
        {
            //to vibes graphics item
            QGraphicsPolygonItem *graphics_polygon = qgraphicsitem_cast<QGraphicsPolygonItem *>(item);
            graphics_polygon->setPen(pen);
            graphics_polygon->setBrush(brush);
            graphics_polygon->setTransformOriginPoint(centerPoint);
            graphics_polygon->setRotation(orientation);
            graphics_polygon->setScale(length / 7.); // initial vehicle's length is 4
        }
    }
    // Else draw the shape for the first time
    else{
        // Set body shape
        {
            QPolygonF body;
            body << QPointF(-4. * length, 0. * length) + centerPoint;
            body << QPointF(-2. * length, 1. * length) + centerPoint;
            body << QPointF(2. * length, 1. * length) + centerPoint;

            for (float i = 90.; i > -90.; i -= 10.) // noise
                body << QPointF((cos(i * M_PI / 180.0) + 2.) * length,
                                (sin(i * M_PI / 180.0) + 0.) * length) + centerPoint;

            body << QPointF(2. * length, -1. * length) + centerPoint;
            body << QPointF(-2. * length, -1. * length) + centerPoint;


            // Draw with the new properties
            QGraphicsPolygonItem *graphics_body = new QGraphicsPolygonItem(body);
            graphics_body->setPen(pen);
            graphics_body->setBrush(brush);
            graphics_body->setTransformOriginPoint(centerPoint);
            graphics_body->setRotation(orientation);
            graphics_body->setScale(length / 7.); // initial vehicle's length is 7
            this->addToGroup(graphics_body);
            graphics_body->setPen(pen);
        }

        // Set propulsion unit shape
        {
            QPolygonF propunit;
            propunit << QPointF(-4. * length, 1 * length) + centerPoint;
            propunit << QPointF(-3.25 * length, 1 * length) + centerPoint;
            propunit << QPointF(-3.25 * length, -1 * length) + centerPoint;
            propunit << QPointF(-4. * length, -1 * length) + centerPoint;

            // Draw with the new properties
            QGraphicsPolygonItem *graphics_propunit = new QGraphicsPolygonItem(propunit);
            graphics_propunit->setPen(pen);
            graphics_propunit->setBrush(brush);
            graphics_propunit->setTransformOriginPoint(centerPoint);
            graphics_propunit->setRotation(orientation);
            graphics_propunit->setScale(length / 7.); // initial vehicle's length is 7
            this->addToGroup(graphics_propunit);
        }
    }



    // Update successful
    return true;
}


//
// VibesGraphicsVehicleTank
//

bool VibesGraphicsVehicleTank::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // JSON type for VibesGraphicsVehicleTank is "vehicle_tank"
        if (type == "vehicle_tank")
        {
            if (json.contains("center") && json.contains("length") && json.contains("orientation"))
            {
                int center_size = json["center"].toArray().size();
                if (center_size == 2 && json["length"].toDouble() > 0.)
                {
                    // Set dimension
                    this->_nbDim = center_size;

                    // Update successful
                    return true;
                }
            }
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}
#include <iostream>
bool VibesGraphicsVehicleTank::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    Q_ASSERT(json.contains("type"));
    Q_ASSERT(json["type"].toString() == "vehicle_tank");

    QJsonArray center = json["center"].toArray();
    double length = json["length"].toDouble();
    double orientation = json["orientation"].toDouble();

    Q_ASSERT(center.size() == 2);
    Q_ASSERT(this->_nbDim == center.size());
    Q_ASSERT(length > 0.);

    // Get center
    const QPointF & centerPoint = QPointF(center[dimX].toDouble(), center[dimY].toDouble());

    /*  This shape is inspired by Luc Jaulin   */

    // If the shape has already been drawn, it has at least one child
    // Update child items if they exist
    if (this->childItems().size() > 0)
    {
        foreach(QGraphicsItem * item, this->childItems())
        {
            //to vibes graphics item
            QGraphicsPolygonItem *graphics_polygon = qgraphicsitem_cast<QGraphicsPolygonItem *>(item);
            graphics_polygon->setPen(pen);
            graphics_polygon->setBrush(brush);
            graphics_polygon->setTransformOriginPoint(centerPoint);
            graphics_polygon->setRotation(orientation);
            graphics_polygon->setScale(length / 4.); // initial vehicle's length is 4
        }
    }
    // Else draw the shape for the first time
    else{

        // Set body shape
        {
            QPolygonF body;
            body << QPointF(1., -1.5) + centerPoint;
            body << QPointF(-1., -1.5) + centerPoint;
            body << QPointF(0., -1.5) + centerPoint;
            body << QPointF(0., -1.) + centerPoint;
            body << QPointF(-1., -1.) + centerPoint;
            body << QPointF(-1., 1.) + centerPoint;
            body << QPointF(0., 1.) + centerPoint;
            body << QPointF(0., 1.5) + centerPoint;
            body << QPointF(-1., 1.5) + centerPoint;
            body << QPointF(1., 1.5) + centerPoint;
            body << QPointF(0., 1.5) + centerPoint;
            body << QPointF(0., 1.) + centerPoint;
            body << QPointF(3., 0.5) + centerPoint;
            body << QPointF(3., -0.5) + centerPoint;
            body << QPointF(0., -1.) + centerPoint;
            body << QPointF(0., -1.5) + centerPoint;


            // Draw with the new properties
            QGraphicsPolygonItem *graphics_body = new QGraphicsPolygonItem(body);
            graphics_body->setPen(pen);
            graphics_body->setBrush(brush);
            graphics_body->setTransformOriginPoint(centerPoint);
            graphics_body->setRotation(orientation);

            graphics_body->setScale(length/4.); // initial vehicle's length is 4
            this->addToGroup(graphics_body);
        }
    }

    // Update successful
    return true;
}


//
// VibesGraphicsArrow
//

bool VibesGraphicsArrow::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsArrow has JSON type "arrow"
        if (type == "arrow")
        {
            // Check that the "points" field is a matrix
            int nbCols, nbRows;
            if (!isJsonMatrix(json["points"], nbRows, nbCols))
                return false;
            if (json["tip_length"].toDouble() < 0)
                return false;
            // Number of coordinates has to be at least 2 (to draw in the plane)
            if (nbCols < 2)
                return 0;

            // Compute dimension
            this->_nbDim = nbCols;

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsArrow::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsArrow has JSON type "arrow"
    Q_ASSERT(json["type"].toString() == "arrow");
    // "bounds" is a matrix
    Q_ASSERT(isJsonMatrix(json["points"]));
    Q_ASSERT(json["tip_length"].toDouble() > 0);

    double before_last_x = 0., before_last_y = 0., last_x = 0., last_y = 0.;

    // If the shape has already been drawn, it has at least one child
    // Update child items if they exist
    if (this->childItems().size() > 0)
    {
        // item type : 2 if path, 5 if polygon
        foreach(QGraphicsItem * item, this->childItems())
        {
            if (item->type() == QGraphicsPathItem::Type)
            {
                //to vibes graphics item
                QGraphicsPathItem *graphics_path = qgraphicsitem_cast<QGraphicsPathItem *>(item);
                graphics_path->setPen(pen);
            }
            else if (item->type() == QGraphicsPolygonItem::Type)
            {
                //to vibes graphics item
                QGraphicsPolygonItem *graphics_tip = qgraphicsitem_cast<QGraphicsPolygonItem *>(item);
                graphics_tip->setPen(pen);
                graphics_tip->setBrush(brush);
            }
        }
    }
    // Else draw the shape for the first time
    else{
        // Body
        {
            QPolygonF line;

            // Update line with projected points

            foreach(const QJsonValue value, json["points"].toArray())
            {
                // Read coordinates and append them to the list of points
                const QJsonArray coords = value.toArray();
                before_last_x = last_x;
                before_last_y = last_y;
                last_x = coords[dimX].toDouble();
                last_y = coords[dimY].toDouble();
                line << QPointF(last_x, last_y);
            }

            QPainterPath path;
            path.addPolygon(line);
            QGraphicsPathItem *graphics_path = new QGraphicsPathItem(path);
            graphics_path->setPen(pen);
            this->addToGroup(graphics_path);
        }

        // Tip
        {
            QPolygonF tip;

            double tip_length = json["tip_length"].toDouble();
            double dx = (before_last_x - last_x);
            double dy = (before_last_y - last_y);
            double arrow_angle = atan2(dy, dx);
            double tip_angle = 160. * M_PI / 180.0;

            double x = last_x;
            double y = last_y;

            // tip (right)
            tip << QPointF(x, y);
            // (upper left)
            tip << QPointF(x - cos(tip_angle + arrow_angle) * tip_length,
                        y - sin(tip_angle + arrow_angle) * tip_length);
            // (left)
            tip << QPointF(x + cos(arrow_angle) * tip_length * 2. / 3.,
                        y + sin(arrow_angle) * tip_length * 2. / 3.);
            // (bottom left)
            tip << QPointF(x - cos(-tip_angle + arrow_angle) * tip_length,
                        y - sin(-tip_angle + arrow_angle) * tip_length);

            QGraphicsPolygonItem *graphics_tip = new QGraphicsPolygonItem(tip);
            graphics_tip->setPen(pen);
            graphics_tip->setBrush(brush);
            this->addToGroup(graphics_tip);
        }
    }

    // Update successful
    return true;
}


//
// VibesGraphicsPie
//

bool VibesGraphicsPie::parseJsonGraphics(const QJsonObject &json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsPie has JSON type "arrow"
        if (type == "pie" && json.contains("center"))
        {
            QJsonArray center = json["center"].toArray();
            if (center.size() != 2) return false;
            if (json.contains("rho"))
            {
                if (json["rho"].toArray().size() != 2) return false;
            }
            else if (json.contains("theta"))
            {
                if (json["theta"].toArray().size() != 2) return false;
            }
            // Compute dimension
            this->_nbDim = center.size();

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsPie::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsPie has JSON type "pie"
    Q_ASSERT(json["type"].toString() == "pie");

    QJsonArray center = json["center"].toArray();
    QJsonArray rho = json["rho"].toArray();
    QJsonArray theta = json["theta"].toArray();

    Q_ASSERT(rho[0].toDouble() >= 0);
    Q_ASSERT(rho[1].toDouble() >= rho[0].toDouble());

    // If the shape has already been drawn, it has at least one child
    // Update child items if they exist
    if (this->childItems().size() > 0)
    {
        foreach(QGraphicsItem * item, this->childItems())
        {
            //to vibes graphics item
            QGraphicsPathItem *graphics_polygon = qgraphicsitem_cast<QGraphicsPathItem *>(item);
            graphics_polygon->setPen(pen);
            graphics_polygon->setBrush(brush);
        }
    }
    // Else draw the shape for the first time
    else{
        // Body
        {

            double cx = center[0].toDouble();
            double cy = center[1].toDouble();

            double rho_m = rho[0].toDouble();
            double rho_p = rho[1].toDouble();
            double theta_m = -theta[0].toDouble();
            double theta_p = -theta[1].toDouble();

            // Angles are in degree and in clock-wise
            double m1_x = cx + rho_m * std::cos(-theta_m * M_PI / 180.0);
            double m1_y = cy + rho_m * std::sin(-theta_m * M_PI / 180.0);

            double m4_x = cx + rho_p * std::cos(-theta_m * M_PI / 180.0);
            double m4_y = cy + rho_p * std::sin(-theta_m * M_PI / 180.0);

            double dtheta = theta_p - theta_m;

            QPainterPath path(QPointF(m1_x, m1_y));
            path.lineTo(m4_x, m4_y);
            path.arcTo(QRectF(QPointF(cx - rho_p, cy - rho_p), QPointF(cx + rho_p, cy + rho_p)), theta_m, dtheta);
            path.arcTo(QRectF(QPointF(cx - rho_m, cy - rho_m), QPointF(cx + rho_m, cy + rho_m)), theta_p, -dtheta);

            // Draw with the new properties
            QGraphicsPathItem *graphics_path = new QGraphicsPathItem(path);
            graphics_path->setPen(pen);
            graphics_path->setBrush(brush);
            this->addToGroup(graphics_path);

        }
    }

    // Update successful
    return true;
}

bool VibesGraphicsPoint::parseJsonGraphics(const QJsonObject& json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();
        // VibesGraphicsPoint has JSON type "point"
        if (type == "point" && json.contains("point"))
        {
            QJsonArray point = json["point"].toArray();
            if (point.size() != 2) return false;

            // Compute dimension
            this->_nbDim = point.size();

            this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
            if (json.contains("FixedScale"))
            {
                // Default behaviour for points is true
                this->setFlag(QGraphicsItem::ItemIgnoresTransformations, json["FixedScale"].toBool(true));
            }
            if (json.contains("Draggable"))
            {
                if (json["Draggable"].isBool())
                {
                    this->setFlag(QGraphicsItem::ItemIsMovable, json["Draggable"].toBool(false));
                }
                else if (json["Draggable"].isDouble())
                {
                    this->setFlag(QGraphicsItem::ItemIsMovable, ((int) json["Draggable"].toDouble(0)) == 1);
                }
            }
            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsPoint::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get arrow color (or default if not specified)


    // VibesGraphicsPie has JSON type "arrow"
    Q_ASSERT(json["type"].toString() == "point");
    // "bounds" is a matrix
    QJsonArray point = json["point"].toArray();

    // Body
    {
        double cx = point[0].toDouble();
        double cy = point[1].toDouble();

        const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
        const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

        // Now process shape-specific properties
        // (we can only update properties of a shape, but mutation into another type is not supported)

        double rad = 0.01;
        if (json.contains("Radius"))
        {
            rad = json["Radius"].toDouble(0.01);
        }

        this->setRect(0, 0, 2 * rad, 2 * rad);

        this->setPos(cx, cy);

        this->setPen(pen);
        this->setBrush(brush);
    }

    // Update successful
    return true;
}

//
// VibesGraphicsPoints
//

bool VibesGraphicsPoints::parseJsonGraphics(const QJsonObject& json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsPie has JSON type "arrow"
        if (type == "points")
        {
            QJsonArray centers = json["centers"].toArray();
            this->_nbDim=centers[0].toArray().size();

            if (json.contains("Draggable"))
            {
                if (json["Draggable"].isBool())
                {
                    this->setFlag(QGraphicsItem::ItemIsMovable, json["Draggable"].toBool(false));
                }
                else if (json["Draggable"].isDouble())
                {
                    this->setFlag(QGraphicsItem::ItemIsMovable, ((int) json["Draggable"].toDouble(0)) == 1);
                }
            }
            QList<QGraphicsItem*> pts = this->childItems();

            foreach(QGraphicsItem* item, pts)
            {
                QGraphicsEllipseItem * disk = qgraphicsitem_cast<QGraphicsEllipseItem*>(item);
                if (!disk) continue;
                disk->setPen(vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString()));
                disk->setBrush(vibesDefaults.brush(jsonValue("FaceColor").toString()));
            }
            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsPoints::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsRing has JSON type "ring"
    Q_ASSERT(json["type"].toString() == "points");

    // Before update, we first remove all existing points
    {
        QList<QGraphicsItem*> disks = this->childItems();
        foreach(QGraphicsItem* disk, disks) delete disk;
    }

    QJsonArray levels, radiuses;
    double radius = 0.01;
    bool levelsExist = false;//json.contains("ColorLevels");
    if (levelsExist)levels = json["ColorLevels"].toArray();
    bool radiusesExist = json.contains("Radiuses");
    if (radiusesExist)radiuses = json["Radiuses"].toArray();
    if (!radiusesExist)
    {
        if (json.contains("Radius"))
        {
            radius = json["Radius"].toDouble(0.01);
        }
    }
    QJsonArray centers = json["centers"].toArray();

    for (unsigned int i = 0; i < centers.size(); i++)
    {
        const QJsonArray point = centers[i].toArray();
        double x = point[dimX].toDouble();
        double y = point[dimY].toDouble();

        double r = radiusesExist ? radiuses[i].toDouble() : radius;

        // Draw with the new properties
        QGraphicsEllipseItem * disk = new QGraphicsEllipseItem(0, 0, 2 * r, 2 * r);
        disk->setPos(x, y);

        disk->setPen(pen);
        disk->setBrush(brush);

        disk->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
        if (json.contains("FixedScale"))
        {
            // Default behaviour for points is true
            disk->setFlag(QGraphicsItem::ItemIgnoresTransformations, json["FixedScale"].toBool(true));
        }
        this->addToGroup(disk);

        //if(levelsExist)
        //{
        // TODO: apply colormap
        //}
    }

    // Update successful
    return true;
}

//
// VibesGraphicsRing
//

bool VibesGraphicsRing::parseJsonGraphics(const QJsonObject& json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsPie has JSON type "arrow"
        if (type == "ring" && json.contains("center") && json.contains("rho"))
        {
            QJsonArray center = json["center"].toArray();
            if (center.size() != 2) return false;
            if (json["rho"].toArray().size() != 2) return false;
            // Compute dimension
            this->_nbDim = center.size();

            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}

bool VibesGraphicsRing::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Get shape color (or default if not specified)
    const QBrush & brush = vibesDefaults.brush(jsonValue("FaceColor").toString());
    const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsRing has JSON type "ring"
    Q_ASSERT(json["type"].toString() == "ring");

    QJsonArray center = json["center"].toArray();
    QJsonArray rho = json["rho"].toArray();

    Q_ASSERT(rho[0].toDouble() >= 0);
    Q_ASSERT(rho[1].toDouble() >= rho[0].toDouble());
    
    //
    // Body
    {
        double cx = center[0].toDouble();
        double cy = center[1].toDouble();

        double rho_m = rho[0].toDouble();
        double rho_p = rho[1].toDouble();

        QRectF boundingBoxP(cx - rho_p, cy - rho_p, 2 * rho_p, 2 * rho_p);

        QPainterPath path;
        path.addEllipse(boundingBoxP);

        QRectF boundingBoxM(cx - rho_m, cy - rho_m, 2 * rho_m, 2 * rho_m);
        QPainterPath pathM;
        pathM.addEllipse(boundingBoxM);

        path = path.subtracted(pathM);


        // Draw with the new properties
        QGraphicsPathItem *graphics_path = new QGraphicsPathItem(path);
        graphics_path->setPen(pen);
        graphics_path->setBrush(brush);
        this->addToGroup(graphics_path);
    }

    // Update successful
    return true;
}

bool VibesGraphicsRaster::parseJsonGraphics(const QJsonObject& json)
{
    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    if (json.contains("type"))
    {
        // Retrieve type
        QString type = json["type"].toString();

        // VibesGraphicsPie has JSON type "arrow"
        if (type == "raster" && json.contains("filename") && json.contains("ul_corner") && json.contains("scale"))
        {
            if (json["ul_corner"].toArray().size() != 2) return false;
            if (json["scale"].toArray().size() != 2) return false;
            // Compute dimension
            this->_nbDim = 2; //center.size();
            // Update successful
            return true;
        }
    }

    // Unknown or empty JSON, update failed
    return false;
}
#include <iostream>
bool VibesGraphicsRaster::computeProjection(int dimX, int dimY)
{
    const QJsonObject & json = this->_json;

    // Now process shape-specific properties
    // (we can only update properties of a shape, but mutation into another type is not supported)
    Q_ASSERT(json.contains("type"));
    // VibesGraphicsRing has JSON type "ring"
    Q_ASSERT(json["type"].toString() == "raster");


    // Body
    {
        QString filename = json["filename"].toString();
        QJsonArray ul_corner = json["ul_corner"].toArray();
        QJsonArray scale = json["scale"].toArray();
        double xlb = ul_corner[0].toDouble();
        double yub = ul_corner[1].toDouble();
        double xres = scale[0].toDouble();
        double yres = scale[1].toDouble();

        QImage image(filename);
        QPixmap pixmap = QPixmap::fromImage(image);
        if (json.contains("EdgeColor")) {
          const QPen & pen = vibesDefaults.pen(jsonValue("EdgeColor").toString(),jsonValue("LineStyle").toString(),jsonValue("LineWidth").toString());
          pixmap.setMask(pixmap.createMaskFromColor(pen.color().rgb()));

        }

        QTransform transform(xres, 0, 0, yres, xlb, yub);
        QGraphicsPixmapItem *pixmap_item = new QGraphicsPixmapItem(pixmap);

        pixmap_item->setShapeMode(QGraphicsPixmapItem::MaskShape);
        pixmap_item->setTransform(transform);

        this->addToGroup(pixmap_item);
    }

    // Update successful
    return true;
}