diff --git a/src/gui/painting/qpagesize.h b/src/gui/painting/qpagesize.h index c8a472747d..2e88d497a9 100644 --- a/src/gui/painting/qpagesize.h +++ b/src/gui/painting/qpagesize.h @@ -290,6 +290,7 @@ public: private: friend class QPageSizePrivate; friend class QPlatformPrintDevice; + friend class QCupsPrintEnginePrivate; QPageSize(const QString &key, const QSize &pointSize, const QString &name); QPageSize(int windowsId, const QSize &pointSize, const QString &name); QPageSize(QPageSizePrivate &dd); diff --git a/src/gui/painting/qpdf.cpp b/src/gui/painting/qpdf.cpp index 147fa3f561..56cfc0f707 100644 --- a/src/gui/painting/qpdf.cpp +++ b/src/gui/painting/qpdf.cpp @@ -1425,19 +1425,60 @@ QPaintEngine::Type QPdfEngine::type() const return QPaintEngine::Pdf; } +void QPdfEngine::setResolution(int resolution) +{ + Q_D(QPdfEngine); + d->resolution = resolution; +} +int QPdfEngine::resolution() const +{ + Q_D(const QPdfEngine); + return d->resolution; +} +void QPdfEngine::setPageLayout(const QPageLayout &pageLayout) +{ + Q_D(QPdfEngine); + d->m_pageLayout = pageLayout; +} + +void QPdfEngine::setPageSize(const QPageSize &pageSize) +{ + Q_D(QPdfEngine); + d->m_pageLayout.setPageSize(pageSize); +} + +void QPdfEngine::setPageOrientation(QPageLayout::Orientation orientation) +{ + Q_D(QPdfEngine); + d->m_pageLayout.setOrientation(orientation); +} + +void QPdfEngine::setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) +{ + Q_D(QPdfEngine); + d->m_pageLayout.setUnits(units); + d->m_pageLayout.setMargins(margins); +} + +QPageLayout QPdfEngine::pageLayout() const +{ + Q_D(const QPdfEngine); + return d->m_pageLayout; +} + +// Metrics are in Device Pixels int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const { Q_D(const QPdfEngine); int val; - QRect r = d->pageRect(); switch (metricType) { case QPaintDevice::PdmWidth: - val = r.width(); + val = d->m_pageLayout.paintRectPixels(d->resolution).width(); break; case QPaintDevice::PdmHeight: - val = r.height(); + val = d->m_pageLayout.paintRectPixels(d->resolution).height(); break; case QPaintDevice::PdmDpiX: case QPaintDevice::PdmDpiY: @@ -1448,10 +1489,10 @@ int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const val = 1200; break; case QPaintDevice::PdmWidthMM: - val = qRound(r.width()*25.4/d->resolution); + val = qRound(d->m_pageLayout.paintRect(QPageLayout::Millimeter).width()); break; case QPaintDevice::PdmHeightMM: - val = qRound(r.height()*25.4/d->resolution); + val = qRound(d->m_pageLayout.paintRect(QPageLayout::Millimeter).height()); break; case QPaintDevice::PdmNumColors: val = INT_MAX; @@ -1469,21 +1510,12 @@ int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const return val; } -static inline QSizeF pageSizeToPostScriptPoints(const QSizeF &pageSizeMM) -{ -#define Q_MM(n) int((n * 720 + 127) / 254) - return QSizeF(Q_MM(pageSizeMM.width()), Q_MM(pageSizeMM.height())); -#undef Q_MM -} - QPdfEnginePrivate::QPdfEnginePrivate() : clipEnabled(false), allClipped(false), hasPen(true), hasBrush(false), simplePen(false), outDevice(0), ownsDevice(false), - fullPage(false), embedFonts(true), - landscape(false), + embedFonts(true), grayscale(false), - paperSize(pageSizeToPostScriptPoints(QSizeF(210, 297))), // A4 - leftMargin(10), topMargin(10), rightMargin(10), bottomMargin(10) // ~3.5 mm + m_pageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF(10, 10, 10, 10)) { resolution = 1200; currentObject = 1; @@ -1495,11 +1527,6 @@ QPdfEnginePrivate::QPdfEnginePrivate() stream = new QDataStream; } -void QPdfEnginePrivate::setPaperSize(const QSizeF &pageSizeMM) -{ - paperSize = pageSizeToPostScriptPoints(pageSizeMM); -} - bool QPdfEngine::begin(QPaintDevice *pdev) { Q_D(QPdfEngine); @@ -1581,31 +1608,6 @@ QPdfEnginePrivate::~QPdfEnginePrivate() delete stream; } -QRect QPdfEnginePrivate::paperRect() const -{ - int w = qRound(paperSize.width()*resolution/72.); - int h = qRound(paperSize.height()*resolution/72.); - - if (!landscape) - return QRect(0, 0, w, h); - else - return QRect(0, 0, h, w); -} - -QRect QPdfEnginePrivate::pageRect() const -{ - QRect r = paperRect(); - - if(!fullPage) - r.adjust(qRound(leftMargin*(resolution/72.)), - qRound(topMargin*(resolution/72.)), - -qRound(rightMargin*(resolution/72.)), - -qRound(bottomMargin*(resolution/72.))); - - return r; -} - - void QPdfEnginePrivate::writeHeader() { addXrefEntry(0,false); @@ -2615,9 +2617,9 @@ void QPdfEnginePrivate::drawTextItem(const QPointF &p, const QTextItemInt &ti) QTransform QPdfEnginePrivate::pageMatrix() const { qreal scale = 72./resolution; - QTransform tmp(scale, 0.0, 0.0, -scale, 0.0, height()); - if (!fullPage) { - QRect r = pageRect(); + QTransform tmp(scale, 0.0, 0.0, -scale, 0.0, m_pageLayout.fullRectPoints().height()); + if (m_pageLayout.mode() != QPageLayout::FullPageMode) { + QRect r = m_pageLayout.paintRectPixels(resolution); tmp.translate(r.left(), r.top()); } return tmp; @@ -2626,12 +2628,12 @@ QTransform QPdfEnginePrivate::pageMatrix() const void QPdfEnginePrivate::newPage() { if (currentPage && currentPage->pageSize.isEmpty()) - currentPage->pageSize = QSize(width(), height()); + currentPage->pageSize = m_pageLayout.fullRectPoints().size(); writePage(); delete currentPage; currentPage = new QPdfPage; - currentPage->pageSize = QSize(width(), height()); + currentPage->pageSize = m_pageLayout.fullRectPoints().size(); stroker.stream = currentPage; pages.append(requestObject()); diff --git a/src/gui/painting/qpdf_p.h b/src/gui/painting/qpdf_p.h index ae2d4b00ac..e9459eff83 100644 --- a/src/gui/painting/qpdf_p.h +++ b/src/gui/painting/qpdf_p.h @@ -64,6 +64,7 @@ #include "private/qpaintengine_p.h" #include "private/qfontengine_p.h" #include "private/qfontsubset_p.h" +#include "qpagelayout.h" // #define USE_NATIVE_GRADIENTS @@ -179,7 +180,9 @@ public: ~QPdfEngine() {} void setOutputFilename(const QString &filename); - inline void setResolution(int resolution); + + void setResolution(int resolution); + int resolution() const; // reimplementations QPaintEngine bool begin(QPaintDevice *pdev); @@ -207,6 +210,14 @@ public: // Printer stuff... bool newPage(); + // Page layout stuff + void setPageLayout(const QPageLayout &pageLayout); + void setPageSize(const QPageSize &pageSize); + void setPageOrientation(QPageLayout::Orientation orientation); + void setPageMargins(const QMarginsF &margins, QPageLayout::Unit units = QPageLayout::Point); + + QPageLayout pageLayout() const; + void setPen(); void setBrush(); void setupGraphicsState(QPaintEngine::DirtyFlags flags); @@ -224,19 +235,6 @@ public: inline uint requestObject() { return currentObject++; } - QRect paperRect() const; - QRect pageRect() const; - void setPaperSize(const QSizeF &pageSizeMM); - - int width() const { - QRect r = paperRect(); - return qRound(r.width()*72./resolution); - } - int height() const { - QRect r = paperRect(); - return qRound(r.height()*72./resolution); - } - void writeHeader(); void writeTail(); @@ -278,15 +276,12 @@ public: QString outputFileName; QString title; QString creator; - bool fullPage; bool embedFonts; int resolution; - bool landscape; bool grayscale; - // in postscript points - QSizeF paperSize; - qreal leftMargin, topMargin, rightMargin, bottomMargin; + // Page layout: size, orientation and margins + QPageLayout m_pageLayout; private: #ifdef USE_NATIVE_GRADIENTS @@ -325,12 +320,6 @@ private: QHash, uint > alphaCache; }; -void QPdfEngine::setResolution(int resolution) -{ - Q_D(QPdfEngine); - d->resolution = resolution; -} - QT_END_NAMESPACE #endif // QT_NO_PDF diff --git a/src/gui/painting/qpdfwriter.cpp b/src/gui/painting/qpdfwriter.cpp index 27fb8b1646..a6f752b129 100644 --- a/src/gui/painting/qpdfwriter.cpp +++ b/src/gui/painting/qpdfwriter.cpp @@ -166,7 +166,7 @@ void QPdfWriter::setPageSize(PageSize size) Q_D(const QPdfWriter); QPagedPaintDevice::setPageSize(size); - d->engine->d_func()->setPaperSize(pageSizeMM()); + d->engine->setPageSize(QPageSize(QPageSize::PageSizeId(size))); } /*! @@ -177,7 +177,7 @@ void QPdfWriter::setPageSizeMM(const QSizeF &size) Q_D(const QPdfWriter); QPagedPaintDevice::setPageSizeMM(size); - d->engine->d_func()->setPaperSize(pageSizeMM()); + d->engine->setPageSize(QPageSize(size, QPageSize::Millimeter)); } /*! @@ -212,10 +212,8 @@ void QPdfWriter::setMargins(const Margins &m) QPagedPaintDevice::setMargins(m); const qreal multiplier = 72./25.4; - d->engine->d_func()->leftMargin = m.left*multiplier; - d->engine->d_func()->rightMargin = m.right*multiplier; - d->engine->d_func()->topMargin = m.top*multiplier; - d->engine->d_func()->bottomMargin = m.bottom*multiplier; + d->engine->setPageMargins(QMarginsF(m.left * multiplier, m.top * multiplier, + m.right * multiplier, m.bottom * multiplier), QPageLayout::Point); } QT_END_NAMESPACE diff --git a/src/plugins/printsupport/cups/qcupsprintengine.cpp b/src/plugins/printsupport/cups/qcupsprintengine.cpp index 2fecdc00e9..c41ee4373c 100644 --- a/src/plugins/printsupport/cups/qcupsprintengine.cpp +++ b/src/plugins/printsupport/cups/qcupsprintengine.cpp @@ -57,6 +57,8 @@ QT_BEGIN_NAMESPACE +extern QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits); + QCupsPrintEngine::QCupsPrintEngine(QPrinter::PrinterMode m) : QPdfPrintEngine(*new QCupsPrintEnginePrivate(m)) { @@ -89,9 +91,8 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v Q_D(QCupsPrintEngine); switch (int(key)) { - case PPK_PaperSize: - d->printerPaperSize = QPrinter::PaperSize(value.toInt()); - d->setPaperSize(); + case PPK_PageSize: + d->setPageSize(QPageSize::PageSizeId(value.toInt())); break; case PPK_CupsPageRect: d->cupsPageRect = value.toRect(); @@ -104,8 +105,7 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v break; case PPK_CupsStringPageSize: case PPK_PaperName: - d->cupsStringPageSize = value.toString(); - d->setPaperName(); + d->setPaperName(value.toString()); break; case PPK_PrinterName: // prevent setting the defaults again for the same printer @@ -143,7 +143,7 @@ QVariant QCupsPrintEngine::property(PrintEnginePropertyKey key) const break; case PPK_CupsStringPageSize: case PPK_PaperName: - ret = d->cupsStringPageSize; + ret = d->m_pageLayout.pageSize().name(); break; default: ret = QPdfPrintEngine::property(key); @@ -215,8 +215,7 @@ void QCupsPrintEnginePrivate::closePrintDevice() prnName = def.printerName().toLocal8Bit(); } - if (!cupsStringPageSize.isEmpty()) - options.append(QPair("media", cupsStringPageSize.toLocal8Bit())); + options.append(QPair("media", m_pageLayout.pageSize().key().toLocal8Bit())); if (copies > 1) options.append(QPair("copies", QString::number(copies).toLocal8Bit())); @@ -229,7 +228,7 @@ void QCupsPrintEnginePrivate::closePrintDevice() options.append(QPair("sides", "one-sided")); break; case QPrinter::DuplexAuto: - if (!landscape) + if (m_pageLayout.orientation() == QPageLayout::Portrait) options.append(QPair("sides", "two-sided-long-edge")); else options.append(QPair("sides", "two-sided-short-edge")); @@ -242,7 +241,7 @@ void QCupsPrintEnginePrivate::closePrintDevice() break; } - if (QCUPSSupport::cupsVersion() >= 10300 && landscape) + if (QCUPSSupport::cupsVersion() >= 10300 && m_pageLayout.orientation() == QPageLayout::Landscape) options.append(QPair("landscape", "")); QStringList::const_iterator it = cupsOptions.constBegin(); @@ -267,43 +266,25 @@ void QCupsPrintEnginePrivate::closePrintDevice() } } -void QCupsPrintEnginePrivate::updatePaperSize() -{ - if (printerPaperSize == QPrinter::Custom) { - paperSize = customPaperSize; - } else if (!cupsPaperRect.isNull()) { - QRect r = cupsPaperRect; - paperSize = r.size(); - } else { - QPdf::PaperSize s = QPdf::paperSize(printerPaperSize); - paperSize = QSize(s.width, s.height); - } -} - -void QCupsPrintEnginePrivate::setPaperSize() +void QCupsPrintEnginePrivate::setPageSize(QPageSize::PageSizeId pageSizeId) { if (QCUPSSupport::isAvailable()) { QCUPSSupport cups; - QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(printerPaperSize)); + QSize size = QPageSize(pageSizeId).sizePoints(); if (cups.currentPPD()) { - cupsStringPageSize = QLatin1String("Custom"); const ppd_option_t* pageSizes = cups.pageSizes(); for (int i = 0; i < pageSizes->num_choices; ++i) { QByteArray cupsPageSize = pageSizes->choices[i].choice; QRect tmpCupsPaperRect = cups.paperRect(cupsPageSize); QRect tmpCupsPageRect = cups.pageRect(cupsPageSize); - if (qAbs(size.width - tmpCupsPaperRect.width()) < 5 && qAbs(size.height - tmpCupsPaperRect.height()) < 5) { + if (qAbs(size.width() - tmpCupsPaperRect.width()) < 5 && qAbs(size.height() - tmpCupsPaperRect.height()) < 5) { + QString key = QString::fromLocal8Bit(pageSizes->choices[i].choice); + QString name = QString::fromLocal8Bit(pageSizes->choices[i].text); cupsPaperRect = tmpCupsPaperRect; cupsPageRect = tmpCupsPageRect; - cupsStringPageSize = pageSizes->choices[i].text; - leftMargin = cupsPageRect.x() - cupsPaperRect.x(); - topMargin = cupsPageRect.y() - cupsPaperRect.y(); - rightMargin = cupsPaperRect.right() - cupsPageRect.right(); - bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom(); - - updatePaperSize(); + setPageSize(key, name); break; } } @@ -311,39 +292,22 @@ void QCupsPrintEnginePrivate::setPaperSize() } } -void QCupsPrintEnginePrivate::setPaperName() +void QCupsPrintEnginePrivate::setPaperName(const QString &paperName) { if (QCUPSSupport::isAvailable()) { QCUPSSupport cups; if (cups.currentPPD()) { const ppd_option_t* pageSizes = cups.pageSizes(); - bool foundPaperName = false; for (int i = 0; i < pageSizes->num_choices; ++i) { - if (cupsStringPageSize == pageSizes->choices[i].text) { - foundPaperName = true; - QByteArray cupsPageSize = pageSizes->choices[i].choice; - cupsPaperRect = cups.paperRect(cupsPageSize); - cupsPageRect = cups.pageRect(cupsPageSize); - leftMargin = cupsPageRect.x() - cupsPaperRect.x(); - topMargin = cupsPageRect.y() - cupsPaperRect.y(); - rightMargin = cupsPaperRect.right() - cupsPageRect.right(); - bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom(); - printerPaperSize = QPrinter::Custom; - customPaperSize = cupsPaperRect.size(); - for (int ps = 0; ps < QPrinter::NPageSize; ++ps) { - QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(ps)); - if (qAbs(size.width - cupsPaperRect.width()) < 5 && qAbs(size.height - cupsPaperRect.height()) < 5) { - printerPaperSize = static_cast(ps); - customPaperSize = QSize(); - break; - } - } - updatePaperSize(); + if (pageSizes->choices[i].text == paperName) { + QString key = QString::fromLocal8Bit(pageSizes->choices[i].choice); + QString name = QString::fromLocal8Bit(pageSizes->choices[i].text); + cupsPaperRect = cups.paperRect(pageSizes->choices[i].choice); + cupsPageRect = cups.pageRect(pageSizes->choices[i].choice); + setPageSize(key, name); break; } } - if (!foundPaperName) - cupsStringPageSize = QString(); } } } @@ -390,33 +354,30 @@ void QCupsPrintEnginePrivate::setCupsDefaults() QByteArray cupsPageSize; for (int i = 0; i < pageSizes->num_choices; ++i) { if (static_cast(pageSizes->choices[i].marked) == 1) { - cupsPageSize = pageSizes->choices[i].choice; - cupsStringPageSize = pageSizes->choices[i].text; + QString key = QString::fromLocal8Bit(pageSizes->choices[i].choice); + QString name = QString::fromLocal8Bit(pageSizes->choices[i].text); + cupsPaperRect = cups.paperRect(pageSizes->choices[i].choice); + cupsPageRect = cups.pageRect(pageSizes->choices[i].choice); + setPageSize(key, name); } } cupsOptions = cups.options(); - cupsPaperRect = cups.paperRect(cupsPageSize); - cupsPageRect = cups.pageRect(cupsPageSize); - - for (int ps = 0; ps < QPrinter::NPageSize; ++ps) { - QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(ps)); - if (qAbs(size.width - cupsPaperRect.width()) < 5 && qAbs(size.height - cupsPaperRect.height()) < 5) { - printerPaperSize = static_cast(ps); - - leftMargin = cupsPageRect.x() - cupsPaperRect.x(); - topMargin = cupsPageRect.y() - cupsPaperRect.y(); - rightMargin = cupsPaperRect.right() - cupsPageRect.right(); - bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom(); - - updatePaperSize(); - break; - } - } } } } +void QCupsPrintEnginePrivate::setPageSize(const QString &key, const QString &name) +{ + QSize size = QSize(cupsPaperRect.width(), cupsPaperRect.height()); + const qreal left = cupsPageRect.x() - cupsPaperRect.x(); + const qreal top = cupsPageRect.y() - cupsPaperRect.y(); + const qreal right = cupsPaperRect.right() - cupsPageRect.right(); + const qreal bottom = cupsPaperRect.bottom() - cupsPageRect.bottom(); + QMarginsF printable = qt_convertMargins(QMarginsF(left, top, right, bottom), + QPageLayout::Point, m_pageLayout.units()); + m_pageLayout.setPageSize(QPageSize(key, size, name), printable); +} QT_END_NAMESPACE diff --git a/src/plugins/printsupport/cups/qcupsprintengine_p.h b/src/plugins/printsupport/cups/qcupsprintengine_p.h index db947a0232..bc85205d50 100644 --- a/src/plugins/printsupport/cups/qcupsprintengine_p.h +++ b/src/plugins/printsupport/cups/qcupsprintengine_p.h @@ -96,15 +96,15 @@ public: void closePrintDevice(); void updatePaperSize(); - void setPaperSize(); - void setPaperName(); + void setPageSize(QPageSize::PageSizeId pageSizeId); + void setPaperName(const QString &name); void setCupsDefaults(); + void setPageSize(const QString &key, const QString &name); private: Q_DISABLE_COPY(QCupsPrintEnginePrivate) QStringList cupsOptions; - QString cupsStringPageSize; QRect cupsPaperRect; QRect cupsPageRect; QString cupsTempFile; diff --git a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp index 6801863a3a..d2975ad9da 100644 --- a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp +++ b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp @@ -371,8 +371,8 @@ void QPageSetupWidget::setupPrinter() const #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) else if (val.type() == QVariant::ByteArray) { for (int papersize = 0; papersize < QPrinter::NPageSize; ++papersize) { - QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(papersize)); - if (size.width == m_paperSize.width() && size.height == m_paperSize.height()) { + QSize size = QPageSize(QPageSize::PageSizeId(papersize)).sizePoints(); + if (size.width() == m_paperSize.width() && size.height() == m_paperSize.height()) { ps = static_cast(papersize); break; } diff --git a/src/printsupport/kernel/qprintengine_pdf.cpp b/src/printsupport/kernel/qprintengine_pdf.cpp index 6c65300462..2ddfb9c49e 100644 --- a/src/printsupport/kernel/qprintengine_pdf.cpp +++ b/src/printsupport/kernel/qprintengine_pdf.cpp @@ -63,34 +63,6 @@ QT_BEGIN_NAMESPACE -//#define FONT_DUMP - -extern QSizeF qt_paperSizeToQSizeF(QPrinter::PaperSize size); - -#define Q_MM(n) int((n * 720 + 127) / 254) -#define Q_IN(n) int(n * 72) - -static const char * const psToStr[QPrinter::NPageSize+1] = -{ - "A4", "B5", "Letter", "Legal", "Executive", - "A0", "A1", "A2", "A3", "A5", "A6", "A7", "A8", "A9", "B0", "B1", - "B10", "B2", "B3", "B4", "B6", "B7", "B8", "B9", "C5E", "Comm10E", - "DLE", "Folio", "Ledger", "Tabloid", 0 -}; - -QPdf::PaperSize QPdf::paperSize(QPrinter::PaperSize paperSize) -{ - QSizeF s = qt_paperSizeToQSizeF(paperSize); - PaperSize p = { Q_MM(s.width()), Q_MM(s.height()) }; - return p; -} - -const char *QPdf::paperSizeToString(QPrinter::PaperSize paperSize) -{ - return psToStr[paperSize]; -} - - QPdfPrintEngine::QPdfPrintEngine(QPrinter::PrinterMode m) : QPdfEngine(*new QPdfPrintEnginePrivate(m)) { @@ -163,8 +135,6 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va // The following keys are settings that are unsupported by the PDF PrintEngine case PPK_CustomBase: break; - case PPK_PaperName: - break; case PPK_WindowsPageSize: break; @@ -182,14 +152,17 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va d->title = value.toString(); break; case PPK_FullPage: - d->fullPage = value.toBool(); + if (value.toBool()) + d->m_pageLayout.setMode(QPageLayout::FullPageMode); + else + d->m_pageLayout.setMode(QPageLayout::StandardMode); break; case PPK_CopyCount: // fallthrough case PPK_NumberOfCopies: d->copies = value.toInt(); break; case PPK_Orientation: - d->landscape = (QPrinter::Orientation(value.toInt()) == QPrinter::Landscape); + d->m_pageLayout.setOrientation(QPageLayout::Orientation(value.toInt())); break; case PPK_OutputFileName: d->outputFileName = value.toString(); @@ -197,10 +170,23 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va case PPK_PageOrder: d->pageOrder = QPrinter::PageOrder(value.toInt()); break; - case PPK_PaperSize: - d->printerPaperSize = QPrinter::PaperSize(value.toInt()); - d->updatePaperSize(); + case PPK_PageSize: { + QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt())); + if (pageSize.isValid()) + d->m_pageLayout.setPageSize(pageSize); break; + } + case PPK_PaperName: { + QString name = value.toString(); + for (int i = 0; i <= QPageSize::LastPageSize; ++i) { + QPageSize pageSize = QPageSize(QPageSize::PageSizeId(i)); + if (name == pageSize.name()) { + d->m_pageLayout.setPageSize(pageSize); + break; + } + } + break; + } case PPK_PaperSource: d->paperSource = QPrinter::PaperSource(value.toInt()); break; @@ -223,19 +209,14 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va d->duplex = static_cast (value.toInt()); break; case PPK_CustomPaperSize: - d->printerPaperSize = QPrinter::Custom; - d->customPaperSize = value.toSizeF(); - d->updatePaperSize(); + d->m_pageLayout.setPageSize(QPageSize(value.toSizeF(), QPageSize::Point)); break; - case PPK_PageMargins: - { + case PPK_PageMargins: { QList margins(value.toList()); Q_ASSERT(margins.size() == 4); - d->leftMargin = margins.at(0).toReal(); - d->topMargin = margins.at(1).toReal(); - d->rightMargin = margins.at(2).toReal(); - d->bottomMargin = margins.at(3).toReal(); - d->pageMarginsSet = true; + d->m_pageLayout.setUnits(QPageLayout::Point); + d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(), + margins.at(2).toReal(), margins.at(3).toReal())); break; } // No default so that compiler will complain if new keys added and not handled in this engine @@ -254,9 +235,6 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const case PPK_CustomBase: // Special case, leave null break; - case PPK_PaperName: - ret = QString(); - break; case PPK_WindowsPageSize: // Special case, leave null break; @@ -275,7 +253,7 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const ret = d->title; break; case PPK_FullPage: - ret = d->fullPage; + ret = d->m_pageLayout.mode() == QPageLayout::FullPageMode; break; case PPK_CopyCount: ret = d->copies; @@ -287,7 +265,7 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const ret = d->copies; break; case PPK_Orientation: - ret = d->landscape ? QPrinter::Landscape : QPrinter::Portrait; + ret = d->m_pageLayout.orientation(); break; case PPK_OutputFileName: ret = d->outputFileName; @@ -295,8 +273,11 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const case PPK_PageOrder: ret = d->pageOrder; break; - case PPK_PaperSize: - ret = d->printerPaperSize; + case PPK_PageSize: + ret = d->m_pageLayout.pageSize().id(); + break; + case PPK_PaperName: + ret = d->m_pageLayout.pageSize().name(); break; case PPK_PaperSource: ret = d->paperSource; @@ -314,10 +295,10 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const ret = QList() << 72; break; case PPK_PaperRect: - ret = d->paperRect(); + ret = d->m_pageLayout.fullRectPixels(d->resolution); break; case PPK_PageRect: - ret = d->pageRect(); + ret = d->m_pageLayout.paintRectPixels(d->resolution); break; case PPK_SelectionOption: ret = d->selectionOption; @@ -329,17 +310,13 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const ret = d->duplex; break; case PPK_CustomPaperSize: - ret = d->customPaperSize; + ret = d->m_pageLayout.fullRectPoints().size(); break; - case PPK_PageMargins: - { - QList margins; - if (d->printerPaperSize == QPrinter::Custom && !d->pageMarginsSet) - margins << 0 << 0 << 0 << 0; - else - margins << d->leftMargin << d->topMargin - << d->rightMargin << d->bottomMargin; - ret = margins; + case PPK_PageMargins: { + QList list; + QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point); + list << margins.left() << margins.top() << margins.right() << margins.bottom(); + ret = list; break; } // No default so that compiler will complain if new keys added and not handled in this engine @@ -390,8 +367,6 @@ QPdfPrintEnginePrivate::QPdfPrintEnginePrivate(QPrinter::PrinterMode m) copies(1), pageOrder(QPrinter::FirstPageFirst), paperSource(QPrinter::Auto), - printerPaperSize(QPrinter::A4), - pageMarginsSet(false), fd(-1) { resolution = 72; @@ -405,18 +380,6 @@ QPdfPrintEnginePrivate::~QPdfPrintEnginePrivate() { } - -void QPdfPrintEnginePrivate::updatePaperSize() -{ - if (printerPaperSize == QPrinter::Custom) { - paperSize = customPaperSize; - } else { - QPdf::PaperSize s = QPdf::paperSize(printerPaperSize); - paperSize = QSize(s.width, s.height); - } -} - - QT_END_NAMESPACE #endif // QT_NO_PRINTER diff --git a/src/printsupport/kernel/qprintengine_pdf_p.h b/src/printsupport/kernel/qprintengine_pdf_p.h index eec6d48181..cc4044d1a0 100644 --- a/src/printsupport/kernel/qprintengine_pdf_p.h +++ b/src/printsupport/kernel/qprintengine_pdf_p.h @@ -77,16 +77,6 @@ class QPen; class QPointF; class QRegion; class QFile; -class QPdfPrintEngine; - -namespace QPdf { - - struct PaperSize { - int width, height; // in postscript points - }; - Q_PRINTSUPPORT_EXPORT PaperSize paperSize(QPrinter::PaperSize paperSize); - Q_PRINTSUPPORT_EXPORT const char *paperSizeToString(QPrinter::PaperSize paperSize); -} class QPdfPrintEnginePrivate; @@ -131,8 +121,6 @@ public: virtual bool openPrintDevice(); virtual void closePrintDevice(); - virtual void updatePaperSize(); - private: Q_DISABLE_COPY(QPdfPrintEnginePrivate) @@ -149,9 +137,6 @@ private: QPrinter::PageOrder pageOrder; QPrinter::PaperSource paperSource; - QPrinter::PaperSize printerPaperSize; - QSizeF customPaperSize; // in postscript points - bool pageMarginsSet; int fd; };