]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Fixes for grid background drawing of arbitrary size.
[architektonas] / src / drawingview.cpp
index 1a6d120852d8d98c52930c7fcc47a708b9dac27f..06f4496c15d44da73474035d00750bf081bdead0 100644 (file)
 #include "painter.h"
 
 
+#define BACKGROUND_MAX_SIZE    512
+
+
 DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        // The value in the settings file will override this.
        useAntialiasing(true),
-       gridBackground(512, 512),
+       gridBackground(BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE),
        scale(1.0), offsetX(-10), offsetY(-10),
        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),
+       gridSpacing(12.0), gridPixels(0), collided(false), rotateTool(false),
+       rx(150.0), ry(150.0),
        scrollDrag(false), addLineTool(false), addCircleTool(false),
        addDimensionTool(false),
-//     selectionInProgress(false),
        toolAction(NULL)
 {
        document.isTopLevelContainer = true;
@@ -112,20 +114,35 @@ inch regardless of the zoom level a piece of text can be larger or smaller than
 this. Maybe this is the case for having a base unit and basing point sizes off
 of that.
 
+Here's what's been figured out. Painter::zoom is simply the ratio of pixels to
+base units. What that means is that if you have a 12px grid with a 6" grid size
+(& base unit of "inches"), Painter::zoom becomes 12px / 6" = 2.0 px/in.
+
+Dimensions now have a "size" parameter to set their absolute size in relation
+to the base unit. ATM, the arrows are drawn in pixels, but also scaled by
+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.
 
 */
+#if 0
        QPainter pmp(&gridBackground);
-       pmp.fillRect(0, 0, 512, 512, QColor(240, 240, 240));
+       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<511; i+=12)
+       for(int i=0; i<(BACKGROUND_MAX_SIZE-1); i+=12)
        {
-               pmp.drawLine(i, 0, i, 511);
-               pmp.drawLine(0, i, 511, i);
+               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
 }
 
 
@@ -176,18 +193,122 @@ void DrawingView::SetAddDimensionToolActive(bool state/*= true*/)
 }
 
 
+void DrawingView::SetGridSize(uint32_t size)
+{
+       // Sanity check
+       if (size == gridPixels)
+               return;
+
+       // Recreate the background bitmap
+       gridPixels = size;
+       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+=gridPixels)
+       {
+               pmp.drawLine(i, 0, i, BACKGROUND_MAX_SIZE - 1);
+               pmp.drawLine(0, i, BACKGROUND_MAX_SIZE - 1, i);
+       }
+
+       pmp.end();
+
+       // Set up new BG brush & zoom level (pixels per base unit)
+       Painter::zoom = gridPixels / gridSpacing;
+       UpdateGridBackground();
+}
+
+
 void DrawingView::UpdateGridBackground(void)
 {
 //was: 128
-#define BG_BRUSH_SPAN 72
+//#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]
-       x = (x < 0 ? 0 : BG_BRUSH_SPAN - 1) - (x % BG_BRUSH_SPAN);
-       y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - (y % BG_BRUSH_SPAN);
+/*
+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.
+It seems the problem is negative numbers: We can't let that happen.
+When taking away the zero, it pops over 1 px at zero, then goes about 1/2 a
+grid at x<0.
+
+The bitmap looks like this:
+
++---+---+---+---+---
+|   |   |   |   |
+|   |   |   |   |
++---+---+---+---+---
+|   |   |   |   |
+|   |   |   |   |
+|   |   |   |   |
+
+@ x = 1, we want it to look like:
+
+-+---+---+---+---+---
+ |   |   |   |   |
+ |   |   |   |   |
+-+---+---+---+---+---
+ |   |   |   |   |
+ |   |   |   |   |
+ |   |   |   |   |
+
+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.
+
+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
+
+
+
+*/
+//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;
+       else
+               x = (BG_BRUSH_SPAN - (x % BG_BRUSH_SPAN)) % BG_BRUSH_SPAN;
+
+//     y = (y < 0 ? 0 : BG_BRUSH_SPAN - 1) - ((y < 0 ? -y : y) % BG_BRUSH_SPAN);
+       if (y < 0)
+               y = -y % BG_BRUSH_SPAN;
+       else
+               y = (BG_BRUSH_SPAN - (y % BG_BRUSH_SPAN)) % BG_BRUSH_SPAN;
+//printf("new x=%i\n", x);
 
        // Here we grab a section of the bigger pixmap, so that the background
        // *looks* like it's scrolling...
@@ -237,7 +358,7 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
        if (useAntialiasing)
                qtPainter.setRenderHint(QPainter::Antialiasing);
 
-       Painter::screenSize = Vector(size().width(), size().height());
+//     Painter::screenSize = Vector(size().width(), size().height());
        Object::SetViewportHeight(size().height());
 
        // Draw coordinate axes
@@ -277,6 +398,15 @@ void DrawingView::paintEvent(QPaintEvent * /*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();
+}
+
+
 void DrawingView::mousePressEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)