From e3ad4d35dcb7f9cb3ba747b92cb33456aec48634 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Fri, 20 Oct 2023 09:14:00 -0400 Subject: [PATCH] [TD]fix printAllPdf info block text --- src/Mod/TechDraw/Gui/PagePrinter.cpp | 93 ++++++++++++++++------------ src/Mod/TechDraw/Gui/PagePrinter.h | 13 ++++ 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/Mod/TechDraw/Gui/PagePrinter.cpp b/src/Mod/TechDraw/Gui/PagePrinter.cpp index 8ce9889257..b51bee0a60 100644 --- a/src/Mod/TechDraw/Gui/PagePrinter.cpp +++ b/src/Mod/TechDraw/Gui/PagePrinter.cpp @@ -77,24 +77,40 @@ void PagePrinter::setScene(QGSPage* scene) void PagePrinter::setDocumentName(const std::string& name) { m_documentName = name; } -//**** printing routines + +PaperAttributes PagePrinter::getPaperAttributes(TechDraw::DrawPage* pageObject) +{ + PaperAttributes result; + if (!pageObject) { + return result; + } + auto pageTemplate(dynamic_cast(pageObject->Template.getValue())); + if (pageTemplate) { + result.pagewidth = pageTemplate->Width.getValue(); + result.pageheight = pageTemplate->Height.getValue(); + } + result.paperSize = QPageSize::id(QSizeF(result.pagewidth, result.pageheight), QPageSize::Millimeter, + QPageSize::FuzzyOrientationMatch); + if (result.pagewidth > result.pageheight) { + result.orientation = QPageLayout::Landscape; + } else { + result.orientation = QPageLayout::Portrait; + } + if (result.paperSize == QPageSize::Ledger) { + // Ledger size paper orientation is reversed inside Qt + result.orientation =(QPageLayout::Orientation)(1 - result.orientation); + } + + return result; +} void PagePrinter::getPaperAttributes() { - App::DocumentObject* obj = m_vpPage->getDrawPage()->Template.getValue(); - auto pageTemplate(dynamic_cast(obj)); - if (pageTemplate) { - m_pagewidth = pageTemplate->Width.getValue(); - m_pageheight = pageTemplate->Height.getValue(); - } - m_paperSize = QPageSize::id(QSizeF(m_pagewidth, m_pageheight), QPageSize::Millimeter, - QPageSize::FuzzyOrientationMatch); - if (m_pagewidth > m_pageheight) { - m_orientation = QPageLayout::Landscape; - } - else { - m_orientation = QPageLayout::Portrait; - } + PaperAttributes attr = getPaperAttributes(m_vpPage->getDrawPage()); + m_pagewidth = attr.pagewidth; + m_pageheight = attr.pageheight; + m_paperSize = attr.paperSize; + m_orientation = attr.orientation; } void PagePrinter::printPdf(std::string file) @@ -104,27 +120,18 @@ void PagePrinter::printPdf(std::string file) Base::Console().Warning("PagePrinter - no file specified\n"); return; } - getPaperAttributes(); - QString filename = QString::fromUtf8(file.data(), file.size()); QPrinter printer(QPrinter::HighResolution); + + getPaperAttributes(); + // setPdfVersion sets the printied PDF Version to comply with PDF/A-1b, more details under: https://www.kdab.com/creating-pdfa-documents-qt/ // printer.setPdfVersion(QPagedPaintDevice::PdfVersion_A1b); printer.setFullPage(true); printer.setOutputFileName(filename); + printer.setPageOrientation(m_orientation); + printer.setPageSize(QPageSize(m_paperSize)); - if (m_paperSize == QPageSize::Ledger) { - printer.setPageOrientation((QPageLayout::Orientation)(1 - m_orientation));//reverse 0/1 - } - else { - printer.setPageOrientation(m_orientation); - } - if (m_paperSize == QPageSize::Custom) { - printer.setPageSize(QPageSize(QSizeF(m_pagewidth, m_pageheight), QPageSize::Millimeter)); - } - else { - printer.setPageSize(QPageSize(m_paperSize)); - } m_scene->setExportingPdf(true); print(&printer); m_scene->setExportingPdf(false); @@ -203,20 +210,20 @@ void PagePrinter::printAll(QPrinter* printer, App::Document* doc) void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) { // Base::Console().Message("PP::printAllPdf()\n"); - // setPdfVersion sets the printied PDF Version to comply with PDF/A-1b, more details under: https://www.kdab.com/creating-pdfa-documents-qt/ - printer->setPdfVersion(QPagedPaintDevice::PdfVersion_A1b); - // Base::Console().Message("PP::printAllPdf()\n"); + double dpmm = printer->resolution() / 25.4; + QString outputFile = printer->outputFileName(); QString documentName = QString::fromUtf8(doc->getName()); QPdfWriter pdfWriter(outputFile); - // setPdfVersion sets the printied PDF Version to comply with PDF/A-1b, more details under: https://www.kdab.com/creating-pdfa-documents-qt/ - pdfWriter.setPdfVersion(QPagedPaintDevice::PdfVersion_A1b); + // setPdfVersion sets the printed PDF Version to comply with PDF/A-1b, more details under: https://www.kdab.com/creating-pdfa-documents-qt/ + // but this is not working as of Qt 5.12 + //printer->setPdfVersion(QPagedPaintDevice::PdfVersion_A1b); + //pdfWriter.setPdfVersion(QPagedPaintDevice::PdfVersion_A1b); pdfWriter.setTitle(documentName); pdfWriter.setResolution(printer->resolution()); QPainter painter(&pdfWriter); QPageLayout pageLayout = printer->pageLayout(); - double dpmm = printer->resolution() / 25.4; bool firstTime = true; std::vector docObjs = doc->getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); @@ -231,8 +238,9 @@ void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) } TechDraw::DrawPage* dp = static_cast(obj); - double width = 297.0;//default to A4 Landscape 297 x 210 - double height = 210.0; + PaperAttributes attr = getPaperAttributes(dp); + double width = attr.pagewidth; + double height = attr.pageheight; setPageLayout(pageLayout, dp, width, height); pdfWriter.setPageLayout(pageLayout); @@ -247,8 +255,9 @@ void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) QRectF sourceRect(0.0, Rez::guiX(-height), Rez::guiX(width), Rez::guiX(height)); QRect targetRect(0, 0, width * dpmm, height * dpmm); - + vpp->getQGSPage()->setExportingPdf(true); renderPage(vpp, painter, sourceRect, targetRect); + vpp->getQGSPage()->setExportingPdf(false); } painter.end(); } @@ -368,3 +377,11 @@ void PagePrinter::savePDF(std::string file) printPdf(file); } +PaperAttributes::PaperAttributes() +{ + // set default values to A4 Landscape + orientation = QPageLayout::Orientation::Landscape; + paperSize = QPageSize::A4; + pagewidth = 297.0; + pageheight = 210.0; +} diff --git a/src/Mod/TechDraw/Gui/PagePrinter.h b/src/Mod/TechDraw/Gui/PagePrinter.h index d21c8d6394..79ebe887a4 100644 --- a/src/Mod/TechDraw/Gui/PagePrinter.h +++ b/src/Mod/TechDraw/Gui/PagePrinter.h @@ -47,6 +47,18 @@ class QGVPage; class QGSPage; class QGIView; +class TechDrawGuiExport PaperAttributes +{ +public: + PaperAttributes(); + ~PaperAttributes() = default; + + QPageLayout::Orientation orientation; + QPageSize::PageSizeId paperSize; + double pagewidth; + double pageheight; +}; + class TechDrawGuiExport PagePrinter { public: @@ -88,6 +100,7 @@ public: ViewProviderPage* getViewProviderPage() {return m_vpPage;} + static PaperAttributes getPaperAttributes(TechDraw::DrawPage* pageObject); void getPaperAttributes(); QPageLayout::Orientation getOrientation() const { return m_orientation; } QPageSize::PageSizeId getPaperSize() const { return m_paperSize; }