diff options
author | David Faure <faure@kde.org> | 2009-10-06 19:39:02 (GMT) |
---|---|---|
committer | Trond Kjernåsen <trond@trolltech.com> | 2009-12-14 12:53:26 (GMT) |
commit | 0295f8da10d6c920511d09f9e506b8bed8c444c3 (patch) | |
tree | 33f30236327d2ff801b85e922c9fac50505ebe3e | |
parent | a3a76a45cf23b0c3981ab2733de13e6f52fe058b (diff) | |
download | Qt-0295f8da10d6c920511d09f9e506b8bed8c444c3.zip Qt-0295f8da10d6c920511d09f9e506b8bed8c444c3.tar.gz Qt-0295f8da10d6c920511d09f9e506b8bed8c444c3.tar.bz2 |
Determine QPrinterInfo's supportedPaperSizes on demand.
The paper size determination requires a cupsGetPPD(), which can be a slow
HTTP download.
So doing it on demand makes QPrinterInfo::defaultPrinter() much faster,
because it doesn't actually need the paper sizes at all. Before this fix,
it was requesting the PPD file of every known CUPS printer, every time
it was called.
My battery of unittests for code that uses QPrinter went from 19 minutes
when the cups print server is switched off, to 13 seconds!
This fix also removes some code duplication, which is always nice.
Task-number: 232664, QTBUG-3033
Reviewed-by: Trond
-rw-r--r-- | src/gui/painting/qprinterinfo_unix.cpp | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/src/gui/painting/qprinterinfo_unix.cpp b/src/gui/painting/qprinterinfo_unix.cpp index 6684ff7..930785b 100644 --- a/src/gui/painting/qprinterinfo_unix.cpp +++ b/src/gui/painting/qprinterinfo_unix.cpp @@ -75,7 +75,9 @@ private: QString m_name; bool m_isNull; bool m_default; - QList<QPrinter::PaperSize> m_paperSizes; + mutable bool m_mustGetPaperSizes; + mutable QList<QPrinter::PaperSize> m_paperSizes; + int m_cupsPrinterIndex; QPrinterInfo* q_ptr; }; @@ -838,16 +840,7 @@ QList<QPrinterInfo> QPrinterInfo::availablePrinters() list.append(QPrinterInfo(printerName)); if (cupsPrinters[i].is_default) list[i].d_ptr->m_default = true; - // Find paper sizes. - cups.setCurrentPrinter(i); - const ppd_option_t* sizes = cups.pageSizes(); - if (sizes) { - for (int j = 0; j < sizes->num_choices; ++j) { - list[i].d_ptr->m_paperSizes.append( - QPrinterInfoPrivate::string2PaperSize( - QLatin1String(sizes->choices[j].choice))); - } - } + list[i].d_ptr->m_cupsPrinterIndex = i; } } else { #endif @@ -909,16 +902,7 @@ QPrinterInfo::QPrinterInfo(const QPrinter& printer) if (printerName == printer.printerName()) { if (cupsPrinters[i].is_default) d->m_default = true; - // Find paper sizes. - cups.setCurrentPrinter(i); - const ppd_option_t* sizes = cups.pageSizes(); - if (sizes) { - for (int j = 0; j < sizes->num_choices; ++j) { - d->m_paperSizes.append( - QPrinterInfoPrivate::string2PaperSize( - QLatin1String(sizes->choices[j].choice))); - } - } + d->m_cupsPrinterIndex = i; return; } } @@ -983,6 +967,26 @@ bool QPrinterInfo::isDefault() const QList< QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const { const Q_D(QPrinterInfo); + if (d->m_mustGetPaperSizes) { + d->m_mustGetPaperSizes = false; + +#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) + QCUPSSupport cups; + if (QCUPSSupport::isAvailable()) { + // Find paper sizes from CUPS. + cups.setCurrentPrinter(d->m_cupsPrinterIndex); + const ppd_option_t* sizes = cups.pageSizes(); + if (sizes) { + for (int j = 0; j < sizes->num_choices; ++j) { + d->m_paperSizes.append( + QPrinterInfoPrivate::string2PaperSize( + QLatin1String(sizes->choices[j].choice))); + } + } + } +#endif + + } return d->m_paperSizes; } @@ -993,6 +997,8 @@ QPrinterInfoPrivate::QPrinterInfoPrivate() { m_isNull = true; m_default = false; + m_mustGetPaperSizes = true; + m_cupsPrinterIndex = 0; q_ptr = 0; } @@ -1001,6 +1007,8 @@ QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name) m_name = name; m_isNull = false; m_default = false; + m_mustGetPaperSizes = true; + m_cupsPrinterIndex = 0; q_ptr = 0; } |