[TD]fix handling of non-standard page sizes

This commit is contained in:
wandererfan
2025-12-12 13:37:31 -05:00
committed by Max Wilfinger
parent 4bb84c87f1
commit 2ea1f99367
3 changed files with 41 additions and 36 deletions

View File

@@ -331,12 +331,12 @@ void MDIViewPage::print()
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
if (pageAttr.pageSize() == QPageSize::Custom) {
if (pageAttr.pageSizeId() == QPageSize::Custom) {
printer.setPageSize(
QPageSize(QSizeF(pageAttr.pageWidth(), pageAttr.pageHeight()), QPageSize::Millimeter));
}
else {
printer.setPageSize(QPageSize(pageAttr.pageSize()));
printer.setPageSize(QPageSize(pageAttr.pageSizeId()));
}
printer.setPageOrientation(pageAttr.orientation());
@@ -352,12 +352,12 @@ void MDIViewPage::printPreview()
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
if (pageAttr.pageSize() == QPageSize::Custom) {
if (pageAttr.pageSizeId() == QPageSize::Custom) {
printer.setPageSize(
QPageSize(QSizeF(pageAttr.pageWidth(), pageAttr.pageHeight()), QPageSize::Millimeter));
}
else {
printer.setPageSize(QPageSize(pageAttr.pageSize()));
printer.setPageSize(QPageSize(pageAttr.pageSizeId()));
}
printer.setPageOrientation(pageAttr.orientation());
@@ -402,7 +402,7 @@ void MDIViewPage::print(QPrinter* printer)
return;
}
}
if (doPrint && psPrtSetting != pageAttr.pageSize()) {
if (doPrint && psPrtSetting != pageAttr.pageSizeId()) {
int ret = QMessageBox::warning(
this, tr("Different paper size"),
tr("The printer uses a different paper size than the drawing.\n"

View File

@@ -55,6 +55,7 @@
#include "QGSPage.h"
#include "Rez.h"
#include "ViewProviderPage.h"
#include "DrawGuiUtil.h"
using namespace TechDrawGui;
using namespace TechDraw;
@@ -67,9 +68,6 @@ constexpr double mmPerInch = 25.4;
/* TRANSLATOR TechDrawGui::PagePrinter */
//TYPESYSTEM_SOURCE_ABSTRACT(TechDrawGui::PagePrinter)
//! retrieve the attributes of a DrawPage and its Template
PaperAttributes PagePrinter::getPaperAttributes(TechDraw::DrawPage* dPage)
{
@@ -84,23 +82,19 @@ PaperAttributes PagePrinter::getPaperAttributes(TechDraw::DrawPage* dPage)
width = pageTemplate->Width.getValue();
height = pageTemplate->Height.getValue();
}
// result.m_pagewidth = width;
// result.m_pageheight = height;
//Qt's page size determination assumes Portrait orientation. To get the right paper size
//we need to ask in the proper form.
QPageSize::PageSizeId paperSizeID =
// Qt's page size determination assumes Portrait orientation. To get the right paper size
// we need to ask in the proper form.
QPageSize::PageSizeId paperSizeId =
QPageSize::id(QSizeF(std::min(width, height), std::max(width, height)),
QPageSize::Millimeter, QPageSize::FuzzyOrientationMatch);
auto paperSize = paperSizeID;
auto orientation = (QPageLayout::Orientation)dPage->getOrientation();
if (paperSize == QPageSize::Ledger) {
auto orientation = static_cast<QPageLayout::Orientation>(dPage->getOrientation());
if (paperSizeId == QPageSize::Ledger) {
// Ledger size paper orientation is reversed inside Qt
orientation = (QPageLayout::Orientation)(1 - orientation);
}
return {orientation, paperSize, width, height};
return {orientation, paperSizeId, width, height};
}
//! retrieve the attributes of a DrawPage by its viewProvider
@@ -118,8 +112,20 @@ void PagePrinter::makePageLayout(TechDraw::DrawPage* dPage, QPageLayout& pageLay
PaperAttributes attr = getPaperAttributes(dPage);
width = attr.pageWidth();
height = attr.pageHeight();
pageLayout.setPageSize(QPageSize(attr.pageSize()));
pageLayout.setOrientation(attr.orientation());
if (attr.pageSizeId() == QPageSize::Custom) {
// Qt thinks about pages in portrait orientation
QPageSize customPageSize =
QPageSize(QSizeF(std::min(width, height), std::max(width, height)), QPageSize::Millimeter);
pageLayout.setPageSize(customPageSize);
QPageLayout::Orientation orient = width >= height ?
QPageLayout::Orientation::Landscape :
QPageLayout::Orientation::Portrait;
pageLayout.setOrientation(orient);
} else {
pageLayout.setPageSize(QPageSize(attr.pageSizeId()));
pageLayout.setOrientation(attr.orientation());
}
pageLayout.setMode(QPageLayout::FullPageMode);
pageLayout.setMargins(QMarginsF());
}
@@ -254,9 +260,9 @@ void PagePrinter::printBannerPage(QPrinter* printer, QPainter& painter, QPageLay
QFont savePainterFont = painter.font();
QFont painterFont;
painterFont.setFamily(Preferences::labelFontQString());
int fontSizeMM = Preferences::labelFontSizeMM();
int fontSizeMM = (int)Preferences::labelFontSizeMM();
double dpmm = printer->resolution() / mmPerInch;
int fontSizePx = fontSizeMM * dpmm;
int fontSizePx = (int)fontSizeMM * dpmm;
painterFont.setPixelSize(fontSizePx);
painter.setFont(painterFont);
@@ -351,7 +357,6 @@ void PagePrinter::printPdf(ViewProviderPage* vpPage, const std::string& file)
QPdfWriter pdfWriter(outputFile);
pdfWriter.setPdfVersion(Gui::Dialog::DlgSettingsPDF::evaluatePDFVersion());
QPageLayout pageLayout = pdfWriter.pageLayout();
auto marginsdb = pageLayout.margins(QPageLayout::Millimeter);
QString documentName = QString::fromUtf8(vpPage->getDrawPage()->getNameInDocument());
pdfWriter.setTitle(documentName);
// default pdfWriter dpi is 1200.
@@ -361,11 +366,10 @@ void PagePrinter::printPdf(ViewProviderPage* vpPage, const std::string& file)
// set up the page layout
auto dPage = vpPage->getDrawPage();
double width = A4Heightmm; // default to A4 Landscape 297 x 210
double height = A4Widthmm;
double width = dPage->getPageWidth();
double height = dPage->getPageHeight();
makePageLayout(dPage, pageLayout, width, height);
pdfWriter.setPageLayout(pageLayout);
marginsdb = pageLayout.margins(QPageLayout::Millimeter);
// first page does not respect page layout unless painter is created after
// pdfWriter layout is established.
@@ -438,11 +442,12 @@ void PagePrinter::savePDF(ViewProviderPage* vpPage, const std::string& file)
}
PaperAttributes::PaperAttributes()
PaperAttributes::PaperAttributes() :
m_orientation(QPageLayout::Orientation::Landscape),
m_paperSizeId(QPageSize::A4),
m_pagewidth(A4Heightmm),
m_pageheight(A4Widthmm)
{
// set default values to A4 Landscape
m_orientation = QPageLayout::Orientation::Landscape;
m_paperSize = QPageSize::A4;
m_pagewidth = A4Heightmm;
m_pageheight = A4Widthmm;
}

View File

@@ -52,11 +52,11 @@ class TechDrawGuiExport PaperAttributes
public:
PaperAttributes();
PaperAttributes(QPageLayout::Orientation orientation,
QPageSize::PageSizeId paperSize,
QPageSize::PageSizeId paperSizeId,
double pageWidth,
double pageHeight)
: m_orientation(orientation)
, m_paperSize(paperSize)
, m_paperSizeId(paperSizeId)
, m_pagewidth(pageWidth)
, m_pageheight(pageHeight)
{}
@@ -65,9 +65,9 @@ public:
{
return m_orientation;
}
QPageSize::PageSizeId pageSize() const
QPageSize::PageSizeId pageSizeId() const
{
return m_paperSize;
return m_paperSizeId;
}
double pageWidth() const
{
@@ -80,7 +80,7 @@ public:
private:
QPageLayout::Orientation m_orientation;
QPageSize::PageSizeId m_paperSize;
QPageSize::PageSizeId m_paperSizeId;
double m_pagewidth;
double m_pageheight;
};