forked from raptorswing/MapGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapcoloroverlay.cpp
95 lines (83 loc) · 2.84 KB
/
mapcoloroverlay.cpp
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
#include "mapcoloroverlay.h"
#include <QXmlStreamReader>
#include <QFile>
#include <QString>
#include <QDebug>
MapColorOverlay::MapColorOverlay()
{
}
MapColorOverlay::MapColorOverlay(QString countryName, QColor color)
{
this->countryName = countryName;
this->overlayColor = color;
}
QList<PolygonObject*> MapColorOverlay::PaintCountryToWidget()
{
QFile *xmlFile = new QFile("WorldBorders.xml");
if (!xmlFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug("error open file");
}
QXmlStreamReader *xmlReader = new QXmlStreamReader(xmlFile);
//Parse the XML until we reach end of it
QPolygonF polygon;
bool match = false;
bool multi = false;
while(!xmlReader->atEnd() && !xmlReader->hasError()) {
// Read next element
QXmlStreamReader::TokenType token = xmlReader->readNext();
//If token is just StartDocument - go to next
if(token == QXmlStreamReader::StartDocument) {
continue;
}
//If token is StartElement - read it
if(token == QXmlStreamReader::StartElement) {
if (xmlReader->name() == "area"){
multi = false;
if (xmlReader->attributes().value("name").toString() == countryName){
match = true;
} else {
match = false;
}
continue;
} else if (xmlReader->name() == "shapes") {
continue;
} else if (xmlReader->name() == "shape") {
if (match) {
multi = true;
}
continue;
} else if (xmlReader->name() == "points") {
continue;
} else if (xmlReader->name() == "point") {
if (match) {
qreal xpos = xmlReader->attributes().value("lng").toString().toDouble();
qreal ypos = xmlReader->attributes().value("lat").toString().toDouble();
QPointF point(xpos, ypos);
polygon << point;
}
continue;
}
} else if (token == QXmlStreamReader::EndElement) {
if (xmlReader->name() == "shape") {
if (multi) {
polygonList.append(polygon);
polygon.clear();
}
continue;
}
continue;
}
}
if(xmlReader->hasError()) {
qDebug("error parse file");
}
//close reader and flush file
xmlReader->clear();
xmlFile->close();
for (int i = 0; i < polygonList.count(); i++) {
polygonObjectList.append(new PolygonObject(polygonList.at(i), overlayColor));
}
// PolygonObject *polygonObj = new PolygonObject(polygon);
//return polygonObj;
return polygonObjectList;
}