]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Fix DrawArcAction to actually allow creation of Arcs.
[architektonas] / src / drawingview.cpp
index 8f689a80bd0e33cd2ef3000ba7bff14fea6d5c59..ab9f565fc2423bcb4f5a0c2b076eeaf63e64f500 100644 (file)
@@ -34,9 +34,6 @@
 #include "arc.h"
 #include "circle.h"
 #include "dimension.h"
-#include "drawcircleaction.h"
-#include "drawdimensionaction.h"
-#include "drawlineaction.h"
 #include "line.h"
 #include "painter.h"
 
@@ -124,25 +121,11 @@ Painter::zoom *and* size. Same with the dimension text; it's drawn at 10pt and
 scaled the same way as the arrowheads.
 
 Need a way to scale line widths as well. :-/ Shouldn't be too difficult, just
-need a thickness parameter similar to the "size" param for dimensions.
+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
 }
 
 
@@ -153,43 +136,14 @@ void DrawingView::SetRotateToolActive(bool state/*= true*/)
 }
 
 
-void DrawingView::SetAddLineToolActive(bool state/*= true*/)
-{
-       if (state)
-       {
-               toolAction = new DrawLineAction();
-               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::SetAddDimensionToolActive(bool state/*= true*/)
+void DrawingView::SetToolActive(Action * action)
 {
-       if (state)
+       if (action != NULL)
        {
-               toolAction = new DrawDimensionAction();
+               toolAction = action;
                connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
                        SLOT(AddNewObjectToDocument(Object *)));
        }
-
-       update();
 }
 
 
@@ -328,6 +282,26 @@ QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
 }
 
 
+//
+// This looks strange, but it's really quite simple: We want a point that's
+// more than half-way to the next grid point to snap there while conversely we
+// want a point that's less than half-way to to the next grid point then snap
+// to the one before it. So we add half of the grid spacing to the point, then
+// divide by it so that we can remove the fractional part, then multiply it
+// back to get back to the correct answer.
+//
+Vector DrawingView::SnapPointToGrid(Vector point)
+{
+       point += gridSpacing / 2.0;             // *This* adds to Z!!!
+       point /= gridSpacing;
+       point.x = floor(point.x);//need to fix this for negative numbers...
+       point.y = floor(point.y);
+       point.z = 0;                                    // Make *sure* Z doesn't go anywhere!!!
+       point *= gridSpacing;
+       return point;
+}
+
+
 void DrawingView::paintEvent(QPaintEvent * /*event*/)
 {
        QPainter qtPainter(this);
@@ -363,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)
        {
@@ -388,6 +367,12 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
        if (event->button() == Qt::LeftButton)
        {
                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);
+
                collided = document.Collided(point);
 
                if (collided)
@@ -437,14 +422,9 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 
        // Grid processing...
 #if 1
-       // This looks strange, but it's really quite simple: We want a point that's
-       // more than half-way to the next grid point to snap there while conversely
-       // we want a point that's less than half-way to to the next grid point then
-       // snap to the one before it. So we add half of the grid spacing to the
-       // point, then divide by it so that we can remove the fractional part, then
-       // multiply it back to get back to the correct answer.
-       if (event->buttons() & Qt::LeftButton)
+       if ((event->buttons() & Qt::LeftButton) && Object::snapToGrid)
        {
+#if 0
                point += gridSpacing / 2.0;                                     // *This* adds to Z!!!
                point /= gridSpacing;
 //200% is ok, gridSpacing = 6 in this case...
@@ -455,8 +435,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
                point.y = floor(point.y);
                point.z = 0;                                                            // Make *sure* Z doesn't go anywhere!!!
                point *= gridSpacing;
+#else
+               point = SnapPointToGrid(point);
+#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);
@@ -466,6 +450,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 
        if (toolAction)
        {
+               if (Object::snapToGrid)
+               {
+                       point = SnapPointToGrid(point);
+                       oldPoint = point;
+               }
+
                toolAction->MouseMoved(point);
                update();
        }