From 510a5ab6a71c4ad7166621c9adee7d9e5266e28e Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 23 Dec 2020 15:42:30 +0200 Subject: [PATCH] Refactoring. --- src/libs/vlayout/vprintlayout.cpp | 72 +++++++++++++++++-------------- src/libs/vlayout/vprintlayout.h | 3 ++ 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/libs/vlayout/vprintlayout.cpp b/src/libs/vlayout/vprintlayout.cpp index 7e882edff..ecffcc46a 100644 --- a/src/libs/vlayout/vprintlayout.cpp +++ b/src/libs/vlayout/vprintlayout.cpp @@ -312,14 +312,6 @@ void VPrintLayout::PrintPreview() //--------------------------------------------------------------------------------------------------------------------- void VPrintLayout::PrintPages(QPrinter *printer) { - // Here we try understand difference between printer's dpi and our. - // Get printer rect acording to our dpi. - const QRectF printerPageRect(0, 0, ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm), - ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm)); - const QRect pageRect = printer->pageLayout().paintRectPixels(printer->resolution()); - const double xscale = pageRect.width() / printerPageRect.width(); - const double yscale = pageRect.height() / printerPageRect.height(); - QPainter painter; if (not painter.begin(printer)) { // failed to open file @@ -391,29 +383,15 @@ void VPrintLayout::PrintPages(QPrinter *printer) copyCount = printer->copyCount(); } - VWatermarkData data; - if (not m_watermarkPath.isEmpty()) - { - try - { - VWatermarkConverter converter(m_watermarkPath); - VWatermark watermark; - watermark.setXMLContent(converter.Convert()); - data = watermark.GetWatermark(); + VWatermarkData data = WatermarkData(); - if (not data.path.isEmpty()) - { - // Clean previous cache - QPixmapCache::remove(AbsoluteMPath(m_watermarkPath, data.path)); - } - } - catch (VException &e) - { - const QString errorMsg = tr("File error.\n\n%1\n\n%2").arg(e.ErrorMessage(), e.DetailedInformation()); - qApp->IsPedantic() ? throw VException(errorMsg) : - qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg; - } - } + // Here we try understand difference between printer's dpi and our. + // Get printer rect acording to our dpi. + const QRectF printerPageRect(0, 0, ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm), + ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm)); + const QRect pageRect = printer->pageLayout().paintRectPixels(printer->resolution()); + const double xscale = pageRect.width() / printerPageRect.width(); + const double yscale = pageRect.height() / printerPageRect.height(); for (int i = 0; i < copyCount; ++i) { @@ -465,8 +443,8 @@ void VPrintLayout::PrintPages(QPrinter *printer) QPageLayout layout = printer->pageLayout(); layout.setUnits(QPageLayout::Millimeter); QMarginsF printerMargins = layout.margins(); - x = qFloor(ToPixel(printerMargins.left(),Unit::Mm)); - y = qFloor(ToPixel(printerMargins.top(),Unit::Mm)); + x = qFloor(ToPixel(printerMargins.left(), Unit::Mm)); + y = qFloor(ToPixel(printerMargins.top(), Unit::Mm)); } else { @@ -736,6 +714,36 @@ auto VPrintLayout::IsPagesFit(QSizeF printPaper) const -> bool return pSize.height() <= printPaper.height() && pSize.width() <= printPaper.width(); } +//--------------------------------------------------------------------------------------------------------------------- +auto VPrintLayout::WatermarkData() const -> VWatermarkData +{ + VWatermarkData data; + if (m_isTiled && not m_watermarkPath.isEmpty()) + { + try + { + VWatermarkConverter converter(m_watermarkPath); + VWatermark watermark; + watermark.setXMLContent(converter.Convert()); + data = watermark.GetWatermark(); + + if (not data.path.isEmpty()) + { + // Clean previous cache + QPixmapCache::remove(AbsoluteMPath(m_watermarkPath, data.path)); + } + } + catch (VException &e) + { + const QString errorMsg = tr("File error.\n\n%1\n\n%2").arg(e.ErrorMessage(), e.DetailedInformation()); + qApp->IsPedantic() ? throw VException(errorMsg) : + qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg; + } + } + + return data; +} + //--------------------------------------------------------------------------------------------------------------------- auto VPrintLayout::ContinueIfLayoutStale(QWidget *parent) -> int { diff --git a/src/libs/vlayout/vprintlayout.h b/src/libs/vlayout/vprintlayout.h index 2440de0cc..46e1033f2 100644 --- a/src/libs/vlayout/vprintlayout.h +++ b/src/libs/vlayout/vprintlayout.h @@ -38,6 +38,7 @@ enum class PrintType : qint8 {PrintPDF, PrintPreview, PrintNative}; class QPrinter; class QGraphicsScene; +struct VWatermarkData; class VPrintLayout : public QObject { @@ -168,6 +169,8 @@ private: bool IsPagesUniform() const; bool IsPagesFit(QSizeF printPaper) const; + + auto WatermarkData() const -> VWatermarkData; }; //---------------------------------------------------------------------------------------------------------------------