From b6a9b533fc60ef68b483ff40190a83af367d7acf Mon Sep 17 00:00:00 2001 From: bhattigurjot Date: Thu, 8 Jan 2015 15:50:03 +0530 Subject: [PATCH] corrects the ellipse rotation for paste --- gui/cadscriptwidget.cpp | 2 +- gui/entities/circle.cpp | 8 +++---- gui/entities/ellipse.cpp | 48 ++++++++++++++++++++++++++++------------ gui/entities/ellipse.h | 1 + 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/gui/cadscriptwidget.cpp b/gui/cadscriptwidget.cpp index 4fd3cbe..318bb3a 100644 --- a/gui/cadscriptwidget.cpp +++ b/gui/cadscriptwidget.cpp @@ -228,7 +228,7 @@ void CadScriptWidget::ellipse(qreal x, qreal y, qreal minR, qreal majR, qreal angle) { // creates an ellipse entity in the scene - ellipseItem = new Ellipse(++id, QPointF(x,y), minR, majR, angle); + ellipseItem = new Ellipse(++id, QPointF(x,y), minR, majR, -angle); currentScene->drawEntity(ellipseItem); } diff --git a/gui/entities/circle.cpp b/gui/entities/circle.cpp index a67d81c..53d150b 100644 --- a/gui/entities/circle.cpp +++ b/gui/entities/circle.cpp @@ -5,7 +5,7 @@ Circle::Circle(QPointF p1, QPointF p2) /** * set values of center point, end point * and calculate radius of circle - */ + */ centerP = p1; endP = p2; radius = qSqrt(qPow((endP.x()-centerP.x()), 2) @@ -20,7 +20,7 @@ Circle::Circle(int i, QPointF p1, QPointF p2) /** * set values of center point, end point * and calculate radius of circle - */ + */ centerP = p1; endP = p2; radius = qSqrt(qPow((endP.x()-centerP.x()), 2) @@ -35,7 +35,7 @@ Circle::Circle(int i, QPointF p1, qreal rad) /** * set values of center point * and radius of circle - */ + */ centerP = p1; radius = rad; } @@ -103,8 +103,6 @@ getEntity *Circle::clone(int i) { Circle *c = new Circle; c->id = i; - c->centerP.x(); - c->centerP.y(); c->radius = radius; return c; } diff --git a/gui/entities/ellipse.cpp b/gui/entities/ellipse.cpp index df6fdc1..f3a6474 100644 --- a/gui/entities/ellipse.cpp +++ b/gui/entities/ellipse.cpp @@ -2,9 +2,10 @@ Ellipse::Ellipse(QPointF point1, QPointF point2, QPointF point3) { - /** set values of three points + /** + * set values of three points * and calculate radii of ellipse - */ + */ p1 = point1; p2 = point2; p3 = point3; @@ -17,9 +18,10 @@ Ellipse::Ellipse(int i, QPointF point1, QPointF point2, QPointF point3) // assigns id id = i; - /** set values of three points + /** + * set values of three points * and calculate radii of ellipse - */ + */ p1 = point1; p2 = point2; p3 = point3; @@ -35,11 +37,17 @@ Ellipse::Ellipse(int i, QPointF point1, qreal rad, qreal radM, qreal angle) /** * set values of center point * and radii of ellipse - */ + */ p1 = point1; minRadius = rad; majRadius = radM; theta = angle; + + /** + * This is set to false to overcome the problem of rotation after pasting + * or if the ellipse is drawn from script widget. + */ + setOrientation(false); } void Ellipse::calculate() @@ -60,13 +68,23 @@ void Ellipse::calculate() theta = atan2((p3.y() - p1.y()), (p3.x() - p1.x())) * (180 / M_PI); } + // This is set to true to rotate the ellipse + setOrientation(true); +} + +void Ellipse::setOrientation(bool b) +{ topLeft.setX(p1.x() - majRadius); topLeft.setY(p1.y() - minRadius); bottomRight.setX(p1.x() + majRadius); bottomRight.setY(p1.y() + minRadius); setTransformOriginPoint(p1); - setRotation(theta); + + if (b) + setRotation(theta); + else + setRotation(-theta); } int Ellipse::type() const @@ -88,8 +106,16 @@ QPainterPath Ellipse::shape() const QRectF Ellipse::boundingRect() const { // bounding rectangle for ellipse - return QRectF(p1.x() - majRadius, p1.y() - majRadius, - majRadius * 2, majRadius * 2); + qreal rad; + qreal extra = 1.0; + + if (majRadius >= minRadius) + rad = majRadius; + else + rad = minRadius; + + return QRectF(p1.x() - rad, p1.y() - rad, rad * 2, rad * 2) + .adjusted(-extra, -extra, extra, extra); } void Ellipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, @@ -132,12 +158,6 @@ getEntity *Ellipse::clone(int i) { Ellipse *e = new Ellipse; e->id = i; - e->p1.x(); - e->p1.y(); - e->p2.x(); - e->p2.y(); - e->p3.x(); - e->p3.y(); e->theta = theta; e->minRadius = minRadius; e->majRadius = majRadius; diff --git a/gui/entities/ellipse.h b/gui/entities/ellipse.h index 9ba31ec..496d71c 100644 --- a/gui/entities/ellipse.h +++ b/gui/entities/ellipse.h @@ -26,6 +26,7 @@ class Ellipse : public getEntity int type() const; getEntity *clone(int); QPointF getCenter(); + void setOrientation(bool); int id; QPointF p1, p2, p3, topLeft, bottomRight;