QPdfPaintEngine - Use QPageLayout and QPageSize

Switch internals of QPdfPageEngine and derived classes to use
QPageLayout and QPageSize to make handling of page layout and size
more consistent by removing multiple implementations.  In particular
remove all use of the QPdf namespace version of page size.

Change-Id: Ie820340015e8812c8162bd1a257dd0f51f4f0b85
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
John Layt 2013-12-29 20:19:48 +01:00 committed by The Qt Project
parent 4ab55a01e9
commit 0c04b31d27
9 changed files with 156 additions and 257 deletions

View File

@ -290,6 +290,7 @@ public:
private: private:
friend class QPageSizePrivate; friend class QPageSizePrivate;
friend class QPlatformPrintDevice; friend class QPlatformPrintDevice;
friend class QCupsPrintEnginePrivate;
QPageSize(const QString &key, const QSize &pointSize, const QString &name); QPageSize(const QString &key, const QSize &pointSize, const QString &name);
QPageSize(int windowsId, const QSize &pointSize, const QString &name); QPageSize(int windowsId, const QSize &pointSize, const QString &name);
QPageSize(QPageSizePrivate &dd); QPageSize(QPageSizePrivate &dd);

View File

@ -1425,19 +1425,60 @@ QPaintEngine::Type QPdfEngine::type() const
return QPaintEngine::Pdf; 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 int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const
{ {
Q_D(const QPdfEngine); Q_D(const QPdfEngine);
int val; int val;
QRect r = d->pageRect();
switch (metricType) { switch (metricType) {
case QPaintDevice::PdmWidth: case QPaintDevice::PdmWidth:
val = r.width(); val = d->m_pageLayout.paintRectPixels(d->resolution).width();
break; break;
case QPaintDevice::PdmHeight: case QPaintDevice::PdmHeight:
val = r.height(); val = d->m_pageLayout.paintRectPixels(d->resolution).height();
break; break;
case QPaintDevice::PdmDpiX: case QPaintDevice::PdmDpiX:
case QPaintDevice::PdmDpiY: case QPaintDevice::PdmDpiY:
@ -1448,10 +1489,10 @@ int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const
val = 1200; val = 1200;
break; break;
case QPaintDevice::PdmWidthMM: case QPaintDevice::PdmWidthMM:
val = qRound(r.width()*25.4/d->resolution); val = qRound(d->m_pageLayout.paintRect(QPageLayout::Millimeter).width());
break; break;
case QPaintDevice::PdmHeightMM: case QPaintDevice::PdmHeightMM:
val = qRound(r.height()*25.4/d->resolution); val = qRound(d->m_pageLayout.paintRect(QPageLayout::Millimeter).height());
break; break;
case QPaintDevice::PdmNumColors: case QPaintDevice::PdmNumColors:
val = INT_MAX; val = INT_MAX;
@ -1469,21 +1510,12 @@ int QPdfEngine::metric(QPaintDevice::PaintDeviceMetric metricType) const
return val; 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() QPdfEnginePrivate::QPdfEnginePrivate()
: clipEnabled(false), allClipped(false), hasPen(true), hasBrush(false), simplePen(false), : clipEnabled(false), allClipped(false), hasPen(true), hasBrush(false), simplePen(false),
outDevice(0), ownsDevice(false), outDevice(0), ownsDevice(false),
fullPage(false), embedFonts(true), embedFonts(true),
landscape(false),
grayscale(false), grayscale(false),
paperSize(pageSizeToPostScriptPoints(QSizeF(210, 297))), // A4 m_pageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF(10, 10, 10, 10))
leftMargin(10), topMargin(10), rightMargin(10), bottomMargin(10) // ~3.5 mm
{ {
resolution = 1200; resolution = 1200;
currentObject = 1; currentObject = 1;
@ -1495,11 +1527,6 @@ QPdfEnginePrivate::QPdfEnginePrivate()
stream = new QDataStream; stream = new QDataStream;
} }
void QPdfEnginePrivate::setPaperSize(const QSizeF &pageSizeMM)
{
paperSize = pageSizeToPostScriptPoints(pageSizeMM);
}
bool QPdfEngine::begin(QPaintDevice *pdev) bool QPdfEngine::begin(QPaintDevice *pdev)
{ {
Q_D(QPdfEngine); Q_D(QPdfEngine);
@ -1581,31 +1608,6 @@ QPdfEnginePrivate::~QPdfEnginePrivate()
delete stream; 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() void QPdfEnginePrivate::writeHeader()
{ {
addXrefEntry(0,false); addXrefEntry(0,false);
@ -2615,9 +2617,9 @@ void QPdfEnginePrivate::drawTextItem(const QPointF &p, const QTextItemInt &ti)
QTransform QPdfEnginePrivate::pageMatrix() const QTransform QPdfEnginePrivate::pageMatrix() const
{ {
qreal scale = 72./resolution; qreal scale = 72./resolution;
QTransform tmp(scale, 0.0, 0.0, -scale, 0.0, height()); QTransform tmp(scale, 0.0, 0.0, -scale, 0.0, m_pageLayout.fullRectPoints().height());
if (!fullPage) { if (m_pageLayout.mode() != QPageLayout::FullPageMode) {
QRect r = pageRect(); QRect r = m_pageLayout.paintRectPixels(resolution);
tmp.translate(r.left(), r.top()); tmp.translate(r.left(), r.top());
} }
return tmp; return tmp;
@ -2626,12 +2628,12 @@ QTransform QPdfEnginePrivate::pageMatrix() const
void QPdfEnginePrivate::newPage() void QPdfEnginePrivate::newPage()
{ {
if (currentPage && currentPage->pageSize.isEmpty()) if (currentPage && currentPage->pageSize.isEmpty())
currentPage->pageSize = QSize(width(), height()); currentPage->pageSize = m_pageLayout.fullRectPoints().size();
writePage(); writePage();
delete currentPage; delete currentPage;
currentPage = new QPdfPage; currentPage = new QPdfPage;
currentPage->pageSize = QSize(width(), height()); currentPage->pageSize = m_pageLayout.fullRectPoints().size();
stroker.stream = currentPage; stroker.stream = currentPage;
pages.append(requestObject()); pages.append(requestObject());

View File

@ -64,6 +64,7 @@
#include "private/qpaintengine_p.h" #include "private/qpaintengine_p.h"
#include "private/qfontengine_p.h" #include "private/qfontengine_p.h"
#include "private/qfontsubset_p.h" #include "private/qfontsubset_p.h"
#include "qpagelayout.h"
// #define USE_NATIVE_GRADIENTS // #define USE_NATIVE_GRADIENTS
@ -179,7 +180,9 @@ public:
~QPdfEngine() {} ~QPdfEngine() {}
void setOutputFilename(const QString &filename); void setOutputFilename(const QString &filename);
inline void setResolution(int resolution);
void setResolution(int resolution);
int resolution() const;
// reimplementations QPaintEngine // reimplementations QPaintEngine
bool begin(QPaintDevice *pdev); bool begin(QPaintDevice *pdev);
@ -207,6 +210,14 @@ public:
// Printer stuff... // Printer stuff...
bool newPage(); 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 setPen();
void setBrush(); void setBrush();
void setupGraphicsState(QPaintEngine::DirtyFlags flags); void setupGraphicsState(QPaintEngine::DirtyFlags flags);
@ -224,19 +235,6 @@ public:
inline uint requestObject() { return currentObject++; } 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 writeHeader();
void writeTail(); void writeTail();
@ -278,15 +276,12 @@ public:
QString outputFileName; QString outputFileName;
QString title; QString title;
QString creator; QString creator;
bool fullPage;
bool embedFonts; bool embedFonts;
int resolution; int resolution;
bool landscape;
bool grayscale; bool grayscale;
// in postscript points // Page layout: size, orientation and margins
QSizeF paperSize; QPageLayout m_pageLayout;
qreal leftMargin, topMargin, rightMargin, bottomMargin;
private: private:
#ifdef USE_NATIVE_GRADIENTS #ifdef USE_NATIVE_GRADIENTS
@ -325,12 +320,6 @@ private:
QHash<QPair<uint, uint>, uint > alphaCache; QHash<QPair<uint, uint>, uint > alphaCache;
}; };
void QPdfEngine::setResolution(int resolution)
{
Q_D(QPdfEngine);
d->resolution = resolution;
}
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // QT_NO_PDF #endif // QT_NO_PDF

View File

@ -166,7 +166,7 @@ void QPdfWriter::setPageSize(PageSize size)
Q_D(const QPdfWriter); Q_D(const QPdfWriter);
QPagedPaintDevice::setPageSize(size); 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); Q_D(const QPdfWriter);
QPagedPaintDevice::setPageSizeMM(size); 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); QPagedPaintDevice::setMargins(m);
const qreal multiplier = 72./25.4; const qreal multiplier = 72./25.4;
d->engine->d_func()->leftMargin = m.left*multiplier; d->engine->setPageMargins(QMarginsF(m.left * multiplier, m.top * multiplier,
d->engine->d_func()->rightMargin = m.right*multiplier; m.right * multiplier, m.bottom * multiplier), QPageLayout::Point);
d->engine->d_func()->topMargin = m.top*multiplier;
d->engine->d_func()->bottomMargin = m.bottom*multiplier;
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -57,6 +57,8 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
extern QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits);
QCupsPrintEngine::QCupsPrintEngine(QPrinter::PrinterMode m) QCupsPrintEngine::QCupsPrintEngine(QPrinter::PrinterMode m)
: QPdfPrintEngine(*new QCupsPrintEnginePrivate(m)) : QPdfPrintEngine(*new QCupsPrintEnginePrivate(m))
{ {
@ -89,9 +91,8 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v
Q_D(QCupsPrintEngine); Q_D(QCupsPrintEngine);
switch (int(key)) { switch (int(key)) {
case PPK_PaperSize: case PPK_PageSize:
d->printerPaperSize = QPrinter::PaperSize(value.toInt()); d->setPageSize(QPageSize::PageSizeId(value.toInt()));
d->setPaperSize();
break; break;
case PPK_CupsPageRect: case PPK_CupsPageRect:
d->cupsPageRect = value.toRect(); d->cupsPageRect = value.toRect();
@ -104,8 +105,7 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v
break; break;
case PPK_CupsStringPageSize: case PPK_CupsStringPageSize:
case PPK_PaperName: case PPK_PaperName:
d->cupsStringPageSize = value.toString(); d->setPaperName(value.toString());
d->setPaperName();
break; break;
case PPK_PrinterName: case PPK_PrinterName:
// prevent setting the defaults again for the same printer // prevent setting the defaults again for the same printer
@ -143,7 +143,7 @@ QVariant QCupsPrintEngine::property(PrintEnginePropertyKey key) const
break; break;
case PPK_CupsStringPageSize: case PPK_CupsStringPageSize:
case PPK_PaperName: case PPK_PaperName:
ret = d->cupsStringPageSize; ret = d->m_pageLayout.pageSize().name();
break; break;
default: default:
ret = QPdfPrintEngine::property(key); ret = QPdfPrintEngine::property(key);
@ -215,8 +215,7 @@ void QCupsPrintEnginePrivate::closePrintDevice()
prnName = def.printerName().toLocal8Bit(); prnName = def.printerName().toLocal8Bit();
} }
if (!cupsStringPageSize.isEmpty()) options.append(QPair<QByteArray, QByteArray>("media", m_pageLayout.pageSize().key().toLocal8Bit()));
options.append(QPair<QByteArray, QByteArray>("media", cupsStringPageSize.toLocal8Bit()));
if (copies > 1) if (copies > 1)
options.append(QPair<QByteArray, QByteArray>("copies", QString::number(copies).toLocal8Bit())); options.append(QPair<QByteArray, QByteArray>("copies", QString::number(copies).toLocal8Bit()));
@ -229,7 +228,7 @@ void QCupsPrintEnginePrivate::closePrintDevice()
options.append(QPair<QByteArray, QByteArray>("sides", "one-sided")); options.append(QPair<QByteArray, QByteArray>("sides", "one-sided"));
break; break;
case QPrinter::DuplexAuto: case QPrinter::DuplexAuto:
if (!landscape) if (m_pageLayout.orientation() == QPageLayout::Portrait)
options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge")); options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge"));
else else
options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-short-edge")); options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-short-edge"));
@ -242,7 +241,7 @@ void QCupsPrintEnginePrivate::closePrintDevice()
break; break;
} }
if (QCUPSSupport::cupsVersion() >= 10300 && landscape) if (QCUPSSupport::cupsVersion() >= 10300 && m_pageLayout.orientation() == QPageLayout::Landscape)
options.append(QPair<QByteArray, QByteArray>("landscape", "")); options.append(QPair<QByteArray, QByteArray>("landscape", ""));
QStringList::const_iterator it = cupsOptions.constBegin(); QStringList::const_iterator it = cupsOptions.constBegin();
@ -267,43 +266,25 @@ void QCupsPrintEnginePrivate::closePrintDevice()
} }
} }
void QCupsPrintEnginePrivate::updatePaperSize() void QCupsPrintEnginePrivate::setPageSize(QPageSize::PageSizeId pageSizeId)
{
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()
{ {
if (QCUPSSupport::isAvailable()) { if (QCUPSSupport::isAvailable()) {
QCUPSSupport cups; QCUPSSupport cups;
QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(printerPaperSize)); QSize size = QPageSize(pageSizeId).sizePoints();
if (cups.currentPPD()) { if (cups.currentPPD()) {
cupsStringPageSize = QLatin1String("Custom");
const ppd_option_t* pageSizes = cups.pageSizes(); const ppd_option_t* pageSizes = cups.pageSizes();
for (int i = 0; i < pageSizes->num_choices; ++i) { for (int i = 0; i < pageSizes->num_choices; ++i) {
QByteArray cupsPageSize = pageSizes->choices[i].choice; QByteArray cupsPageSize = pageSizes->choices[i].choice;
QRect tmpCupsPaperRect = cups.paperRect(cupsPageSize); QRect tmpCupsPaperRect = cups.paperRect(cupsPageSize);
QRect tmpCupsPageRect = cups.pageRect(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; cupsPaperRect = tmpCupsPaperRect;
cupsPageRect = tmpCupsPageRect; cupsPageRect = tmpCupsPageRect;
cupsStringPageSize = pageSizes->choices[i].text; setPageSize(key, name);
leftMargin = cupsPageRect.x() - cupsPaperRect.x();
topMargin = cupsPageRect.y() - cupsPaperRect.y();
rightMargin = cupsPaperRect.right() - cupsPageRect.right();
bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom();
updatePaperSize();
break; break;
} }
} }
@ -311,39 +292,22 @@ void QCupsPrintEnginePrivate::setPaperSize()
} }
} }
void QCupsPrintEnginePrivate::setPaperName() void QCupsPrintEnginePrivate::setPaperName(const QString &paperName)
{ {
if (QCUPSSupport::isAvailable()) { if (QCUPSSupport::isAvailable()) {
QCUPSSupport cups; QCUPSSupport cups;
if (cups.currentPPD()) { if (cups.currentPPD()) {
const ppd_option_t* pageSizes = cups.pageSizes(); const ppd_option_t* pageSizes = cups.pageSizes();
bool foundPaperName = false;
for (int i = 0; i < pageSizes->num_choices; ++i) { for (int i = 0; i < pageSizes->num_choices; ++i) {
if (cupsStringPageSize == pageSizes->choices[i].text) { if (pageSizes->choices[i].text == paperName) {
foundPaperName = true; QString key = QString::fromLocal8Bit(pageSizes->choices[i].choice);
QByteArray cupsPageSize = pageSizes->choices[i].choice; QString name = QString::fromLocal8Bit(pageSizes->choices[i].text);
cupsPaperRect = cups.paperRect(cupsPageSize); cupsPaperRect = cups.paperRect(pageSizes->choices[i].choice);
cupsPageRect = cups.pageRect(cupsPageSize); cupsPageRect = cups.pageRect(pageSizes->choices[i].choice);
leftMargin = cupsPageRect.x() - cupsPaperRect.x(); setPageSize(key, name);
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<QPrinter::PaperSize>(ps);
customPaperSize = QSize();
break; break;
} }
} }
updatePaperSize();
break;
}
}
if (!foundPaperName)
cupsStringPageSize = QString();
} }
} }
} }
@ -390,33 +354,30 @@ void QCupsPrintEnginePrivate::setCupsDefaults()
QByteArray cupsPageSize; QByteArray cupsPageSize;
for (int i = 0; i < pageSizes->num_choices; ++i) { for (int i = 0; i < pageSizes->num_choices; ++i) {
if (static_cast<int>(pageSizes->choices[i].marked) == 1) { if (static_cast<int>(pageSizes->choices[i].marked) == 1) {
cupsPageSize = pageSizes->choices[i].choice; QString key = QString::fromLocal8Bit(pageSizes->choices[i].choice);
cupsStringPageSize = pageSizes->choices[i].text; 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(); 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<QPrinter::PaperSize>(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 QT_END_NAMESPACE

View File

@ -96,15 +96,15 @@ public:
void closePrintDevice(); void closePrintDevice();
void updatePaperSize(); void updatePaperSize();
void setPaperSize(); void setPageSize(QPageSize::PageSizeId pageSizeId);
void setPaperName(); void setPaperName(const QString &name);
void setCupsDefaults(); void setCupsDefaults();
void setPageSize(const QString &key, const QString &name);
private: private:
Q_DISABLE_COPY(QCupsPrintEnginePrivate) Q_DISABLE_COPY(QCupsPrintEnginePrivate)
QStringList cupsOptions; QStringList cupsOptions;
QString cupsStringPageSize;
QRect cupsPaperRect; QRect cupsPaperRect;
QRect cupsPageRect; QRect cupsPageRect;
QString cupsTempFile; QString cupsTempFile;

View File

@ -371,8 +371,8 @@ void QPageSetupWidget::setupPrinter() const
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
else if (val.type() == QVariant::ByteArray) { else if (val.type() == QVariant::ByteArray) {
for (int papersize = 0; papersize < QPrinter::NPageSize; ++papersize) { for (int papersize = 0; papersize < QPrinter::NPageSize; ++papersize) {
QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(papersize)); QSize size = QPageSize(QPageSize::PageSizeId(papersize)).sizePoints();
if (size.width == m_paperSize.width() && size.height == m_paperSize.height()) { if (size.width() == m_paperSize.width() && size.height() == m_paperSize.height()) {
ps = static_cast<QPrinter::PaperSize>(papersize); ps = static_cast<QPrinter::PaperSize>(papersize);
break; break;
} }

View File

@ -63,34 +63,6 @@
QT_BEGIN_NAMESPACE 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) QPdfPrintEngine::QPdfPrintEngine(QPrinter::PrinterMode m)
: QPdfEngine(*new QPdfPrintEnginePrivate(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 // The following keys are settings that are unsupported by the PDF PrintEngine
case PPK_CustomBase: case PPK_CustomBase:
break; break;
case PPK_PaperName:
break;
case PPK_WindowsPageSize: case PPK_WindowsPageSize:
break; break;
@ -182,14 +152,17 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va
d->title = value.toString(); d->title = value.toString();
break; break;
case PPK_FullPage: case PPK_FullPage:
d->fullPage = value.toBool(); if (value.toBool())
d->m_pageLayout.setMode(QPageLayout::FullPageMode);
else
d->m_pageLayout.setMode(QPageLayout::StandardMode);
break; break;
case PPK_CopyCount: // fallthrough case PPK_CopyCount: // fallthrough
case PPK_NumberOfCopies: case PPK_NumberOfCopies:
d->copies = value.toInt(); d->copies = value.toInt();
break; break;
case PPK_Orientation: case PPK_Orientation:
d->landscape = (QPrinter::Orientation(value.toInt()) == QPrinter::Landscape); d->m_pageLayout.setOrientation(QPageLayout::Orientation(value.toInt()));
break; break;
case PPK_OutputFileName: case PPK_OutputFileName:
d->outputFileName = value.toString(); d->outputFileName = value.toString();
@ -197,10 +170,23 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va
case PPK_PageOrder: case PPK_PageOrder:
d->pageOrder = QPrinter::PageOrder(value.toInt()); d->pageOrder = QPrinter::PageOrder(value.toInt());
break; break;
case PPK_PaperSize: case PPK_PageSize: {
d->printerPaperSize = QPrinter::PaperSize(value.toInt()); QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
d->updatePaperSize(); if (pageSize.isValid())
d->m_pageLayout.setPageSize(pageSize);
break; 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: case PPK_PaperSource:
d->paperSource = QPrinter::PaperSource(value.toInt()); d->paperSource = QPrinter::PaperSource(value.toInt());
break; break;
@ -223,19 +209,14 @@ void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va
d->duplex = static_cast<QPrinter::DuplexMode> (value.toInt()); d->duplex = static_cast<QPrinter::DuplexMode> (value.toInt());
break; break;
case PPK_CustomPaperSize: case PPK_CustomPaperSize:
d->printerPaperSize = QPrinter::Custom; d->m_pageLayout.setPageSize(QPageSize(value.toSizeF(), QPageSize::Point));
d->customPaperSize = value.toSizeF();
d->updatePaperSize();
break; break;
case PPK_PageMargins: case PPK_PageMargins: {
{
QList<QVariant> margins(value.toList()); QList<QVariant> margins(value.toList());
Q_ASSERT(margins.size() == 4); Q_ASSERT(margins.size() == 4);
d->leftMargin = margins.at(0).toReal(); d->m_pageLayout.setUnits(QPageLayout::Point);
d->topMargin = margins.at(1).toReal(); d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
d->rightMargin = margins.at(2).toReal(); margins.at(2).toReal(), margins.at(3).toReal()));
d->bottomMargin = margins.at(3).toReal();
d->pageMarginsSet = true;
break; break;
} }
// No default so that compiler will complain if new keys added and not handled in this engine // 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: case PPK_CustomBase:
// Special case, leave null // Special case, leave null
break; break;
case PPK_PaperName:
ret = QString();
break;
case PPK_WindowsPageSize: case PPK_WindowsPageSize:
// Special case, leave null // Special case, leave null
break; break;
@ -275,7 +253,7 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const
ret = d->title; ret = d->title;
break; break;
case PPK_FullPage: case PPK_FullPage:
ret = d->fullPage; ret = d->m_pageLayout.mode() == QPageLayout::FullPageMode;
break; break;
case PPK_CopyCount: case PPK_CopyCount:
ret = d->copies; ret = d->copies;
@ -287,7 +265,7 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const
ret = d->copies; ret = d->copies;
break; break;
case PPK_Orientation: case PPK_Orientation:
ret = d->landscape ? QPrinter::Landscape : QPrinter::Portrait; ret = d->m_pageLayout.orientation();
break; break;
case PPK_OutputFileName: case PPK_OutputFileName:
ret = d->outputFileName; ret = d->outputFileName;
@ -295,8 +273,11 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const
case PPK_PageOrder: case PPK_PageOrder:
ret = d->pageOrder; ret = d->pageOrder;
break; break;
case PPK_PaperSize: case PPK_PageSize:
ret = d->printerPaperSize; ret = d->m_pageLayout.pageSize().id();
break;
case PPK_PaperName:
ret = d->m_pageLayout.pageSize().name();
break; break;
case PPK_PaperSource: case PPK_PaperSource:
ret = d->paperSource; ret = d->paperSource;
@ -314,10 +295,10 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const
ret = QList<QVariant>() << 72; ret = QList<QVariant>() << 72;
break; break;
case PPK_PaperRect: case PPK_PaperRect:
ret = d->paperRect(); ret = d->m_pageLayout.fullRectPixels(d->resolution);
break; break;
case PPK_PageRect: case PPK_PageRect:
ret = d->pageRect(); ret = d->m_pageLayout.paintRectPixels(d->resolution);
break; break;
case PPK_SelectionOption: case PPK_SelectionOption:
ret = d->selectionOption; ret = d->selectionOption;
@ -329,17 +310,13 @@ QVariant QPdfPrintEngine::property(PrintEnginePropertyKey key) const
ret = d->duplex; ret = d->duplex;
break; break;
case PPK_CustomPaperSize: case PPK_CustomPaperSize:
ret = d->customPaperSize; ret = d->m_pageLayout.fullRectPoints().size();
break; break;
case PPK_PageMargins: case PPK_PageMargins: {
{ QList<QVariant> list;
QList<QVariant> margins; QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point);
if (d->printerPaperSize == QPrinter::Custom && !d->pageMarginsSet) list << margins.left() << margins.top() << margins.right() << margins.bottom();
margins << 0 << 0 << 0 << 0; ret = list;
else
margins << d->leftMargin << d->topMargin
<< d->rightMargin << d->bottomMargin;
ret = margins;
break; break;
} }
// No default so that compiler will complain if new keys added and not handled in this engine // 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), copies(1),
pageOrder(QPrinter::FirstPageFirst), pageOrder(QPrinter::FirstPageFirst),
paperSource(QPrinter::Auto), paperSource(QPrinter::Auto),
printerPaperSize(QPrinter::A4),
pageMarginsSet(false),
fd(-1) fd(-1)
{ {
resolution = 72; 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 QT_END_NAMESPACE
#endif // QT_NO_PRINTER #endif // QT_NO_PRINTER

View File

@ -77,16 +77,6 @@ class QPen;
class QPointF; class QPointF;
class QRegion; class QRegion;
class QFile; 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; class QPdfPrintEnginePrivate;
@ -131,8 +121,6 @@ public:
virtual bool openPrintDevice(); virtual bool openPrintDevice();
virtual void closePrintDevice(); virtual void closePrintDevice();
virtual void updatePaperSize();
private: private:
Q_DISABLE_COPY(QPdfPrintEnginePrivate) Q_DISABLE_COPY(QPdfPrintEnginePrivate)
@ -149,9 +137,6 @@ private:
QPrinter::PageOrder pageOrder; QPrinter::PageOrder pageOrder;
QPrinter::PaperSource paperSource; QPrinter::PaperSource paperSource;
QPrinter::PaperSize printerPaperSize;
QSizeF customPaperSize; // in postscript points
bool pageMarginsSet;
int fd; int fd;
}; };