From 8dc7177ae90a5c8233ba3ea6da8994c79adbdc2e Mon Sep 17 00:00:00 2001 From: Tats Date: Thu, 28 Jan 2016 21:52:47 -0500 Subject: [PATCH] Read/write of texture mappings for triangle and ellipses works. --- Ellipse.h | 8 ++++++-- Mapping.cpp | 1 + Mapping.h | 6 ++++++ Mesh.cpp | 21 +++++++++++++-------- Mesh.h | 2 ++ Quad.h | 1 + Serializable.cpp | 20 ++++++++++++++++---- Serializable.h | 3 +++ Shape.cpp | 12 +++++++++++- Shape.h | 19 +++++++++++++------ Triangle.h | 1 + 11 files changed, 73 insertions(+), 21 deletions(-) diff --git a/Ellipse.h b/Ellipse.h index 4a48eb4..23fbcb3 100644 --- a/Ellipse.h +++ b/Ellipse.h @@ -36,7 +36,7 @@ public: _addVertex(p3); _addVertex(p4); _addVertex(p5); - sanitize(); + build(); } Ellipse(QPointF p1, QPointF p2, QPointF p3, QPointF p4, bool hasCenterControl=true) @@ -47,7 +47,7 @@ public: _addVertex(p4); if (hasCenterControl) _addVertex(getCenter()); - sanitize(); + build(); } virtual ~Ellipse() {} @@ -56,6 +56,10 @@ public: /// reference for the horizzontal axis. void sanitize(); + virtual void build() { + sanitize(); + } + virtual QString getType() const { return "ellipse"; } qreal getRotationRadians() const diff --git a/Mapping.cpp b/Mapping.cpp index ff9bf49..8d406e2 100644 --- a/Mapping.cpp +++ b/Mapping.cpp @@ -113,6 +113,7 @@ void Mapping::_readShape(const QDomElement& obj, bool isOutput) if (isOutput) setShape(shape); else + setInputShape(shape); qDebug() << "Shit!!!!" << endl; } diff --git a/Mapping.h b/Mapping.h index 2add6ee..30f2904 100644 --- a/Mapping.h +++ b/Mapping.h @@ -159,6 +159,9 @@ public: uid id=NULL_UID) : Mapping(paint, shape, id) {} + /// Returns true iff the mapping possesses an input (source) shape. + virtual bool hasInputShape() const { return false; } + virtual QString getType() const { return getShape()->getType() + "_color"; } @@ -184,6 +187,9 @@ public: Q_ASSERT(shape->getType() == inputShape->getType()); } + /// Returns true iff the mapping possesses an input (source) shape. + virtual bool hasInputShape() const { return true; } + virtual QString getType() const { return getShape()->getType() + "_texture"; } diff --git a/Mesh.cpp b/Mesh.cpp index eccfe8a..bd87b94 100644 --- a/Mesh.cpp +++ b/Mesh.cpp @@ -39,14 +39,8 @@ Mesh::Mesh(const QVector& points, int nColumns, int nRows) : Quad() init(points, nColumns, nRows); } -void Mesh::init(const QVector& points, int nColumns, int nRows) +void Mesh::build() { - Q_ASSERT(nColumns >= 2 && nRows >= 2); - Q_ASSERT(points.size() == nColumns * nRows); - - _nColumns = nColumns; - _nRows = nRows; - // Resize the vertices2d vector to appropriate dimensions. resizeVertices2d(_vertices2d, _nColumns, _nRows); @@ -55,12 +49,23 @@ void Mesh::init(const QVector& points, int nColumns, int nRows) for (int y=0; y<_nRows; y++) for (int x=0; x<_nColumns; x++) { - vertices.push_back( points[k] ); _vertices2d[x][y] = k; k++; } } +void Mesh::init(const QVector& points, int nColumns, int nRows) +{ + Q_ASSERT(nColumns >= 2 && nRows >= 2); + Q_ASSERT(points.size() == nColumns * nRows); + + _nColumns = nColumns; + _nRows = nRows; + + setVertices(points); + build(); +} + QPolygonF Mesh::toPolygon() const { QPolygonF polygon; diff --git a/Mesh.h b/Mesh.h index 17b11ca..9ef841e 100644 --- a/Mesh.h +++ b/Mesh.h @@ -47,6 +47,8 @@ public: // Performs the actual adding of points (used for loading). void init(const QVector& points, int nColumns, int nRows); + virtual void build(); + virtual QString getType() const { return "mesh"; } /// Returns a polygon that is formed by all the contour points of the mesh. diff --git a/Quad.h b/Quad.h index eec1753..d983013 100644 --- a/Quad.h +++ b/Quad.h @@ -39,6 +39,7 @@ public: _addVertex(p2); _addVertex(p3); _addVertex(p4); + build(); } virtual ~Quad() {} diff --git a/Serializable.cpp b/Serializable.cpp index 0186133..16d2bb7 100644 --- a/Serializable.cpp +++ b/Serializable.cpp @@ -22,6 +22,7 @@ void Serializable::read(const QDomElement& obj) { QList attributeNames = _propertiesAttributes(); + QList specialNames = _propertiesSpecial(); // Fill up properties. int count = metaObject()->propertyCount(); @@ -29,11 +30,16 @@ void Serializable::read(const QDomElement& obj) // Get property/tag. QMetaProperty property = metaObject()->property(i); + // Name of property. + const char* propertyName = property.name(); + + // Don't try to write special properties (leave it to children). + if (specialNames.contains(propertyName)) + continue; + // If property is writable, try to find it and rewrite it. if (property.isWritable()) { - // Name of property. - const char* propertyName = property.name(); // Always ignore objectName default property. if (QString(propertyName) == QString("objectName")) @@ -64,6 +70,7 @@ void Serializable::read(const QDomElement& obj) void Serializable::write(QDomElement& obj) { QList attributeNames = _propertiesAttributes(); + QList specialNames = _propertiesSpecial(); // Write up classname. obj.setAttribute("className", metaObject()->className()); @@ -74,6 +81,13 @@ void Serializable::write(QDomElement& obj) // Get property/tag. QMetaProperty property = metaObject()->property(i); + // Name of property. + const char* propertyName = property.name(); + + // Don't try to write special properties (leave it to children). + if (specialNames.contains(propertyName)) + continue; + // Don't save unstored properties. if (!property.isStored(this)) continue; @@ -81,8 +95,6 @@ void Serializable::write(QDomElement& obj) // If property is writable, try to find it and rewrite it. if (property.isWritable() && property.isReadable()) { - // Name of property. - const char* propertyName = property.name(); qDebug() << "Read " << propertyName << " : " << property.read(this) << endl; QString propertyValue = property.read(this).toString(); diff --git a/Serializable.h b/Serializable.h index 36e948a..bc5523f 100644 --- a/Serializable.h +++ b/Serializable.h @@ -40,6 +40,9 @@ public: protected: // Lists QProperties that should be represented as XML attributes, not as childen nodes. virtual QList _propertiesAttributes() const { return QList(); } + + // Lists QProperties that should NOT be parsed automatically. + virtual QList _propertiesSpecial() const { return QList(); } }; #endif /* SERIALIZABLE_H_ */ diff --git a/Shape.cpp b/Shape.cpp index 1ef0e5d..2947ffd 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -19,10 +19,15 @@ #include "Shape.h" +MShape::MShape(const QVector& vertices_) { + setVertices(vertices_); + build(); +} + void MShape::copyFrom(const MShape& shape) { // Just copy vertices. - vertices = shape.vertices; + setVertices(shape.getVertices()); } MShape* MShape::clone() const { @@ -56,7 +61,12 @@ void MShape::read(const QDomElement& obj) vertexNode = vertexNode.nextSibling(); } + + // Set the vertices. setVertices(vertices); + + // Rebuild. + build(); } void MShape::write(QDomElement& obj) diff --git a/Shape.h b/Shape.h index dd8c29e..889c1a3 100644 --- a/Shape.h +++ b/Shape.h @@ -53,11 +53,13 @@ public: typedef QSharedPointer ptr; MShape() {} - MShape(QVector vertices_) : - vertices(vertices_) - {} + MShape(const QVector& vertices_); virtual ~MShape() {} + /** + * This method should be called after vertices and other properties have been set + * to compute any other information needed by the object and possibly do some sanitizing. + */ virtual void build() {} int nVertices() const { return vertices.size(); } @@ -96,10 +98,12 @@ public: virtual MShape* clone() const; - QVector getVertices() const { return vertices; } - virtual void setVertices(QVector vertices_) + const QVector& getVertices() const { return vertices; } + virtual void setVertices(const QVector& vertices_) { - vertices = vertices_; + // Deep copy. + vertices.resize(vertices_.size()); + qCopy(vertices_.begin(), vertices_.end(), vertices.begin()); } virtual void read(const QDomElement& obj); @@ -120,6 +124,9 @@ protected: /// Returns a new MShape (using default constructor). virtual MShape* _create() const = 0; + + // Lists QProperties that should NOT be parsed automatically. + virtual QList _propertiesSpecial() const { return Serializable::_propertiesSpecial() << "vertices"; } }; diff --git a/Triangle.h b/Triangle.h index 38d3d4e..e010d4f 100644 --- a/Triangle.h +++ b/Triangle.h @@ -36,6 +36,7 @@ public: _addVertex(p1); _addVertex(p2); _addVertex(p3); + build(); } virtual ~Triangle() {} virtual QString getType() const { return "triangle"; }