From a398911ea125eb6b25320e96599e0ae39b720cf8 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Sun, 26 Apr 2015 20:26:54 -0500 Subject: [PATCH] Tool for adding Lines works now. --- src/applicationwindow.cpp | 1 + src/drawingview.cpp | 102 +++++++++++++++++++++++++++++++++++++- src/drawingview.h | 12 ++++- src/global.cpp | 1 + src/global.h | 1 + src/structs.h | 2 + 6 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index f5013ad..866750d 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -420,6 +420,7 @@ void ApplicationWindow::SetInternalToolStates(void) // 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. + Global::toolState = TSNone; #if 0 if (drawing->toolAction) { diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 957aaf8..015431c 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -334,6 +334,13 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) painter.DrawCrosshair(oldPoint); toolAction->Draw(&painter); } +#else + if (Global::tool) + { + painter.SetPen(QPen(QColor(200, 100, 0, 255), 1.0, Qt::DashLine)); + painter.DrawCrosshair(oldPoint); + ToolDraw(&painter); + } #endif #if 1 @@ -595,6 +602,73 @@ void DrawingView::resizeEvent(QResizeEvent * /*event*/) } +void DrawingView::ToolMouseDown(Point p) +{ + if (Global::tool == TTLine) + { + if (Global::toolState == TSNone) + { + toolPoint[0] = p; +// toolState = TSPoint1; +// toolObject = (Object *)(new Line(p, Vector(0, 0))); + } + else //if (Global::toolState == TSPoint1) + { + toolPoint[1] = p; + } + } +} + + +void DrawingView::ToolMouseMove(Point p) +{ + if (Global::tool == TTLine) + { + if (Global::toolState == TSNone) + toolPoint[0] = p; + else + toolPoint[1] = p; + } +} + + +void DrawingView::ToolMouseUp(Point p) +{ + if (Global::tool == TTLine) + { + if (Global::toolState == TSNone) + { + Global::toolState = TSPoint2; + // Prevent spurious line from drawing... + toolPoint[1] = toolPoint[0]; + } + else + { + Line * l = new Line(toolPoint[0], toolPoint[1]); + document.objects.push_back(l); + toolPoint[0] = toolPoint[1]; + } + } +} + + +void DrawingView::ToolDraw(Painter * painter) +{ + if (Global::tool == TTLine) + { + if (Global::toolState == TSNone) + { + painter->DrawHandle(toolPoint[0]); + } + else + { + painter->DrawLine(toolPoint[0], toolPoint[1]); + painter->DrawHandle(toolPoint[1]); + } + } +} + + void DrawingView::mousePressEvent(QMouseEvent * event) { if (event->button() == Qt::LeftButton) @@ -626,6 +700,15 @@ void DrawingView::mousePressEvent(QMouseEvent * event) #else if (Global::tool) { + if (Global::snapToGrid) + point = SnapPointToGrid(point); + + // Snap to object point if valid... + + + ToolMouseDown(point); + //Also, may want to figure out if hovering over a snap point on an object, + //snap to grid if not. return; } #endif @@ -682,7 +765,8 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) #if 1 // Grid processing... (only snap here is left button is down) - if ((event->buttons() & Qt::LeftButton) && Global::snapToGrid) + // Umm, WHY?? + if (/*(event->buttons() & Qt::LeftButton) &&*/ Global::snapToGrid) { point = SnapPointToGrid(point); } @@ -885,7 +969,9 @@ selected object last and thus fucks up the algorithm. Need to fix this... toolAction->MouseMoved(point); } +#else #endif + bool needUpdate = false; // Don't do this kind of checking unless we're not doing a selection rectangle! @@ -967,6 +1053,13 @@ selected object last and thus fucks up the algorithm. Need to fix this... } //printf("MouseMove: numHovered = %i\n", numHovered); + // Tool handling... + if (Global::tool) + { + // Need to do snapping, etc. as well + ToolMouseMove(point); + } + // This is used to draw the tool crosshair... oldPoint = point; @@ -994,6 +1087,13 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event) #if 0 if (toolAction) toolAction->MouseReleased(); +#else + if (Global::tool) + { + Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y())); + ToolMouseUp(point); + return; + } #endif if (Global::selectionInProgress) diff --git a/src/drawingview.h b/src/drawingview.h index c4a7234..3d22b44 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -15,7 +15,6 @@ class DrawingView: public QWidget DrawingView(QWidget * parent = NULL); public: -// void SetToolActive(Action * action); void SetGridSize(uint32_t); void UpdateGridBackground(void); Point SnapPointToGrid(Point); @@ -25,6 +24,10 @@ class DrawingView: public QWidget void AddHoveredToSelection(void); void GetSelection(std::vector &); void GetHovered(std::vector &); + void ToolMouseDown(Point); + void ToolMouseMove(Point); + void ToolMouseUp(Point); + void ToolDraw(Painter *); public slots: void AddNewObjectToDocument(Object *); @@ -67,8 +70,13 @@ class DrawingView: public QWidget public: std::vector select; std::vector hover; -// Action * toolAction; +// int toolState; + Point toolPoint[32]; +// Object * toolObject; + // Tool methods (static) + public: +// static void foo(); // public: // static Container document; }; diff --git a/src/global.cpp b/src/global.cpp index c52a1ee..fe75856 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -24,6 +24,7 @@ bool Global::selectionInProgress = false; QRectF Global::selection; int Global::tool = TTNone; +int Global::toolState = TSNone; double Global::gridSpacing; int Global::currentLayer = 0; diff --git a/src/global.h b/src/global.h index fb05765..d59155e 100644 --- a/src/global.h +++ b/src/global.h @@ -35,6 +35,7 @@ class Global static bool dontMove; static uint32_t objectID; static int tool; + static int toolState; static Point origin; static double zoom; diff --git a/src/structs.h b/src/structs.h index 1bbfc3f..b639212 100644 --- a/src/structs.h +++ b/src/structs.h @@ -13,6 +13,8 @@ enum DimensionType { DTLinear, DTLinearVert, DTLinearHorz, DTRadial, DTDiametric enum ToolType { TTNone, TTLine, TTCircle, TTEllipse, TTArc, TTDimension, TTText, TTPolygon, TTSpline, TTRotate, TTMirror, TTTrim, TTTriangulate, TTDelete }; +enum ToolState { TSNone, TSPoint1, TSPoint2, TSDone }; + #define OBJECT_COMMON \ int type; \ uint32_t id; \ -- 2.37.2