From 39a902c0081f442141353c466ada09d62036c04e Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Sat, 29 Dec 2012 17:08:12 +0100 Subject: Add support for setting/getting the paper name on the QPrinter This adds support for specifying a paper name which will be set on the printer if it is available for the driver. Change-Id: Id7fd0c8cf68745db3d7a8de7e2ac98d3e2ba9b79 Reviewed-by: Pierre Rossi --- src/plugins/printsupport/cups/qcupsprintengine.cpp | 47 +++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'src/plugins/printsupport/cups/qcupsprintengine.cpp') diff --git a/src/plugins/printsupport/cups/qcupsprintengine.cpp b/src/plugins/printsupport/cups/qcupsprintengine.cpp index 21895e9d28..1c86420cb2 100644 --- a/src/plugins/printsupport/cups/qcupsprintengine.cpp +++ b/src/plugins/printsupport/cups/qcupsprintengine.cpp @@ -103,7 +103,9 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v d->cupsOptions = value.toStringList(); break; case PPK_CupsStringPageSize: + case PPK_PaperName: d->cupsStringPageSize = value.toString(); + d->setPaperName(); break; case PPK_PrinterName: // prevent setting the defaults again for the same printer @@ -140,6 +142,7 @@ QVariant QCupsPrintEngine::property(PrintEnginePropertyKey key) const ret = d->cupsOptions; break; case PPK_CupsStringPageSize: + case PPK_PaperName: ret = d->cupsStringPageSize; break; default: @@ -284,6 +287,7 @@ void QCupsPrintEnginePrivate::setPaperSize() QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(printerPaperSize)); 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; @@ -293,7 +297,7 @@ void QCupsPrintEnginePrivate::setPaperSize() if (qAbs(size.width - tmpCupsPaperRect.width()) < 5 && qAbs(size.height - tmpCupsPaperRect.height()) < 5) { cupsPaperRect = tmpCupsPaperRect; cupsPageRect = tmpCupsPageRect; - + cupsStringPageSize = pageSizes->choices[i].text; leftMargin = cupsPageRect.x() - cupsPaperRect.x(); topMargin = cupsPageRect.y() - cupsPaperRect.y(); rightMargin = cupsPaperRect.right() - cupsPageRect.right(); @@ -307,6 +311,43 @@ void QCupsPrintEnginePrivate::setPaperSize() } } +void QCupsPrintEnginePrivate::setPaperName() +{ + 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(); + break; + } + } + if (!foundPaperName) + cupsStringPageSize = QString(); + } + } +} + void QCupsPrintEnginePrivate::setCupsDefaults() { if (QCUPSSupport::isAvailable()) { @@ -348,8 +389,10 @@ void QCupsPrintEnginePrivate::setCupsDefaults() const ppd_option_t* pageSizes = cups.pageSizes(); QByteArray cupsPageSize; for (int i = 0; i < pageSizes->num_choices; ++i) { - if (static_cast(pageSizes->choices[i].marked) == 1) + if (static_cast(pageSizes->choices[i].marked) == 1) { cupsPageSize = pageSizes->choices[i].choice; + cupsStringPageSize = pageSizes->choices[i].text; + } } cupsOptions = cups.options(); -- cgit v1.2.3