From e11a07565fe5ef3dfafbf9a933d7a1575e058867 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Thu, 27 Jun 2013 16:44:27 -0500 Subject: [PATCH] Fix tool handling and circle highlighting. --- src/applicationwindow.cpp | 11 ++++++++ src/circle.cpp | 14 +++++++++-- src/dimension.cpp | 8 +++--- src/dimension.h | 4 +-- src/drawdimensionaction.cpp | 4 ++- src/drawingview.cpp | 50 +++++++++++++++++++++---------------- src/drawingview.h | 22 +++------------- src/ellipse.h | 10 ++++++++ 8 files changed, 74 insertions(+), 49 deletions(-) diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index 0911b8e..2038731 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -346,8 +346,19 @@ void ApplicationWindow::SetInternalToolStates(void) Object::SetDeleteActive(deleteAct->isChecked()); Object::SetDimensionActive(addDimensionAct->isChecked()); drawing->SetRotateToolActive(rotateAct->isChecked()); + + // We can be sure that if we've come here, then either an active tool is + // being deactivated, or a new tool is being created. In either case, the + // old tool needs to be deleted. + if (drawing->toolAction) + { + delete drawing->toolAction; + drawing->toolAction = NULL; + } + drawing->SetAddLineToolActive(addLineAct->isChecked()); drawing->SetAddCircleToolActive(addCircleAct->isChecked()); + drawing->SetAddDimensionToolActive(addDimensionAct->isChecked()); } diff --git a/src/circle.cpp b/src/circle.cpp index af8f91f..0780f62 100644 --- a/src/circle.cpp +++ b/src/circle.cpp @@ -127,10 +127,20 @@ bool Circle::HitTest(Point point) Document passes in the correct Cartesian coordinates being pointed to by the mouse. So all we have to be concerned with is properly scaling our hot zones/handle sizes, since we generally *don't* want those to scale with the zoom level. ;-) + +What is going on here? +If we're zoomed out to, say, 50%, & our radius is 10.0 (absolute), then on screen +the radius will be 5.0. By multiplying the length by the zoom factor, we align our +pointed at length with our on screen length. */ - if (length < 8.0) + if ((length * Painter::zoom) < 8.0) hitCenter = true; - else if ((length < (radius + 2.0)) && (length > (radius - 2.0))) +//wrong: else if ((length < (radius + 2.0)) && (length > (radius - 2.0))) +/*NB: The following should be identical to what we have down below, but it doesn't work out that way... :-P */ +//close, but no else if (((length * Painter::zoom) < ((radius * Painter::zoom) + 2.0)) && ((length * Painter::zoom) > ((radius * Painter::zoom) - 2.0))) +//really wrong! else if (((length * Painter::zoom) < (radius + 2.0)) && ((length * Painter::zoom) > (radius - 2.0))) +// close again, but sill no else if (((length * Painter::zoom) < ((radius + 2.0) * Painter::zoom)) && ((length * Painter::zoom) > ((radius - 2.0) * Painter::zoom))) + else if ((fabs(length - radius) * Painter::zoom) < 2.0) hitCircle = true; return StateChanged(); diff --git a/src/dimension.cpp b/src/dimension.cpp index eb978fd..2107d67 100644 --- a/src/dimension.cpp +++ b/src/dimension.cpp @@ -344,19 +344,19 @@ about keeping track of old states... /*virtual*/ void Dimension::Disconnect(Object * obj, double param) { if (point1.object == obj && point1.t == param) - point1 = NULL; + point1.object = NULL; else if (point2.object == obj && point2.t == param) - point2 = NULL; + point2.object = NULL; } /*virtual*/ void Dimension::DisconnectAll(Object * obj) { if (point1.object == obj) - point1 = NULL; + point1.object = NULL; if (point2.object == obj) - point2 = NULL; + point2.object = NULL; } diff --git a/src/dimension.h b/src/dimension.h index 2784e83..4fbcaa8 100644 --- a/src/dimension.h +++ b/src/dimension.h @@ -4,7 +4,7 @@ #include "connection.h" #include "object.h" -enum DimensionType { DTLinear, DTRadial, DTDiametric, DTCircumferential, DTLeader }; +enum DimensionType { DTLinear, DTLinearVert, DTLinearHorz, DTRadial, DTDiametric, DTCircumferential, DTAngular, DTLeader }; class Dimension: public Object { @@ -39,7 +39,7 @@ class Dimension: public Object DimensionType type; // We use these in lieu of the built-in connected[] array; no reason to - // do this way especially + // do it this way especially Connection point1; Connection point2; }; diff --git a/src/drawdimensionaction.cpp b/src/drawdimensionaction.cpp index 2414afa..58fa095 100644 --- a/src/drawdimensionaction.cpp +++ b/src/drawdimensionaction.cpp @@ -78,10 +78,12 @@ DrawDimensionAction::~DrawDimensionAction() // We create the new object here, and then pass it off to the // DrawingView which stuffs it into the document. // line = new Line(p1, p2); + dimension = new Dimension(p1, p2); // We don't need no stinkin' sentinels, when we have signals & slots! emit ObjectReady(dimension); - p1 = p2; + state = FIRST_POINT; +// p1 = p2; } } diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 3f27f96..330f733 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -35,6 +35,7 @@ #include "circle.h" #include "dimension.h" #include "drawcircleaction.h" +#include "drawdimensionaction.h" #include "drawlineaction.h" #include "line.h" #include "painter.h" @@ -48,7 +49,8 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), document(Vector(0, 0)), // gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0), gridSpacing(12.0), collided(false), rotateTool(false), rx(150.0), ry(150.0), - scrollDrag(false), addLineTool(false), toolAction(NULL) + scrollDrag(false), addLineTool(false), addCircleTool(false), + addDimensionTool(false), toolAction(NULL) { setBackgroundRole(QPalette::Base); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); @@ -102,58 +104,54 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), UpdateGridBackground(); } + void DrawingView::SetRotateToolActive(bool state/*= true*/) { rotateTool = state; update(); } + void DrawingView::SetAddLineToolActive(bool state/*= true*/) { - if (state)// && toolAction == NULL) + if (state) { - if (toolAction) - delete toolAction; - - addCircleTool = false; toolAction = new DrawLineAction(); connect(toolAction, SIGNAL(ObjectReady(Object *)), this, SLOT(AddNewObjectToDocument(Object *))); } - else if (!state && addLineTool && toolAction) - { - delete toolAction; - toolAction = NULL; - } - addLineTool = state; update(); //printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction); } + void DrawingView::SetAddCircleToolActive(bool state/*= true*/) { - if (state)// && toolAction == NULL) + if (state) { - if (toolAction) - delete toolAction; - - addLineTool = false; toolAction = new DrawCircleAction(); connect(toolAction, SIGNAL(ObjectReady(Object *)), this, SLOT(AddNewObjectToDocument(Object *))); } - else if (!state && addCircleTool && toolAction) + + update(); +} + + +void DrawingView::SetAddDimensionToolActive(bool state/*= true*/) +{ + if (state) { - delete toolAction; - toolAction = NULL; + toolAction = new DrawDimensionAction(); + connect(toolAction, SIGNAL(ObjectReady(Object *)), this, + SLOT(AddNewObjectToDocument(Object *))); } - addCircleTool = state; update(); -//printf("DrawingView::SetAddCircleToolActive(). toolAction=%08X\n", toolAction); } + void DrawingView::UpdateGridBackground(void) { #if 0 @@ -194,6 +192,7 @@ setPalette(pal); #endif } + void DrawingView::AddNewObjectToDocument(Object * object) { if (object) @@ -205,6 +204,7 @@ void DrawingView::AddNewObjectToDocument(Object * object) //printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object); } + QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event) { // This is undoing the transform, e.g. going from client coords to local coords. @@ -213,6 +213,7 @@ QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event) return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y())); } + QPoint DrawingView::GetAdjustedClientPosition(int x, int y) { // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?) @@ -221,6 +222,7 @@ QPoint DrawingView::GetAdjustedClientPosition(int x, int y) return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0); } + void DrawingView::paintEvent(QPaintEvent * /*event*/) { QPainter qtPainter(this); @@ -280,6 +282,7 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) toolAction->Draw(&painter); } + void DrawingView::mousePressEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) @@ -302,6 +305,7 @@ void DrawingView::mousePressEvent(QMouseEvent * event) } } + void DrawingView::mouseMoveEvent(QMouseEvent * event) { Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y())); @@ -358,6 +362,7 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) } } + void DrawingView::mouseReleaseEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) @@ -380,3 +385,4 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event) setCursor(Qt::ArrowCursor); } } + diff --git a/src/drawingview.h b/src/drawingview.h index b8f1760..ac7170d 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -17,6 +17,7 @@ class DrawingView: public QWidget void SetRotateToolActive(bool state = true); void SetAddLineToolActive(bool state = true); void SetAddCircleToolActive(bool state = true); + void SetAddDimensionToolActive(bool state = true); void UpdateGridBackground(void); public slots: @@ -52,25 +53,10 @@ class DrawingView: public QWidget Vector oldPoint; bool addLineTool; bool addCircleTool; - Action * toolAction; -/* QSize minimumSizeHint() const; - QSize sizeHint() const; - - private: - void CreateCursors(void); - QPoint GetAdjustedMousePosition(QMouseEvent * event); - QPoint GetAdjustedClientPosition(int x, int y); - - QImage image; - QPoint pt, ptOffset, ptPrevious; - ToolType tool; // Current tool - GlyphPoints pts; // Glyph point structure - int32 ptHighlight, oldPtHighlight, ptNextHighlight, oldPtNextHighlight; - bool polyFirstPoint; + bool addDimensionTool; - ToolWindow * toolPalette; - QCursor cur[8]; -*/ + public: + Action * toolAction; }; #endif // __DRAWINGVIEW_H__ diff --git a/src/ellipse.h b/src/ellipse.h index e69de29..b200f78 100644 --- a/src/ellipse.h +++ b/src/ellipse.h @@ -0,0 +1,10 @@ +/* +An ellipse is just a variant of a circle, do we really need a separate class +for them? + +We have a separate class for arcs, and those are circle variants too. + +Maybe. The ellipse (and elliptical arc) have two focii and a rotation angle. +But there's no reason why those extras couldn't go into the Circle class as +well. +*/ -- 2.37.2