]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Fixed loading code, added "Base Unit" dialog.
[architektonas] / src / drawingview.cpp
index 06f4496c15d44da73474035d00750bf081bdead0..5bbcddf62ffeb0396b5f5edc5924e46ceaea9a5e 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*/)
+void DrawingView::SetToolActive(Action * action)
 {
-       if (state)
+       if (action != NULL)
        {
-               toolAction = new DrawCircleAction();
+               toolAction = action;
                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();
 }
 
 
@@ -221,15 +175,11 @@ void DrawingView::SetGridSize(uint32_t size)
 
 void DrawingView::UpdateGridBackground(void)
 {
-//was: 128
-//#define BG_BRUSH_SPAN 72
-#define BG_BRUSH_SPAN (gridPixels)
        // Transform the origin to Qt coordinates
        Vector pixmapOrigin = Painter::CartesianToQtCoords(Vector());
        int x = (int)pixmapOrigin.x;
        int y = (int)pixmapOrigin.y;
        // Use mod arithmetic to grab the correct swatch of background
-       // Problem with mod 128: Negative numbers screw it up... [FIXED]
 /*
 Negative numbers still screw it up... Need to think about what we're
 trying to do here. The fact that it worked with 72 seems to have been pure luck.
@@ -268,51 +218,33 @@ Which means we need to grab the sample from x = 3. @ x = -1:
    |   |   |   |
 
 Which means we need to grab the sample from x = 1. Which means we have to take
-the inverse of the modulus of gridPixels.
+the mirror of the modulus of gridPixels.
 
 Doing a mod of a negative number is problematic: 1st, the compiler converts the
 negative number to an unsigned int, then it does the mod. Gets you wrong answers
-most of the time, unless you use a power of 2. :-P
-
-This has been solved below:
-UGB: pmo=(2.000000,687.000000) X=2, X%48=2, new x=45
-UGB: pmo=(1.000000,687.000000) X=1, X%48=1, new x=46
-UGB: pmo=(0.000000,687.000000) X=0, X%48=0, new x=47
-UGB: pmo=(-1.000000,687.000000) X=-1, X%48=15, new x=1
-UGB: pmo=(-2.000000,686.000000) X=-2, X%48=14, new x=2
-UGB: pmo=(-3.000000,686.000000) X=-3, X%48=13, new x=3
-
-Problem with changing grid size causes x/y origin to be shown incorrectly:
-UGB: pmo=(10.000000,812.000000) X=10, X%12=10, new x=2
-UGB: pmo=(10.000000,812.000000) X=10, X%4=2,   new x=2
-UGB: pmo=(3.333333,818.666667)  X=3,  X%48=3,  new x=45
-UGB: pmo=(39.000000,782.000000) X=39, X%48=39, new x=9   <-- MMB move is here
-UGB: pmo=(39.000000,781.000000) X=39, X%48=39, new x=9
-
-
-
+most of the time, unless you use a power of 2. :-P So what we do here is just
+take the modulus of the negation, which means we don't have to worry about
+mirroring it later.
+
+The positive case looks gruesome (and it is) but it boils down to this: We take
+the modulus of the X coordinate, then mirror it by subtraction from the
+maximum (in this case, gridPixels). This gives us a number in the range of 1 to
+gridPixels. But we need the case where the result equalling gridPixels to be
+zero; so we do another modulus operation on the result to achieve this.
 */
-//printf("UGB: pmo=(%f,%f) X=%i, X%%%i=%i, ", pixmapOrigin.x, pixmapOrigin.y, x, BG_BRUSH_SPAN, x % BG_BRUSH_SPAN);
-//     x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN);
-//     y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN);
-//     x = (BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN);
-//     y = (BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN);
-//     x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - ((x < 0 ? -x : x) % BG_BRUSH_SPAN);
        if (x < 0)
-               x = -x % BG_BRUSH_SPAN;
+               x = -x % gridPixels;
        else
-               x = (BG_BRUSH_SPAN - (x % BG_BRUSH_SPAN)) % BG_BRUSH_SPAN;
+               x = (gridPixels - (x % gridPixels)) % gridPixels;
 
-//     y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - ((y < 0 ? -y : y) % BG_BRUSH_SPAN);
        if (y < 0)
-               y = -y % BG_BRUSH_SPAN;
+               y = -y % gridPixels;
        else
-               y = (BG_BRUSH_SPAN - (y % BG_BRUSH_SPAN)) % BG_BRUSH_SPAN;
-//printf("new x=%i\n", x);
+               y = (gridPixels - (y % gridPixels)) % gridPixels;
 
        // Here we grab a section of the bigger pixmap, so that the background
        // *looks* like it's scrolling...
-       QPixmap pm = gridBackground.copy(x, y, BG_BRUSH_SPAN, BG_BRUSH_SPAN);
+       QPixmap pm = gridBackground.copy(x, y, gridPixels, gridPixels);
        QPalette pal = palette();
        pal.setBrush(backgroundRole(), QBrush(pm));
        setAutoFillBackground(true);
@@ -350,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);
@@ -385,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)
        {
@@ -398,11 +355,9 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
 }
 
 
-void DrawingView::resizeEvent(QResizeEvent * event)
+void DrawingView::resizeEvent(QResizeEvent * /*event*/)
 {
        Painter::screenSize = Vector(size().width(), size().height());
-//     Painter::screenSize = Vector(event->size().width(), event->size().height());
-//printf("DrawingView::resizeEvent: new size:
        UpdateGridBackground();
 }
 
@@ -412,6 +367,18 @@ 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, as sometimes things don't fall on the grid.
+/*
+So, how to fix this? Have the Object check itself?
+Maybe we can fix this by having the initial point not be snapped, but when there's
+a drag, we substitute the snapped point 'oldPoint' which the Object keeps track of
+internally to know how far it was dragged...
+*/
+               if (Object::snapToGrid)
+                       point = SnapPointToGrid(point);
+
                collided = document.Collided(point);
 
                if (collided)
@@ -461,14 +428,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...
@@ -479,8 +441,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);
@@ -490,6 +456,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
 
        if (toolAction)
        {
+               if (Object::snapToGrid)
+               {
+                       point = SnapPointToGrid(point);
+                       oldPoint = point;
+               }
+
                toolAction->MouseMoved(point);
                update();
        }