Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qprintengine_pdf.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#ifndef QT_NO_PRINTER
7
8#include <qiodevice.h>
9#include <qfile.h>
10#include <qdebug.h>
11#include <qbuffer.h>
12#include "qprinterinfo.h"
13#include <QtGui/qpagelayout.h>
14
15#ifdef Q_OS_UNIX
16#include "private/qcore_unix_p.h" // overrides QT_OPEN
17#endif
18
19#ifdef Q_OS_WIN
20#include <io.h> // _close.
21#endif
22
24
32
38
42
44{
45 Q_D(QPdfPrintEngine);
46
47 if (!d->openPrintDevice()) {
49 return false;
50 }
52
53 return QPdfEngine::begin(pdev);
54}
55
57{
58 Q_D(QPdfPrintEngine);
59
61
62 d->closePrintDevice();
64
65 return true;
66}
67
72
77
79{
80 Q_D(QPdfPrintEngine);
81
82 switch (int(key)) {
83
84 // The following keys are properties or derived values and so cannot be set
85 case PPK_PageRect:
86 break;
87 case PPK_PaperRect:
88 break;
90 break;
92 break;
94 break;
95
96 // The following keys are settings that are unsupported by the PDF PrintEngine
97 case PPK_CustomBase:
98 break;
99 case PPK_Duplex:
100 break;
101
102 // The following keys are properties and settings that are supported by the PDF PrintEngine
104 d->collate = value.toBool();
105 break;
106 case PPK_ColorMode:
107 switch (QPrinter::ColorMode(value.toInt())) {
110 break;
111 case QPrinter::Color:
112 d->colorModel = QPdfEngine::ColorModel::Auto;
113 break;
114 }
115 break;
116 case PPK_Creator:
117 d->creator = value.toString();
118 break;
119 case PPK_DocumentName:
120 d->title = value.toString();
121 break;
122 case PPK_FullPage:
123 if (value.toBool())
124 d->m_pageLayout.setMode(QPageLayout::FullPageMode);
125 else
126 d->m_pageLayout.setMode(QPageLayout::StandardMode);
127 break;
128 case PPK_CopyCount:
130 d->copies = value.toInt();
131 break;
132 case PPK_Orientation:
133 d->m_pageLayout.setOrientation(QPageLayout::Orientation(value.toInt()));
134 break;
136 d->outputFileName = value.toString();
137 break;
138 case PPK_PageOrder:
139 d->pageOrder = QPrinter::PageOrder(value.toInt());
140 break;
141 case PPK_PageSize: {
142 QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
143 if (pageSize.isValid())
144 d->m_pageLayout.setPageSize(pageSize);
145 break;
146 }
147 case PPK_PaperName: {
148 QString name = value.toString();
149 for (int i = 0; i <= QPageSize::LastPageSize; ++i) {
151 if (name == pageSize.name()) {
152 d->m_pageLayout.setPageSize(pageSize);
153 break;
154 }
155 }
156 break;
157 }
159 d->m_pageLayout.setPageSize(QPageSize(QPageSize::id(value.toInt())));
160 break;
161 case PPK_PaperSource:
162 d->paperSource = QPrinter::PaperSource(value.toInt());
163 break;
164 case PPK_PrinterName:
165 d->printerName = value.toString();
166 break;
168 d->printProgram = value.toString();
169 break;
170 case PPK_Resolution:
171 d->resolution = value.toInt();
172 break;
174 d->selectionOption = value.toString();
175 break;
177 d->embedFonts = value.toBool();
178 break;
180 d->m_pageLayout.setPageSize(QPageSize(value.toSizeF(), QPageSize::Point));
181 break;
182 case PPK_PageMargins: {
183 QList<QVariant> margins(value.toList());
184 Q_ASSERT(margins.size() == 4);
185 d->m_pageLayout.setUnits(QPageLayout::Point);
186 d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
187 margins.at(2).toReal(), margins.at(3).toReal()),
189 break;
190 }
191 case PPK_QPageSize: {
192 QPageSize pageSize = qvariant_cast<QPageSize>(value);
193 if (pageSize.isValid())
194 d->m_pageLayout.setPageSize(pageSize);
195 break;
196 }
197 case PPK_QPageMargins: {
198 QPair<QMarginsF, QPageLayout::Unit> pair = qvariant_cast<QPair<QMarginsF, QPageLayout::Unit> >(value);
199 d->m_pageLayout.setUnits(pair.second);
200 d->m_pageLayout.setMargins(pair.first, QPageLayout::OutOfBoundsPolicy::Clamp);
201 break;
202 }
203 case PPK_QPageLayout: {
204 QPageLayout pageLayout = qvariant_cast<QPageLayout>(value);
205 if (pageLayout.isValid())
206 d->m_pageLayout = pageLayout;
207 break;
208 }
209 // No default so that compiler will complain if new keys added and not handled in this engine
210 }
211}
212
214{
215 Q_D(const QPdfPrintEngine);
216
218 switch (int(key)) {
219
220 // The following keys are settings that are unsupported by the PDF PrintEngine
221 // Return sensible default values to ensure consistent behavior across platforms
222 case PPK_CustomBase:
223 case PPK_Duplex:
224 // Special case, leave null
225 break;
226
227 // The following keys are properties and settings that are supported by the PDF PrintEngine
229 ret = d->collate;
230 break;
231 case PPK_ColorMode:
232 ret = d->printerColorMode();
233 break;
234 case PPK_Creator:
235 ret = d->creator;
236 break;
237 case PPK_DocumentName:
238 ret = d->title;
239 break;
240 case PPK_FullPage:
241 ret = d->m_pageLayout.mode() == QPageLayout::FullPageMode;
242 break;
243 case PPK_CopyCount:
244 ret = d->copies;
245 break;
247 ret = false;
248 break;
250 ret = d->copies;
251 break;
252 case PPK_Orientation:
253 ret = d->m_pageLayout.orientation();
254 break;
256 ret = d->outputFileName;
257 break;
258 case PPK_PageOrder:
259 ret = d->pageOrder;
260 break;
261 case PPK_PageSize:
262 ret = d->m_pageLayout.pageSize().id();
263 break;
264 case PPK_PaperName:
265 ret = d->m_pageLayout.pageSize().name();
266 break;
268 ret = d->m_pageLayout.pageSize().windowsId();
269 break;
270 case PPK_PaperSource:
271 ret = d->paperSource;
272 break;
273 case PPK_PrinterName:
274 ret = d->printerName;
275 break;
277 ret = d->printProgram;
278 break;
279 case PPK_Resolution:
280 ret = d->resolution;
281 break;
283 ret = QList<QVariant>() << 72;
284 break;
285 case PPK_PaperRect:
286 ret = d->m_pageLayout.fullRectPixels(d->resolution);
287 break;
288 case PPK_PageRect:
289 ret = d->m_pageLayout.paintRectPixels(d->resolution);
290 break;
292 ret = d->selectionOption;
293 break;
295 ret = d->embedFonts;
296 break;
298 ret = d->m_pageLayout.fullRectPoints().size();
299 break;
300 case PPK_PageMargins: {
301 QList<QVariant> list;
302 QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point);
303 list << margins.left() << margins.top() << margins.right() << margins.bottom();
304 ret = list;
305 break;
306 }
307 case PPK_QPageSize:
308 ret.setValue(d->m_pageLayout.pageSize());
309 break;
310 case PPK_QPageMargins: {
311 QPair<QMarginsF, QPageLayout::Unit> pair = qMakePair(d->m_pageLayout.margins(), d->m_pageLayout.units());
312 ret.setValue(pair);
313 break;
314 }
315 case PPK_QPageLayout:
316 ret.setValue(d->m_pageLayout);
317 break;
318 // No default so that compiler will complain if new keys added and not handled in this engine
319 }
320 return ret;
321}
322
323
325{
326 if (outDevice)
327 return false;
328
329 if (!outputFileName.isEmpty()) {
332 delete file;
333 return false;
334 }
335 outDevice = file;
336 }
337
338 return true;
339}
340
342{
343 if (outDevice) {
344 outDevice->close();
345 if (fd >= 0)
346 #if defined(Q_OS_WIN) && defined(Q_CC_MSVC)
347 ::_close(fd);
348 #else
349 ::close(fd);
350 #endif
351 fd = -1;
352 delete outDevice;
353 outDevice = nullptr;
354 }
355}
356
357
358
361 collate(true),
362 copies(1),
363 pageOrder(QPrinter::FirstPageFirst),
364 paperSource(QPrinter::Auto),
365 fd(-1)
366{
367 resolution = 72;
369 resolution = 1200;
370 else if (m == QPrinter::ScreenResolution)
372}
373
377
392
393
395
396#endif // QT_NO_PRINTER
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
\inmodule QtCore
Definition qmargins.h:270
constexpr qreal right() const noexcept
Returns the right margin.
Definition qmargins.h:383
constexpr qreal left() const noexcept
Returns the left margin.
Definition qmargins.h:377
constexpr qreal top() const noexcept
Returns the top margin.
Definition qmargins.h:380
constexpr qreal bottom() const noexcept
Returns the bottom margin.
Definition qmargins.h:386
\inmodule QtGui
Definition qpagelayout.h:20
bool isValid() const
Returns true if this page layout is valid.
Orientation
This enum type defines the page orientation.
Definition qpagelayout.h:33
\inmodule QtGui
Definition qpagesize.h:22
bool isValid() const
Returns true if this page size is valid.
QString name() const
Returns a localized human-readable name for the page size.
PageSizeId id() const
Returns the standard QPageSize::PageSizeId of the page, or QPageSize::Custom.
PageSizeId
This enum type lists the available page sizes as defined in the Postscript PPD standard.
Definition qpagesize.h:25
QPdfEngine::ColorModel colorModel
Definition qpdf_p.h:251
QString outputFileName
Definition qpdf_p.h:262
QIODevice * outDevice
Definition qpdf_p.h:258
QPageLayout pageLayout() const
Definition qpdf.cpp:1380
int metric(QPaintDevice::PaintDeviceMetric metricType) const
Definition qpdf.cpp:1387
bool begin(QPaintDevice *pdev) override
Reimplement this function to initialise your paint engine when painting is to start on the paint devi...
Definition qpdf.cpp:1450
bool newPage()
Definition qpdf.cpp:1306
bool end() override
Reimplement this function to finish painting on the current paint device.
Definition qpdf.cpp:1508
void setPdfVersion(PdfVersion version)
Definition qpdf.cpp:1337
QPrinter::ColorMode printerColorMode() const
QPdfPrintEnginePrivate(QPrinter::PrinterMode m)
bool end() override
Reimplement this function to finish painting on the current paint device.
int metric(QPaintDevice::PaintDeviceMetric) const override
Returns the metric for the given id.
QPrinter::PrinterState state
QPdfPrintEngine(QPrinter::PrinterMode m, QPdfEngine::PdfVersion version=QPdfEngine::Version_1_4)
bool begin(QPaintDevice *pdev) override
Reimplement this function to initialise your paint engine when painting is to start on the paint devi...
bool newPage() override
Instructs the print engine to start a new page.
virtual void setProperty(PrintEnginePropertyKey key, const QVariant &value) override
Sets the print engine's property specified by key to the given value.
virtual QVariant property(PrintEnginePropertyKey key) const override
Returns the print engine's property specified by key.
PrintEnginePropertyKey
This enum is used to communicate properties between the print engine and QPrinter.
@ PPK_SupportedResolutions
@ PPK_SupportsMultipleCopies
\reentrant
Definition qprinter.h:28
PaperSource
This enum type specifies what paper source QPrinter is to use.
Definition qprinter.h:45
ColorMode
This enum type is used to indicate whether QPrinter should print in color or not.
Definition qprinter.h:42
@ GrayScale
Definition qprinter.h:42
PrinterMode
This enum describes the mode the printer should work in.
Definition qprinter.h:31
@ ScreenResolution
Definition qprinter.h:31
@ HighResolution
Definition qprinter.h:31
PageOrder
This enum type is used by QPrinter to tell the application program how to print.
Definition qprinter.h:39
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
Q_GUI_EXPORT int qt_defaultDpi()
Definition qfont.cpp:140
return ret
const GLfloat * m
GLuint64 key
GLuint64 GLenum GLint fd
GLuint name
GLfloat GLfloat p
[1]
QT_BEGIN_NAMESPACE constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition qpair.h:19
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QList< int > list
[14]
QFile file
[0]