-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvgreader.cpp
141 lines (126 loc) · 4.59 KB
/
svgreader.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
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
#include "svgreader.h"
#include <QPen>
#include <QFile>
#include <QMessageBox>
#include <QDomDocument>
#include <QStringList>
#include <QDebug>
SvgReader::SvgReader()
{
}
QList<RoomLabel *> SvgReader::getElements(const QString filename, const QString objectName){
QList<RoomLabel *> rectList; // Объявим в стеке список прямоугольников
QDomDocument doc; // объект документа
QFile file(filename); // Открываем наш SVG-файл
if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file)){
return rectList; // то возвратим список, но пустой
}
QDomNodeList gList = doc.elementsByTagName("g");
int i;
for (i = 0; i < gList.size(); i++) {
if (gList.item(i).toElement().attribute("name").toLower() == objectName.toLower()){
QDomNodeList Polygons = gList.item(i).toElement().elementsByTagName("polygon");
rectList.append(getPolygon(Polygons.item(0), gList.item(i), true));
for (int j = 1; j < Polygons.size(); j++){
rectList.append(getPolygon(Polygons.item(j), gList.item(i)));
}
}
}
file.close();
return rectList;
}
QStringList getlegend(const QString filename){
QStringList legends;
QDomDocument doc;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file)){
return legends;
}
QDomNodeList gList = doc.elementsByTagName("g");
int i;
for (i = 0; i < gList.size(); i++) {
if (gList.item(i).toElement().attribute("legend").size() > 5){
QString color = gList.item(i).toElement().attribute("legend");
QString name = gList.item(i).toElement().attribute("name");
legends.push_back("<font size=4 color=" + color + ">" + name + "</font>");
}
}
file.close();
return legends;
}
RoomLabel *SvgReader::getPolygon(QDomNode node, QDomNode rect, bool f){
QDomElement p = node.toElement();
QVector <QPoint> points;
QStringList txtpoints = p.attribute("points").trimmed().split(" ");
foreach(QString s, txtpoints){
QStringList point = s.split(",");
QString x = point[0];
QString y = point[1];
points << QPoint(x.toDouble(), y.toDouble());
}
RoomLabel * room;
if (f){
room = new RoomLabel(getSize(rect),points,getName(rect), getBrush(rect), getInfo(rect));
return room;
} else {
room = new RoomLabel(QRectF(0, 0, 0, 0),points,getName(rect), getBrush(rect));
return room;
} return room;
}
//QStringList getId(const QString filename){
// QFile file(filename);
// QStringList objId;
// if(!file.open(QFile::ReadOnly | QFile::Text)){
// qDebug() << "Ошибка открытия файла для чтения ID комнат";
// return objId;
// }
// while (!file.atEnd()){
// objId << file.readLine().trimmed();
// }
// foreach (QString s, objId) {
// }
// return objId;
//}
QRectF getSize(const QDomNode svgElement)
{
QStringList parameters = svgElement.toElement().attribute("viewBox").trimmed().split(" ");
if (parameters.size() != 4){
return QRectF(0,0,0,0);
} else{
return QRectF(QPointF(QPoint(parameters.at(0).toDouble(),
parameters.at(1).toDouble())),
QPointF(QPoint(parameters.at(2).toDouble(),
parameters.at(3).toDouble())));
}
}
QString getName(const QDomNode element){;
return element.toElement().attribute("name");
}
QString getBrush (const QDomNode element){
return element.toElement().attribute("fill");
}
QString getInfo (const QDomNode element){
return element.toElement().attribute("info");
}
QList <QList <RoomLabel *>> SvgReader::getAllObjects(const QString filename){
QList <QList <RoomLabel *>> rectList;
QDomDocument doc;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly) || !doc.setContent(&file)){
return rectList;
}
QDomNodeList gList = doc.elementsByTagName("g");
for (int i = 0; i < gList.size(); i++){
if (gList.item(i).toElement().attribute("name") != ""){
QDomNodeList Polygons = gList.item(i).toElement().elementsByTagName("polygon");
QList <RoomLabel *> list;
list.append(getPolygon(Polygons.item(0), gList.item(i), true));
for (int j = 1; j < Polygons.size(); j++){
list.append(getPolygon(Polygons.item(j), gList.item(i)));
}
rectList.append(list);
}
file.close();
}
return rectList;
}