Qt 6.x
The Qt SDK
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
27{
29
30 setPdfVersion(version);
31}
32
34 : QPdfEngine(p)
35{
37}
38
40{
41}
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
69{
70 return QPdfEngine::newPage();
71}
72
74{
75 return QPdfEngine::metric(m);
76}
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 d->grayscale = (QPrinter::ColorMode(value.toInt()) == QPrinter::GrayScale);
108 break;
109 case PPK_Creator:
110 d->creator = value.toString();
111 break;
112 case PPK_DocumentName:
113 d->title = value.toString();
114 break;
115 case PPK_FullPage:
116 if (value.toBool())
117 d->m_pageLayout.setMode(QPageLayout::FullPageMode);
118 else
119 d->m_pageLayout.setMode(QPageLayout::StandardMode);
120 break;
121 case PPK_CopyCount:
123 d->copies = value.toInt();
124 break;
125 case PPK_Orientation:
126 d->m_pageLayout.setOrientation(QPageLayout::Orientation(value.toInt()));
127 break;
129 d->outputFileName = value.toString();
130 break;
131 case PPK_PageOrder:
132 d->pageOrder = QPrinter::PageOrder(value.toInt());
133 break;
134 case PPK_PageSize: {
135 QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
136 if (pageSize.isValid())
137 d->m_pageLayout.setPageSize(pageSize);
138 break;
139 }
140 case PPK_PaperName: {
141 QString name = value.toString();
142 for (int i = 0; i <= QPageSize::LastPageSize; ++i) {
144 if (name == pageSize.name()) {
145 d->m_pageLayout.setPageSize(pageSize);
146 break;
147 }
148 }
149 break;
150 }
152 d->m_pageLayout.setPageSize(QPageSize(QPageSize::id(value.toInt())));
153 break;
154 case PPK_PaperSource:
155 d->paperSource = QPrinter::PaperSource(value.toInt());
156 break;
157 case PPK_PrinterName:
158 d->printerName = value.toString();
159 break;
161 d->printProgram = value.toString();
162 break;
163 case PPK_Resolution:
164 d->resolution = value.toInt();
165 break;
167 d->selectionOption = value.toString();
168 break;
170 d->embedFonts = value.toBool();
171 break;
173 d->m_pageLayout.setPageSize(QPageSize(value.toSizeF(), QPageSize::Point));
174 break;
175 case PPK_PageMargins: {
176 QList<QVariant> margins(value.toList());
177 Q_ASSERT(margins.size() == 4);
178 d->m_pageLayout.setUnits(QPageLayout::Point);
179 d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
180 margins.at(2).toReal(), margins.at(3).toReal()));
181 break;
182 }
183 case PPK_QPageSize: {
184 QPageSize pageSize = qvariant_cast<QPageSize>(value);
185 if (pageSize.isValid())
186 d->m_pageLayout.setPageSize(pageSize);
187 break;
188 }
189 case PPK_QPageMargins: {
190 QPair<QMarginsF, QPageLayout::Unit> pair = qvariant_cast<QPair<QMarginsF, QPageLayout::Unit> >(value);
191 d->m_pageLayout.setUnits(pair.second);
192 d->m_pageLayout.setMargins(pair.first);
193 break;
194 }
195 case PPK_QPageLayout: {
196 QPageLayout pageLayout = qvariant_cast<QPageLayout>(value);
197 if (pageLayout.isValid())
198 d->m_pageLayout = pageLayout;
199 break;
200 }
201 // No default so that compiler will complain if new keys added and not handled in this engine
202 }
203}
204
206{
207 Q_D(const QPdfPrintEngine);
208
210 switch (int(key)) {
211
212 // The following keys are settings that are unsupported by the PDF PrintEngine
213 // Return sensible default values to ensure consistent behavior across platforms
214 case PPK_CustomBase:
215 case PPK_Duplex:
216 // Special case, leave null
217 break;
218
219 // The following keys are properties and settings that are supported by the PDF PrintEngine
221 ret = d->collate;
222 break;
223 case PPK_ColorMode:
224 ret = d->grayscale ? QPrinter::GrayScale : QPrinter::Color;
225 break;
226 case PPK_Creator:
227 ret = d->creator;
228 break;
229 case PPK_DocumentName:
230 ret = d->title;
231 break;
232 case PPK_FullPage:
233 ret = d->m_pageLayout.mode() == QPageLayout::FullPageMode;
234 break;
235 case PPK_CopyCount:
236 ret = d->copies;
237 break;
239 ret = false;
240 break;
242 ret = d->copies;
243 break;
244 case PPK_Orientation:
245 ret = d->m_pageLayout.orientation();
246 break;
248 ret = d->outputFileName;
249 break;
250 case PPK_PageOrder:
251 ret = d->pageOrder;
252 break;
253 case PPK_PageSize:
254 ret = d->m_pageLayout.pageSize().id();
255 break;
256 case PPK_PaperName:
257 ret = d->m_pageLayout.pageSize().name();
258 break;
260 ret = d->m_pageLayout.pageSize().windowsId();
261 break;
262 case PPK_PaperSource:
263 ret = d->paperSource;
264 break;
265 case PPK_PrinterName:
266 ret = d->printerName;
267 break;
269 ret = d->printProgram;
270 break;
271 case PPK_Resolution:
272 ret = d->resolution;
273 break;
275 ret = QList<QVariant>() << 72;
276 break;
277 case PPK_PaperRect:
278 ret = d->m_pageLayout.fullRectPixels(d->resolution);
279 break;
280 case PPK_PageRect:
281 ret = d->m_pageLayout.paintRectPixels(d->resolution);
282 break;
284 ret = d->selectionOption;
285 break;
287 ret = d->embedFonts;
288 break;
290 ret = d->m_pageLayout.fullRectPoints().size();
291 break;
292 case PPK_PageMargins: {
294 QMarginsF margins = d->m_pageLayout.margins(QPageLayout::Point);
295 list << margins.left() << margins.top() << margins.right() << margins.bottom();
296 ret = list;
297 break;
298 }
299 case PPK_QPageSize:
300 ret.setValue(d->m_pageLayout.pageSize());
301 break;
302 case PPK_QPageMargins: {
303 QPair<QMarginsF, QPageLayout::Unit> pair = qMakePair(d->m_pageLayout.margins(), d->m_pageLayout.units());
304 ret.setValue(pair);
305 break;
306 }
307 case PPK_QPageLayout:
308 ret.setValue(d->m_pageLayout);
309 break;
310 // No default so that compiler will complain if new keys added and not handled in this engine
311 }
312 return ret;
313}
314
315
317{
318 if (outDevice)
319 return false;
320
321 if (!outputFileName.isEmpty()) {
324 delete file;
325 return false;
326 }
327 outDevice = file;
328 }
329
330 return true;
331}
332
334{
335 if (outDevice) {
336 outDevice->close();
337 if (fd >= 0)
338 #if defined(Q_OS_WIN) && defined(Q_CC_MSVC)
339 ::_close(fd);
340 #else
341 ::close(fd);
342 #endif
343 fd = -1;
344 delete outDevice;
345 outDevice = nullptr;
346 }
347}
348
349
350
353 collate(true),
354 copies(1),
355 pageOrder(QPrinter::FirstPageFirst),
356 paperSource(QPrinter::Auto),
357 fd(-1)
358{
359 resolution = 72;
361 resolution = 1200;
362 else if (m == QPrinter::ScreenResolution)
364}
365
367{
368}
369
371
372#endif // QT_NO_PRINTER
\inmodule QtCore
Definition qfile.h:93
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:881
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
\inmodule QtCore
Definition qmargins.h:274
constexpr qreal right() const noexcept
Returns the right margin.
Definition qmargins.h:370
constexpr qreal left() const noexcept
Returns the left margin.
Definition qmargins.h:364
constexpr qreal top() const noexcept
Returns the top margin.
Definition qmargins.h:367
constexpr qreal bottom() const noexcept
Returns the bottom margin.
Definition qmargins.h:373
\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
QString outputFileName
Definition qpdf_p.h:253
QIODevice * outDevice
Definition qpdf_p.h:249
QPageLayout pageLayout() const
Definition qpdf.cpp:1403
int metric(QPaintDevice::PaintDeviceMetric metricType) const
Definition qpdf.cpp:1410
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:1473
bool newPage()
Definition qpdf.cpp:1329
bool end() override
Reimplement this function to finish painting on the current paint device.
Definition qpdf.cpp:1529
void setPdfVersion(PdfVersion version)
Definition qpdf.cpp:1360
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:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\inmodule QtCore
Definition qvariant.h:64
Combined button and popup list for selecting options.
std::pair< T1, T2 > QPair
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
Q_GUI_EXPORT int qt_defaultDpi()
Definition qfont.cpp:137
return ret
const GLfloat * m
GLuint64 key
GLuint64 GLenum GLint fd
GLuint name
GLfloat GLfloat p
[1]
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]