From: Shamus Hammons Date: Wed, 14 Aug 2013 22:43:16 +0000 (-0500) Subject: Fix DrawArcAction to actually allow creation of Arcs. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=architektonas;a=commitdiff_plain;h=86caae9cadd6e1877a4e6226533521ef0d1c6389 Fix DrawArcAction to actually allow creation of Arcs. --- diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index b812e4d..c510000 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -31,6 +31,10 @@ #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(); } diff --git a/src/drawarcaction.cpp b/src/drawarcaction.cpp index 96db309..624e4ee 100644 --- a/src/drawarcaction.cpp +++ b/src/drawarcaction.cpp @@ -12,15 +12,13 @@ // #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; } } diff --git a/src/drawarcaction.h b/src/drawarcaction.h index afb9f91..f47d461 100644 --- a/src/drawarcaction.h +++ b/src/drawarcaction.h @@ -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__ diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 31aeaf0..ab9f565 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -34,10 +34,6 @@ #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(); } diff --git a/src/drawingview.h b/src/drawingview.h index b296f28..44420e9 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -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); diff --git a/src/painter.cpp b/src/painter.cpp index 308cab6..a649306 100644 --- a/src/painter.cpp +++ b/src/painter.cpp @@ -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); +} + diff --git a/src/painter.h b/src/painter.h index 84e3e14..bdc46e4 100644 --- a/src/painter.h +++ b/src/painter.h @@ -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);