From 83e6d1fe6006ad8e3cf37d5ca412aedae5aab9a4 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 1 Jan 2013 01:33:42 +0100 Subject: [PATCH] Add support for getting the paper names available for the printer Task-number: QTBUG-27714 Change-Id: I9bc6f1188f262e43f581add058d7895e1b5bd9e3 Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../platforms/cocoa/qcocoaprintersupport.h | 1 + .../platforms/cocoa/qcocoaprintersupport.mm | 31 ++++++++++++++++++ .../printsupport/cups/qcupsprintersupport.cpp | 5 +++ .../printsupport/cups/qcupsprintersupport_p.h | 1 + .../windows/qwindowsprintersupport.cpp | 5 +++ .../windows/qwindowsprintersupport.h | 1 + src/printsupport/kernel/qcups.cpp | 21 ++++++++++++ src/printsupport/kernel/qcups_p.h | 1 + .../kernel/qplatformprintersupport.cpp | 5 +++ .../kernel/qplatformprintersupport.h | 2 +- src/printsupport/kernel/qprintengine_win.cpp | 32 +++++++++++++++++++ src/printsupport/kernel/qprintengine_win_p.h | 1 + src/printsupport/kernel/qprinterinfo.cpp | 19 +++++++++++ src/printsupport/kernel/qprinterinfo.h | 3 +- src/printsupport/kernel/qprinterinfo_p.h | 6 +++- 15 files changed, 131 insertions(+), 3 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoaprintersupport.h b/src/plugins/platforms/cocoa/qcocoaprintersupport.h index 040b687c4e..83cf1ffada 100644 --- a/src/plugins/platforms/cocoa/qcocoaprintersupport.h +++ b/src/plugins/platforms/cocoa/qcocoaprintersupport.h @@ -55,6 +55,7 @@ public: QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode) Q_DECL_OVERRIDE; QPaintEngine *createPaintEngine(QPrintEngine *, QPrinter::PrinterMode printerMode) Q_DECL_OVERRIDE; QList supportedPaperSizes(const QPrinterInfo &) const Q_DECL_OVERRIDE; + QList > supportedSizesWithNames(const QPrinterInfo &) const Q_DECL_OVERRIDE; QList availablePrinters() Q_DECL_OVERRIDE; QPrinterInfo printerInfo(const QString &printerName) Q_DECL_OVERRIDE; diff --git a/src/plugins/platforms/cocoa/qcocoaprintersupport.mm b/src/plugins/platforms/cocoa/qcocoaprintersupport.mm index a48db02949..cfa23b7a30 100644 --- a/src/plugins/platforms/cocoa/qcocoaprintersupport.mm +++ b/src/plugins/platforms/cocoa/qcocoaprintersupport.mm @@ -138,3 +138,34 @@ QPrinterInfo QCocoaPrinterSupport::printerInfoFromPMPrinter(const PMPrinter &pri return createPrinterInfo(name, description, location, makeAndModel, isDefault, 0); } + +QList > QCocoaPrinterSupport::supportedSizesWithNames(const QPrinterInfo &printerInfo) const +{ + QList > returnValue; + if (printerInfo.isNull()) + return returnValue; + + PMPrinter printer = PMPrinterCreateFromPrinterID(QCFString::toCFStringRef(printerInfo.printerName())); + if (!printer) + return returnValue; + + CFArrayRef array; + if (PMPrinterGetPaperList(printer, &array) != noErr) { + PMRelease(printer); + return returnValue; + } + + int count = CFArrayGetCount(array); + for (int i = 0; i < count; ++i) { + PMPaper paper = static_cast(const_cast(CFArrayGetValueAtIndex(array, i))); + double width, height; + if (PMPaperGetWidth(paper, &width) == noErr && PMPaperGetHeight(paper, &height) == noErr) { + static const double OnePointInMillimeters = 1.0 / 72.0 * 25.4; + QCFString paperName; + if (PMPaperCreateLocalizedName(paper, printer, &paperName) == noErr) + returnValue.append(qMakePair(QString(paperName), QSizeF(width * OnePointInMillimeters, height * OnePointInMillimeters))); + } + } + PMRelease(printer); + return returnValue; +} diff --git a/src/plugins/printsupport/cups/qcupsprintersupport.cpp b/src/plugins/printsupport/cups/qcupsprintersupport.cpp index c2e9bd445f..f41d4f5047 100644 --- a/src/plugins/printsupport/cups/qcupsprintersupport.cpp +++ b/src/plugins/printsupport/cups/qcupsprintersupport.cpp @@ -86,6 +86,11 @@ QList QCupsPrinterSupport::supportedPaperSizes(const QPrint return QCUPSSupport::getCupsPrinterPaperSizes(printerIndex(printerInfo)); } +QList > QCupsPrinterSupport::supportedSizesWithNames(const QPrinterInfo &printerInfo) const +{ + return QCUPSSupport::getCupsPrinterPaperSizesWithNames(printerIndex(printerInfo)); +} + void QCupsPrinterSupport::loadCups() { cupsGetDests = (CupsGetDests) m_cups.resolve("cupsGetDests"); diff --git a/src/plugins/printsupport/cups/qcupsprintersupport_p.h b/src/plugins/printsupport/cups/qcupsprintersupport_p.h index d3f0ff5d90..e9fe24203e 100644 --- a/src/plugins/printsupport/cups/qcupsprintersupport_p.h +++ b/src/plugins/printsupport/cups/qcupsprintersupport_p.h @@ -67,6 +67,7 @@ public: virtual QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode); virtual QPaintEngine *createPaintEngine(QPrintEngine *printEngine, QPrinter::PrinterMode); virtual QList supportedPaperSizes(const QPrinterInfo &) const; + virtual QList > supportedSizesWithNames(const QPrinterInfo &) const; virtual QString printerOption(const QPrinterInfo &printer, const QString &key) const; virtual PrinterOptions printerOptions(const QPrinterInfo &printer) const; diff --git a/src/plugins/printsupport/windows/qwindowsprintersupport.cpp b/src/plugins/printsupport/windows/qwindowsprintersupport.cpp index 469dbdb34e..36e7a3fb8e 100644 --- a/src/plugins/printsupport/windows/qwindowsprintersupport.cpp +++ b/src/plugins/printsupport/windows/qwindowsprintersupport.cpp @@ -93,4 +93,9 @@ QList QWindowsPrinterSupport::supportedPaperSizes(const QPr return QWin32PrintEngine::supportedPaperSizes(printerInfo); } +QList >QWindowsPrinterSupport::supportedSizesWithNames(const QPrinterInfo &printerInfo) const +{ + return QWin32PrintEngine::supportedSizesWithNames(printerInfo); +} + QT_END_NAMESPACE diff --git a/src/plugins/printsupport/windows/qwindowsprintersupport.h b/src/plugins/printsupport/windows/qwindowsprintersupport.h index 3550c3f50c..1d5a4f3da4 100644 --- a/src/plugins/printsupport/windows/qwindowsprintersupport.h +++ b/src/plugins/printsupport/windows/qwindowsprintersupport.h @@ -57,6 +57,7 @@ public: virtual QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode); virtual QPaintEngine *createPaintEngine(QPrintEngine *printEngine, QPrinter::PrinterMode); virtual QList supportedPaperSizes(const QPrinterInfo &) const; + virtual QList >supportedSizesWithNames(const QPrinterInfo &printerInfo) const; }; QT_END_NAMESPACE diff --git a/src/printsupport/kernel/qcups.cpp b/src/printsupport/kernel/qcups.cpp index 30de3d301c..47447b21a1 100644 --- a/src/printsupport/kernel/qcups.cpp +++ b/src/printsupport/kernel/qcups.cpp @@ -52,6 +52,8 @@ QT_BEGIN_NAMESPACE +extern double qt_multiplierForUnit(QPrinter::Unit unit, int resolution); + typedef int (*CupsGetDests)(cups_dest_t **dests); typedef void (*CupsFreeDests)(int num_dests, cups_dest_t *dests); typedef const char* (*CupsGetPPD)(const char *printer); @@ -500,6 +502,25 @@ QList QCUPSSupport::getCupsPrinterPaperSizes(int cupsPrinte return result; } +QList > QCUPSSupport::getCupsPrinterPaperSizesWithNames(int cupsPrinterIndex) +{ + QList > result; + if (!QCUPSSupport::isAvailable() || cupsPrinterIndex < 0) + return result; + // Find paper sizes from CUPS. + QCUPSSupport cups; + cups.setCurrentPrinter(cupsPrinterIndex); + if (const ppd_option_t* size = cups.pageSizes()) { + for (int j = 0; j < size->num_choices; ++j) { + double multiplier = qt_multiplierForUnit(QPrinter::Millimeter, 0); // resolution is not needed here + QSize sz = cups.paperRect(size->choices[j].choice).size(); + result.append(qMakePair(QString(size->choices[j].text), QSizeF(sz.width() / multiplier, sz.height() / multiplier))); + } + } + return result; +} + + QT_END_NAMESPACE #endif // QT_NO_CUPS diff --git a/src/printsupport/kernel/qcups_p.h b/src/printsupport/kernel/qcups_p.h index 67a8955d93..0828f582a3 100644 --- a/src/printsupport/kernel/qcups_p.h +++ b/src/printsupport/kernel/qcups_p.h @@ -121,6 +121,7 @@ public: static QList availableUnixPrinters(); static QList getCupsPrinterPaperSizes(int cupsPrinterIndex); + static QList > getCupsPrinterPaperSizesWithNames(int cupsPrinterIndex); private: void collectMarkedOptions(QStringList& list, const ppd_group_t* group = 0) const; diff --git a/src/printsupport/kernel/qplatformprintersupport.cpp b/src/printsupport/kernel/qplatformprintersupport.cpp index f20ac5d177..4d80e55ab6 100644 --- a/src/printsupport/kernel/qplatformprintersupport.cpp +++ b/src/printsupport/kernel/qplatformprintersupport.cpp @@ -82,6 +82,11 @@ QList QPlatformPrinterSupport::supportedPaperSizes(const QP return QList(); } +QList > QPlatformPrinterSupport::supportedSizesWithNames(const QPrinterInfo &) const +{ + return QList >(); +} + QList QPlatformPrinterSupport::availablePrinters() { return m_printers; diff --git a/src/printsupport/kernel/qplatformprintersupport.h b/src/printsupport/kernel/qplatformprintersupport.h index 5cb3b805fd..c4ffcffd1e 100644 --- a/src/printsupport/kernel/qplatformprintersupport.h +++ b/src/printsupport/kernel/qplatformprintersupport.h @@ -72,7 +72,7 @@ public: virtual QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode); virtual QPaintEngine *createPaintEngine(QPrintEngine *, QPrinter::PrinterMode printerMode); virtual QList supportedPaperSizes(const QPrinterInfo &) const; - + virtual QList > supportedSizesWithNames(const QPrinterInfo &printerInfo) const; virtual QList availablePrinters(); virtual QPrinterInfo defaultPrinter(); virtual QPrinterInfo printerInfo(const QString &printerName); diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp index 97453f2e0e..f5690c12f3 100644 --- a/src/printsupport/kernel/qprintengine_win.cpp +++ b/src/printsupport/kernel/qprintengine_win.cpp @@ -1596,6 +1596,38 @@ QList QWin32PrintEngine::supportedPaperSizes(const QPrinter return returnList; } +QList > QWin32PrintEngine::supportedSizesWithNames(const QPrinterInfo &printerInfo) +{ + QList > paperSizes; + if (printerInfo.isNull()) + return paperSizes; + DWORD size = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), + NULL, DC_PAPERNAMES, NULL, NULL); + if ((int)size != -1) { + wchar_t *papers = new wchar_t[size*64]; + size = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), + NULL, DC_PAPERNAMES, papers, NULL); + DWORD size2 = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), + NULL, DC_PAPERSIZE, NULL, NULL); + if ((int)size2 != -1) { + POINT *points = new POINT[size2*sizeof(POINT)]; + + size2 = DeviceCapabilities(reinterpret_cast(printerInfo.printerName().utf16()), + NULL, DC_PAPERSIZE, (wchar_t *)points, NULL); + wchar_t copyOfPaper[65]; + for (int i=0;i<(int)size;i++) { + wcscpy_s(copyOfPaper, 64, papers + (i * 64)); + copyOfPaper[64] = '\0'; + QString str = QString::fromWCharArray(copyOfPaper); + paperSizes << qMakePair(str, QSizeF(points[i].x / 10, points[i].y / 10)); + } + delete [] points; + } + delete [] papers; + } + return paperSizes; +} + void QWin32PrintEngine::queryDefaultPrinter(QString &name, QString &program, QString &port) { /* Read the default printer name, driver and port with the intuitive function diff --git a/src/printsupport/kernel/qprintengine_win_p.h b/src/printsupport/kernel/qprintengine_win_p.h index 5197918710..ffeebb0704 100644 --- a/src/printsupport/kernel/qprintengine_win_p.h +++ b/src/printsupport/kernel/qprintengine_win_p.h @@ -104,6 +104,7 @@ public: void releaseDC(HDC) const; static QList supportedPaperSizes(const QPrinterInfo &printerInfo); + static QList > supportedSizesWithNames(const QPrinterInfo &printerInfo); static void queryDefaultPrinter(QString &name, QString &program, QString &port); private: diff --git a/src/printsupport/kernel/qprinterinfo.cpp b/src/printsupport/kernel/qprinterinfo.cpp index 32ed4fa5af..f863b23e34 100644 --- a/src/printsupport/kernel/qprinterinfo.cpp +++ b/src/printsupport/kernel/qprinterinfo.cpp @@ -222,6 +222,25 @@ QList QPrinterInfo::supportedPaperSizes() const return d->paperSizes; } +/*! + Returns a list of all the paper names supported by the driver with the + corresponding size in millimeters. + + Not all printer drivers support this query, so the list may be empty. + + \since 5.1 +*/ + +QList > QPrinterInfo::supportedSizesWithNames() const +{ + Q_D(const QPrinterInfo); + if (!isNull() && !d->hasPaperNames) { + d->paperNames = QPlatformPrinterSupportPlugin::get()->supportedSizesWithNames(*this); + d->hasPaperNames = true; + } + return d->paperNames; +} + QList QPrinterInfo::availablePrinters() { QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get(); diff --git a/src/printsupport/kernel/qprinterinfo.h b/src/printsupport/kernel/qprinterinfo.h index b831898632..0dc19c1da7 100644 --- a/src/printsupport/kernel/qprinterinfo.h +++ b/src/printsupport/kernel/qprinterinfo.h @@ -43,7 +43,7 @@ #define QPRINTERINFO_H #include - +#include #include QT_BEGIN_NAMESPACE @@ -71,6 +71,7 @@ public: bool isDefault() const; QList supportedPaperSizes() const; + QList > supportedSizesWithNames() const; static QList availablePrinters(); static QPrinterInfo defaultPrinter(); diff --git a/src/printsupport/kernel/qprinterinfo_p.h b/src/printsupport/kernel/qprinterinfo_p.h index 97f4fd8a56..d4bb08f1f5 100644 --- a/src/printsupport/kernel/qprinterinfo_p.h +++ b/src/printsupport/kernel/qprinterinfo_p.h @@ -58,6 +58,7 @@ #ifndef QT_NO_PRINTER #include "QtCore/qlist.h" +#include "QtCore/qpair.h" QT_BEGIN_NAMESPACE @@ -65,7 +66,8 @@ class QPrinterInfoPrivate { public: QPrinterInfoPrivate(const QString& name = QString()) : - name(name), isDefault(false), index(-1), hasPaperSizes(false) + name(name), isDefault(false), index(-1), hasPaperSizes(false), + hasPaperNames(false) {} ~QPrinterInfoPrivate() {} @@ -81,6 +83,8 @@ public: mutable bool hasPaperSizes; mutable QList paperSizes; + mutable bool hasPaperNames; + mutable QList > paperNames; };