From: Shamus Hammons Date: Mon, 12 Apr 2021 14:50:44 +0000 (-0500) Subject: Fix for Print Preview for centering of drawing on page. X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=architektonas;a=commitdiff_plain;h=1d17841ed1d003060250dc2ef8dd6785fa02a07f Fix for Print Preview for centering of drawing on page. --- diff --git a/.gitignore b/.gitignore index 2c5004a..52b3102 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ res/*.xcf dxflib/ fparser/ architektonas +spline/ +work/ +.qmake.stash diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index 4c765cb..b80130e 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -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; iRenderObjects(&painter, drawing->document.objects, i); - } + + // Restore vars + Global::origin = originSave; + Global::zoom = zoomSave; + Global::screenSize = screenSizeSave; } void ApplicationWindow::contextMenuEvent(QContextMenuEvent * event) diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 5b0eeee..911ca67 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -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) diff --git a/src/drawingview.h b/src/drawingview.h index 0c6cc46..704b79d 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -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); diff --git a/src/painter.cpp b/src/painter.cpp index 322e150..853d350 100644 --- a/src/painter.cpp +++ b/src/painter.cpp @@ -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 diff --git a/src/rect.cpp b/src/rect.cpp index 693cf31..54f44dd 100644 --- a/src/rect.cpp +++ b/src/rect.cpp @@ -15,35 +15,31 @@ #include "rect.h" //#include - 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); } - diff --git a/src/rect.h b/src/rect.h index 2689bae..0098a8d 100644 --- a/src/rect.h +++ b/src/rect.h @@ -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__ -