]> Shamusworld >> Repos - architektonas/commitdiff
Fix for Print Preview for centering of drawing on page.
authorShamus Hammons <jlhamm@acm.org>
Mon, 12 Apr 2021 14:50:44 +0000 (09:50 -0500)
committerShamus Hammons <jlhamm@acm.org>
Mon, 12 Apr 2021 14:50:44 +0000 (09:50 -0500)
.gitignore
src/applicationwindow.cpp
src/drawingview.cpp
src/drawingview.h
src/painter.cpp
src/rect.cpp
src/rect.h

index 2c5004a00aaed97d58c5530c3c1bfe35c1e45000..52b3102cdf638568dc57f755aa8ca218350d8090 100644 (file)
@@ -9,3 +9,6 @@ res/*.xcf
 dxflib/
 fparser/
 architektonas
+spline/
+work/
+.qmake.stash
index 4c765cbc4e4e507b2248124b4be7ecf456e52ac4..b80130e8989cf77740f5e66ad97dcc9ba801c092 100644 (file)
@@ -264,12 +264,38 @@ void ApplicationWindow::HandlePrintRequest(QPrinter * printer)
        QPainter qtPainter(printer);
        Painter painter(&qtPainter);
 
+       // Save vars for screen
+       Point originSave = Global::origin;
+       double zoomSave = Global::zoom;
+       Vector screenSizeSave = Global::screenSize;
+
+       // Adjust zoom + origin to fit the paper (or NxM pages if we have 'em)
+       Rect r = drawing->GetObjectExtents((Object *)(&(drawing->document)));
+
+       QPageLayout pageLayout = printer->pageLayout();
+       QRect pageRect = pageLayout.paintRectPixels(printer->resolution());
+
+       Global::origin = r.BottomLeft();
+       Global::screenSize.x = pageRect.width();
+       Global::screenSize.y = pageRect.height();
+
+       double xScale = (double)pageRect.width() / r.Width();
+       double yScale = (double)pageRect.height() / r.Height();
+       Global::zoom = qMin(xScale, yScale);
+
+       if (xScale < yScale)
+               Global::origin.y -= (((double)pageRect.height() / Global::zoom) - r.Height()) / 2.0;
+       else
+               Global::origin.x -= (((double)pageRect.width() / Global::zoom) - r.Width()) / 2.0;
+
        // Do object rendering...
        for(int i=0; i<Global::numLayers; i++)
-       {
-//             if (Global::layerHidden[i] == false)
                drawing->RenderObjects(&painter, drawing->document.objects, i);
-       }
+
+       // Restore vars
+       Global::origin = originSave;
+       Global::zoom = zoomSave;
+       Global::screenSize = screenSizeSave;
 }
 
 void ApplicationWindow::contextMenuEvent(QContextMenuEvent * event)
index 5b0eeeef6b214c5d4ef4b67f8f87b6af9c2c195c..911ca677dd3b4269bde8f864038faba0ef518120 100644 (file)
@@ -121,10 +121,7 @@ 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. (And now
 we do! :-)
-
 */
-//     gridPixels = 12; //tmp???
-//     SetGridSize(12.0);      // This is in pixels
 }
 
 void DrawingView::DrawBackground(Painter * painter)
@@ -187,182 +184,6 @@ void DrawingView::DrawSubGrid(Painter * painter, uint32_t color, double step, Ve
                painter->DrawHLine(start.y + i);
 }
 
-#if 0
-void DrawingView::SetGridSize(uint32_t size)
-{
-#if 0
-       // Sanity check
-       if (size == gridPixels)
-               return;
-
-       // tmp...
-       if (size <= 1)
-               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)
-// This shouldn't be done here, because it fucks up the scrollwheel zooming...
-//     Global::zoom = gridPixels / Global::gridSpacing;
-       UpdateGridBackground();
-#endif
-}
-
-void DrawingView::UpdateGridBackground(void)
-{
-#if 0
-       // 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
-/*
-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 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 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.
-*/
-       if (x < 0)
-               x = -x % gridPixels;
-       else
-               x = (gridPixels - (x % gridPixels)) % gridPixels;
-
-       if (y < 0)
-               y = -y % gridPixels;
-       else
-               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, gridPixels, gridPixels);
-       QPalette pal = palette();
-       pal.setBrush(backgroundRole(), QBrush(pm));
-       setAutoFillBackground(true);
-       setPalette(pal);
-#endif
-}
-
-void DrawingView::SetGridSize(double size)
-{
-#if 0
-       // Sanity check
-       if (size == gridPixelsF)
-               return;
-
-       // tmp...
-       if (size <= 1)
-               return;
-
-       // Recreate the background bitmap
-       gridPixelsF = 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(double i=0; i<(BACKGROUND_MAX_SIZE-1); i+=gridPixelsF)
-       {
-               pmp.drawLine(i, 0, i, (double)(BACKGROUND_MAX_SIZE - 1));
-               pmp.drawLine(0, i, (double)(BACKGROUND_MAX_SIZE - 1), i);
-       }
-
-       pmp.end();
-
-       // Set up new BG brush & zoom level (pixels per base unit)
-// This shouldn't be done here, because it fucks up the scrollwheel zooming...
-//     Global::zoom = gridPixels / Global::gridSpacing;
-       UpdateGridBackgroundF();
-#endif
-}
-
-
-void DrawingView::UpdateGridBackgroundF(void)
-{
-#if 0
-       // Transform the origin to Qt coordinates
-       Vector pixmapOrigin = Painter::CartesianToQtCoords(Vector());
-       int x = 0;// (int)pixmapOrigin.x;
-       int y = 0;// (int)pixmapOrigin.y;
-       // Use mod arithmetic to grab the correct swatch of background
-
-/*     if (x < 0)
-               x = -x % gridPixels;
-       else
-               x = (gridPixels - (x % gridPixels)) % gridPixels;
-
-       if (y < 0)
-               y = -y % gridPixels;
-       else
-               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, gridPixelsF, gridPixelsF);
-       QPalette pal = palette();
-       pal.setBrush(backgroundRole(), QBrush(pm));
-       setAutoFillBackground(true);
-       setPalette(pal);
-#endif
-}
-#endif
-
 //
 // Basically, we just make a single pass through the Container. If the layer #
 // is less than the layer # being deleted, then do nothing. If the layer # is
@@ -934,7 +755,6 @@ VPVector DrawingView::GetHovered(void)
 void DrawingView::resizeEvent(QResizeEvent * /*event*/)
 {
        Global::screenSize = Vector(size().width(), size().height());
-//     UpdateGridBackground();
 }
 
 void DrawingView::ToolHandler(int mode, Point p)
index 0c6cc46cf76d18390d908dd8447e7b4661355ac9..704b79d357f60b1b610a0afbf4d802ea8f271fd1 100644 (file)
@@ -19,10 +19,6 @@ class DrawingView: public QWidget
 
        public:
                void DrawBackground(Painter *);
-//             void SetGridSize(uint32_t);
-//             void SetGridSize(double);
-//             void UpdateGridBackground(void);
-//             void UpdateGridBackgroundF(void);
                Point SnapPointToGrid(Point);
                Point SnapPointToAngle(Point);
                void RenderObjects(Painter *, VPVector &, int, bool ignoreLayer = false);
@@ -52,6 +48,7 @@ class DrawingView: public QWidget
                void DeleteCurrentLayer(int);
                void HandleLayerToggle(void);
                void HandleLayerSwap(int, int);
+               void HandleLayerSwap(int, int, VPVector &);
                void HandlePenWidth(float);
                void HandlePenStyle(int);
                void HandlePenColor(uint32_t);
@@ -76,7 +73,6 @@ class DrawingView: public QWidget
                void keyReleaseEvent(QKeyEvent * event);
 
        private:
-               void HandleLayerSwap(int, int, VPVector &);
                void DrawSubGrid(Painter *, uint32_t, double, Vector, Vector);
                QPoint GetAdjustedMousePosition(QMouseEvent * event);
                QPoint GetAdjustedClientPosition(int x, int y);
index 322e1508ebe7f5bd55172b93ecae6c591fc4a74f..853d3502690a494afdeb3635925d275fa087e6c8 100644 (file)
@@ -44,12 +44,12 @@ origin is -10, -10 and zoom level is 2 (200%)
 
 1st, invert the Y: 10, 10 -> 10, 90
 2nd, add origin:   10, 90 ->  0, 80 (no, not right--err, yes, it is)
-3rd, aply zoom:     0, 80 ->  0, 40
+3rd, apply zoom:    0, 80 ->  0, 40
 
 or, is it:
 
 1st, invert the Y: 10, 10 -> 10, 90
-2nd, aply zoom:    10, 90 ->  5, 45
+2nd, apply zoom:   10, 90 ->  5, 45
 3rd, add origin:    5, 45 -> -5, 35
 
 it depends on whether or not origin is in Qt coords or cartesian. If Qt, then the 1st
index 693cf31582e34b3533bb1df7bbd73a9a60b71fa6..54f44ddf73abcb404c95d1b6d397674a90a16b8c 100644 (file)
 #include "rect.h"
 //#include <stdio.h>
 
-
 Rect::Rect(): l(0), r(0), t(0), b(0)
 {
 }
 
-
 Rect::Rect(double ll, double rr, double tt, double bb):
        l(ll), r(rr), t(tt), b(bb)
 {
        Normalize();
 }
 
-
 Rect::Rect(Point tl, Point br): l(tl.x), r(br.x), t(tl.y), b(br.y)
 {
        Normalize();
 }
 
-
 Rect & Rect::operator*=(double scale)
 {
        l *= scale;
        r *= scale;
        t *= scale;
        b *= scale;
+
        return *this;
 }
 
-
 Rect & Rect::operator|=(Rect r2)
 {
 //printf("operatore|=\nthis = (%lf, %lf, %lf, %lf), r = (%lf, %lf, %lf, %lf)\n", l, t, r, b, r2.l, r2.t, r2.r, r2.b);
@@ -62,7 +58,6 @@ Rect & Rect::operator|=(Rect r2)
        return *this;
 }
 
-
 void Rect::Normalize(void)
 {
        if (l > r)
@@ -80,7 +75,6 @@ void Rect::Normalize(void)
        }
 }
 
-
 void Rect::Translate(Point p)
 {
        l += p.x;
@@ -89,7 +83,6 @@ void Rect::Translate(Point p)
        b += p.y;
 }
 
-
 void Rect::Expand(double amt)
 {
        l -= amt;
@@ -98,33 +91,37 @@ void Rect::Expand(double amt)
        b -= amt;
 }
 
-
 double Rect::Width(void)
 {
        return r - l;
 }
 
-
 double Rect::Height(void)
 {
        return t - b;
 }
 
-
 bool Rect::Contains(Point p)
 {
        return ((p.x >= l) && (p.x <= r) && (p.y >= b) && (p.y <= t) ? true : false);
 }
 
-
 Point Rect::TopLeft(void)
 {
        return Point(l, t);
 }
 
+Point Rect::TopRight(void)
+{
+       return Point(r, t);
+}
+
+Point Rect::BottomLeft(void)
+{
+       return Point(l, b);
+}
 
 Point Rect::BottomRight(void)
 {
        return Point(r, b);
 }
-
index 2689bae3f5469e15bc73873c07b168bba623b919..0098a8d54cd32066d2a0a5e6b3a969f4422206ec 100644 (file)
@@ -24,8 +24,9 @@ struct Rect
        double Height(void);
        bool Contains(Point p);
        Point TopLeft(void);
+       Point TopRight(void);
+       Point BottomLeft(void);
        Point BottomRight(void);
 };
 
 #endif // __RECT_H__
-