QtPrintSupport: use CUPS defaults for UNIX print dialog
authorTeemu Katajisto <teemu.katajisto@digia.com>
Wed, 22 Aug 2012 09:09:21 +0000 (12:09 +0300)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 22 Nov 2012 21:56:05 +0000 (22:56 +0100)
Update printer defaults in UNIX print dialog when printer
is changed.

Task-number: QTBUG-23037
Task-number: QTBUG-6239
Task-number: QTBUG-15351
Task-number: QTBUG-19233
Task-number: QTBUG-22036

Change-Id: Ib03635035211bd69b5b23da20ffd2c03ac318a1a
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
src/printsupport/dialogs/qpagesetupdialog_unix.cpp
src/printsupport/dialogs/qpagesetupdialog_unix_p.h
src/printsupport/dialogs/qprintdialog_unix.cpp

index 3fb3e41..0fc547d 100644 (file)
@@ -58,7 +58,6 @@
 #  include <private/qcups_p.h>
 #endif
 
-
 QT_BEGIN_NAMESPACE
 
 QSizeF qt_printerPaperSize(QPrinter::Orientation, QPrinter::PaperSize, QPrinter::Unit, int);
@@ -224,26 +223,14 @@ public:
     void init();
 
     QPageSetupWidget *widget;
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    QCUPSSupport *cups;
-#endif
 };
 
 QUnixPageSetupDialogPrivate::QUnixPageSetupDialogPrivate(QPrinter *printer) : QPageSetupDialogPrivate(printer)
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    , cups(0)
-#endif
 {
 }
 
 QUnixPageSetupDialogPrivate::~QUnixPageSetupDialogPrivate()
 {
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    if (cups) {
-        delete cups;
-        cups = 0;
-    }
-#endif
 }
 
 void QUnixPageSetupDialogPrivate::init()
@@ -252,14 +239,6 @@ void QUnixPageSetupDialogPrivate::init()
 
     widget = new QPageSetupWidget(q);
     widget->setPrinter(printer);
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    if (printer->outputFormat() == QPrinter::NativeFormat && QCUPSSupport::isAvailable()) {
-        cups = new QCUPSSupport;
-        widget->selectPrinter(cups);
-    } else {
-        cups = 0;
-    }
-#endif
 
     QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok
                                                      | QDialogButtonBox::Cancel,
@@ -276,7 +255,7 @@ QPageSetupWidget::QPageSetupWidget(QWidget *parent)
     : QWidget(parent),
     m_printer(0),
     m_blockSignals(false),
-    m_cups(0)
+    m_cups(false)
 {
     widget.setupUi(this);
 
@@ -365,29 +344,22 @@ void QPageSetupWidget::setupPrinter() const
         ps = val.toInt();
     }
 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    else if (m_cups && QCUPSSupport::isAvailable() && m_cups->currentPPD()) {
-        QByteArray cupsPageSize = val.toByteArray();
-        QPrintEngine *engine = m_printer->printEngine();
-        engine->setProperty(PPK_CupsStringPageSize, QString::fromLatin1(cupsPageSize));
-        engine->setProperty(PPK_CupsOptions, m_cups->options());
-
-        QRect pageRect = m_cups->pageRect(cupsPageSize);
-        engine->setProperty(PPK_CupsPageRect, pageRect);
-
-        QRect paperRect = m_cups->paperRect(cupsPageSize);
-        engine->setProperty(PPK_CupsPaperRect, paperRect);
-
-        for(ps = 0; ps < QPrinter::NPageSize; ++ps) {
-            QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(ps));
-            if (size.width == paperRect.width() && size.height == paperRect.height())
+    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()) {
+                ps = static_cast<QPrinter::PaperSize>(papersize);
                 break;
+            }
         }
     }
 #endif
-    if (ps == QPrinter::Custom)
+    if (ps == QPrinter::Custom) {
         m_printer->setPaperSize(sizeForOrientation(orientation, m_paperSize), QPrinter::Point);
-    else
+    }
+    else {
         m_printer->setPaperSize(static_cast<QPrinter::PaperSize>(ps));
+    }
 
 #ifdef PSD_ENABLE_PAPERSOURCE
     m_printer->setPaperSource((QPrinter::PaperSource)widget.paperSource->currentIndex());
@@ -396,13 +368,14 @@ void QPageSetupWidget::setupPrinter() const
 
 }
 
-void QPageSetupWidget::selectPrinter(QCUPSSupport *cups)
+void QPageSetupWidget::selectPrinter()
 {
-    m_cups = cups;
     widget.paperSize->clear();
 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    if (m_cups && QCUPSSupport::isAvailable()) {
-        const ppd_option_t* pageSizes = m_cups->pageSizes();
+    if (QCUPSSupport::isAvailable()) {
+        m_cups = true;
+        QCUPSSupport cups;
+        const ppd_option_t* pageSizes = cups.pageSizes();
         const int numChoices = pageSizes ? pageSizes->num_choices : 0;
 
         int cupsDefaultSize = 0;
@@ -412,13 +385,11 @@ void QPageSetupWidget::selectPrinter(QCUPSSupport *cups)
             widget.paperSize->addItem(QString::fromLocal8Bit(pageSizes->choices[i].text), QByteArray(pageSizes->choices[i].choice));
             if (static_cast<int>(pageSizes->choices[i].marked) == 1)
                 cupsDefaultSize = i;
-            if (m_printer->d_func()->hasUserSetPageSize) {
-                QRect cupsPaperSize = m_cups->paperRect(pageSizes->choices[i].choice);
-                QSize diff = cupsPaperSize.size() - qtPreferredSize;
-                if (qAbs(diff.width()) < 5 && qAbs(diff.height()) < 5) {
-                    widget.paperSize->setCurrentIndex(i);
-                    preferredSizeMatched = true;
-                }
+            QRect cupsPaperSize = cups.paperRect(pageSizes->choices[i].choice);
+            QSize diff = cupsPaperSize.size() - qtPreferredSize;
+            if (qAbs(diff.width()) < 5 && qAbs(diff.height()) < 5) {
+                widget.paperSize->setCurrentIndex(i);
+                preferredSizeMatched = true;
             }
         }
         if (!preferredSizeMatched)
@@ -427,15 +398,16 @@ void QPageSetupWidget::selectPrinter(QCUPSSupport *cups)
             m_printer->getPageMargins(&m_leftMargin, &m_topMargin, &m_rightMargin, &m_bottomMargin, QPrinter::Point);
         } else {
             QByteArray cupsPaperSizeChoice = widget.paperSize->itemData(widget.paperSize->currentIndex()).toByteArray();
-            QRect paper = m_cups->paperRect(cupsPaperSizeChoice);
-            QRect content = m_cups->pageRect(cupsPaperSizeChoice);
+            QRect paper = cups.paperRect(cupsPaperSizeChoice);
+            QRect content = cups.pageRect(cupsPaperSizeChoice);
 
             m_leftMargin = content.x() - paper.x();
             m_topMargin = content.y() - paper.y();
             m_rightMargin = paper.right() - content.right();
             m_bottomMargin = paper.bottom() - content.bottom();
         }
-    }
+    } else
+        m_cups = false;
 #endif
     if (widget.paperSize->count() == 0) {
         populatePaperSizes(widget.paperSize);
@@ -445,19 +417,17 @@ void QPageSetupWidget::selectPrinter(QCUPSSupport *cups)
 
     unitChanged(widget.unit->currentIndex());
     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
+
+    // setup printer here the first time
+    setupPrinter();
 }
 
 void QPageSetupWidget::selectPdfPsPrinter(const QPrinter *p)
 {
-    m_cups = 0;
+    m_cups = false;
     widget.paperSize->clear();
     populatePaperSizes(widget.paperSize);
     widget.paperSize->setCurrentIndex(widget.paperSize->findData(p->paperSize()));
-
-    m_leftMargin = 90;
-    m_topMargin = 72;
-    m_bottomMargin = 72;
-    m_rightMargin = 90;
     unitChanged(widget.unit->currentIndex());
     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
 }
@@ -482,7 +452,7 @@ void QPageSetupWidget::_q_paperSizeChanged()
     bool custom = size == QPrinter::Custom;
 
 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    custom = custom ? !m_cups : custom;
+    custom = custom ? m_cups : custom;
 #endif
 
     widget.paperWidth->setEnabled(custom);
@@ -496,9 +466,10 @@ void QPageSetupWidget::_q_paperSizeChanged()
     } else {
         Q_ASSERT(m_printer);
 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-        if (m_cups) { // combobox is filled with cups based data
+        if (m_cups && QCUPSSupport::isAvailable()) { // combobox is filled with cups based data
+            QCUPSSupport cups;
             QByteArray cupsPageSize = widget.paperSize->itemData(widget.paperSize->currentIndex()).toByteArray();
-            m_paperSize = m_cups->paperRect(cupsPageSize).size();
+            m_paperSize = cups.paperRect(cupsPageSize).size();
             if (orientation == QPrinter::Landscape)
                 m_paperSize = QSizeF(m_paperSize.height(), m_paperSize.width()); // swap
         }
index cea6b6b..3a2d05b 100644 (file)
@@ -73,7 +73,7 @@ public:
     void setPrinter(QPrinter *printer);
     /// copy information from the widget and apply that to the printer.
     void setupPrinter() const;
-    void selectPrinter(QCUPSSupport *m_cups);
+    void selectPrinter();
     void selectPdfPsPrinter(const QPrinter *p);
 
 private slots:
@@ -96,7 +96,7 @@ private:
     QSizeF m_paperSize;
     qreal m_currentMultiplier;
     bool m_blockSignals;
-    QCUPSSupport *m_cups;
+    bool m_cups;
 };
 
 QT_END_NAMESPACE
index 71b8d6a..6f317cf 100644 (file)
@@ -52,6 +52,7 @@
 #include <QtWidgets/qfilesystemmodel.h>
 #include <QtWidgets/qstyleditemdelegate.h>
 #include <QtPrintSupport/qprinter.h>
+#include <QtPrintSupport/qprinterinfo.h>
 #include <private/qprintengine_pdf_p.h>
 
 #include <QtWidgets/qdialogbuttonbox.h>
@@ -113,10 +114,6 @@ public:
     QPrintPropertiesDialog(QAbstractPrintDialog *parent = 0);
     ~QPrintPropertiesDialog();
 
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    void setCups(QCUPSSupport *cups) { m_cups = cups; }
-#endif
-
     void selectPrinter();
     void selectPdfPsPrinter(const QPrinter *p);
 
@@ -130,9 +127,6 @@ protected:
 private:
     Ui::QPrintPropertiesWidget widget;
     QDialogButtonBox *m_buttons;
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    QCUPSSupport *m_cups;
-#endif
 };
 
 class QUnixPrintWidgetPrivate;
@@ -158,18 +152,15 @@ private:
 class QUnixPrintWidgetPrivate
 {
 public:
-    QUnixPrintWidgetPrivate(QUnixPrintWidget *q);
+    QUnixPrintWidgetPrivate(QUnixPrintWidget *q, QPrinter *prn);
     ~QUnixPrintWidgetPrivate();
 
     /// copy printer properties to the widget
-    void applyPrinterProperties(QPrinter *p);
+    void applyPrinterProperties();
     bool checkFields();
     void setupPrinter();
     void setOptionsPane(QPrintDialogPrivate *pane);
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    void setCupsProperties();
-#endif
-
+    void setupPrinterProperties();
 // slots
     void _q_printerChanged(int index);
     void _q_btnPropertiesClicked();
@@ -185,12 +176,7 @@ public:
 private:
     QPrintDialogPrivate *optionsPane;
     bool filePrintersAdded;
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    QCUPSSupport* cups;
-    int cupsPrinterCount;
-    const cups_dest_t* cupsPrinters;
-    const ppd_file_t* cupsPPD;
-#endif
+    bool propertiesDialogShown;
 };
 
 class QPrintDialogPrivate : public QAbstractPrintDialogPrivate
@@ -203,11 +189,9 @@ public:
 
     void init();
     /// copy printer properties to the widget
-    void applyPrinterProperties(QPrinter *p);
+    void applyPrinterProperties();
 
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    void selectPrinter(QCUPSSupport *cups);
-#endif
+    void selectPrinter();
 
     void _q_chbPrintLastFirstToggled(bool);
 #ifndef QT_NO_MESSAGEBOX
@@ -242,9 +226,6 @@ public:
 
 QPrintPropertiesDialog::QPrintPropertiesDialog(QAbstractPrintDialog *parent)
     : QDialog(parent)
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    , m_cups(0)
-#endif
 {
     QVBoxLayout *lay = new QVBoxLayout(this);
     this->setLayout(lay);
@@ -274,14 +255,7 @@ void QPrintPropertiesDialog::setupPrinter() const
 
 void QPrintPropertiesDialog::selectPrinter()
 {
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    if (m_cups && QCUPSSupport::isAvailable()) {
-        widget.pageSetup->selectPrinter(m_cups);
-    } else
-#endif
-    {
-        widget.pageSetup->selectPrinter(0);
-    }
+    widget.pageSetup->selectPrinter();
 }
 
 void QPrintPropertiesDialog::selectPdfPsPrinter(const QPrinter *p)
@@ -305,7 +279,6 @@ void QPrintPropertiesDialog::showEvent(QShowEvent* event)
     tabs that expands when the Options button is selected.
 
 */
-
 QPrintDialogPrivate::QPrintDialogPrivate()
     : top(0), bottom(0), buttons(0), collapseButton(0)
 {
@@ -319,7 +292,7 @@ void QPrintDialogPrivate::init()
 {
     Q_Q(QPrintDialog);
 
-    top = new QUnixPrintWidget(0, q);
+    top = new QUnixPrintWidget(q->printer(), q);
     bottom = new QWidget(q);
     options.setupUi(bottom);
     options.color->setIconSize(QSize(32, 32));
@@ -343,10 +316,6 @@ void QPrintDialogPrivate::init()
     lay->addWidget(bottom);
     lay->addWidget(buttons);
 
-    QPrinter* p = q->printer();
-
-    applyPrinterProperties(p);
-
 #ifdef QT_NO_MESSAGEBOX
     QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(accept()));
 #else
@@ -360,63 +329,36 @@ void QPrintDialogPrivate::init()
     QObject::connect(collapseButton, SIGNAL(released()), q, SLOT(_q_collapseOrExpandDialog()));
 }
 
-void QPrintDialogPrivate::applyPrinterProperties(QPrinter *p)
+// initialize printer options
+void QPrintDialogPrivate::selectPrinter()
 {
-    if (p->colorMode() == QPrinter::Color)
-        options.color->setChecked(true);
-    else
-        options.grayscale->setChecked(true);
-
-    switch(p->duplex()) {
-    case QPrinter::DuplexNone:
-        options.noDuplex->setChecked(true); break;
-    case QPrinter::DuplexLongSide:
-    case QPrinter::DuplexAuto:
-        options.duplexLong->setChecked(true); break;
-    case QPrinter::DuplexShortSide:
-        options.duplexShort->setChecked(true); break;
-    }
-    options.copies->setValue(p->copyCount());
-    options.collate->setChecked(p->collateCopies());
-    options.reverse->setChecked(p->pageOrder() == QPrinter::LastPageFirst);
-    top->d->applyPrinterProperties(p);
-}
+        Q_Q(QPrintDialog);
+        QPrinter *p = q->printer();
 
-void QPrintDialogPrivate::_q_chbPrintLastFirstToggled(bool checked)
-{
-    Q_Q(QPrintDialog);
-    if (checked)
-        q->printer()->setPageOrder(QPrinter::LastPageFirst);
-    else
-        q->printer()->setPageOrder(QPrinter::FirstPageFirst);
-}
-
-void QPrintDialogPrivate::_q_collapseOrExpandDialog()
-{
-    int collapseHeight = 0;
-    Q_Q(QPrintDialog);
-    QWidget *widgetToHide = bottom;
-    if (widgetToHide->isVisible()) {
-        collapseButton->setText(QPrintDialog::tr("&Options >>"));
-        collapseHeight = widgetToHide->y() + widgetToHide->height() - (top->y() + top->height());
-    }
-    else
-        collapseButton->setText(QPrintDialog::tr("&Options <<"));
-    widgetToHide->setVisible(! widgetToHide->isVisible());
-    if (! widgetToHide->isVisible()) { // make it shrink
-        q->layout()->activate();
-        q->resize( QSize(q->width(), q->height() - collapseHeight) );
-    }
+        if (p->colorMode() == QPrinter::Color)
+            options.color->setChecked(true);
+        else
+            options.grayscale->setChecked(true);
+
+        switch (p->duplex()) {
+        case QPrinter::DuplexNone:
+            options.noDuplex->setChecked(true); break;
+        case QPrinter::DuplexLongSide:
+        case QPrinter::DuplexAuto:
+            options.duplexLong->setChecked(true); break;
+        case QPrinter::DuplexShortSide:
+            options.duplexShort->setChecked(true); break;
+        }
+        options.copies->setValue(p->copyCount());
+        options.collate->setChecked(p->collateCopies());
+        options.reverse->setChecked(p->pageOrder() == QPrinter::LastPageFirst);
 }
 
-#ifndef QT_NO_MESSAGEBOX
-void QPrintDialogPrivate::_q_checkFields()
+void QPrintDialogPrivate::applyPrinterProperties()
 {
-    Q_Q(QPrintDialog);
-    if (top->d->checkFields())
-        q->accept();
+    // apply printer options to property dialog
+    top->d->applyPrinterProperties();
 }
-#endif // QT_NO_MESSAGEBOX
 
 void QPrintDialogPrivate::setupPrinter()
 {
@@ -432,7 +374,7 @@ void QPrintDialogPrivate::setupPrinter()
             p->setDuplex(QPrinter::DuplexShortSide);
     }
 
-    p->setColorMode( options.color->isChecked() ? QPrinter::Color : QPrinter::GrayScale );
+    p->setColorMode(options.color->isChecked() ? QPrinter::Color : QPrinter::GrayScale);
 
     // print range
     if (options.printAll->isChecked()) {
@@ -456,6 +398,43 @@ void QPrintDialogPrivate::setupPrinter()
     top->d->setupPrinter();
 }
 
+void QPrintDialogPrivate::_q_chbPrintLastFirstToggled(bool checked)
+{
+    Q_Q(QPrintDialog);
+    if (checked)
+        q->printer()->setPageOrder(QPrinter::LastPageFirst);
+    else
+        q->printer()->setPageOrder(QPrinter::FirstPageFirst);
+}
+
+void QPrintDialogPrivate::_q_collapseOrExpandDialog()
+{
+    int collapseHeight = 0;
+    Q_Q(QPrintDialog);
+    QWidget *widgetToHide = bottom;
+    if (widgetToHide->isVisible()) {
+        collapseButton->setText(QPrintDialog::tr("&Options >>"));
+        collapseHeight = widgetToHide->y() + widgetToHide->height() - (top->y() + top->height());
+    }
+    else
+        collapseButton->setText(QPrintDialog::tr("&Options <<"));
+    widgetToHide->setVisible(! widgetToHide->isVisible());
+    if (! widgetToHide->isVisible()) { // make it shrink
+        q->layout()->activate();
+        q->resize( QSize(q->width(), q->height() - collapseHeight) );
+    }
+}
+
+#ifndef QT_NO_MESSAGEBOX
+void QPrintDialogPrivate::_q_checkFields()
+{
+    Q_Q(QPrintDialog);
+    if (top->d->checkFields())
+        q->accept();
+}
+#endif // QT_NO_MESSAGEBOX
+
+
 void QPrintDialogPrivate::updateWidgets()
 {
     Q_Q(QPrintDialog);
@@ -500,9 +479,6 @@ void QPrintDialogPrivate::updateWidgets()
 
 void QPrintDialogPrivate::setTabs(const QList<QWidget*> &tabWidgets)
 {
-    while(options.tabs->count() > 2)
-        delete options.tabs->widget(2);
-
     QList<QWidget*>::ConstIterator iter = tabWidgets.begin();
     while(iter != tabWidgets.constEnd()) {
         QWidget *tab = *iter;
@@ -511,13 +487,6 @@ void QPrintDialogPrivate::setTabs(const QList<QWidget*> &tabWidgets)
     }
 }
 
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-void QPrintDialogPrivate::selectPrinter(QCUPSSupport *cups)
-{
-    options.duplex->setEnabled(cups && cups->ppdOption("Duplex"));
-}
-#endif
-
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -587,11 +556,9 @@ void QPrintDialog::accept()
 
 /*! \internal
 */
-QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(QUnixPrintWidget *p)
-    : parent(p), propertiesDialog(0), printer(0), optionsPane(0), filePrintersAdded(false)
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    , cups(0), cupsPrinterCount(0), cupsPrinters(0), cupsPPD(0)
-#endif
+QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(QUnixPrintWidget *p, QPrinter *prn)
+    : parent(p), propertiesDialog(0), printer(prn), optionsPane(0),
+      filePrintersAdded(false), propertiesDialogShown(false)
 {
     q = 0;
     if (parent)
@@ -600,30 +567,15 @@ QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(QUnixPrintWidget *p)
     widget.setupUi(parent);
 
     int currentPrinterIndex = 0;
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    cups = new QCUPSSupport;
-    if (QCUPSSupport::isAvailable()) {
-        cupsPPD = cups->currentPPD();
-        cupsPrinterCount = cups->availablePrintersCount();
-        cupsPrinters = cups->availablePrinters();
-
-        for (int i = 0; i < cupsPrinterCount; ++i) {
-            QString printerName(QString::fromLocal8Bit(cupsPrinters[i].name));
-            if (cupsPrinters[i].instance)
-                printerName += QLatin1Char('/') + QString::fromLocal8Bit(cupsPrinters[i].instance);
-
-            widget.printers->addItem(printerName);
-            if (cupsPrinters[i].is_default)
-                widget.printers->setCurrentIndex(i);
-        }
-        // the model depends on valid ppd. so before enabling the
-        // properties button we make sure the ppd is in fact valid.
-        if (cupsPrinterCount && cups->currentPPD()) {
-            widget.properties->setEnabled(true);
-        }
-        currentPrinterIndex = cups->currentPrinterIndex();
+    QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
+
+    for (int i = 0; i < printers.size(); ++i) {
+        QPrinterInfo pInfo = printers.at(i);
+        widget.printers->addItem(pInfo.printerName());
+        if (pInfo.isDefault())
+            currentPrinterIndex = i;
     }
-#endif
+    widget.properties->setEnabled(true);
 
 #if !defined(QT_NO_FILESYSTEMMODEL) && !defined(QT_NO_COMPLETER)
     QFileSystemModel *fsm = new QFileSystemModel(widget.filename);
@@ -675,9 +627,6 @@ void QUnixPrintWidgetPrivate::updateWidget()
 
 QUnixPrintWidgetPrivate::~QUnixPrintWidgetPrivate()
 {
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    delete cups;
-#endif
 }
 
 void QUnixPrintWidgetPrivate::_q_printerChanged(int index)
@@ -688,6 +637,13 @@ void QUnixPrintWidgetPrivate::_q_printerChanged(int index)
     widget.filename->setEnabled(false);
     widget.lOutput->setEnabled(false);
 
+    // Reset properties dialog when printer is changed
+    if (propertiesDialog){
+        delete propertiesDialog;
+        propertiesDialog = 0;
+        propertiesDialogShown = false;
+    }
+
     if (filePrintersAdded) {
         Q_ASSERT(index != printerCount - 2); // separator
         if (index == printerCount - 1) { // PDF
@@ -696,52 +652,31 @@ void QUnixPrintWidgetPrivate::_q_printerChanged(int index)
             widget.properties->setEnabled(true);
             widget.filename->setEnabled(true);
             QString filename = widget.filename->text();
-            QString suffix = QFileInfo(filename).suffix();
             widget.filename->setText(filename);
             widget.lOutput->setEnabled(true);
-            if (propertiesDialog)
-                propertiesDialog->selectPdfPsPrinter(printer);
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
             if (optionsPane)
-                optionsPane->selectPrinter(0);
-#endif
+                optionsPane->selectPrinter();
             return;
         }
     }
 
-    widget.location->setText(QString());
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-    if (QCUPSSupport::isAvailable()) {
-        cups->setCurrentPrinter(index);
-
-        const cups_option_t *opt = cups->printerOption(QString::fromLatin1("printer-location"));
-        QString location;
-        if (opt)
-            location = QString::fromLocal8Bit(opt->value);
-        widget.location->setText(location);
-
-        cupsPPD = cups->currentPPD();
-        // set printer type line
-        QString type;
-        if (cupsPPD)
-            type = QString::fromLocal8Bit(cupsPPD->manufacturer) + QLatin1String(" - ") + QString::fromLocal8Bit(cupsPPD->modelname);
-        widget.type->setText(type);
-        if (propertiesDialog)
-            propertiesDialog->selectPrinter();
-        if (optionsPane)
-            optionsPane->selectPrinter(cups);
-    } else {
+    if (printer) {
+        QString printerName = widget.printers->itemText(index);
+        printer->setPrinterName(printerName);
+
+        QPrinterInfo printerInfo = QPrinterInfo::printerInfo(printer->printerName());
+        widget.location->setText(printerInfo.location());
+        widget.type->setText(printerInfo.makeAndModel());
         if (optionsPane)
-            optionsPane->selectPrinter(0);
+            optionsPane->selectPrinter();
     }
-#endif
 }
 
 void QUnixPrintWidgetPrivate::setOptionsPane(QPrintDialogPrivate *pane)
 {
     optionsPane = pane;
     if (optionsPane)
-        _q_printerChanged(widget.printers->currentIndex());
+        optionsPane->selectPrinter();
 }
 
 void QUnixPrintWidgetPrivate::_q_btnBrowseClicked()
@@ -759,12 +694,11 @@ void QUnixPrintWidgetPrivate::_q_btnBrowseClicked()
     }
 }
 
-void QUnixPrintWidgetPrivate::applyPrinterProperties(QPrinter *p)
+void QUnixPrintWidgetPrivate::applyPrinterProperties()
 {
-    if (p == 0)
+    if (printer == 0)
         return;
-    printer = p;
-    if (p->outputFileName().isEmpty()) {
+    if (printer->outputFileName().isEmpty()) {
         QString home = QDir::homePath();
         QString cur = QDir::currentPath();
         if (home.at(home.length()-1) != QLatin1Char('/'))
@@ -774,14 +708,14 @@ void QUnixPrintWidgetPrivate::applyPrinterProperties(QPrinter *p)
         if (cur.left(home.length()) != home)
             cur = home;
         if (QGuiApplication::platformName() == QLatin1String("xcb")) {
-            if (p->docName().isEmpty()) {
+            if (printer->docName().isEmpty()) {
                 cur += QLatin1String("print.pdf");
             } else {
                 QRegExp re(QString::fromLatin1("(.*)\\.\\S+"));
-                if (re.exactMatch(p->docName()))
+                if (re.exactMatch(printer->docName()))
                     cur += re.cap(1);
                 else
-                    cur += p->docName();
+                    cur += printer->docName();
                 cur += QLatin1String(".pdf");
             }
         } // xcb
@@ -789,11 +723,11 @@ void QUnixPrintWidgetPrivate::applyPrinterProperties(QPrinter *p)
         widget.filename->setText(cur);
     }
     else
-        widget.filename->setText( p->outputFileName() );
-    QString printer = p->printerName();
-    if (!printer.isEmpty()) {
+        widget.filename->setText( printer->outputFileName() );
+    QString printerName = printer->printerName();
+    if (!printerName.isEmpty()) {
         for (int i = 0; i < widget.printers->count(); ++i) {
-            if (widget.printers->itemText(i) == printer) {
+            if (widget.printers->itemText(i) == printerName) {
                 widget.printers->setCurrentIndex(i);
                 break;
             }
@@ -802,7 +736,7 @@ void QUnixPrintWidgetPrivate::applyPrinterProperties(QPrinter *p)
     // PDF and PS printers are not added to the dialog yet, we'll handle those cases in QUnixPrintWidgetPrivate::updateWidget
 
     if (propertiesDialog)
-        propertiesDialog->applyPrinterProperties(p);
+        propertiesDialog->applyPrinterProperties(printer);
 }
 
 #ifndef QT_NO_MESSAGEBOX
@@ -841,56 +775,40 @@ bool QUnixPrintWidgetPrivate::checkFields()
 }
 #endif // QT_NO_MESSAGEBOX
 
-void QUnixPrintWidgetPrivate::_q_btnPropertiesClicked()
+void QUnixPrintWidgetPrivate::setupPrinterProperties()
 {
-    if (!propertiesDialog) {
-        propertiesDialog = new QPrintPropertiesDialog(q);
-        propertiesDialog->setResult(QDialog::Rejected);
-    }
+    if (propertiesDialog)
+        delete propertiesDialog;
 
-    if (propertiesDialog->result() == QDialog::Rejected) {
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-        propertiesDialog->setCups(cups);
-#endif
-        propertiesDialog->applyPrinterProperties(q->printer());
+    propertiesDialog = new QPrintPropertiesDialog(q);
+    propertiesDialog->setResult(QDialog::Rejected);
+    propertiesDialogShown = false;
 
-        if (q->isOptionEnabled(QPrintDialog::PrintToFile)
-            && (widget.printers->currentIndex() == widget.printers->count() - 1)) // PDF
-            propertiesDialog->selectPdfPsPrinter(q->printer());
-        else
-            propertiesDialog->selectPrinter();
+    propertiesDialog->applyPrinterProperties(q->printer());
+
+    if (q->isOptionEnabled(QPrintDialog::PrintToFile)
+        && (widget.printers->currentIndex() == widget.printers->count() - 1)) {// PDF
+        propertiesDialog->selectPdfPsPrinter(q->printer());
     }
-    propertiesDialog->exec();
+    else
+        propertiesDialog->selectPrinter();
 }
 
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
-void QUnixPrintWidgetPrivate::setCupsProperties()
-{
-    if (cups && QCUPSSupport::isAvailable() && cups->pageSizes()) {
-        QPrintEngine *engine = printer->printEngine();
-        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;
-        }
-        engine->setProperty(PPK_CupsStringPageSize, QString::fromLatin1(cupsPageSize));
-        engine->setProperty(PPK_CupsOptions, cups->options());
-
-        QRect pageRect = cups->pageRect(cupsPageSize);
-        engine->setProperty(PPK_CupsPageRect, pageRect);
-
-        QRect paperRect = cups->paperRect(cupsPageSize);
-        engine->setProperty(PPK_CupsPaperRect, paperRect);
-
-        for (int ps = 0; ps < QPrinter::NPageSize; ++ps) {
-            QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(ps));
-            if (size.width == paperRect.width() && size.height == paperRect.height())
-                printer->setPaperSize(static_cast<QPrinter::PaperSize>(ps));
-        }
-    }
+void QUnixPrintWidgetPrivate::_q_btnPropertiesClicked()
+{
+    if (!propertiesDialog)
+        setupPrinterProperties();
+    propertiesDialog->exec();
+    if (propertiesDialog->result() == QDialog::Rejected) {
+        // If properties dialog was rejected the dialog is deleted and
+        // the properties are set to defaults when printer is setup
+        delete propertiesDialog;
+        propertiesDialog = 0;
+        propertiesDialogShown = false;
+    } else
+        // properties dialog was shown and accepted
+        propertiesDialogShown = true;
 }
-#endif
 
 void QUnixPrintWidgetPrivate::setupPrinter()
 {
@@ -911,20 +829,19 @@ void QUnixPrintWidgetPrivate::setupPrinter()
         printer->setOutputFileName(QString());
     }
 
-    if (propertiesDialog && propertiesDialog->result() == QDialog::Accepted)
-        propertiesDialog->setupPrinter();
-#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
     if (!propertiesDialog)
-        setCupsProperties();
-#endif
+        setupPrinterProperties();
+
+    if (propertiesDialog->result() == QDialog::Accepted || !propertiesDialogShown)
+        propertiesDialog->setupPrinter();
 }
 
 /*! \internal
 */
 QUnixPrintWidget::QUnixPrintWidget(QPrinter *printer, QWidget *parent)
-    : QWidget(parent), d(new QUnixPrintWidgetPrivate(this))
+    : QWidget(parent), d(new QUnixPrintWidgetPrivate(this, printer))
 {
-    d->applyPrinterProperties(printer);
+    d->applyPrinterProperties();
 }
 
 /*! \internal