]> Shamusworld >> Repos - architektonas/commitdiff
Fix DrawArcAction to actually allow creation of Arcs.
authorShamus Hammons <jlhamm@acm.org>
Wed, 14 Aug 2013 22:43:16 +0000 (17:43 -0500)
committerShamus Hammons <jlhamm@acm.org>
Wed, 14 Aug 2013 22:43:16 +0000 (17:43 -0500)
src/applicationwindow.cpp
src/drawarcaction.cpp
src/drawarcaction.h
src/drawingview.cpp
src/drawingview.h
src/painter.cpp
src/painter.h

index b812e4d8a05e70897b830587af40a456d826df10..c51000044ba82085b214772d0c2740d521b5349d 100644 (file)
 #include "about.h"
 #include "blockwidget.h"
 #include "drawingview.h"
+#include "drawarcaction.h"
+#include "drawcircleaction.h"
+#include "drawdimensionaction.h"
+#include "drawlineaction.h"
 #include "fileio.h"
 #include "generaltab.h"
 #include "layerwidget.h"
@@ -384,10 +388,19 @@ void ApplicationWindow::SetInternalToolStates(void)
                drawing->toolAction = NULL;
        }
 
+#if 0
        drawing->SetAddLineToolActive(addLineAct->isChecked());
        drawing->SetAddCircleToolActive(addCircleAct->isChecked());
        drawing->SetAddArcToolActive(addArcAct->isChecked());
        drawing->SetAddDimensionToolActive(addDimensionAct->isChecked());
+#else
+       drawing->SetToolActive(addLineAct->isChecked() ? new DrawLineAction() : NULL);
+       drawing->SetToolActive(addCircleAct->isChecked() ? new DrawCircleAction() : NULL);
+       drawing->SetToolActive(addArcAct->isChecked() ? new DrawArcAction() : NULL);
+       drawing->SetToolActive(addDimensionAct->isChecked() ? new DrawDimensionAction() : NULL);
+#endif
+
+       update();
 }
 
 
index 96db3098d2b59c82e0f8c18bf098708f00e7824b..624e4ee74a04a23f0e4b682d892f22eeef4e52e1 100644 (file)
 //
 
 #include "drawarcaction.h"
-#include "painter.h"
 #include "arc.h"
+#include "mathconstants.h"
+#include "painter.h"
 //#include "vector.h"
 
 
 enum { FIRST_POINT, SECOND_POINT, THIRD_POINT };
-//#define FIRST_POINT 0
-//#define SECOND_POINT 1
-#define NEXT_POINT 2
 
 
 DrawArcAction::DrawArcAction(): state(FIRST_POINT), arc(NULL)
@@ -38,14 +36,21 @@ DrawArcAction::~DrawArcAction()
        painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
 
        // I think stuff like crosshairs should be done in the DrawingView, tho
+       // (and it is! :-D)
        if (state == FIRST_POINT)
        {
                painter->DrawHandle(p1);
        }
-       else
+       else if (state == SECOND_POINT)
        {
-//             painter->DrawLine(p1, p2);
-//             painter->DrawHandle(p2);
+               painter->DrawLine(p1, p2);
+               painter->DrawHandle(p2);
+       }
+       else if (state == THIRD_POINT)
+       {
+               painter->DrawLine(p1, p2);
+               painter->DrawArc(p1, radius, startAngle, span);
+               painter->DrawHandle(p3);
        }
 }
 
@@ -54,8 +59,22 @@ DrawArcAction::~DrawArcAction()
 {
        if (state == FIRST_POINT)
                p1 = point;
-//     else
-//             p2 = point;
+       else if (state == SECOND_POINT)
+       {
+               p2 = point;
+               Vector r1(p2, p1);
+               radius = Vector::Magnitude(p1, p2);
+               startAngle = r1.Angle();
+       }
+       else if (state == THIRD_POINT)
+       {
+               p3 = point;
+               Vector r2(p3, p1);
+               span = r2.Angle() - startAngle;
+
+               if (span < 0)
+                       span += 2.0 * PI;
+       }
 }
 
 
@@ -63,8 +82,22 @@ DrawArcAction::~DrawArcAction()
 {
        if (state == FIRST_POINT)
                p1 = point;
-//     else
-//             p2 = point;
+       else if (state == SECOND_POINT)
+       {
+               p2 = point;
+               Vector r1(p2, p1);
+               radius = Vector::Magnitude(p1, p2);
+               startAngle = r1.Angle();
+       }
+       else if (state == THIRD_POINT)
+       {
+               p3 = point;
+               Vector r2(p3, p1);
+               span = r2.Angle() - startAngle;
+
+               if (span < 0)
+                       span += 2.0 * PI;
+       }
 }
 
 
@@ -72,17 +105,21 @@ DrawArcAction::~DrawArcAction()
 {
        if (state == FIRST_POINT)
        {
-//             p2 = p1;
-               state = NEXT_POINT;
+               state = SECOND_POINT;
+       }
+       else if (state == SECOND_POINT)
+       {
+               state = THIRD_POINT;
        }
-       else if (state == NEXT_POINT)
+       else if (state == THIRD_POINT)
        {
                // We create the new object here, and then pass it off to the
                // DrawingView which stuffs it into the document.
-//             text = new Text(p1, p2);
-//             arc = new Arc(...);
+               arc = new Arc(p1, radius, startAngle, span);
                // We don't need no stinkin' sentinels, when we have signals & slots!
                emit ObjectReady(arc);
+
+               state = FIRST_POINT;
        }
 }
 
index afb9f916917e854d27ed1ea08b75233784ea1143..f47d461ddbc55c30fd1731d2e80777c63ce9527d 100644 (file)
@@ -19,7 +19,8 @@ class DrawArcAction: public Action
        private:
                int state;
                Arc * arc;
-               Vector p1, p2;
+               Vector p1, p2, p3;
+               double radius, startAngle, span;
 };
 
 #endif // __DRAWARCACTION_H__
index 31aeaf028193e6fe9c43cb530a9a166d7c6befce..ab9f565fc2423bcb4f5a0c2b076eeaf63e64f500 100644 (file)
 #include "arc.h"
 #include "circle.h"
 #include "dimension.h"
-#include "drawarcaction.h"
-#include "drawcircleaction.h"
-#include "drawdimensionaction.h"
-#include "drawlineaction.h"
 #include "line.h"
 #include "painter.h"
 
@@ -129,22 +125,7 @@ need a thickness parameter similar to the "size" param for dimensions. (And now
 we do! :-)
 
 */
-#if 0
-       QPainter pmp(&gridBackground);
-       pmp.fillRect(0, 0, BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE, QColor(240, 240, 240));
-       pmp.setPen(QPen(QColor(210, 210, 255), 2.0, Qt::SolidLine));
-
-       for(int i=0; i<(BACKGROUND_MAX_SIZE-1); i+=12)
-       {
-               pmp.drawLine(i, 0, i, BACKGROUND_MAX_SIZE - 1);
-               pmp.drawLine(0, i, BACKGROUND_MAX_SIZE - 1, i);
-       }
-
-       pmp.end();
-       UpdateGridBackground();
-#else
        SetGridSize(12);
-#endif
 }
 
 
@@ -155,56 +136,14 @@ void DrawingView::SetRotateToolActive(bool state/*= true*/)
 }
 
 
-void DrawingView::SetAddLineToolActive(bool state/*= true*/)
+void DrawingView::SetToolActive(Action * action)
 {
-       if (state)
+       if (action != NULL)
        {
-               toolAction = new DrawLineAction();
+               toolAction = action;
                connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
                        SLOT(AddNewObjectToDocument(Object *)));
        }
-
-       update();
-//printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction);
-}
-
-
-void DrawingView::SetAddCircleToolActive(bool state/*= true*/)
-{
-       if (state)
-       {
-               toolAction = new DrawCircleAction();
-               connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
-                       SLOT(AddNewObjectToDocument(Object *)));
-       }
-
-       update();
-}
-
-
-void DrawingView::SetAddArcToolActive(bool state/*= true*/)
-{
-       if (state)
-       {
-               toolAction = new DrawArcAction();
-               connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
-                       SLOT(AddNewObjectToDocument(Object *)));
-       }
-
-       update();
-}
-
-
-void DrawingView::SetAddDimensionToolActive(bool state/*= true*/)
-{
-       if (state)
-       {
-               toolAction = new DrawDimensionAction();
-               connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
-                       SLOT(AddNewObjectToDocument(Object *)));
-       }
-
-       update();
 }
 
 
@@ -398,7 +337,12 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
        document.Draw(&painter);
 
        if (toolAction)
+       {
+//             painter.SetPen(QPen(Qt::green, 1.0, Qt::DashLine));
+               painter.SetPen(QPen(QColor(200, 100, 0, 255), 1.0, Qt::DashLine));
+               painter.DrawCrosshair(oldPoint);
                toolAction->Draw(&painter);
+       }
 
        if (Object::selectionInProgress)
        {
@@ -424,6 +368,8 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
        {
                Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
 
+// Problem with this: Can't select stuff very well with the snap grid on.
+// Completely screws things up.
                if (Object::snapToGrid)
                        point = SnapPointToGrid(point);
 
@@ -494,6 +440,7 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 #endif
        }
 #endif
+       oldPoint = point;
 //we should keep track of the last point here and only pass this down *if* the point
 //changed...
        document.PointerMoved(point);
@@ -503,6 +450,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 
        if (toolAction)
        {
+               if (Object::snapToGrid)
+               {
+                       point = SnapPointToGrid(point);
+                       oldPoint = point;
+               }
+
                toolAction->MouseMoved(point);
                update();
        }
index b296f28e64c887f2a8276d5a775cd76f178fe5f6..44420e9e08216fe2f325201d008e4e4139f21843 100644 (file)
@@ -15,10 +15,14 @@ class DrawingView: public QWidget
 
        public:
                void SetRotateToolActive(bool state = true);
+#if 0
                void SetAddLineToolActive(bool state = true);
                void SetAddCircleToolActive(bool state = true);
                void SetAddArcToolActive(bool state = true);
                void SetAddDimensionToolActive(bool state = true);
+#endif
+//             void SetToolActive(Action * action, bool state = true);
+               void SetToolActive(Action * action);//, bool state = true);
                void SetGridSize(uint32_t);
                void UpdateGridBackground(void);
 
index 308cab6b7bd87ed587cb884ae52113f1173abaad..a649306c74ecd96be81cbba047c8cba253be5d99 100644 (file)
@@ -208,7 +208,7 @@ void Painter::DrawPoint(int x, int y)
 }
 
 
-// This is drawn in Qt coordinates...
+// The rect passed in is in Qt coordinates...
 void Painter::DrawRoundedRect(QRectF rect, double radiusX, double radiusY)
 {
        if (!painter)
@@ -218,9 +218,8 @@ void Painter::DrawRoundedRect(QRectF rect, double radiusX, double radiusY)
 }
 
 
-// This is drawn partially in Cartesian coordinates, and partially in Qt
-// coordinates. The rect itself is in Cartesian but we want to pad it by a set
-// number of pixels.
+// The rect passed in is in Cartesian but we want to pad it by a set number of
+// pixels (currently set at 8), so the pad looks the same regardless of zoom.
 void Painter::DrawPaddedRect(QRectF rect)
 {
        if (!painter)
@@ -257,6 +256,9 @@ void Painter::DrawText(QRectF rect, int type, QString text)
 
 void Painter::DrawArrowhead(Vector head, Vector tail, double size)
 {
+       if (!painter)
+               return;
+
        QPolygonF arrow;
 
        // We draw the arrowhead aligned along the line from tail to head
@@ -278,3 +280,15 @@ void Painter::DrawArrowhead(Vector head, Vector tail, double size)
        painter->drawPolygon(arrow);
 }
 
+
+// Point is given in Cartesian coordinates
+void Painter::DrawCrosshair(Vector point)
+{
+       if (!painter)
+               return;
+
+       Vector screenPoint = CartesianToQtCoords(point);
+       painter->drawLine(0, screenPoint.y, screenSize.x, screenPoint.y);
+       painter->drawLine(screenPoint.x, 0, screenPoint.x, screenSize.y);
+}
+
index 84e3e1477a5d90b98822f70dd6bff0173fc22fc9..bdc46e413bfa88caeb284b96b98d31d14bdf40ed 100644 (file)
@@ -30,6 +30,7 @@ class Painter
                void DrawRect(QRectF);
                void DrawText(QRectF, int, QString);
                void DrawArrowhead(Vector, Vector, double);
+               void DrawCrosshair(Vector);
 
        public:
                static Vector CartesianToQtCoords(Vector);