summaryrefslogtreecommitdiffstats
path: root/src/printsupport
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@digia.com>2012-12-29 17:08:12 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-03-12 09:16:42 +0100
commit39a902c0081f442141353c466ada09d62036c04e (patch)
tree6a203f89bd6a4b781acfb27ea7672c6d68471e94 /src/printsupport
parent62fe32b37c173f9dc82f4f95ae50b2e95a719d77 (diff)
Add support for setting/getting the paper name on the QPrinter
This adds support for specifying a paper name which will be set on the printer if it is available for the driver. Change-Id: Id7fd0c8cf68745db3d7a8de7e2ac98d3e2ba9b79 Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'src/printsupport')
-rw-r--r--src/printsupport/dialogs/qpagesetupdialog_unix.cpp16
-rw-r--r--src/printsupport/kernel/qprintengine.h1
-rw-r--r--src/printsupport/kernel/qprintengine_win.cpp87
-rw-r--r--src/printsupport/kernel/qprinter.cpp33
-rw-r--r--src/printsupport/kernel/qprinter.h3
5 files changed, 124 insertions, 16 deletions
diff --git a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp
index 322d47d1fb..1e0251c6d2 100644
--- a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp
+++ b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp
@@ -360,7 +360,7 @@ void QPageSetupWidget::setupPrinter() const
else {
m_printer->setPaperSize(static_cast<QPrinter::PaperSize>(ps));
}
-
+ m_printer->setPaperName(widget.paperSize->currentText());
#ifdef PSD_ENABLE_PAPERSOURCE
m_printer->setPaperSource((QPrinter::PaperSource)widget.paperSource->currentIndex());
#endif
@@ -380,16 +380,22 @@ void QPageSetupWidget::selectPrinter()
int cupsDefaultSize = 0;
QSize qtPreferredSize = m_printer->paperSize(QPrinter::Point).toSize();
+ QString qtPaperName = m_printer->paperName();
bool preferredSizeMatched = false;
for (int i = 0; i < numChoices; ++i) {
widget.paperSize->addItem(QString::fromLocal8Bit(pageSizes->choices[i].text), QByteArray(pageSizes->choices[i].choice));
if (static_cast<int>(pageSizes->choices[i].marked) == 1)
cupsDefaultSize = i;
- QRect cupsPaperSize = cups.paperRect(pageSizes->choices[i].choice);
- QSize diff = cupsPaperSize.size() - qtPreferredSize;
- if (qAbs(diff.width()) < 5 && qAbs(diff.height()) < 5) {
+ if (qtPaperName == QString::fromLocal8Bit(pageSizes->choices[i].text)) {
widget.paperSize->setCurrentIndex(i);
preferredSizeMatched = true;
+ } else {
+ QRect cupsPaperSize = cups.paperRect(pageSizes->choices[i].choice);
+ QSize diff = cupsPaperSize.size() - qtPreferredSize;
+ if (qAbs(diff.width()) < 5 && qAbs(diff.height()) < 5) {
+ widget.paperSize->setCurrentIndex(i);
+ preferredSizeMatched = true;
+ }
}
}
if (!preferredSizeMatched)
@@ -452,7 +458,7 @@ void QPageSetupWidget::_q_paperSizeChanged()
bool custom = size == QPrinter::Custom;
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
- custom = custom ? m_cups : custom;
+ custom = custom && m_cups && (m_printer->paperName() == QLatin1String("Custom"));
#endif
widget.paperWidth->setEnabled(custom);
diff --git a/src/printsupport/kernel/qprintengine.h b/src/printsupport/kernel/qprintengine.h
index dabd39c5c0..3993a22bef 100644
--- a/src/printsupport/kernel/qprintengine.h
+++ b/src/printsupport/kernel/qprintengine.h
@@ -84,6 +84,7 @@ public:
PPK_PageMargins,
PPK_CopyCount,
PPK_SupportsMultipleCopies,
+ PPK_PaperName,
PPK_PaperSize = PPK_PageSize,
PPK_CustomBase = 0xff00
diff --git a/src/printsupport/kernel/qprintengine_win.cpp b/src/printsupport/kernel/qprintengine_win.cpp
index 7c67fd2845..c798ac0c7f 100644
--- a/src/printsupport/kernel/qprintengine_win.cpp
+++ b/src/printsupport/kernel/qprintengine_win.cpp
@@ -167,6 +167,16 @@ static int mapPaperSourceDevmode(QPrinter::PaperSource s)
return sources[i].winSourceName ? sources[i].winSourceName : s;
}
+static inline uint qwcsnlen(const wchar_t *str, uint maxlen)
+{
+ uint length = 0;
+ if (str) {
+ while (length < maxlen && *str++)
+ length++;
+ }
+ return length;
+}
+
QWin32PrintEngine::QWin32PrintEngine(QPrinter::PrinterMode mode)
: QAlphaPaintEngine(*(new QWin32PrintEnginePrivate),
PaintEngineFeatures(PrimitiveTransform
@@ -1285,7 +1295,39 @@ void QWin32PrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &
d->has_custom_paper_size = (QPrinter::PaperSize(value.toInt()) == QPrinter::Custom);
d->doReinit();
break;
-
+ case PPK_PaperName:
+ {
+ if (!d->devMode)
+ break;
+ DWORD size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERNAMES, NULL, NULL);
+ if ((int)size > 0) {
+ wchar_t *paperNames = new wchar_t[size*64];
+ size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERNAMES, paperNames, NULL);
+ int paperPos = -1;
+ for (int i=0;i<(int)size;i++) {
+ wchar_t *copyOfPaper = paperNames + (i * 64);
+ if (value.toString() == QString::fromWCharArray(copyOfPaper, qwcsnlen(copyOfPaper, 64))) {
+ paperPos = i;
+ break;
+ }
+ }
+ delete [] paperNames;
+ size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERS, NULL, NULL);
+ if ((int)size > 0) {
+ wchar_t *papers = new wchar_t[size];
+ size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERS, papers, NULL);
+ d->has_custom_paper_size = false;
+ d->devMode->dmPaperSize = papers[paperPos];
+ d->doReinit();
+ delete [] papers;
+ }
+ }
+ }
+ break;
case PPK_PaperSource:
{
if (!d->devMode)
@@ -1479,7 +1521,40 @@ QVariant QWin32PrintEngine::property(PrintEnginePropertyKey key) const
value = QTransform(1/d->stretch_x, 0, 0, 1/d->stretch_y, 0, 0).mapRect(d->devPaperRect);
}
break;
+ case PPK_PaperName:
+ if (!d->devMode) {
+ value = QLatin1String("A4");
+ } else {
+ DWORD size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERS, NULL, NULL);
+ int paperSizePos = -1;
+ if ((int)size > 0) {
+ wchar_t *papers = new wchar_t[size];
+ size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERS, papers, NULL);
+ for (int i=0;i<(int)size;i++) {
+ if (papers[i] == d->devMode->dmPaperSize) {
+ paperSizePos = i;
+ break;
+ }
+ }
+ delete [] papers;
+ }
+ if (paperSizePos != -1) {
+ size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERNAMES, NULL, NULL);
+ if ((int)size > 0) {
+ wchar_t *papers = new wchar_t[size*64];
+ size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->name.utf16()),
+ NULL, DC_PAPERNAMES, papers, NULL);
+ wchar_t *copyOfPaper = papers + (paperSizePos * 64);
+ value = QString::fromWCharArray(copyOfPaper, qwcsnlen(copyOfPaper, 64));
+ delete [] papers;
+ }
+ }
+ }
+ break;
case PPK_PaperSource:
if (!d->devMode) {
value = QPrinter::Auto;
@@ -1596,16 +1671,6 @@ QList<QPrinter::PaperSize> QWin32PrintEngine::supportedPaperSizes(const QPrinter
return returnList;
}
-static inline uint qwcsnlen(const wchar_t *str, uint maxlen)
-{
- uint length = 0;
- if (str) {
- while (length < maxlen && *str++)
- length++;
- }
- return length;
-}
-
QList<QPair<QString, QSizeF> > QWin32PrintEngine::supportedSizesWithNames(const QPrinterInfo &printerInfo)
{
QList<QPair<QString, QSizeF> > paperSizes;
diff --git a/src/printsupport/kernel/qprinter.cpp b/src/printsupport/kernel/qprinter.cpp
index c1a2483f24..387101dc9b 100644
--- a/src/printsupport/kernel/qprinter.cpp
+++ b/src/printsupport/kernel/qprinter.cpp
@@ -1023,6 +1023,37 @@ QSizeF QPrinter::paperSize(Unit unit) const
}
/*!
+ \since 5.1
+
+ Sets the paper used by the printer to \a paperName.
+
+ \sa paperName()
+*/
+
+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);
+}
+
+/*!
+ \since 5.1
+
+ Returns the paper name of the paper set on the printer.
+
+ The default value for this is driver-dependent.
+*/
+
+QString QPrinter::paperName() const
+{
+ Q_D(const QPrinter);
+ return d->printEngine->property(QPrintEngine::PPK_PaperName).toString();
+}
+
+/*!
Sets the page order to \a pageOrder.
The page order can be QPrinter::FirstPageFirst or
@@ -1959,6 +1990,8 @@ QPrinter::PrintRange QPrinter::printRange() const
\value PPK_PaperSources Specifies more than one QPrinter::PaperSource value.
+ \value PPK_PaperName A string specifying the name of the paper.
+
\value PPK_PaperSize Specifies a QPrinter::PaperSize value.
\value PPK_PrinterName A string specifying the name of the printer.
diff --git a/src/printsupport/kernel/qprinter.h b/src/printsupport/kernel/qprinter.h
index 017c2c61c0..2528157532 100644
--- a/src/printsupport/kernel/qprinter.h
+++ b/src/printsupport/kernel/qprinter.h
@@ -167,6 +167,9 @@ public:
void setPaperSize(const QSizeF &paperSize, Unit unit);
QSizeF paperSize(Unit unit) const;
+ void setPaperName(const QString &paperName);
+ QString paperName() const;
+
void setPageOrder(PageOrder);
PageOrder pageOrder() const;