From e91fe5d862c6ebdec6af58e77a1bf33521f65ab5 Mon Sep 17 00:00:00 2001 From: Tats Date: Thu, 23 Jan 2014 22:01:21 -0500 Subject: [PATCH] Statesaving of color paints / mappings --- MainWindow.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++ MainWindow.h | 14 +++++++++++ ProjectReader.cpp | 55 +++++++++++++++++++++++++++++++++++++------ ProjectReader.h | 2 ++ ProjectWriter.cpp | 12 ++++++++++ 5 files changed, 135 insertions(+), 7 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index 0829f65..4df2814 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -455,6 +455,65 @@ uid MainWindow::createTriangleTextureMapping(uid mappingId, } } +uid MainWindow::createQuadColorMapping(uid mappingId, + uid paintId, + const QList &dst) +{ + // Cannot create element with already existing id or element for which no paint exists. + if (Mapping::getUidAllocator().exists(mappingId) || + !Paint::getUidAllocator().exists(paintId) || + paintId == NULL_UID) + return NULL_UID; + + else + { + Paint::ptr paint = mappingManager->getPaintById(paintId); + Q_ASSERT(dst.size() == 4); + + Shape::ptr outputQuad(new Quad(dst[0], dst[1], dst[2], dst[3])); + + // Add it to the manager. + Mapping::ptr mapping(new ColorMapping(paint, outputQuad, mappingId)); + uid id = mappingManager->addMapping(mapping); + + // Add it to the GUI. + addMappingItem(mappingId); + + // Return the id. + return id; + } +} + +uid MainWindow::createTriangleColorMapping(uid mappingId, + uid paintId, + const QList &dst) +{ + // Cannot create element with already existing id or element for which no paint exists. + if (Mapping::getUidAllocator().exists(mappingId) || + !Paint::getUidAllocator().exists(paintId) || + paintId == NULL_UID) + return NULL_UID; + + else + { + Paint::ptr paint = mappingManager->getPaintById(paintId); + Q_ASSERT(dst.size() == 3); + + Shape::ptr outputQuad(new Triangle(dst[0], dst[1], dst[2])); + + // Add it to the manager. + Mapping::ptr mapping(new ColorMapping(paint, outputQuad, mappingId)); + uid id = mappingManager->addMapping(mapping); + + // Add it to the GUI. + addMappingItem(mappingId); + + // Return the id. + return id; + } +} + + void MainWindow::windowModified() { setWindowModified(true); diff --git a/MainWindow.h b/MainWindow.h index 71d2a0f..9ce23f7 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -114,6 +114,20 @@ public slots: const QList &src, const QList &dst); + /** + * Creates a color quad. + */ + uid createQuadColorMapping(uid mappingId, + uid paintId, + const QList &dst); + + /** + * Creates a color triangle. + */ + uid createTriangleColorMapping(uid mappingId, + uid paintId, + const QList &dst); + private: // Methods. void createLayout(); diff --git a/ProjectReader.cpp b/ProjectReader.cpp index a9a71c5..d8cbbb1 100644 --- a/ProjectReader.cpp +++ b/ProjectReader.cpp @@ -98,6 +98,16 @@ void ProjectReader::parsePaint(const QDomElement& paint) if (id == NULL_UID) _xml.raiseError(QObject::tr("Cannot create image with uri %1.").arg(uri)); } + else if (paintAttrType == "color") + { + QString rgb = paint.firstChildElement("rgb").text(); + QColor color(rgb); + + uid id = _window->createColorPaint(paintAttrId.toInt(), color); + if (id == NULL_UID) + _xml.raiseError(QObject::tr("Cannot create color with RGB hex code %1.").arg(rgb)); + + } else _xml.raiseError(QObject::tr("Unsupported paint type: %1.").arg(paintAttrType)); @@ -146,30 +156,61 @@ void ProjectReader::parseMapping(const QDomElement& mapping) _xml.raiseError(QObject::tr("Cannot create mesh texture mapping")); } + else if (mappingAttrType == "triangle_color") + { + // Parse destination triangle. + _parseTriangle(dst, dstPoints); + + uid id = _window->createTriangleColorMapping(mappingAttrId.toInt(), mappingAttrPaintId.toInt(), dstPoints); + + if (id == NULL_UID) + _xml.raiseError(QObject::tr("Cannot create triangle color mapping")); + } + else if (mappingAttrType == "quad_color") + { + // Parse destination triangle. + _parseQuad(dst, dstPoints); + + uid id = _window->createQuadColorMapping(mappingAttrId.toInt(), mappingAttrPaintId.toInt(), dstPoints); + + if (id == NULL_UID) + _xml.raiseError(QObject::tr("Cannot create quad color mapping")); + } else _xml.raiseError(QObject::tr("Unsupported mapping type: %1.").arg(mappingAttrType)); } -void ProjectReader::_parseTriangle(const QDomElement& triangle, QList& points) +void ProjectReader::_parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QList& points) { // Check that the element is really a triangle. - QString type = triangle.attribute("shape", ""); - if (type != "triangle") - _xml.raiseError(QObject::tr("Wrong shape type for destination: %1.").arg(type)); + QString typeAttr = shape.attribute("shape", ""); + if (typeAttr != type) + _xml.raiseError(QObject::tr("Wrong shape type \"%1\" for destination: expected \"%2\".").arg(typeAttr).arg(type)); // Reset list of points. points.clear(); // Add vertices. - QDomElement vertex = triangle.firstChildElement("vertex"); + QDomElement vertex = shape.firstChildElement("vertex"); while (!vertex.isNull()) { points.push_back(_parseVertex(vertex)); vertex = vertex.nextSiblingElement("vertex"); } - if (points.size() != 3) - _xml.raiseError(QObject::tr("Shape has wrong number of vertices.")); + if (points.size() != nVertices) + _xml.raiseError(QObject::tr("Shape has %1 vertices: expected %2.").arg(points.size()).arg(nVertices)); +} + +void ProjectReader::_parseQuad(const QDomElement& quad, QList& points) +{ + _parseStandardShape("quad", 4, quad, points); +} + + +void ProjectReader::_parseTriangle(const QDomElement& triangle, QList& points) +{ + _parseStandardShape("triangle", 3, triangle, points); } void ProjectReader::_parseMesh(const QDomElement& mesh, QList& points, int& nColumns, int& nRows) diff --git a/ProjectReader.h b/ProjectReader.h index 2983191..d42a762 100644 --- a/ProjectReader.h +++ b/ProjectReader.h @@ -37,6 +37,8 @@ private: void parsePaint(const QDomElement& paint); void parseMapping(const QDomElement& mapping); + void _parseStandardShape(const QString& type, int nVertices, const QDomElement& shape, QList& points); + void _parseQuad(const QDomElement& quad, QList& points); void _parseTriangle(const QDomElement& triangle, QList& points); void _parseMesh(const QDomElement& mesh, QList& points, int& nColumns, int& nRows); QPointF _parseVertex(const QDomElement& vertex); diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp index a6ef499..742f872 100644 --- a/ProjectWriter.cpp +++ b/ProjectWriter.cpp @@ -78,6 +78,14 @@ void ProjectWriter::writeItem(Paint *item) _xml.writeEndElement(); //_xml.writeEmptyElement("hello"); } + else if (item->getType() == "color") + { + // FIXME: check paint type before casting to Image + Color *color = (Color *) item; + + _xml.writeTextElement("rgb", color->getColor().name()); + _xml.writeEndElement(); + } else qDebug() << "Unknown type, cannot save: " << item->getType() << endl; } @@ -128,6 +136,10 @@ void ProjectWriter::writeItem(Mapping *item) _xml.writeEndElement(); // mapping } + else if (item->getType().endsWith("_color")) + { + _xml.writeEndElement(); // mapping + } else qDebug() << "Unknown type, cannot save: " << item->getType() << endl; }