summaryrefslogtreecommitdiffstats
path: root/src/plugins/printsupport/cups/qcupsprintengine.cpp
diff options
context:
space:
mode:
authorJohn Layt <jlayt@kde.org>2013-12-29 20:19:48 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-03-17 13:45:49 +0100
commit0c04b31d2715f68aa883107b4aa593fd95aefdfe (patch)
treeb882b4835f0acd3b188e473c99170ac12951eacc /src/plugins/printsupport/cups/qcupsprintengine.cpp
parent4ab55a01e96b6ac398d3a9afa2c93032e305f30b (diff)
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>
Diffstat (limited to 'src/plugins/printsupport/cups/qcupsprintengine.cpp')
-rw-r--r--src/plugins/printsupport/cups/qcupsprintengine.cpp115
1 files changed, 38 insertions, 77 deletions
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<QByteArray, QByteArray>("media", cupsStringPageSize.toLocal8Bit()));
+ options.append(QPair<QByteArray, QByteArray>("media", m_pageLayout.pageSize().key().toLocal8Bit()));
if (copies > 1)
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"));
break;
case QPrinter::DuplexAuto:
- if (!landscape)
+ if (m_pageLayout.orientation() == QPageLayout::Portrait)
options.append(QPair<QByteArray, QByteArray>("sides", "two-sided-long-edge"));
else
options.append(QPair<QByteArray, QByteArray>("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<QByteArray, QByteArray>("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<QPrinter::PaperSize>(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<int>(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<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