-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProjectReader.cpp
174 lines (147 loc) · 4.91 KB
/
ProjectReader.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
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
/*
* ProjectReader.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ProjectReader.h"
#include <sstream>
#include <iostream>
#include <string>
namespace mmp {
ProjectReader::ProjectReader(MainWindow *window) : _window(window)
{
}
bool ProjectReader::readFile(QIODevice *device)
{
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(device, false, &errorStr, &errorLine, &errorColumn)) {
std::cerr << "Error: Parse error at line " << errorLine << ", "
<< "column " << errorColumn << ": "
<< qPrintable(errorStr) << std::endl;
return false;
}
QDomElement root = doc.documentElement();
// The handling of the version number will get fancier as we go.
if (root.tagName() != "project" || root.attribute("version") != MM::VERSION)
{
_xml.raiseError(QObject::tr("The file is not a mapmap version %1 file.").arg(MM::VERSION));
return false;
}
parseProject(root);
return (! _xml.hasError() );
}
QString ProjectReader::errorString() const
{
return QObject::tr("%1\nLine %2, column %3")
.arg(_xml.errorString())
.arg(_xml.lineNumber())
.arg(_xml.columnNumber());
}
void ProjectReader::parseProject(const QDomElement& project)
{
// TODO: this is dangerous if we have
MappingManager& manager = _window->getMappingManager();
manager.clearAll();
QDomElement paints = project.firstChildElement(ProjectLabels::PAINTS);
QDomElement mappings = project.firstChildElement(ProjectLabels::MAPPINGS);
// Parse paints.
QDomNode paintNode = paints.firstChild();
while (!paintNode.isNull())
{
Paint::ptr paint = parsePaint(paintNode.toElement());
if (paint.isNull())
{
qDebug() << "Problem creating paint." << endl;
}
else
{
manager.addPaint(paint);
_window->addPaintItem(paint->getId(), paint->getIcon(), paint->getName());
}
paintNode = paintNode.nextSibling();
}
// Parse mappings.
QDomNode mappingNode = mappings.firstChild();
while (!mappingNode.isNull())
{
Mapping::ptr mapping = parseMapping(mappingNode.toElement());
if (mapping.isNull())
{
qDebug() << "Problem creating mapping." << endl;
}
else
{
manager.addMapping(mapping);
_window->addMappingItem(mapping->getId());
}
mappingNode = mappingNode.nextSibling();
}
}
Paint::ptr ProjectReader::parsePaint(const QDomElement& paintElem)
{
QString className = Serializable::classNameCleanToReal(paintElem.attribute(ProjectLabels::CLASS_NAME));
int id = paintElem.attribute(ProjectLabels::ID, QString::number(NULL_UID)).toInt();
qDebug() << "Found paint with classname: " << className << endl;
const QMetaObject* metaObject = MetaObjectRegistry::instance().getMetaObject(className);
if (metaObject)
{
// Create new instance.
Paint::ptr paint (qobject_cast<Paint*>(metaObject->newInstance( Q_ARG(int, id)) ));
if (paint.isNull())
{
qDebug() << QObject::tr("Problem at creation of paint.") << endl;
// _xml.raiseError(QObject::tr("Problem at creation of paint."));
}
else
qDebug() << "Created new instance with id: " << paint->getId();
paint->read(paintElem);
return paint;
}
else
{
_xml.raiseError(QObject::tr("Unable to create paint of type '%1'.").arg(className));
return Paint::ptr();
}
}
Mapping::ptr ProjectReader::parseMapping(const QDomElement& mappingElem)
{
// Get attributes.
QString className = Serializable::classNameCleanToReal(mappingElem.attribute(ProjectLabels::CLASS_NAME));
int id = mappingElem.attribute(ProjectLabels::ID, QString::number(NULL_UID)).toInt();
const QMetaObject* metaObject = MetaObjectRegistry::instance().getMetaObject(className);
if (metaObject)
{
// Create new instance.
Mapping::ptr mapping (qobject_cast<Mapping*>(metaObject->newInstance( Q_ARG(int, id)) ));
if (mapping.isNull())
{
qDebug() << QObject::tr("Problem at creation of mapping.") << endl;
// _xml.raiseError(QObject::tr("Problem at creation of paint."));
}
mapping->read(mappingElem);
return mapping;
}
else
{
_xml.raiseError(QObject::tr("Unable to create paint of type '%1'.").arg(className));
return Mapping::ptr();
}
}
}