#include "rectangle.h" Rectangle::Rectangle() : Line() { m_rectangle.setTopLeft(QPoint(10, 50)); m_rectangle.setTopRight(QPoint(30, 50)); m_rectangle.setBottomLeft(QPoint(10, 70)); m_rectangle.setBottomRight(QPoint(30, 70)); } bool Rectangle::isHit(const QPoint &clickPoint) { return m_rectangle.contains(clickPoint); } void Rectangle::draw(QPainter *painter) { if (m_selected) { QPen penTemp(Qt::DotLine); penTemp.setColor(Qt::red); painter->setPen(penTemp); painter->drawEllipse(m_rectangle.topLeft(), 5, 5); painter->drawEllipse(m_rectangle.topRight(), 5, 5); painter->drawEllipse(m_rectangle.bottomLeft(), 5, 5); painter->drawEllipse(m_rectangle.bottomRight(), 5, 5); } else { QPen penNormal(Qt::SolidLine); penNormal.setColor(Qt::black); painter->setPen(penNormal); } painter->drawRect(m_rectangle); } void Rectangle::move(const QPoint &oldPoint, const QPoint &newPoint) { if(m_selected) { QPoint offset = newPoint - oldPoint; QVector3D vecOld(oldPoint); if(vecOld.distanceToPoint(QVector3D(m_rectangle.topLeft())) < 5) { m_rectangle.setTopLeft(m_rectangle.topLeft() + offset); } else if(vecOld.distanceToPoint(QVector3D(m_rectangle.topRight())) < 5) { m_rectangle.setTopRight(m_rectangle.topRight() + offset); } else if(vecOld.distanceToPoint(QVector3D(m_rectangle.bottomLeft())) < 5) { m_rectangle.setBottomLeft(m_rectangle.bottomLeft() + offset); } else if(vecOld.distanceToPoint(QVector3D(m_rectangle.bottomRight())) < 5) { m_rectangle.setBottomRight(m_rectangle.bottomRight() + offset); } else { m_rectangle.setTopLeft(m_rectangle.topLeft() + offset); m_rectangle.setBottomRight(m_rectangle.bottomRight() + offset); } } }