Setup correct page orientation.

This commit is contained in:
Roman Telezhynskyi 2021-09-07 12:31:52 +03:00
parent c32a4f60b0
commit 22da66e4d8
4 changed files with 42 additions and 60 deletions

View File

@ -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 auto VPSheet::SheetUnits() const -> Unit
{ {

View File

@ -261,6 +261,8 @@ public:
void ClearSelection() const; void ClearSelection() const;
auto GetSheetOrientation() const -> QPageLayout::Orientation;
public slots: public slots:
void CheckPiecePositionValidity(const VPPiecePtr &piece) const; void CheckPiecePositionValidity(const VPPiecePtr &piece) const;

View File

@ -2358,7 +2358,6 @@ void VPMainWindow::GenerateUnifiedPdfFile(const VPExportData &data, const QStrin
printer.setOutputFileName(name); printer.setOutputFileName(name);
printer.setDocName(QFileInfo(name).fileName()); printer.setDocName(QFileInfo(name).fileName());
printer.setResolution(static_cast<int>(PrintDPI)); printer.setResolution(static_cast<int>(PrintDPI));
printer.setPageOrientation(QPageLayout::Portrait);
QPainter painter; QPainter painter;
@ -2372,17 +2371,31 @@ void VPMainWindow::GenerateUnifiedPdfFile(const VPExportData &data, const QStrin
margins = sheet->GetSheetMargins(); margins = sheet->GetSheetMargins();
} }
QPageLayout::Orientation sheetOrientation = sheet->GetSheetOrientation();
QRectF imageRect = sheet->GetMarginsRect(); QRectF imageRect = sheet->GetMarginsRect();
qreal width = FromPixel(imageRect.width() * data.xScale + margins.left() + margins.right(), Unit::Mm); 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); 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 (firstPage)
{ {
if (not printer.setPageSize(QPageSize(QSizeF(width, height), QPageSize::Millimeter)))
{
qWarning() << tr("Cannot set printer page size");
}
if (not painter.begin(&printer)) if (not painter.begin(&printer))
{ // failed to open file { // failed to open file
qCritical() << qUtf8Printable(tr("Can't open file '%1'").arg(name)); 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?"); qCritical() << tr("Failed in flushing page to disk, disk full?");
return; 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(); sheet->SceneData()->PrepareForExport();
@ -2451,33 +2442,20 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data)
QPageLayout::Orientation tiledPDFOrientation = m_layout->LayoutSettings().GetTilesOrientation(); QPageLayout::Orientation tiledPDFOrientation = m_layout->LayoutSettings().GetTilesOrientation();
printer.setPageOrientation(tiledPDFOrientation); QSizeF tileSize = m_layout->LayoutSettings().GetTilesSize(Unit::Mm);
QSizeF pageSize = tiledPDFOrientation == QPageLayout::Portrait ? tileSize
if (not printer.setPageSize(QPageSize(m_layout->LayoutSettings().GetTilesSize(Unit::Mm), QPageSize::Millimeter))) : QSizeF(tileSize.height(), tileSize.width());
if (not printer.setPageSize(QPageSize(pageSize, QPageSize::Millimeter)))
{ {
qWarning() << tr("Cannot set printer page size"); qWarning() << tr("Cannot set printer page size");
} }
printer.setPageOrientation(tiledPDFOrientation);
printer.setFullPage(m_layout->LayoutSettings().IgnoreTilesMargins()); printer.setFullPage(m_layout->LayoutSettings().IgnoreTilesMargins());
if (not m_layout->LayoutSettings().IgnoreTilesMargins()) if (not m_layout->LayoutSettings().IgnoreTilesMargins())
{ {
if (not printer.setPageMargins(m_layout->LayoutSettings().GetTilesMargins(Unit::Mm), QPageLayout::Millimeter))
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))
{ {
qWarning() << tr("Cannot set printer margins"); qWarning() << tr("Cannot set printer margins");
} }
@ -2485,17 +2463,6 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data)
printer.setResolution(static_cast<int>(PrintDPI)); printer.setResolution(static_cast<int>(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) if (data.exportUnified)
{ {
const QString name = data.path + '/' + data.fileName + VLayoutExporter::ExportFormatSuffix(data.format); 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.setOutputFileName(name);
printer.setDocName(QFileInfo(name).baseName()); printer.setDocName(QFileInfo(name).baseName());
QPainter painter;
if (not painter.begin(&printer)) if (not painter.begin(&printer))
{ // failed to open file { // failed to open file
qCritical() << tr("Failed to open file, is it writable?"); qCritical() << tr("Failed to open file, is it writable?");
@ -2533,6 +2501,7 @@ void VPMainWindow::ExportPdfTiledFile(const VPExportData &data)
printer.setOutputFileName(name); printer.setOutputFileName(name);
printer.setDocName(QFileInfo(name).baseName()); printer.setDocName(QFileInfo(name).baseName());
QPainter painter;
if (not painter.begin(&printer)) if (not painter.begin(&printer))
{ // failed to open file { // failed to open file
qCritical() << tr("Failed to open file, is it writable?"); qCritical() << tr("Failed to open file, is it writable?");

View File

@ -415,17 +415,22 @@ void VLayoutExporter::ExportToPDF(QGraphicsScene *scene, const QString &filename
printer.setOutputFileName(filename); printer.setOutputFileName(filename);
printer.setDocName(QFileInfo(filename).fileName()); printer.setDocName(QFileInfo(filename).fileName());
printer.setResolution(static_cast<int>(PrintDPI)); printer.setResolution(static_cast<int>(PrintDPI));
printer.setPageOrientation(QPageLayout::Portrait);
printer.setFullPage(m_ignorePrinterMargins); 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 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); 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"); qWarning() << tr("Cannot set printer page size");
} }
printer.setPageOrientation(imageOrientation);
if (not m_ignorePrinterMargins) if (not m_ignorePrinterMargins)
{ {
const qreal left = FromPixel(m_margins.left(), Unit::Mm); const qreal left = FromPixel(m_margins.left(), Unit::Mm);