]> Shamusworld >> Repos - architektonas/blobdiff - src/applicationwindow.cpp
Fix for Print Preview for centering of drawing on page.
[architektonas] / src / applicationwindow.cpp
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)