diff options
| -rw-r--r-- | ellipse.cpp | 23 | ||||
| -rw-r--r-- | ellipse.h | 1 | ||||
| -rw-r--r-- | interactioncanvas.cpp | 9 | ||||
| -rw-r--r-- | line.cpp | 22 | ||||
| -rw-r--r-- | line.h | 1 | ||||
| -rw-r--r-- | polygon.cpp | 53 | ||||
| -rw-r--r-- | polygon.h | 3 | ||||
| -rw-r--r-- | rectangle.cpp | 55 | ||||
| -rw-r--r-- | rectangle.h | 4 |
9 files changed, 112 insertions, 59 deletions
diff --git a/ellipse.cpp b/ellipse.cpp index 0377b66..dc0f4f8 100644 --- a/ellipse.cpp +++ b/ellipse.cpp @@ -1,22 +1,11 @@ #include "ellipse.h" Ellipse::Ellipse() + :Rectangle() { } -bool Ellipse::isHit(const QPoint &clickPoint) -{ - QVector3D a(p2() - p1()); - QVector3D r1(p1()); - - QVector3D rq(clickPoint); - - float dist = rq.distanceToLine(r1, a.normalized()); - - return dist < 5; -} - void Ellipse::draw(QPainter *painter) { if (m_selected) @@ -25,10 +14,10 @@ void Ellipse::draw(QPainter *painter) penTemp.setColor(Qt::red); painter->setPen(penTemp); - painter->drawEllipse(QPoint(p1().rx() - (p2().rx() - p1().rx()), p1().ry()), 5, 5); - painter->drawEllipse(QPoint(p1().rx() + (p2().rx() - p1().rx()), p1().ry()), 5, 5); - painter->drawEllipse(QPoint(p1().rx(), p1().ry() + (p2().ry() - p1().ry())), 5, 5); - painter->drawEllipse(QPoint(p1().rx(), p1().ry() - (p2().ry() - p1().ry())), 5, 5); + 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 { @@ -37,5 +26,5 @@ void Ellipse::draw(QPainter *painter) painter->setPen(penNormal); } - painter->drawEllipse(p1(), p2().rx() - p1().rx(), p2().ry() - p1().ry()); + painter->drawEllipse(m_rectangle); } @@ -11,7 +11,6 @@ public: // Line interface public: - virtual bool isHit(const QPoint &clickPoint); virtual void draw(QPainter *painter); }; diff --git a/interactioncanvas.cpp b/interactioncanvas.cpp index 28b8e74..80a4317 100644 --- a/interactioncanvas.cpp +++ b/interactioncanvas.cpp @@ -42,8 +42,6 @@ void InteractionCanvas::addLine() void InteractionCanvas::addRectangle() { Line *tmpLine = new Rectangle(); - tmpLine->setP1(QPoint(10, 50)); - tmpLine->setP2(QPoint(30, 70)); addNewLine(tmpLine); m_NewLine = NULL; m_Lines.append(tmpLine); @@ -53,8 +51,6 @@ void InteractionCanvas::addRectangle() void InteractionCanvas::addEllipse() { Line *tmpLine = new Ellipse(); - tmpLine->setP1(QPoint(10, 50)); - tmpLine->setP2(QPoint(30, 70)); addNewLine(tmpLine); m_NewLine = NULL; m_Lines.append(tmpLine); @@ -64,8 +60,6 @@ void InteractionCanvas::addEllipse() void InteractionCanvas::addPolygon() { Line *tmpLine = new Polygon(); - tmpLine->setP1(QPoint(10, 50)); - tmpLine->setP2(QPoint(30, 70)); addNewLine(tmpLine); m_NewLine = NULL; m_Lines.append(tmpLine); @@ -80,8 +74,7 @@ void InteractionCanvas::mouseMoveEvent(QMouseEvent *mouseEvent) { if(m_SelectedLine) { - m_SelectedLine->setP1(m_SelectedLine->p1()+(mouseEvent->pos()-m_LastMousePosition)); - m_SelectedLine->setP2(m_SelectedLine->p2()+(mouseEvent->pos()-m_LastMousePosition)); + m_SelectedLine->move(m_LastMousePosition, mouseEvent->pos()); } if(m_NewLine) { @@ -44,3 +44,25 @@ void Line::draw(QPainter *painter) painter->drawLine(p1(), p2()); } + +void Line::move(const QPoint &oldPoint, const QPoint &newPoint) +{ + if(m_selected) + { + QPoint offset = newPoint - oldPoint; + QVector3D vecOld(oldPoint); + if(vecOld.distanceToPoint(QVector3D(p1())) < 5) + { + setP1(p1() + offset); + } + else if(vecOld.distanceToPoint(QVector3D(p2())) < 5) + { + setP2(p2() + offset); + } + else + { + setP1(p1() + offset); + setP2(p2() + offset); + } + } +} @@ -13,6 +13,7 @@ public: virtual bool isHit(const QPoint & clickPoint); void setSelected(bool selected); virtual void draw(QPainter * painter); + virtual void move(const QPoint & oldPoint, const QPoint & newPoint); protected: bool m_selected; diff --git a/polygon.cpp b/polygon.cpp index a292653..d8b71b7 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -3,21 +3,14 @@ Polygon::Polygon() : Line() { - m_polygon.append(QPoint(0,0)); - m_polygon.append(QPoint(0,0)); - m_polygon.append(QPoint(0,0)); + m_polygon.append(QPoint(10, 50)); + m_polygon.append(QPoint(30, 70)); + m_polygon.append(QPoint(50, 50)); } bool Polygon::isHit(const QPoint &clickPoint) { - QVector3D a(p2() - p1()); - QVector3D r1(p1()); - - QVector3D rq(clickPoint); - - float dist = rq.distanceToLine(r1, a.normalized()); - - return dist < 5; + return m_polygon.containsPoint(clickPoint, Qt::OddEvenFill); } void Polygon::draw(QPainter *painter) @@ -28,9 +21,9 @@ void Polygon::draw(QPainter *painter) penTemp.setColor(Qt::red); painter->setPen(penTemp); - painter->drawEllipse(p1(), 5, 5); - painter->drawEllipse(p2(), 5, 5); - painter->drawEllipse(QPoint(p1().rx() + 20, p1().ry() - 20), 5, 5); + 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 { @@ -39,9 +32,33 @@ void Polygon::draw(QPainter *painter) painter->setPen(penNormal); } - m_polygon.replace(0, p1()); - m_polygon.replace(1, p2()); - m_polygon.replace(2, QPoint(p1().rx() + 20, p1().ry() - 20)); - 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); + } + } +} @@ -14,8 +14,9 @@ public: public: virtual bool isHit(const QPoint &clickPoint); virtual void draw(QPainter *painter); + virtual void move(const QPoint &oldPoint, const QPoint &newPoint); -private: +protected: QPolygon m_polygon; }; diff --git a/rectangle.cpp b/rectangle.cpp index f07651c..1a58add 100644 --- a/rectangle.cpp +++ b/rectangle.cpp @@ -3,19 +3,15 @@ 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) { - QVector3D a(p2() - p1()); - QVector3D r1(p1()); - - QVector3D rq(clickPoint); - - float dist = rq.distanceToLine(r1, a.normalized()); - - return dist < 5; + return m_rectangle.contains(clickPoint); } void Rectangle::draw(QPainter *painter) @@ -26,10 +22,10 @@ void Rectangle::draw(QPainter *painter) penTemp.setColor(Qt::red); painter->setPen(penTemp); - painter->drawEllipse(p1(), 5, 5); - painter->drawEllipse(QPoint(p1().rx() + (p2().rx() - p1().rx()), p1().ry()), 5, 5); - painter->drawEllipse(QPoint(p1().rx(), p1().ry() + (p2().ry() - p1().ry())), 5, 5); - painter->drawEllipse(p2(), 5, 5); + 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 { @@ -38,5 +34,36 @@ void Rectangle::draw(QPainter *painter) painter->setPen(penNormal); } - painter->drawRect(QRect(p1(), p2())); + 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); + } + } } diff --git a/rectangle.h b/rectangle.h index 5e9445d..cf29bb4 100644 --- a/rectangle.h +++ b/rectangle.h @@ -12,6 +12,10 @@ public: public: virtual bool isHit(const QPoint &clickPoint); virtual void draw(QPainter *painter); + virtual void move(const QPoint &oldPoint, const QPoint &newPoint); + +protected: + QRect m_rectangle; }; #endif // RECTANGLE_H |
