diff --git a/Mapping.cpp b/Mapping.cpp index 1701421..1198c24 100644 --- a/Mapping.cpp +++ b/Mapping.cpp @@ -19,6 +19,7 @@ */ #include "Mapping.h" +#include "MainWindow.h" UidAllocator Mapping::allocator; @@ -35,3 +36,90 @@ Mapping::~Mapping() { allocator.free(getId()); } +void Mapping::read(const QDomElement& obj) +{ + // Read basic data. + Element::read(obj); + + // Read paint. + int paintId = obj.attribute("paintId").toInt(); + setPaint(MainWindow::instance()->getMappingManager().getPaintById(paintId)); + + // Read output shape. + _readShape(obj, true); + + // Read input shape. + if (hasInputShape()) + { + _readShape(obj, false); + } + +} + +void Mapping::write(QDomElement& obj) +{ + // Write basic data. + Element::write(obj); + + // Write paint ID. + obj.setAttribute("paintId", getPaint()->getId()); + + // Write output shape. + _writeShape(obj, true); + + // Write input shape. + if (hasInputShape()) + { + _writeShape(obj, false); + } +} + +void Mapping::_readShape(const QDomElement& obj, bool isOutput) +{ + QString tag = isOutput ? "output" : "input"; + + QDomElement shapeObj = obj.firstChildElement(tag); + + QString className = shapeObj.attribute("className"); + + qDebug() << "Found shape with classname: " << className << endl; + + const QMetaObject* metaObject = MetaObjectRegistry::instance().getMetaObject(className); + if (metaObject) + { + // Create new instance. + MShape::ptr shape (qobject_cast(metaObject->newInstance())); + if (shape.isNull()) + { + qDebug() << QObject::tr("Problem at creation of shape.") << endl; +// _xml.raiseError(QObject::tr("Problem at creation of paint.")); + } + else + qDebug() << "Created new shape" << endl; + + // Read shape. + shape->read(shapeObj); + + // Set shape. + if (isOutput) + setShape(shape); + else + qDebug() << "Shit!!!!" << endl; + + } + + else + { + qDebug() << QObject::tr("Unable to create paint of type '%1'.").arg(className) << endl; + } + +} + +void Mapping::_writeShape(QDomElement& obj, bool isOutput) +{ + QString tag = isOutput ? "output" : "input"; + MShape::ptr shape = isOutput ? getShape() : getInputShape(); + QDomElement shapeObj = obj.ownerDocument().createElement(tag); + shape->write(shapeObj); + obj.appendChild(shapeObj); +} diff --git a/Mapping.h b/Mapping.h index 85f6b8c..499b02d 100644 --- a/Mapping.h +++ b/Mapping.h @@ -30,6 +30,11 @@ #include "UidAllocator.h" +#include "MetaObjectRegistry.h" + +// TODO: replace by ProjectAttribute +//#include "ProjectWriter.h" + /** * Mapping is the central concept of this software. * @@ -119,6 +124,17 @@ public: float getComputedOpacity() const { return getOpacity() * _paint->getOpacity(); } void setPaint(Paint::ptr p) { _paint = p; } + void setShape(MShape::ptr s) { _shape = s; } + + virtual void read(const QDomElement& obj); + virtual void write(QDomElement& obj); + +protected: + virtual QList _propertiesAttributes() const + { return Element::_propertiesAttributes() << "solo" << "visible" << "depth"; } + + void _readShape(const QDomElement& obj, bool isOutput); + void _writeShape(QDomElement& obj, bool isOutput); }; /** @@ -128,14 +144,13 @@ class ColorMapping : public Mapping { Q_OBJECT public: - ColorMapping(Paint::ptr paint, MShape::ptr shape, + Q_INVOKABLE ColorMapping(Paint::ptr paint, MShape::ptr shape, uid id=NULL_UID) : Mapping(paint, shape, id) {} virtual QString getType() const { return getShape()->getType() + "_color"; } - }; /** @@ -149,6 +164,14 @@ private: MShape::ptr _inputShape; public: + Q_INVOKABLE TextureMapping( + Paint::ptr paint, + MShape::ptr shape, uid id=NULL_UID) + : Mapping(paint, shape, id), + _inputShape() + { + + } TextureMapping(Paint::ptr paint, MShape::ptr shape, MShape::ptr inputShape, uid id=NULL_UID) @@ -171,6 +194,7 @@ public: public: virtual bool hasInputShape() const { return true; } virtual MShape::ptr getInputShape() const { return _inputShape; } + }; #endif /* MAPPING_H_ */ diff --git a/Mesh.h b/Mesh.h index 742275f..17b11ca 100644 --- a/Mesh.h +++ b/Mesh.h @@ -27,13 +27,13 @@ class Mesh : public Quad { Q_OBJECT - Q_PROPERTY(int nColumns READ nColumns) - Q_PROPERTY(int nRows READ nRows) + Q_PROPERTY(int nColumns READ nColumns WRITE setNColumns) + Q_PROPERTY(int nRows READ nRows WRITE setNRows) typedef QVector > IndexVector2d; public: - Mesh(); + Q_INVOKABLE Mesh(); // This constructor creates a quad mesh (four corners) using the same order as for the quad // constructor (ie. clockwise). @@ -87,6 +87,13 @@ public: int nColumns() const { return _nColumns; } int nRows() const { return _nRows; } + void setNColumns(int nColumns_) { + resize(nColumns_, nRows()); + } + void setNRows(int nRows_) { + resize(nColumns(), nRows_); + } + int nHorizontalQuads() const { return _nColumns-1; } int nVerticalQuads() const { return _nRows-1; } diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp index 657c28c..398cdfe 100644 --- a/ProjectWriter.cpp +++ b/ProjectWriter.cpp @@ -39,8 +39,8 @@ bool ProjectWriter::writeFile(QIODevice *device) QDomElement project = doc.createElement("project"); project.setAttribute("version", MM::VERSION); + // Paints. QDomElement paints = doc.createElement("paints"); - for (int i=0; iwrite(mapping); + mappings.appendChild(mapping); + } + project.appendChild(paints); + project.appendChild(mappings); doc.appendChild(project); QTextStream out(device); diff --git a/ProjectWriter.h b/ProjectWriter.h index 028d29b..da6c973 100644 --- a/ProjectWriter.h +++ b/ProjectWriter.h @@ -47,7 +47,7 @@ public: bool writeFile (QIODevice *device); private: - void writeProject(); +// void writeProject(); // void writePaint(Paint::ptr paint); // void writeItem (Paint *item); // void writeItem (Mapping *item); diff --git a/Quad.h b/Quad.h index 89c0a40..eec1753 100644 --- a/Quad.h +++ b/Quad.h @@ -32,7 +32,7 @@ class Quad : public Polygon public: typedef QSharedPointer ptr; - Quad() {} + Q_INVOKABLE Quad() {} Quad(QPointF p1, QPointF p2, QPointF p3, QPointF p4) { _addVertex(p1); diff --git a/Shape.cpp b/Shape.cpp index 1e0ab57..0f6841e 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -38,3 +38,27 @@ void MShape::translate(const QPointF& offset) *it += offset; } +void MShape::read(const QDomElement& obj) +{ + // Read basic data. + Serializable::read(obj); +} + +void MShape::write(QDomElement& obj) +{ + // Write basic data. + Serializable::write(obj); + + // Write vertices. + QDomElement verticesObj = obj.ownerDocument().createElement("vertices"); + for (int i=0; i #include "Maths.h" +#include "Serializable.h" /** * Shape represented by a series of control points. */ -class MShape : public QObject +class MShape : public Serializable { Q_OBJECT - Q_PROPERTY(QVector vertices READ getVertices WRITE setVertices) + Q_PROPERTY(QVector vertices READ getVertices WRITE setVertices STORED false) public: typedef QSharedPointer ptr; @@ -101,6 +102,9 @@ public: vertices = vertices_; } + virtual void read(const QDomElement& obj); + virtual void write(QDomElement& obj); + protected: QVector vertices; diff --git a/Triangle.h b/Triangle.h index 2dc6aab..38d3d4e 100644 --- a/Triangle.h +++ b/Triangle.h @@ -30,7 +30,7 @@ class Triangle : public Polygon { Q_OBJECT public: - Triangle() {} + Q_INVOKABLE Triangle() {} Triangle(QPointF p1, QPointF p2, QPointF p3) { _addVertex(p1);