From 58426c243a3d84d97170882ccd66e10e87f9d861 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Thu, 24 Oct 2024 20:28:17 -0400 Subject: [PATCH] [TD]do not mark document as changed after print --- src/Mod/TechDraw/Gui/MDIViewPage.cpp | 80 ++++++++++++++++++++++++++-- src/Mod/TechDraw/Gui/MDIViewPage.h | 5 +- src/Mod/TechDraw/Gui/PagePrinter.cpp | 20 ++++--- 3 files changed, 93 insertions(+), 12 deletions(-) diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.cpp b/src/Mod/TechDraw/Gui/MDIViewPage.cpp index 11c599db24..3e344c9d21 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.cpp +++ b/src/Mod/TechDraw/Gui/MDIViewPage.cpp @@ -309,11 +309,40 @@ void MDIViewPage::fixSceneDependencies() /// as file name selection and error messages /// PagePrinter handles the actual printing mechanics. +/// save the page state so it can be restore after printing +void MDIViewPage::savePageExportState(ViewProviderPage* page) +{ + auto guiDoc = page->getDocument(); + if (!guiDoc) { + return; + } + m_docModStateBeforePrint = guiDoc->isModified(); +} +/// ensure that the page reverts to its normal state after any changes made for printing. +void MDIViewPage::resetPageExportState(ViewProviderPage* page) const +{ + auto pageFeature = page->getDrawPage(); + if (!pageFeature) { + // how did this happen? + return; + } + + auto guiDoc = page->getDocument(); + if (!guiDoc) { + return; + } + auto scene = page->getQGSPage(); + scene->setExportingPdf(false); + scene->setExportingSvg(false); + guiDoc->setModified(m_docModStateBeforePrint); + pageFeature->redrawCommand(); +} + + /// overrides of MDIView print methods so that they print the QGraphicsScene instead /// of the COIN3d scenegraph. void MDIViewPage::printPdf() { -// Base::Console().Message("MDIVP::printPdf()\n"); QStringList filter; filter << QObject::tr("PDF (*.pdf)"); filter << QObject::tr("All Files (*.*)"); @@ -325,19 +354,35 @@ void MDIViewPage::printPdf() } Gui::WaitCursor wc; + auto vpp = getViewProviderPage(); + if (!vpp) { + // how did this happen? + return; + } + + savePageExportState(vpp); + std::string utf8Content = fn.toUtf8().constData(); if (m_pagePrinter) { m_pagePrinter->printPdf(utf8Content); + resetPageExportState(vpp); } } void MDIViewPage::print() { -// Base::Console().Message("MDIVP::print()\n"); - if (!m_pagePrinter) { return; } + + auto vpp = getViewProviderPage(); + if (!vpp) { + // how did this happen? + return; + } + + savePageExportState(vpp); + m_pagePrinter->getPaperAttributes(); QPrinter printer(QPrinter::HighResolution); @@ -353,6 +398,7 @@ void MDIViewPage::print() QPrintDialog dlg(&printer, this); if (dlg.exec() == QDialog::Accepted) { print(&printer); + resetPageExportState(vpp); } } @@ -383,10 +429,17 @@ void MDIViewPage::printPreview() void MDIViewPage::print(QPrinter* printer) { -// Base::Console().Message("MDIVP::print(printer)\n"); + // Base::Console().Message("MDIVP::print(printer)\n"); if (!m_pagePrinter) { return; } + auto vpp = getViewProviderPage(); + if (!vpp) { + // how did this happen? + return; + } + savePageExportState(vpp); + m_pagePrinter->getPaperAttributes(); // As size of the render area paperRect() should be used. When performing a real // print pageRect() may also work but the output is cropped at the bottom part. @@ -431,6 +484,7 @@ void MDIViewPage::print(QPrinter* printer) if (m_pagePrinter) { m_pagePrinter->print(printer); + resetPageExportState(vpp); } } @@ -438,7 +492,7 @@ void MDIViewPage::print(QPrinter* printer) //static routine to print all pages in a document void MDIViewPage::printAll(QPrinter* printer, App::Document* doc) { -// Base::Console().Message("MDIVP::printAll()\n"); + // Base::Console().Message("MDIVP::printAll()\n"); PagePrinter::printAll(printer, doc); } @@ -482,10 +536,19 @@ void MDIViewPage::viewAll() void MDIViewPage::saveSVG(std::string filename) { + auto vpp = getViewProviderPage(); + if (!vpp) { + // how did this happen? + return; + } + savePageExportState(vpp); if (m_pagePrinter) { m_pagePrinter->saveSVG(filename); + resetPageExportState(vpp); } } + + void MDIViewPage::saveSVG() { QStringList filter; @@ -525,8 +588,15 @@ void MDIViewPage::saveDXF() void MDIViewPage::savePDF(std::string filename) { + auto vpp = getViewProviderPage(); + if (!vpp) { + // how did this happen? + return; + } + savePageExportState(vpp); if (m_pagePrinter) { m_pagePrinter->savePDF(filename); + resetPageExportState(vpp); } } diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.h b/src/Mod/TechDraw/Gui/MDIViewPage.h index 3fec4e8b93..67f90b8726 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.h +++ b/src/Mod/TechDraw/Gui/MDIViewPage.h @@ -99,8 +99,9 @@ public: PyObject* getPyObject() override; TechDraw::DrawPage * getPage() { return m_vpPage->getDrawPage(); } - ViewProviderPage* getViewProviderPage() {return m_vpPage;} + void savePageExportState(ViewProviderPage* page); + void resetPageExportState(ViewProviderPage* page) const; void setTabText(std::string tabText); @@ -161,6 +162,8 @@ private: void getPaperAttributes(); PagePrinter* m_pagePrinter; + bool m_docModStateBeforePrint{false}; + }; class MDIViewPagePy : public Py::PythonExtension diff --git a/src/Mod/TechDraw/Gui/PagePrinter.cpp b/src/Mod/TechDraw/Gui/PagePrinter.cpp index 208195d129..33ea9b97b8 100644 --- a/src/Mod/TechDraw/Gui/PagePrinter.cpp +++ b/src/Mod/TechDraw/Gui/PagePrinter.cpp @@ -56,6 +56,7 @@ #include "QGSPage.h" #include "Rez.h" #include "ViewProviderPage.h" +#include "MDIViewPage.h" using namespace TechDrawGui; using namespace TechDraw; @@ -143,7 +144,7 @@ void PagePrinter::makePageLayout(TechDraw::DrawPage* dPage, QPageLayout& pageLay /// print the Page associated with the parent MDIViewPage as a Pdf file void PagePrinter::printPdf(std::string file) { -// Base::Console().Message("PP::printPdf(%s)\n", file.c_str()); + // Base::Console().Message("PP::printPdf(%s)\n", file.c_str()); if (file.empty()) { Base::Console().Warning("PagePrinter - no file specified\n"); return; @@ -151,7 +152,7 @@ void PagePrinter::printPdf(std::string file) // set up the pdfwriter auto filespec = Base::Tools::escapeEncodeFilename(file); - filespec = DU::cleanFilespecBackslash(file); + filespec = DU::cleanFilespecBackslash(filespec); QString outputFile = Base::Tools::fromStdString(filespec); QPdfWriter pdfWriter(outputFile); QPageLayout pageLayout = pdfWriter.pageLayout(); @@ -206,7 +207,7 @@ void PagePrinter::print(QPrinter* printer) //static routine to print all pages in a document void PagePrinter::printAll(QPrinter* printer, App::Document* doc) { -// Base::Console().Message("PP::printAll()\n"); + Base::Console().Message("PP::printAll()\n"); QPageLayout pageLayout = printer->pageLayout(); std::vector docObjs = @@ -230,6 +231,9 @@ void PagePrinter::printAll(QPrinter* printer, App::Document* doc) if (!vpp) { continue;// can't print this one } + // is there always a mdi when printAll is called? + auto mdi = vpp->getMDIViewPage(); + mdi->savePageExportState(vpp); auto dPage = static_cast(obj); double width = A4Heightmm;//default to A4 Landscape 297 x 210 @@ -245,14 +249,14 @@ void PagePrinter::printAll(QPrinter* printer, App::Document* doc) QRect targetRect = printer->pageLayout().fullRectPixels(printer->resolution()); renderPage(vpp, painter, sourceRect, targetRect); - + mdi->resetPageExportState(vpp); } } //static routine to print all pages in a document to pdf void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) { -// Base::Console().Message("PP::printAllPdf()\n"); + // Base::Console().Message("PP::printAllPdf()\n"); double dpmm = printer->resolution() / mmPerInch; QString outputFile = printer->outputFileName(); @@ -291,6 +295,9 @@ void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) if (!vpp) { continue;// can't print this one } + // is there always a mdi when printAll is called? + auto mdi = vpp->getMDIViewPage(); + mdi->savePageExportState(vpp); auto scene = vpp->getQGSPage(); scene->setExportingPdf(true); @@ -308,7 +315,8 @@ 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); renderPage(vpp, painter, sourceRect, targetRect); - scene->setExportingPdf(false); + mdi->resetPageExportState(vpp); + } }