Fix QPrinter test in Windows

Fixed Q_WS_WIN flagging to Q_OS_WIN in QPrinter API and related
implementation to make API match the documentation and Qt 4.8.
Also Removed the unused internal HDC related functions from the API,
that were previously behind Q_WS_WIN flag.

Some of the properties tested are documented to be valid for native
print engine only in X11 environment, so skipped testing those in
non-xcb environments.

Copy collation is also apparently not supported in Windows native print
engine, though this seems to be undocumented, so skipped that only in
Windows.

At least one of the test blocks in tst_QPrinter::valuePreservation()
failed due to default printer not getting set properly, so fixed that,
too.

Task-number: QTBUG-24191
Task-number: QTBUG-22927
Change-Id: I44a5e3d647a1279fcc7f1e99de6881f9be330246
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
This commit is contained in:
Miikka Heikkinen 2012-03-21 17:17:42 +02:00 committed by Qt by Nokia
parent 82c974f753
commit b188221fee
7 changed files with 69 additions and 89 deletions

View File

@ -58,12 +58,13 @@ QWindowsPrinterSupport::QWindowsPrinterSupport()
LPBYTE buffer = new BYTE[needed]; LPBYTE buffer = new BYTE[needed];
if (EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, buffer, needed, &needed, &returned)) { if (EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, buffer, needed, &needed, &returned)) {
PPRINTER_INFO_4 infoList = reinterpret_cast<PPRINTER_INFO_4>(buffer); PPRINTER_INFO_4 infoList = reinterpret_cast<PPRINTER_INFO_4>(buffer);
QPrinterInfo defPrn = defaultPrinter(); QString defaultPrinterName;
QWin32PrintEngine::queryDefaultPrinter(defaultPrinterName, QString(), QString());
for (uint i = 0; i < returned; ++i) { for (uint i = 0; i < returned; ++i) {
QString printerName(QString::fromWCharArray(infoList[i].pPrinterName)); QString printerName(QString::fromWCharArray(infoList[i].pPrinterName));
QPrinterInfo printerInfo(printerName); QPrinterInfo printerInfo(printerName);
if (printerInfo.printerName() == defPrn.printerName()) if (printerInfo.printerName() == defaultPrinterName)
printerInfo.d_ptr->isDefault = true; printerInfo.d_ptr->isDefault = true;
mPrinterList.append(printerInfo); mPrinterList.append(printerInfo);
} }

View File

@ -101,12 +101,6 @@ public:
virtual int metric(QPaintDevice::PaintDeviceMetric) const = 0; virtual int metric(QPaintDevice::PaintDeviceMetric) const = 0;
virtual QPrinter::PrinterState printerState() const = 0; virtual QPrinter::PrinterState printerState() const = 0;
#ifdef Q_WS_WIN
virtual HDC getPrinterDC() const { return 0; }
virtual void releasePrinterDC(HDC) const { }
#endif
}; };
#endif // QT_NO_PRINTER #endif // QT_NO_PRINTER

View File

@ -966,28 +966,7 @@ void QWin32PrintEngine::drawPolygon(const QPointF *points, int pointCount, Polyg
void QWin32PrintEnginePrivate::queryDefault() void QWin32PrintEnginePrivate::queryDefault()
{ {
/* Read the default printer name, driver and port with the intuitive function QWin32PrintEngine::queryDefaultPrinter(name, program, port);
* Strings "windows" and "device" are specified in the MSDN under EnumPrinters()
*/
QString noPrinters(QLatin1String("qt_no_printers"));
wchar_t buffer[256];
GetProfileString(L"windows", L"device",
reinterpret_cast<const wchar_t *>(noPrinters.utf16()),
buffer, 256);
QString output = QString::fromWCharArray(buffer);
if (output.isEmpty() || output == noPrinters) // no printers
return;
QStringList info = output.split(QLatin1Char(','));
int infoSize = info.size();
if (infoSize > 0) {
if (name.isEmpty())
name = info.at(0);
if (program.isEmpty() && infoSize > 1)
program = info.at(1);
if (port.isEmpty() && infoSize > 2)
port = info.at(2);
}
} }
QWin32PrintEnginePrivate::~QWin32PrintEnginePrivate() QWin32PrintEnginePrivate::~QWin32PrintEnginePrivate()
@ -1601,6 +1580,32 @@ QList<QPrinter::PaperSize> QWin32PrintEngine::supportedPaperSizes(const QPrinter
return returnList; return returnList;
} }
void QWin32PrintEngine::queryDefaultPrinter(QString &name, QString &program, QString &port)
{
/* Read the default printer name, driver and port with the intuitive function
* Strings "windows" and "device" are specified in the MSDN under EnumPrinters()
*/
QString noPrinters(QLatin1String("qt_no_printers"));
wchar_t buffer[256];
GetProfileString(L"windows", L"device",
reinterpret_cast<const wchar_t *>(noPrinters.utf16()),
buffer, 256);
QString output = QString::fromWCharArray(buffer);
if (output.isEmpty() || output == noPrinters) // no printers
return;
QStringList info = output.split(QLatin1Char(','));
int infoSize = info.size();
if (infoSize > 0) {
if (name.isEmpty())
name = info.at(0);
if (program.isEmpty() && infoSize > 1)
program = info.at(1);
if (port.isEmpty() && infoSize > 2)
port = info.at(2);
}
}
HGLOBAL *QWin32PrintEnginePrivate::createDevNames() HGLOBAL *QWin32PrintEnginePrivate::createDevNames()
{ {
int size = sizeof(DEVNAMES) int size = sizeof(DEVNAMES)

View File

@ -103,10 +103,8 @@ public:
HDC getDC() const; HDC getDC() const;
void releaseDC(HDC) const; void releaseDC(HDC) const;
HDC getPrinterDC() const { return getDC(); }
void releasePrinterDC(HDC dc) const { releaseDC(dc); }
static QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &printerInfo); static QList<QPrinter::PaperSize> supportedPaperSizes(const QPrinterInfo &printerInfo);
static void queryDefaultPrinter(QString &name, QString &program, QString &port);
private: private:
friend class QPrintDialog; friend class QPrintDialog;

View File

@ -56,10 +56,6 @@
#include <QtPrintSupport/QPlatformPrinterSupport> #include <QtPrintSupport/QPlatformPrinterSupport>
#include <private/qpagedpaintdevice_p.h> #include <private/qpagedpaintdevice_p.h>
#if defined (Q_WS_WIN)
#include <private/qprintengine_win_p.h>
#endif
#if defined(Q_WS_X11) #if defined(Q_WS_X11)
#include <private/qt_x11_p.h> #include <private/qt_x11_p.h>
#endif #endif
@ -1722,7 +1718,7 @@ QPrintEngine *QPrinter::printEngine() const
return d->printEngine; return d->printEngine;
} }
#if defined (Q_WS_WIN) #if defined (Q_OS_WIN)
/*! /*!
Sets the page size to be used by the printer under Windows to \a Sets the page size to be used by the printer under Windows to \a
pageSize. pageSize.
@ -1753,7 +1749,7 @@ int QPrinter::winPageSize() const
Q_D(const QPrinter); Q_D(const QPrinter);
return d->printEngine->property(QPrintEngine::PPK_WindowsPageSize).toInt(); return d->printEngine->property(QPrintEngine::PPK_WindowsPageSize).toInt();
} }
#endif // Q_WS_WIN #endif // Q_OS_WIN
/*! /*!
Returns a list of the resolutions (a list of dots-per-inch Returns a list of the resolutions (a list of dots-per-inch
@ -1864,25 +1860,7 @@ QPrinter::PrinterState QPrinter::printerState() const
Use printerState() == QPrinter::Aborted instead. Use printerState() == QPrinter::Aborted instead.
*/ */
#ifdef Q_WS_WIN #ifdef Q_OS_WIN
/*!
\internal
*/
HDC QPrinter::getDC() const
{
Q_D(const QPrinter);
return d->printEngine->getPrinterDC();
}
/*!
\internal
*/
void QPrinter::releaseDC(HDC hdc) const
{
Q_D(const QPrinter);
d->printEngine->releasePrinterDC(hdc);
}
/*! /*!
Returns the supported paper sizes for this printer. Returns the supported paper sizes for this printer.
@ -1907,7 +1885,7 @@ QList<QPrinter::PaperSource> QPrinter::supportedPaperSources() const
return int_list; return int_list;
} }
#endif #endif // Q_OS_WIN
/*! /*!
\fn QString QPrinter::printerSelectionOption() const \fn QString QPrinter::printerSelectionOption() const
@ -1941,7 +1919,7 @@ QList<QPrinter::PaperSource> QPrinter::supportedPaperSources() const
\sa printerSelectionOption() \sa printerSelectionOption()
*/ */
#ifndef Q_WS_WIN #ifndef Q_OS_WIN
QString QPrinter::printerSelectionOption() const QString QPrinter::printerSelectionOption() const
{ {
Q_D(const QPrinter); Q_D(const QPrinter);
@ -2207,16 +2185,6 @@ QPrinter::PrintRange QPrinter::printRange() const
Returns the current state of the printer being used by the print engine. Returns the current state of the printer being used by the print engine.
*/ */
/*!
\fn HDC QPrintEngine::getPrinterDC() const
\internal
*/
/*!
\fn void QPrintEngine::releasePrinterDC(HDC) const
\internal
*/
/* /*
Returns the dimensions for the given paper size, \a size, in millimeters. Returns the dimensions for the given paper size, \a size, in millimeters.
*/ */

View File

@ -200,7 +200,7 @@ public:
QList<int> supportedResolutions() const; QList<int> supportedResolutions() const;
#ifdef Q_WS_WIN #ifdef Q_OS_WIN
QList<PaperSource> supportedPaperSources() const; QList<PaperSource> supportedPaperSources() const;
#endif #endif
@ -210,7 +210,7 @@ public:
void setDoubleSidedPrinting(bool enable); void setDoubleSidedPrinting(bool enable);
bool doubleSidedPrinting() const; bool doubleSidedPrinting() const;
#ifdef Q_WS_WIN #ifdef Q_OS_WIN
void setWinPageSize(int winPageSize); void setWinPageSize(int winPageSize);
int winPageSize() const; int winPageSize() const;
#endif #endif
@ -220,7 +220,7 @@ public:
QRectF paperRect(Unit) const; QRectF paperRect(Unit) const;
QRectF pageRect(Unit) const; QRectF pageRect(Unit) const;
#if !defined(Q_WS_WIN) || defined(qdoc) #if !defined(Q_OS_WIN) || defined(qdoc)
QString printerSelectionOption() const; QString printerSelectionOption() const;
void setPrinterSelectionOption(const QString &); void setPrinterSelectionOption(const QString &);
#endif #endif
@ -233,11 +233,6 @@ public:
QPaintEngine *paintEngine() const; QPaintEngine *paintEngine() const;
QPrintEngine *printEngine() const; QPrintEngine *printEngine() const;
#ifdef Q_WS_WIN
HDC getDC() const;
void releaseDC(HDC hdc) const;
#endif
void setFromTo(int fromPage, int toPage); void setFromTo(int fromPage, int toPage);
int fromPage() const; int fromPage() const;
int toPage() const; int toPage() const;

View File

@ -235,8 +235,8 @@ void tst_QPrinter::testPageSetupDialog()
void tst_QPrinter::testPageSize() void tst_QPrinter::testPageSize()
{ {
#if 1 #ifndef Q_OS_WIN
QSKIP("QPrinter::winPageSize(): Windows only and currently not implemented / QTBUG-22927"); QSKIP("QPrinter::winPageSize(): Windows only.");
#else #else
QPrinter prn; QPrinter prn;
@ -255,7 +255,7 @@ void tst_QPrinter::testPageSize()
prn.setWinPageSize(DMPAPER_A4); prn.setWinPageSize(DMPAPER_A4);
MYCOMPARE(prn.winPageSize(), DMPAPER_A4); MYCOMPARE(prn.winPageSize(), DMPAPER_A4);
MYCOMPARE(prn.pageSize(), QPrinter::A4); MYCOMPARE(prn.pageSize(), QPrinter::A4);
#endif #endif // Q_OS_WIN
} }
void tst_QPrinter::testPageRectAndPaperRect_data() void tst_QPrinter::testPageRectAndPaperRect_data()
@ -625,7 +625,25 @@ void tst_QPrinter::valuePreservation()
QPrinter::OutputFormat oldFormat = QPrinter::PdfFormat; QPrinter::OutputFormat oldFormat = QPrinter::PdfFormat;
QPrinter::OutputFormat newFormat = QPrinter::NativeFormat; // TODO: Correct? QPrinter::OutputFormat newFormat = QPrinter::NativeFormat; // TODO: Correct?
{ // Some properties are documented to only be supported by NativeFormat in X11 environment
bool doX11Tests = QGuiApplication::platformName().compare(QLatin1String("xcb"), Qt::CaseInsensitive) == 0;
bool windowsPlatform = QGuiApplication::platformName().compare(QLatin1String("windows"), Qt::CaseInsensitive) == 0;
bool manualSourceSupported = true;
#ifdef Q_OS_WIN
// QPrinter::supportedPaperSources() is only available on Windows, so just assuming manual is supported on others.
QPrinter printer;
printer.setOutputFormat(newFormat);
QList<QPrinter::PaperSource> sources = printer.supportedPaperSources();
if (!sources.contains(QPrinter::Manual)) {
manualSourceSupported = false;
qWarning() << "Manual paper source not supported by native printer, skipping related test.";
}
#endif // Q_OS_WIN
// Querying PPK_CollateCopies is hardcoded to return false with Windows native print engine,
// so skip testing that in Windows.
if (!windowsPlatform) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
bool status = printer.collateCopies(); bool status = printer.collateCopies();
@ -653,7 +671,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status)); QCOMPARE(printer.colorMode(), QPrinter::ColorMode(!status));
} }
{ if (doX11Tests) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QString status = printer.creator(); QString status = printer.creator();
@ -683,7 +701,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.docName(), status); QCOMPARE(printer.docName(), status);
} }
{ if (doX11Tests) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
bool status = printer.doubleSidedPrinting(); bool status = printer.doubleSidedPrinting();
@ -697,7 +715,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.doubleSidedPrinting(), !status); QCOMPARE(printer.doubleSidedPrinting(), !status);
} }
{ if (doX11Tests) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
bool status = printer.fontEmbeddingEnabled(); bool status = printer.fontEmbeddingEnabled();
@ -754,7 +772,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.outputFileName(), status); QCOMPARE(printer.outputFileName(), status);
} }
{ if (doX11Tests) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QPrinter::PageOrder status = printer.pageOrder(); QPrinter::PageOrder status = printer.pageOrder();
@ -782,7 +800,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.pageSize(), QPrinter::B5); QCOMPARE(printer.pageSize(), QPrinter::B5);
} }
{ if (manualSourceSupported) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QPrinter::PaperSource status = printer.paperSource(); QPrinter::PaperSource status = printer.paperSource();
@ -796,7 +814,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.paperSource(), QPrinter::Manual); QCOMPARE(printer.paperSource(), QPrinter::Manual);
} }
{ if (doX11Tests) {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QString status = printer.printProgram(); QString status = printer.printProgram();
@ -840,6 +858,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.printerName(), status); QCOMPARE(printer.printerName(), status);
} }
// QPrinter::printerSelectionOption is explicitly documented not to be available on Windows.
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
{ {
QPrinter printer; QPrinter printer;
@ -856,7 +875,7 @@ void tst_QPrinter::valuePreservation()
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);
QCOMPARE(printer.printerSelectionOption(), status); QCOMPARE(printer.printerSelectionOption(), status);
} }
#endif #endif // Q_OS_WIN
{ {
QPrinter printer; QPrinter printer;
printer.setOutputFormat(oldFormat); printer.setOutputFormat(oldFormat);