Add support for getting the paper names available for the printer

Task-number: QTBUG-27714

Change-Id: I9bc6f1188f262e43f581add058d7895e1b5bd9e3
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
This commit is contained in:
Andy Shaw 2013-01-01 01:33:42 +01:00 committed by The Qt Project
parent 78d4c949a4
commit 83e6d1fe60
15 changed files with 131 additions and 3 deletions

View File

@ -55,6 +55,7 @@ public:
QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode) Q_DECL_OVERRIDE;
QPaintEngine *createPaintEngine(QPrintEngine *, QPrinter::PrinterMode printerMode) Q_DECL_OVERRIDE;
QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &) const Q_DECL_OVERRIDE;
QList<QPair<QString, QSizeF> > supportedSizesWithNames(const QPrinterInfo &) const Q_DECL_OVERRIDE;
QList<QPrinterInfo> availablePrinters() Q_DECL_OVERRIDE;
QPrinterInfo printerInfo(const QString &printerName) Q_DECL_OVERRIDE;

View File

@ -138,3 +138,34 @@ QPrinterInfo QCocoaPrinterSupport::printerInfoFromPMPrinter(const PMPrinter &pri
return createPrinterInfo(name, description, location, makeAndModel, isDefault, 0);
}
QList<QPair<QString, QSizeF> > QCocoaPrinterSupport::supportedSizesWithNames(const QPrinterInfo &printerInfo) const
{
QList<QPair<QString, QSizeF> > 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<PMPaper>(const_cast<void *>(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;
}

View File

@ -86,6 +86,11 @@ QList<QPrinter::PaperSize> QCupsPrinterSupport::supportedPaperSizes(const QPrint
return QCUPSSupport::getCupsPrinterPaperSizes(printerIndex(printerInfo));
}
QList<QPair<QString, QSizeF> > QCupsPrinterSupport::supportedSizesWithNames(const QPrinterInfo &printerInfo) const
{
return QCUPSSupport::getCupsPrinterPaperSizesWithNames(printerIndex(printerInfo));
}
void QCupsPrinterSupport::loadCups()
{
cupsGetDests = (CupsGetDests) m_cups.resolve("cupsGetDests");

View File

@ -67,6 +67,7 @@ public:
virtual QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode);
virtual QPaintEngine *createPaintEngine(QPrintEngine *printEngine, QPrinter::PrinterMode);
virtual QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &) const;
virtual QList<QPair<QString, QSizeF> > supportedSizesWithNames(const QPrinterInfo &) const;
virtual QString printerOption(const QPrinterInfo &printer, const QString &key) const;
virtual PrinterOptions printerOptions(const QPrinterInfo &printer) const;

View File

@ -93,4 +93,9 @@ QList<QPrinter::PaperSize> QWindowsPrinterSupport::supportedPaperSizes(const QPr
return QWin32PrintEngine::supportedPaperSizes(printerInfo);
}
QList<QPair<QString, QSizeF> >QWindowsPrinterSupport::supportedSizesWithNames(const QPrinterInfo &printerInfo) const
{
return QWin32PrintEngine::supportedSizesWithNames(printerInfo);
}
QT_END_NAMESPACE

View File

@ -57,6 +57,7 @@ public:
virtual QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode);
virtual QPaintEngine *createPaintEngine(QPrintEngine *printEngine, QPrinter::PrinterMode);
virtual QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &) const;
virtual QList<QPair<QString, QSizeF> >supportedSizesWithNames(const QPrinterInfo &printerInfo) const;
};
QT_END_NAMESPACE

View File

@ -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<QPrinter::PaperSize> QCUPSSupport::getCupsPrinterPaperSizes(int cupsPrinte
return result;
}
QList<QPair<QString, QSizeF> > QCUPSSupport::getCupsPrinterPaperSizesWithNames(int cupsPrinterIndex)
{
QList<QPair<QString, QSizeF> > 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

View File

@ -121,6 +121,7 @@ public:
static QList<Printer> availableUnixPrinters();
static QList<QPrinter::PaperSize> getCupsPrinterPaperSizes(int cupsPrinterIndex);
static QList<QPair<QString, QSizeF> > getCupsPrinterPaperSizesWithNames(int cupsPrinterIndex);
private:
void collectMarkedOptions(QStringList& list, const ppd_group_t* group = 0) const;

View File

@ -82,6 +82,11 @@ QList<QPrinter::PaperSize> QPlatformPrinterSupport::supportedPaperSizes(const QP
return QList<QPrinter::PaperSize>();
}
QList<QPair<QString, QSizeF> > QPlatformPrinterSupport::supportedSizesWithNames(const QPrinterInfo &) const
{
return QList<QPair<QString, QSizeF> >();
}
QList<QPrinterInfo> QPlatformPrinterSupport::availablePrinters()
{
return m_printers;

View File

@ -72,7 +72,7 @@ public:
virtual QPrintEngine *createNativePrintEngine(QPrinter::PrinterMode printerMode);
virtual QPaintEngine *createPaintEngine(QPrintEngine *, QPrinter::PrinterMode printerMode);
virtual QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &) const;
virtual QList<QPair<QString, QSizeF> > supportedSizesWithNames(const QPrinterInfo &printerInfo) const;
virtual QList<QPrinterInfo> availablePrinters();
virtual QPrinterInfo defaultPrinter();
virtual QPrinterInfo printerInfo(const QString &printerName);

View File

@ -1596,6 +1596,38 @@ QList<QPrinter::PaperSize> QWin32PrintEngine::supportedPaperSizes(const QPrinter
return returnList;
}
QList<QPair<QString, QSizeF> > QWin32PrintEngine::supportedSizesWithNames(const QPrinterInfo &printerInfo)
{
QList<QPair<QString, QSizeF> > paperSizes;
if (printerInfo.isNull())
return paperSizes;
DWORD size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(printerInfo.printerName().utf16()),
NULL, DC_PAPERNAMES, NULL, NULL);
if ((int)size != -1) {
wchar_t *papers = new wchar_t[size*64];
size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(printerInfo.printerName().utf16()),
NULL, DC_PAPERNAMES, papers, NULL);
DWORD size2 = DeviceCapabilities(reinterpret_cast<const wchar_t*>(printerInfo.printerName().utf16()),
NULL, DC_PAPERSIZE, NULL, NULL);
if ((int)size2 != -1) {
POINT *points = new POINT[size2*sizeof(POINT)];
size2 = DeviceCapabilities(reinterpret_cast<const wchar_t*>(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

View File

@ -104,6 +104,7 @@ public:
void releaseDC(HDC) const;
static QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &printerInfo);
static QList<QPair<QString, QSizeF> > supportedSizesWithNames(const QPrinterInfo &printerInfo);
static void queryDefaultPrinter(QString &name, QString &program, QString &port);
private:

View File

@ -222,6 +222,25 @@ QList<QPrinter::PaperSize> 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<QPair<QString, QSizeF> > 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> QPrinterInfo::availablePrinters()
{
QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();

View File

@ -43,7 +43,7 @@
#define QPRINTERINFO_H
#include <QtCore/QList>
#include <QtCore/QPair>
#include <QtPrintSupport/QPrinter>
QT_BEGIN_NAMESPACE
@ -71,6 +71,7 @@ public:
bool isDefault() const;
QList<QPrinter::PaperSize> supportedPaperSizes() const;
QList<QPair<QString, QSizeF> > supportedSizesWithNames() const;
static QList<QPrinterInfo> availablePrinters();
static QPrinterInfo defaultPrinter();

View File

@ -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<QPrinter::PaperSize> paperSizes;
mutable bool hasPaperNames;
mutable QList<QPair<QString, QSizeF> > paperNames;
};