diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-05-31 22:55:45 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-05-31 22:55:45 +0200 |
| commit | 45813562e266cf8e58ea9cb04f655bbb542a555e (patch) | |
| tree | c8864c50a1bd84ec8f2e8e527e71c9825fabf92a /rectangle.cpp | |
| parent | 16cdefa1a8546d23efc357010e62c7c031319e56 (diff) | |
| download | GUI_SS2015-45813562e266cf8e58ea9cb04f655bbb542a555e.tar.gz GUI_SS2015-45813562e266cf8e58ea9cb04f655bbb542a555e.zip | |
Make every line subclass editable
Diffstat (limited to 'rectangle.cpp')
| -rw-r--r-- | rectangle.cpp | 55 |
1 files changed, 41 insertions, 14 deletions
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); + } + } } |
