QtPrintSupport: use PPD defaults in CUPS plugin
authorTeemu Katajisto <teemu.katajisto@digia.com>
Wed, 22 Aug 2012 09:05:53 +0000 (12:05 +0300)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 26 Sep 2012 08:08:59 +0000 (10:08 +0200)
Initialize CUPS plugin with default values from the PPD file. Implement
paper size handling when using CUPS paper sizes to make paper sizes to
map properly when used through QPrinter interface.

Change-Id: I84d20aa4b1c7250fca754aecde419467f57ef16b
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/plugins/printsupport/cups/qcupsprintengine.cpp
src/plugins/printsupport/cups/qcupsprintengine_p.h

index 3d360a5..82735ee 100644 (file)
@@ -70,6 +70,7 @@ QCupsPrintEngine::QCupsPrintEngine(QPrinter::PrinterMode m)
         for (int i = 0; i <  prnCount; ++i) {
             if (printers[i].is_default) {
                 d->printerName = QString::fromLocal8Bit(printers[i].name);
+                d->setCupsDefaults();
                 break;
             }
         }
@@ -88,6 +89,10 @@ 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();
+        break;
     case PPK_CupsPageRect:
         d->cupsPageRect = value.toRect();
         break;
@@ -100,6 +105,13 @@ void QCupsPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &v
     case PPK_CupsStringPageSize:
         d->cupsStringPageSize = value.toString();
         break;
+    case PPK_PrinterName:
+        // prevent setting the defaults again for the same printer
+        if (d->printerName != value.toString()) {
+            d->printerName = value.toString();
+            d->setCupsDefaults();
+        }
+        break;
     default:
         QPdfPrintEngine::setProperty(key, value);
         break;
@@ -264,6 +276,104 @@ void QCupsPrintEnginePrivate::updatePaperSize()
     }
 }
 
+void QCupsPrintEnginePrivate::setPaperSize()
+{
+    if (QCUPSSupport::isAvailable()) {
+        QCUPSSupport cups;
+        QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(printerPaperSize));
+
+        if (cups.currentPPD()) {
+            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) {
+                    cupsPaperRect = tmpCupsPaperRect;
+                    cupsPageRect = tmpCupsPageRect;
+
+                    leftMargin = cupsPageRect.x() - cupsPaperRect.x();
+                    topMargin = cupsPageRect.y() - cupsPaperRect.y();
+                    rightMargin = cupsPaperRect.right() - cupsPageRect.right();
+                    bottomMargin = cupsPaperRect.bottom() - cupsPageRect.bottom();
+
+                    updatePaperSize();
+                    break;
+                }
+            }
+        }
+    }
+}
+
+void QCupsPrintEnginePrivate::setCupsDefaults()
+{
+    if (QCUPSSupport::isAvailable()) {
+        int cupsPrinterIndex = -1;
+        QCUPSSupport cups;
+
+        const cups_dest_t* printers = cups.availablePrinters();
+        int prnCount = cups.availablePrintersCount();
+        for (int i = 0; i <  prnCount; ++i) {
+            QString name = QString::fromLocal8Bit(printers[i].name);
+            if (name == printerName) {
+                cupsPrinterIndex = i;
+                break;
+            }
+        }
+
+        if (cupsPrinterIndex < 0)
+            return;
+
+        cups.setCurrentPrinter(cupsPrinterIndex);
+
+        if (cups.currentPPD()) {
+            const ppd_option_t *ppdDuplex = cups.ppdOption("Duplex");
+            if (ppdDuplex) {
+                if (qstrcmp(ppdDuplex->defchoice, "DuplexTumble") == 0)
+                    duplex = QPrinter::DuplexShortSide;
+                else if (qstrcmp(ppdDuplex->defchoice, "DuplexNoTumble") == 0)
+                    duplex = QPrinter::DuplexLongSide;
+                else
+                    duplex = QPrinter::DuplexNone;
+            }
+
+            grayscale = !cups.currentPPD()->color_device;
+
+            const ppd_option_t *ppdCollate = cups.ppdOption("Collate");
+            if (ppdCollate)
+                collate = qstrcmp(ppdCollate->defchoice, "True") == 0;
+
+            const ppd_option_t* pageSizes = cups.pageSizes();
+            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;
+            }
+
+            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;
+                }
+            }
+        }
+    }
+}
+
+
 QT_END_NAMESPACE
 
 #endif // QT_NO_PRINTER
index e67c888..6124ea0 100644 (file)
@@ -96,6 +96,8 @@ public:
     void closePrintDevice();
 
     void updatePaperSize();
+    void setPaperSize();
+    void setCupsDefaults();
 
 private:
     Q_DISABLE_COPY(QCupsPrintEnginePrivate)