#include "polygon.h" Polygon::Polygon() : Line() { m_polygon.append(QPoint(10, 50)); m_polygon.append(QPoint(30, 70)); m_polygon.append(QPoint(50, 50)); } bool Polygon::isHit(const QPoint &clickPoint) { return m_polygon.containsPoint(clickPoint, Qt::OddEvenFill); } void Polygon::draw(QPainter *painter) { if (m_selected) { QPen penTemp(Qt::DotLine); penTemp.setColor(Qt::red); painter->setPen(penTemp); painter->drawEllipse(m_polygon.point(0), 5, 5); painter->drawEllipse(m_polygon.point(1), 5, 5); painter->drawEllipse(m_polygon.point(2), 5, 5); } else { QPen penNormal(Qt::SolidLine); penNormal.setColor(Qt::black); painter->setPen(penNormal); } painter->drawPolygon(m_polygon); } void Polygon::move(const QPoint &oldPoint, const QPoint &newPoint) { if (m_selected) { QPoint offset = newPoint - oldPoint; QVector3D vecOld(oldPoint); 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) { m_polygon.replace(1, m_polygon.point(1) + offset); } else if (vecOld.distanceToPoint(QVector3D(m_polygon.point(2))) < 5) { m_polygon.replace(2, m_polygon.point(2) + offset); } else { m_polygon.replace(0, m_polygon.point(0) + offset); m_polygon.replace(1, m_polygon.point(1) + offset); m_polygon.replace(2, m_polygon.point(2) + offset); } } } 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"; }