From 9da0b5cab51a35058cae40cfce746cc7a45f5fab Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 6 Oct 2016 19:13:06 +0300 Subject: [PATCH] Refactoring methods GetMinPrinterFields and GetPrinterFields. --HG-- branch : develop --- .../dialogs/dialoglayoutsettings.cpp | 43 ++---------------- .../valentina/dialogs/dialoglayoutsettings.h | 4 +- src/libs/vmisc/def.cpp | 44 +++++++++++++++++++ src/libs/vmisc/def.h | 4 ++ 4 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/app/valentina/dialogs/dialoglayoutsettings.cpp b/src/app/valentina/dialogs/dialoglayoutsettings.cpp index 1637a3ce9..6cb62b425 100644 --- a/src/app/valentina/dialogs/dialoglayoutsettings.cpp +++ b/src/app/valentina/dialogs/dialoglayoutsettings.cpp @@ -583,7 +583,7 @@ void DialogLayoutSettings::DialogAccepted() } else { - const QMarginsF minFields = RoundMargins(GetMinPrinterFields()); + const QMarginsF minFields = RoundMargins(MinPrinterFields()); const QMarginsF fields = RoundMargins(GetFields()); if (fields.left() < minFields.left() || fields.right() < minFields.right() || fields.top() < minFields.top() || fields.bottom() < minFields.bottom()) @@ -959,28 +959,13 @@ QMarginsF DialogLayoutSettings::RoundMargins(const QMarginsF &margins) const } //--------------------------------------------------------------------------------------------------------------------- -QMarginsF DialogLayoutSettings::GetMinPrinterFields() const +QMarginsF DialogLayoutSettings::MinPrinterFields() const { QPrinterInfo printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText()); if (not printer.isNull()) { QSharedPointer pr = QSharedPointer(new QPrinter(printer)); -#if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0) - QPageLayout layout = pr->pageLayout(); - layout.setUnits(QPageLayout::Millimeter); - const QMarginsF minMargins = layout.minimumMargins(); - - QMarginsF min; - min.setLeft(UnitConvertor(minMargins.left(), Unit::Mm, Unit::Px)); - min.setRight(UnitConvertor(minMargins.right(), Unit::Mm, Unit::Px)); - min.setTop(UnitConvertor(minMargins.top(), Unit::Mm, Unit::Px)); - min.setBottom(UnitConvertor(minMargins.bottom(), Unit::Mm, Unit::Px)); - return min; -#else - pr->setFullPage(false); - pr->setPageMargins(0, 0, 0, 0, QPrinter::Millimeter); - return GetPrinterFields(QSharedPointer(new QPrinter(printer))); -#endif //QT_VERSION >= QT_VERSION_CHECK(5, 3, 0) + return GetMinPrinterFields(pr); } else { @@ -1158,25 +1143,3 @@ void DialogLayoutSettings::SetAdditionalOptions(bool value) SetUnitePages(value); SetStripOptimization(value); } - -//--------------------------------------------------------------------------------------------------------------------- -QMarginsF DialogLayoutSettings::GetPrinterFields(const QSharedPointer &printer) -{ - if (printer.isNull()) - { - return QMarginsF(); - } - - qreal left = 0; - qreal top = 0; - qreal right = 0; - qreal bottom = 0; - printer->getPageMargins(&left, &top, &right, &bottom, QPrinter::Millimeter); - // We can't use Unit::Px because our dpi in most cases is different - QMarginsF def; - def.setLeft(UnitConvertor(left, Unit::Mm, Unit::Px)); - def.setRight(UnitConvertor(right, Unit::Mm, Unit::Px)); - def.setTop(UnitConvertor(top, Unit::Mm, Unit::Px)); - def.setBottom(UnitConvertor(bottom, Unit::Mm, Unit::Px)); - return def; -} diff --git a/src/app/valentina/dialogs/dialoglayoutsettings.h b/src/app/valentina/dialogs/dialoglayoutsettings.h index 69f26e2a8..0a2115d86 100644 --- a/src/app/valentina/dialogs/dialoglayoutsettings.h +++ b/src/app/valentina/dialogs/dialoglayoutsettings.h @@ -159,7 +159,7 @@ private: QSizeF TemplateSize(const PaperSizeTemplate &tmpl) const; QSizeF RoundTemplateSize(qreal width, qreal height) const; QMarginsF RoundMargins(const QMarginsF &margins) const; - QMarginsF GetMinPrinterFields() const; + QMarginsF MinPrinterFields() const; QMarginsF GetDefPrinterFields() const; Unit PaperUnit() const; @@ -176,8 +176,6 @@ private: void SheetSize(const QSizeF &size); void SetAdditionalOptions(bool value); - - static QMarginsF GetPrinterFields(const QSharedPointer &printer); }; #endif // DIALOGLAYOUTSETTINGS_H diff --git a/src/libs/vmisc/def.cpp b/src/libs/vmisc/def.cpp index 9aeb2904a..af7e43258 100644 --- a/src/libs/vmisc/def.cpp +++ b/src/libs/vmisc/def.cpp @@ -1849,6 +1849,50 @@ QSharedPointer PreparePrinter(const QPrinterInfo &info, QPrinter::Prin return printer; } +//--------------------------------------------------------------------------------------------------------------------- +QMarginsF GetMinPrinterFields(const QSharedPointer &printer) +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0) + QPageLayout layout = printer->pageLayout(); + layout.setUnits(QPageLayout::Millimeter); + const QMarginsF minMargins = layout.minimumMargins(); + + QMarginsF min; + min.setLeft(UnitConvertor(minMargins.left(), Unit::Mm, Unit::Px)); + min.setRight(UnitConvertor(minMargins.right(), Unit::Mm, Unit::Px)); + min.setTop(UnitConvertor(minMargins.top(), Unit::Mm, Unit::Px)); + min.setBottom(UnitConvertor(minMargins.bottom(), Unit::Mm, Unit::Px)); + return min; +#else + auto tempPrinter = QSharedPointer(new QPrinter(QPrinterInfo(* printer))); + tempPrinter->setFullPage(false); + tempPrinter->setPageMargins(0, 0, 0, 0, QPrinter::Millimeter); + return GetPrinterFields(tempPrinter); +#endif //QT_VERSION >= QT_VERSION_CHECK(5, 3, 0) +} + +//--------------------------------------------------------------------------------------------------------------------- +QMarginsF GetPrinterFields(const QSharedPointer &printer) +{ + if (printer.isNull()) + { + return QMarginsF(); + } + + qreal left = 0; + qreal top = 0; + qreal right = 0; + qreal bottom = 0; + printer->getPageMargins(&left, &top, &right, &bottom, QPrinter::Millimeter); + // We can't use Unit::Px because our dpi in most cases is different + QMarginsF def; + def.setLeft(UnitConvertor(left, Unit::Mm, Unit::Px)); + def.setRight(UnitConvertor(right, Unit::Mm, Unit::Px)); + def.setTop(UnitConvertor(top, Unit::Mm, Unit::Px)); + def.setBottom(UnitConvertor(bottom, Unit::Mm, Unit::Px)); + return def; +} + //--------------------------------------------------------------------------------------------------------------------- QPixmap darkenPixmap(const QPixmap &pixmap) { diff --git a/src/libs/vmisc/def.h b/src/libs/vmisc/def.h index 1cab5c9c9..e75f431ef 100644 --- a/src/libs/vmisc/def.h +++ b/src/libs/vmisc/def.h @@ -50,6 +50,7 @@ template class QSharedPointer; #endif /* Q_CC_MSVC */ class QComboBox; +class QMarginsF; #define SceneSize 50000 #define DefPointRadius 1.5//mm @@ -634,6 +635,9 @@ QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) QSharedPointer PreparePrinter(const QPrinterInfo &info, QPrinter::PrinterMode mode = QPrinter::ScreenResolution) Q_REQUIRED_RESULT; +QMarginsF GetMinPrinterFields(const QSharedPointer &printer); +QMarginsF GetPrinterFields(const QSharedPointer &printer); + QPixmap darkenPixmap(const QPixmap &pixmap) Q_REQUIRED_RESULT; void ShowInGraphicalShell(const QString &filePath);