summaryrefslogtreecommitdiffstats
path: root/src/printsupport/kernel/qprinter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/printsupport/kernel/qprinter.cpp')
-rw-r--r--src/printsupport/kernel/qprinter.cpp238
1 files changed, 129 insertions, 109 deletions
diff --git a/src/printsupport/kernel/qprinter.cpp b/src/printsupport/kernel/qprinter.cpp
index 819f9343f7..5eb840c52a 100644
--- a/src/printsupport/kernel/qprinter.cpp
+++ b/src/printsupport/kernel/qprinter.cpp
@@ -48,7 +48,6 @@
#include <qpa/qplatformprintersupport.h>
#include "qprintengine.h"
-#include "qprinterinfo.h"
#include "qlist.h"
#include <qcoreapplication.h>
#include <qfileinfo.h>
@@ -163,10 +162,39 @@ Q_PRINTSUPPORT_EXPORT QSizeF qt_printerPaperSize(QPrinter::Orientation orientati
(qt_paperSizes[paperSize][height_index] * 72 / 25.4) / multiplier);
}
-void QPrinterPrivate::createDefaultEngines()
+QPrinterInfo QPrinterPrivate::findValidPrinter(const QPrinterInfo &printer)
{
- QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
- if (outputFormat == QPrinter::NativeFormat && ps) {
+ // Try find a valid printer to use, either the one given, the default or the first available
+ QPrinterInfo printerToUse = printer;
+ if (printerToUse.isNull()) {
+ printerToUse = QPrinterInfo::defaultPrinter();
+ if (printerToUse.isNull()) {
+ QList<QPrinterInfo> availablePrinters = QPrinterInfo::availablePrinters();
+ if (!availablePrinters.isEmpty())
+ printerToUse = availablePrinters.at(0);
+ }
+ }
+ return printerToUse;
+}
+
+void QPrinterPrivate::initEngines(QPrinter::OutputFormat format, const QPrinterInfo &printer)
+{
+ // Default to PdfFormat
+ outputFormat = QPrinter::PdfFormat;
+ QPlatformPrinterSupport *ps = 0;
+ QString printerName;
+
+ // Only set NativeFormat if we have a valid plugin and printer to use
+ if (format == QPrinter::NativeFormat) {
+ ps = QPlatformPrinterSupportPlugin::get();
+ QPrinterInfo printerToUse = findValidPrinter(printer);
+ if (ps && !printerToUse.isNull()) {
+ outputFormat = QPrinter::NativeFormat;
+ printerName = printerToUse.printerName();
+ }
+ }
+
+ if (outputFormat == QPrinter::NativeFormat) {
printEngine = ps->createNativePrintEngine(printerMode);
paintEngine = ps->createPaintEngine(printEngine, printerMode);
} else {
@@ -174,8 +202,38 @@ void QPrinterPrivate::createDefaultEngines()
paintEngine = pdfEngine;
printEngine = pdfEngine;
}
+
use_default_engine = true;
had_default_engines = true;
+ setProperty(QPrintEngine::PPK_PrinterName, printerName);
+ validPrinter = true;
+}
+
+void QPrinterPrivate::changeEngines(QPrinter::OutputFormat format, const QPrinterInfo &printer)
+{
+ QPrintEngine *oldPrintEngine = printEngine;
+ const bool def_engine = use_default_engine;
+
+ initEngines(format, printer);
+
+ if (oldPrintEngine) {
+ foreach (QPrintEngine::PrintEnginePropertyKey key, m_properties.values()) {
+ QVariant prop;
+ // PPK_NumberOfCopies need special treatmeant since it in most cases
+ // will return 1, disregarding the actual value that was set
+ // PPK_PrinterName also needs special treatment as initEngines has set it already
+ if (key == QPrintEngine::PPK_NumberOfCopies)
+ prop = QVariant(q_ptr->copyCount());
+ else if (key != QPrintEngine::PPK_PrinterName)
+ prop = oldPrintEngine->property(key);
+
+ if (prop.isValid())
+ setProperty(key, prop);
+ }
+ }
+
+ if (def_engine)
+ delete oldPrintEngine;
}
#ifndef QT_NO_PRINTPREVIEWWIDGET
@@ -205,15 +263,14 @@ void QPrinterPrivate::setPreviewMode(bool enable)
}
#endif // QT_NO_PRINTPREVIEWWIDGET
-void QPrinterPrivate::addToManualSetList(QPrintEngine::PrintEnginePropertyKey key)
+void QPrinterPrivate::setProperty(QPrintEngine::PrintEnginePropertyKey key, const QVariant &value)
{
- for (int c = 0; c < manualSetList.size(); ++c) {
- if (manualSetList[c] == key) return;
- }
- manualSetList.append(key);
+ printEngine->setProperty(key, value);
+ m_properties.insert(key);
}
+
/*!
\class QPrinter
\reentrant
@@ -510,13 +567,7 @@ QPrinter::QPrinter(PrinterMode mode)
: QPagedPaintDevice(),
d_ptr(new QPrinterPrivate(this))
{
- d_ptr->init(mode);
- QPrinterInfo defPrn(QPrinterInfo::defaultPrinter());
- if (!defPrn.isNull()) {
- setPrinterName(defPrn.printerName());
- } else if (QPrinterInfo::availablePrinters().isEmpty()) {
- setOutputFormat(QPrinter::PdfFormat);
- }
+ d_ptr->init(QPrinterInfo(), mode);
}
/*!
@@ -528,11 +579,10 @@ QPrinter::QPrinter(const QPrinterInfo& printer, PrinterMode mode)
: QPagedPaintDevice(),
d_ptr(new QPrinterPrivate(this))
{
- d_ptr->init(mode);
- setPrinterName(printer.printerName());
+ d_ptr->init(printer, mode);
}
-void QPrinterPrivate::init(QPrinter::PrinterMode mode)
+void QPrinterPrivate::init(const QPrinterInfo &printer, QPrinter::PrinterMode mode)
{
if (!QCoreApplication::instance()) {
qFatal("QPrinter: Must construct a QCoreApplication before a QPrinter");
@@ -540,14 +590,8 @@ void QPrinterPrivate::init(QPrinter::PrinterMode mode)
}
printerMode = mode;
- outputFormat = QPrinter::NativeFormat;
- createDefaultEngines();
-#ifndef QT_NO_PRINTPREVIEWWIDGET
- previewEngine = 0;
-#endif
- realPrintEngine = 0;
- realPaintEngine = 0;
+ initEngines(QPrinter::NativeFormat, printer);
}
/*!
@@ -612,40 +656,30 @@ QPrinter::~QPrinter()
\since 4.1
Sets the output format for this printer to \a format.
+
+ If \a format is the same value as currently set then no change will be made.
+
+ If \a format is NativeFormat then the printerName will be set to the default
+ printer. If there are no valid printers configured then no change will be made.
+ If you want to set NativeFormat with a specific printerName then use
+ setPrinterName().
+
+ \sa setPrinterName()
*/
void QPrinter::setOutputFormat(OutputFormat format)
{
Q_D(QPrinter);
- if (d->validPrinter && d->outputFormat == format)
- return;
- d->outputFormat = format;
- QPrintEngine *oldPrintEngine = d->printEngine;
- const bool def_engine = d->use_default_engine;
- d->printEngine = 0;
-
- d->createDefaultEngines();
+ if (d->outputFormat == format)
+ return;
- if (oldPrintEngine) {
- for (int i = 0; i < d->manualSetList.size(); ++i) {
- QPrintEngine::PrintEnginePropertyKey key = d->manualSetList[i];
- QVariant prop;
- // PPK_NumberOfCopies need special treatmeant since it in most cases
- // will return 1, disregarding the actual value that was set
- if (key == QPrintEngine::PPK_NumberOfCopies)
- prop = QVariant(copyCount());
- else
- prop = oldPrintEngine->property(key);
- if (prop.isValid())
- d->printEngine->setProperty(key, prop);
- }
+ if (format == QPrinter::NativeFormat) {
+ QPrinterInfo printerToUse = d->findValidPrinter();
+ if (!printerToUse.isNull())
+ d->changeEngines(format, printerToUse);
+ } else {
+ d->changeEngines(format, QPrinterInfo());
}
-
- if (def_engine)
- delete oldPrintEngine;
-
- if (d->outputFormat == QPrinter::PdfFormat)
- d->validPrinter = true;
}
/*!
@@ -683,30 +717,37 @@ QString QPrinter::printerName() const
/*!
Sets the printer name to \a name.
- \sa printerName(), isValid()
+ If the \a name is empty then the output format will be set to PdfFormat.
+
+ If the \a name is not a valid printer then no change will be made.
+
+ If the \a name is a valid printer then the output format will be set to NativeFormat.
+
+ \sa printerName(), isValid(), setOutputFormat()
*/
void QPrinter::setPrinterName(const QString &name)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setPrinterName");
- QList<QPrinterInfo> prnList = QPrinterInfo::availablePrinters();
+ if (printerName() == name)
+ return;
+
if (name.isEmpty()) {
- d->validPrinter = d->outputFormat == QPrinter::PdfFormat;
- } else {
- d->validPrinter = false;
- for (int i = 0; i < prnList.size(); ++i) {
- if (prnList[i].printerName() == name) {
- d->validPrinter = true;
- break;
- }
- }
+ setOutputFormat(QPrinter::PdfFormat);
+ return;
}
- d->printEngine->setProperty(QPrintEngine::PPK_PrinterName, name);
- d->addToManualSetList(QPrintEngine::PPK_PrinterName);
-}
+ QPrinterInfo printerToUse = QPrinterInfo::printerInfo(name);
+ if (printerToUse.isNull())
+ return;
+ if (outputFormat() == QPrinter::PdfFormat) {
+ d->changeEngines(QPrinter::NativeFormat, printerToUse);
+ } else {
+ d->setProperty(QPrintEngine::PPK_PrinterName, name);
+ }
+}
/*!
\since 4.4
@@ -774,8 +815,7 @@ void QPrinter::setOutputFileName(const QString &fileName)
else if (fileName.isEmpty())
setOutputFormat(QPrinter::NativeFormat);
- d->printEngine->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
- d->addToManualSetList(QPrintEngine::PPK_OutputFileName);
+ d->setProperty(QPrintEngine::PPK_OutputFileName, fileName);
}
@@ -810,8 +850,7 @@ void QPrinter::setPrintProgram(const QString &printProg)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setPrintProgram");
- d->printEngine->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
- d->addToManualSetList(QPrintEngine::PPK_PrinterProgram);
+ d->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
}
@@ -841,8 +880,7 @@ void QPrinter::setDocName(const QString &name)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setDocName");
- d->printEngine->setProperty(QPrintEngine::PPK_DocumentName, name);
- d->addToManualSetList(QPrintEngine::PPK_DocumentName);
+ d->setProperty(QPrintEngine::PPK_DocumentName, name);
}
@@ -872,8 +910,7 @@ void QPrinter::setCreator(const QString &creator)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setCreator");
- d->printEngine->setProperty(QPrintEngine::PPK_Creator, creator);
- d->addToManualSetList(QPrintEngine::PPK_Creator);
+ d->setProperty(QPrintEngine::PPK_Creator, creator);
}
@@ -910,8 +947,7 @@ QPrinter::Orientation QPrinter::orientation() const
void QPrinter::setOrientation(Orientation orientation)
{
Q_D(QPrinter);
- d->printEngine->setProperty(QPrintEngine::PPK_Orientation, orientation);
- d->addToManualSetList(QPrintEngine::PPK_Orientation);
+ d->setProperty(QPrintEngine::PPK_Orientation, orientation);
}
@@ -979,8 +1015,7 @@ void QPrinter::setPageSize(PageSize newPageSize)
qWarning("QPrinter::setPaperSize: Illegal paper size %d", newPageSize);
return;
}
- d->printEngine->setProperty(QPrintEngine::PPK_PaperSize, newPageSize);
- d->addToManualSetList(QPrintEngine::PPK_PaperSize);
+ d->setProperty(QPrintEngine::PPK_PaperSize, newPageSize);
d->hasUserSetPageSize = true;
}
@@ -1010,8 +1045,7 @@ void QPrinter::setPageSizeMM(const QSizeF &size)
QPagedPaintDevice::setPageSizeMM(size);
QSizeF s = size * 72./25.4;
- d->printEngine->setProperty(QPrintEngine::PPK_CustomPaperSize, s);
- d->addToManualSetList(QPrintEngine::PPK_CustomPaperSize);
+ d->setProperty(QPrintEngine::PPK_CustomPaperSize, s);
d->hasUserSetPageSize = true;
}
@@ -1051,8 +1085,7 @@ void QPrinter::setPaperName(const QString &paperName)
Q_D(QPrinter);
if (d->paintEngine->type() != QPaintEngine::Pdf)
ABORT_IF_ACTIVE("QPrinter::setPaperName");
- d->printEngine->setProperty(QPrintEngine::PPK_PaperName, paperName);
- d->addToManualSetList(QPrintEngine::PPK_PaperName);
+ d->setProperty(QPrintEngine::PPK_PaperName, paperName);
}
/*!
@@ -1088,8 +1121,7 @@ void QPrinter::setPageOrder(PageOrder pageOrder)
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setPageOrder");
- d->printEngine->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
- d->addToManualSetList(QPrintEngine::PPK_PageOrder);
+ d->setProperty(QPrintEngine::PPK_PageOrder, pageOrder);
}
@@ -1117,8 +1149,7 @@ void QPrinter::setColorMode(ColorMode newColorMode)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setColorMode");
- d->printEngine->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
- d->addToManualSetList(QPrintEngine::PPK_ColorMode);
+ d->setProperty(QPrintEngine::PPK_ColorMode, newColorMode);
}
@@ -1197,8 +1228,7 @@ void QPrinter::setNumCopies(int numCopies)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setNumCopies");
- d->printEngine->setProperty(QPrintEngine::PPK_NumberOfCopies, numCopies);
- d->addToManualSetList(QPrintEngine::PPK_NumberOfCopies);
+ d->setProperty(QPrintEngine::PPK_NumberOfCopies, numCopies);
}
/*!
@@ -1216,8 +1246,7 @@ void QPrinter::setCopyCount(int count)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setCopyCount;");
- d->printEngine->setProperty(QPrintEngine::PPK_CopyCount, count);
- d->addToManualSetList(QPrintEngine::PPK_CopyCount);
+ d->setProperty(QPrintEngine::PPK_CopyCount, count);
}
/*!
@@ -1286,8 +1315,7 @@ void QPrinter::setCollateCopies(bool collate)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setCollateCopies");
- d->printEngine->setProperty(QPrintEngine::PPK_CollateCopies, collate);
- d->addToManualSetList(QPrintEngine::PPK_CollateCopies);
+ d->setProperty(QPrintEngine::PPK_CollateCopies, collate);
}
@@ -1316,8 +1344,7 @@ void QPrinter::setCollateCopies(bool collate)
void QPrinter::setFullPage(bool fp)
{
Q_D(QPrinter);
- d->printEngine->setProperty(QPrintEngine::PPK_FullPage, fp);
- d->addToManualSetList(QPrintEngine::PPK_FullPage);
+ d->setProperty(QPrintEngine::PPK_FullPage, fp);
}
@@ -1355,8 +1382,7 @@ void QPrinter::setResolution(int dpi)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setResolution");
- d->printEngine->setProperty(QPrintEngine::PPK_Resolution, dpi);
- d->addToManualSetList(QPrintEngine::PPK_Resolution);
+ d->setProperty(QPrintEngine::PPK_Resolution, dpi);
}
@@ -1385,8 +1411,7 @@ int QPrinter::resolution() const
void QPrinter::setPaperSource(PaperSource source)
{
Q_D(QPrinter);
- d->printEngine->setProperty(QPrintEngine::PPK_PaperSource, source);
- d->addToManualSetList(QPrintEngine::PPK_PaperSource);
+ d->setProperty(QPrintEngine::PPK_PaperSource, source);
}
/*!
@@ -1412,8 +1437,7 @@ QPrinter::PaperSource QPrinter::paperSource() const
void QPrinter::setFontEmbeddingEnabled(bool enable)
{
Q_D(QPrinter);
- d->printEngine->setProperty(QPrintEngine::PPK_FontEmbedding, enable);
- d->addToManualSetList(QPrintEngine::PPK_FontEmbedding);
+ d->setProperty(QPrintEngine::PPK_FontEmbedding, enable);
}
/*!
@@ -1484,8 +1508,7 @@ bool QPrinter::doubleSidedPrinting() const
void QPrinter::setDuplex(DuplexMode duplex)
{
Q_D(QPrinter);
- d->printEngine->setProperty(QPrintEngine::PPK_Duplex, duplex);
- d->addToManualSetList(QPrintEngine::PPK_Duplex);
+ d->setProperty(QPrintEngine::PPK_Duplex, duplex);
}
/*!
@@ -1613,8 +1636,7 @@ void QPrinter::setMargins(const Margins &m)
QList<QVariant> margins;
margins << (m.left * multiplier) << (m.top * multiplier)
<< (m.right * multiplier) << (m.bottom * multiplier);
- d->printEngine->setProperty(QPrintEngine::PPK_PageMargins, margins);
- d->addToManualSetList(QPrintEngine::PPK_PageMargins);
+ d->setProperty(QPrintEngine::PPK_PageMargins, margins);
d->hasCustomPageMargins = true;
}
@@ -1685,8 +1707,7 @@ void QPrinter::setWinPageSize(int pageSize)
{
Q_D(QPrinter);
ABORT_IF_ACTIVE("QPrinter::setWinPageSize");
- d->printEngine->setProperty(QPrintEngine::PPK_WindowsPageSize, pageSize);
- d->addToManualSetList(QPrintEngine::PPK_WindowsPageSize);
+ d->setProperty(QPrintEngine::PPK_WindowsPageSize, pageSize);
}
/*!
@@ -1834,8 +1855,7 @@ QString QPrinter::printerSelectionOption() const
void QPrinter::setPrinterSelectionOption(const QString &option)
{
Q_D(QPrinter);
- d->printEngine->setProperty(QPrintEngine::PPK_SelectionOption, option);
- d->addToManualSetList(QPrintEngine::PPK_SelectionOption);
+ d->setProperty(QPrintEngine::PPK_SelectionOption, option);
}
#endif