From d847e6bf7929c8565bff4e17d9fd077b8e0ca74d Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 8 Jun 2015 10:27:31 +0200 Subject: Add XML files for reading and writing --- polygon.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) (limited to 'polygon.cpp') diff --git a/polygon.cpp b/polygon.cpp index d8b71b7..c785e7b 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -38,19 +38,20 @@ void Polygon::draw(QPainter *painter) void Polygon::move(const QPoint &oldPoint, const QPoint &newPoint) { - if(m_selected) + if (m_selected) { QPoint offset = newPoint - oldPoint; QVector3D vecOld(oldPoint); - if(vecOld.distanceToPoint(QVector3D(m_polygon.point(0))) < 5) + + if (vecOld.distanceToPoint(QVector3D(m_polygon.point(0))) < 5) { m_polygon.replace(0, m_polygon.point(0) + offset); } - else if(vecOld.distanceToPoint(QVector3D(m_polygon.point(1))) < 5) + else if (vecOld.distanceToPoint(QVector3D(m_polygon.point(1))) < 5) { m_polygon.replace(1, m_polygon.point(1) + offset); } - else if(vecOld.distanceToPoint(QVector3D(m_polygon.point(2))) < 5) + else if (vecOld.distanceToPoint(QVector3D(m_polygon.point(2))) < 5) { m_polygon.replace(2, m_polygon.point(2) + offset); } @@ -62,3 +63,59 @@ void Polygon::move(const QPoint &oldPoint, const QPoint &newPoint) } } } + +bool Polygon::attributesToDom(QDomDocument *doc, QDomElement &e) +{ + if (e.isNull()) + { + qDebug() << className() << "::attributesToDom Error: NULL element passed."; + return false; + } + else + { + QDomElement tmp; + + foreach(QPoint point, m_polygon.toList()) + { + tmp = doc->createElement("Point"); + + tmp.setAttribute("x", point.rx()); + tmp.setAttribute("y", point.ry()); + + e.appendChild(tmp); + } + + return true; + } +} + +bool Polygon::attributesFromDom(const QDomElement &e) +{ + if (e.isNull()) + { + qDebug() << className() << "::attributesFromDom Error: NULL element passed."; + return false; + } + else + { + QDomElement child = e.firstChildElement("Point"); + + m_polygon.clear(); + + while(!child.isNull()) + { + QPoint tmp(child.attribute("x", "0").toInt(), child.attribute("y", "0").toInt()); + + m_polygon.append(tmp); + + child = child.nextSiblingElement("Point"); + } + + return true; + } +} + +QString Polygon::className() +{ + return "Polygon"; +} -- cgit v1.2.3-70-g09d2