From cf11335c1d2e0f6f0d602fac903a881097e30abe Mon Sep 17 00:00:00 2001 From: Tats Date: Thu, 28 Jan 2016 12:28:19 -0500 Subject: [PATCH] Implemented parsing of mappings and shapes. --- ProjectReader.cpp | 66 ++++++++++++++++++++++++++++++++++++----------- ProjectReader.h | 2 +- ProjectWriter.cpp | 13 ++++++---- ProjectWriter.h | 3 +++ Shape.cpp | 15 +++++++++++ 5 files changed, 78 insertions(+), 21 deletions(-) diff --git a/ProjectReader.cpp b/ProjectReader.cpp index 4987538..dcbf764 100644 --- a/ProjectReader.cpp +++ b/ProjectReader.cpp @@ -89,21 +89,23 @@ void ProjectReader::parseProject(const QDomElement& project) 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 -// { -// } -// -// mappingNode = mappingNode.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) @@ -137,6 +139,40 @@ Paint::ptr ProjectReader::parsePaint(const QDomElement& paintElem) return Paint::ptr(); } } + +Mapping::ptr ProjectReader::parseMapping(const QDomElement& mappingElem) +{ + // Get attributes. + QString className = mappingElem.attribute(ProjectAttributes::CLASS_NAME); + int id = mappingElem.attribute(ProjectAttributes::ID, QString::number(NULL_UID)).toInt(); + + qDebug() << "Found mapping with classname: " << className << endl; + + const QMetaObject* metaObject = MetaObjectRegistry::instance().getMetaObject(className); + if (metaObject) + { + // Create new instance. + Mapping::ptr mapping (qobject_cast(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.")); + } + else + qDebug() << "Created new instance with id: " << mapping->getId(); + + mapping->read(mappingElem); + + return mapping; + } + + else + { + _xml.raiseError(QObject::tr("Unable to create paint of type '%1'.").arg(className)); + return Mapping::ptr(); + } +} + // //Mapping::ptr ProjectReader::parseMapping(const QDomElement& mappingElem) //{ diff --git a/ProjectReader.h b/ProjectReader.h index c6d5cb5..aae2dc8 100644 --- a/ProjectReader.h +++ b/ProjectReader.h @@ -39,7 +39,7 @@ private: void readProject(); void parseProject(const QDomElement& project); Paint::ptr parsePaint(const QDomElement& paint); -// Mapping::ptr parseMapping(const QDomElement& mapping); + Mapping::ptr parseMapping(const QDomElement& mapping); // // void _parseStandardShape(const QString& type, const QDomElement& shape, QVector& points, int nVertices=-1); // void _parseQuad(const QDomElement& quad, QVector& points); diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp index 398cdfe..fac1795 100644 --- a/ProjectWriter.cpp +++ b/ProjectWriter.cpp @@ -20,11 +20,14 @@ #include "ProjectWriter.h" #include -const char* ProjectAttributes::CLASS_NAME = "className"; -const char* ProjectAttributes::PAINTS = "paints"; -const char* ProjectAttributes::MAPPINGS = "mappings"; -const char* ProjectAttributes::ID = "id"; -const char* ProjectAttributes::NAME = "name"; +const char* ProjectAttributes::CLASS_NAME = "className"; +const char* ProjectAttributes::PAINTS = "paints"; +const char* ProjectAttributes::MAPPINGS = "mappings"; +const char* ProjectAttributes::ID = "id"; +const char* ProjectAttributes::PAINT_ID = "paintId"; +const char* ProjectAttributes::NAME = "name"; +const char* ProjectAttributes::SOURCE = "id"; +const char* ProjectAttributes::DESTINATION = "name"; ProjectWriter::ProjectWriter(MainWindow *window) : _window(window) diff --git a/ProjectWriter.h b/ProjectWriter.h index da6c973..5bb4ef5 100644 --- a/ProjectWriter.h +++ b/ProjectWriter.h @@ -38,6 +38,9 @@ public: static const char* ID; static const char* NAME; static const char* PAINT_ID; + + static const char* DESTINATION; + static const char* SOURCE; }; class ProjectWriter diff --git a/Shape.cpp b/Shape.cpp index 0f6841e..ed03fbb 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -42,6 +42,21 @@ void MShape::read(const QDomElement& obj) { // Read basic data. Serializable::read(obj); + + // Read vertices. + QDomElement verticesObj = obj.firstChildElement("vertices"); + QDomNode vertexNode = verticesObj.firstChild(); + QVector vertices; + while (!vertexNode.isNull()) + { + const QDomElement& vertexElem = vertexNode.toElement(); + qreal x = vertexElem.attribute("x").toDouble(); + qreal y = vertexElem.attribute("y").toDouble(); + vertices.append(QPointF(x, y)); + + vertexNode = vertexNode.nextSibling(); + } + setVertices(vertices); } void MShape::write(QDomElement& obj)