aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Pisa <pisa@cmp.felk.cvut.cz>2019-06-12 15:47:58 +0200
committerPavel Pisa <pisa@cmp.felk.cvut.cz>2019-06-12 15:47:58 +0200
commitc4b77becfcea294078ef6d33e7bec8b865fff94d (patch)
tree2211d3da73689022dd2e3c44f840630aaa78ddc0
parent8eaacd566948f2c594b76d1e1890ed905e48c0a7 (diff)
downloadqtmips-c4b77becfcea294078ef6d33e7bec8b865fff94d.tar.gz
qtmips-c4b77becfcea294078ef6d33e7bec8b865fff94d.tar.bz2
qtmips-c4b77becfcea294078ef6d33e7bec8b865fff94d.zip
Printing/export to PDF file reduces print area/page to actual image size.
This allows direct inclusion of print outputs into documentation and papers. Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
-rw-r--r--qtmips_gui/mainwindow.cpp18
1 files changed, 17 insertions, 1 deletions
diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp
index 8f13a01..32bdd5b 100644
--- a/qtmips_gui/mainwindow.cpp
+++ b/qtmips_gui/mainwindow.cpp
@@ -252,8 +252,24 @@ void MainWindow::print_action() {
printer.setColorMode(QPrinter::Color);
QPrintDialog print_dialog(&printer, this);
if (print_dialog.exec() == QDialog::Accepted) {
- QPainter painter(&printer);
QRectF scene_rect = corescene->sceneRect();
+ if (printer.outputFormat() == QPrinter::PdfFormat && scene_rect.height()) {
+ QPageLayout layout = printer.pageLayout();
+ layout.setOrientation(QPageLayout::Portrait);
+ QPageSize pagesize = layout.pageSize();
+ QRectF paint_rect = layout.paintRect(QPageLayout::Point);
+ QSize pointsize = pagesize.sizePoints();
+ qreal ratio = scene_rect.width() / scene_rect.height();
+ if (paint_rect.height() * ratio > paint_rect.width()) {
+ pointsize.setHeight(pointsize.height() - paint_rect.height() + paint_rect.width() / ratio);
+ } else {
+ pointsize.setWidth(pointsize.width() - paint_rect.width() + paint_rect.height() * ratio);
+ }
+ pagesize = QPageSize(pointsize, "custom", QPageSize::ExactMatch);
+ layout.setPageSize(pagesize, layout.margins());
+ printer.setPageLayout(layout);
+ }
+ QPainter painter(&printer);
QRectF target_rect = painter.viewport();
corescene->render(&painter, target_rect, scene_rect, Qt::KeepAspectRatio);
}