From c4b77becfcea294078ef6d33e7bec8b865fff94d Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Wed, 12 Jun 2019 15:47:58 +0200 Subject: 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 --- qtmips_gui/mainwindow.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'qtmips_gui/mainwindow.cpp') 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); } -- cgit v1.2.3