From 22da66e4d8625d2255fa6d753eabdc7fa21c8c89 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 7 Sep 2021 12:31:52 +0300 Subject: [PATCH] Setup correct page orientation. --- src/app/puzzle/layout/vpsheet.cpp | 6 ++ src/app/puzzle/layout/vpsheet.h | 2 + src/app/puzzle/vpmainwindow.cpp | 85 +++++++++------------------- src/libs/vlayout/vlayoutexporter.cpp | 9 ++- 4 files changed, 42 insertions(+), 60 deletions(-) diff --git a/src/app/puzzle/layout/vpsheet.cpp b/src/app/puzzle/layout/vpsheet.cpp index 090f0e6f1..812e83dd6 100644 --- a/src/app/puzzle/layout/vpsheet.cpp +++ b/src/app/puzzle/layout/vpsheet.cpp @@ -650,6 +650,12 @@ void VPSheet::ClearSelection() const } } +//--------------------------------------------------------------------------------------------------------------------- +QPageLayout::Orientation VPSheet::GetSheetOrientation() const +{ + return m_size.height() >= m_size.width() ? QPageLayout::Portrait : QPageLayout::Landscape; +} + //--------------------------------------------------------------------------------------------------------------------- auto VPSheet::SheetUnits() const -> Unit { diff --git a/src/app/puzzle/layout/vpsheet.h b/src/app/puzzle/layout/vpsheet.h index c8aca1a5c..7560a72fe 100644 --- a/src/app/puzzle/layout/vpsheet.h +++ b/src/app/puzzle/layout/vpsheet.h @@ -261,6 +261,8 @@ public: void ClearSelection() const; + auto GetSheetOrientation() const -> QPageLayout::Orientation; + public slots: void CheckPiecePositionValidity(const VPPiecePtr &piece) const; diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index e22340107..e5ddcbc56 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -2358,7 +2358,6 @@ void VPMainWindow::GenerateUnifiedPdfFile(const VPExportData &data, const QStrin printer.setOutputFileName(name); printer.setDocName(QFileInfo(name).fileName()); printer.setResolution(static_cast(PrintDPI)); - printer.setPageOrientation(QPageLayout::Portrait); QPainter painter; @@ -2372,17 +2371,31 @@ void VPMainWindow::GenerateUnifiedPdfFile(const VPExportData &data, const QStrin margins = sheet->GetSheetMargins(); } + QPageLayout::Orientation sheetOrientation = sheet->GetSheetOrientation(); + QRectF imageRect = sheet->GetMarginsRect(); qreal width = FromPixel(imageRect.width() * data.xScale + margins.left() + margins.right(), Unit::Mm); qreal height = FromPixel(imageRect.height() * data.yScale + margins.top() + margins.bottom(), Unit::Mm); + QSizeF pageSize = sheetOrientation == QPageLayout::Portrait ? QSizeF(width, height) : QSizeF(height, width); + if (not printer.setPageSize(QPageSize(pageSize, QPageSize::Millimeter))) + { + qWarning() << tr("Cannot set printer page size"); + } + + printer.setPageOrientation(sheetOrientation); + printer.setFullPage(sheet->IgnoreMargins()); + + if (not sheet->IgnoreMargins()) + { + if (not printer.setPageMargins(UnitConvertor(margins, Unit::Px, Unit::Mm), QPageLayout::Millimeter)) + { + qWarning() << tr("Cannot set printer margins"); + } + } + if (firstPage) { - if (not printer.setPageSize(QPageSize(QSizeF(width, height), QPageSize::Millimeter))) - { - qWarning() << tr("Cannot set printer page size"); - } - if (not painter.begin(&printer)) { // failed to open file qCritical() << qUtf8Printable(tr("Can't open file '%1'").arg(name)); @@ -2402,28 +2415,6 @@ void VPMainWindow::GenerateUnifiedPdfFile(const VPExportData &data, const QStrin qCritical() << tr("Failed in flushing page to disk, disk full?"); return; } - - if (not printer.setPageSize(QPageSize(QSizeF(width, height), QPageSize::Millimeter))) - { - qWarning() << tr("Cannot set printer page size"); - } - } - - if (not sheet->IgnoreMargins()) - { - const qreal left = FromPixel(margins.left(), Unit::Mm); - const qreal top = FromPixel(margins.top(), Unit::Mm); - const qreal right = FromPixel(margins.right(), Unit::Mm); - const qreal bottom = FromPixel(margins.bottom(), Unit::Mm); - - if (not printer.setPageMargins(QMarginsF(left, top, right, bottom), QPageLayout::Millimeter)) - { - qWarning() << tr("Cannot set printer margins"); - } - } - else - { - printer.setFullPage(sheet->IgnoreMargins()); } sheet->SceneData()->PrepareForExport(); @@ -2451,33 +2442,20 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data) QPageLayout::Orientation tiledPDFOrientation = m_layout->LayoutSettings().GetTilesOrientation(); - printer.setPageOrientation(tiledPDFOrientation); - - if (not printer.setPageSize(QPageSize(m_layout->LayoutSettings().GetTilesSize(Unit::Mm), QPageSize::Millimeter))) + QSizeF tileSize = m_layout->LayoutSettings().GetTilesSize(Unit::Mm); + QSizeF pageSize = tiledPDFOrientation == QPageLayout::Portrait ? tileSize + : QSizeF(tileSize.height(), tileSize.width()); + if (not printer.setPageSize(QPageSize(pageSize, QPageSize::Millimeter))) { qWarning() << tr("Cannot set printer page size"); } + printer.setPageOrientation(tiledPDFOrientation); printer.setFullPage(m_layout->LayoutSettings().IgnoreTilesMargins()); if (not m_layout->LayoutSettings().IgnoreTilesMargins()) { - - QMarginsF printerMargins; - QMarginsF tiledMargins = m_layout->LayoutSettings().GetTilesMargins(); - if(tiledPDFOrientation == QPageLayout::Landscape) - { - // because when painting we have a -90rotation in landscape mode, - // see function PrintPages. - printerMargins = QMarginsF(tiledMargins.bottom(), tiledMargins.left(), tiledMargins.top(), - tiledMargins.right()); - } - else - { - printerMargins = tiledMargins; - } - - if (not printer.setPageMargins(UnitConvertor(printerMargins, Unit::Px, Unit::Mm), QPageLayout::Millimeter)) + if (not printer.setPageMargins(m_layout->LayoutSettings().GetTilesMargins(Unit::Mm), QPageLayout::Millimeter)) { qWarning() << tr("Cannot set printer margins"); } @@ -2485,17 +2463,6 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data) printer.setResolution(static_cast(PrintDPI)); - QPainter painter; - - // when tiled, the landscape tiles have to be rotated, because the pages - // stay portrait in the pdf - if(tiledPDFOrientation == QPageLayout::Landscape) - { - const int angle = -90; - painter.rotate(angle); - painter.translate(-ToPixel(printer.pageRect(QPrinter::Millimeter).height(), Unit::Mm), 0); - } - if (data.exportUnified) { const QString name = data.path + '/' + data.fileName + VLayoutExporter::ExportFormatSuffix(data.format); @@ -2503,6 +2470,7 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data) printer.setOutputFileName(name); printer.setDocName(QFileInfo(name).baseName()); + QPainter painter; if (not painter.begin(&printer)) { // failed to open file qCritical() << tr("Failed to open file, is it writable?"); @@ -2533,6 +2501,7 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data) printer.setOutputFileName(name); printer.setDocName(QFileInfo(name).baseName()); + QPainter painter; if (not painter.begin(&printer)) { // failed to open file qCritical() << tr("Failed to open file, is it writable?"); diff --git a/src/libs/vlayout/vlayoutexporter.cpp b/src/libs/vlayout/vlayoutexporter.cpp index 659808b51..bf2732f9d 100644 --- a/src/libs/vlayout/vlayoutexporter.cpp +++ b/src/libs/vlayout/vlayoutexporter.cpp @@ -415,17 +415,22 @@ void VLayoutExporter::ExportToPDF(QGraphicsScene *scene, const QString &filename printer.setOutputFileName(filename); printer.setDocName(QFileInfo(filename).fileName()); printer.setResolution(static_cast(PrintDPI)); - printer.setPageOrientation(QPageLayout::Portrait); printer.setFullPage(m_ignorePrinterMargins); + QPageLayout::Orientation imageOrientation = m_imageRect.height() >= m_imageRect.width() ? QPageLayout::Portrait + : QPageLayout::Landscape; + qreal width = FromPixel(m_imageRect.width() * m_xScale + m_margins.left() + m_margins.right(), Unit::Mm); qreal height = FromPixel(m_imageRect.height() * m_yScale + m_margins.top() + m_margins.bottom(), Unit::Mm); - if (not printer.setPageSize(QPageSize(QSizeF(width, height), QPageSize::Millimeter))) + QSizeF pageSize = imageOrientation == QPageLayout::Portrait ? QSizeF(width, height) : QSizeF(height, width); + if (not printer.setPageSize(QPageSize(pageSize, QPageSize::Millimeter))) { qWarning() << tr("Cannot set printer page size"); } + printer.setPageOrientation(imageOrientation); + if (not m_ignorePrinterMargins) { const qreal left = FromPixel(m_margins.left(), Unit::Mm);