Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qplatformprintdevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 John Layt <jlayt@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
8
9#include <QtCore/qcoreapplication.h>
10#include <QtGui/qpagelayout.h>
11
13
14#ifndef QT_NO_PRINTER
15
16QPlatformPrintDevice::QPlatformPrintDevice(const QString &id)
17 : m_id(id),
18 m_isRemote(false),
19 m_supportsMultipleCopies(false),
20 m_supportsCollateCopies(false),
21 m_havePageSizes(false),
22 m_supportsCustomPageSizes(false),
23 m_haveResolutions(false),
24 m_haveInputSlots(false),
25 m_haveOutputBins(false),
26 m_haveDuplexModes(false),
27 m_haveColorModes(false)
28#if QT_CONFIG(mimetype)
29 , m_haveMimeTypes(false)
30#endif
31{
32}
33
34QPlatformPrintDevice::~QPlatformPrintDevice()
35{
36}
37
38QString QPlatformPrintDevice::id() const
39{
40 return m_id;
41}
42
43QString QPlatformPrintDevice::name() const
44{
45 return m_name;
46}
47
48QString QPlatformPrintDevice::location() const
49{
50 return m_location;
51}
52
53QString QPlatformPrintDevice::makeAndModel() const
54{
55 return m_makeAndModel;
56}
57
58bool QPlatformPrintDevice::isValid() const
59{
60 return false;
61}
62
63bool QPlatformPrintDevice::isDefault() const
64{
65 return false;
66}
67
68bool QPlatformPrintDevice::isRemote() const
69{
70 return m_isRemote;
71}
72
73bool QPlatformPrintDevice::isValidPageLayout(const QPageLayout &layout, int resolution) const
74{
75 // Check the page size is supported
76 if (!supportedPageSize(layout.pageSize()).isValid())
77 return false;
78
79 // In fullpage mode, margins outside the printable area are valid
80 if (layout.mode() == QPageLayout::FullPageMode)
81 return true;
82
83 // Check the margins are valid
84 QMarginsF pointMargins = layout.margins(QPageLayout::Point);
85 QMarginsF printMargins = printableMargins(layout.pageSize(), layout.orientation(), resolution);
86 return pointMargins.left() >= printMargins.left()
87 && pointMargins.right() >= printMargins.right()
88 && pointMargins.top() >= printMargins.top()
89 && pointMargins.bottom() >= printMargins.bottom();
90}
91
92QPrint::DeviceState QPlatformPrintDevice::state() const
93{
94 return QPrint::Error;
95}
96
97bool QPlatformPrintDevice::supportsMultipleCopies() const
98{
99 return m_supportsMultipleCopies;
100}
101
102bool QPlatformPrintDevice::supportsCollateCopies() const
103{
104 return m_supportsCollateCopies;
105}
106
107void QPlatformPrintDevice::loadPageSizes() const
108{
109}
110
111QPageSize QPlatformPrintDevice::defaultPageSize() const
112{
113 return QPageSize();
114}
115
116QList<QPageSize> QPlatformPrintDevice::supportedPageSizes() const
117{
118 if (!m_havePageSizes)
119 loadPageSizes();
120 return m_pageSizes;
121}
122
123QPageSize QPlatformPrintDevice::supportedPageSize(const QPageSize &pageSize) const
124{
125 if (!pageSize.isValid())
126 return QPageSize();
127
128 if (!m_havePageSizes)
129 loadPageSizes();
130
131 // First try match on name and id for case where printer defines same size twice with different names
132 // e.g. Windows defines DMPAPER_11X17 and DMPAPER_TABLOID with names "11x17" and "Tabloid", but both
133 // map to QPageSize::Tabloid / PPD Key "Tabloid" / ANSI B Tabloid
134 if (pageSize.id() != QPageSize::Custom) {
135 for (const QPageSize &ps : std::as_const(m_pageSizes)) {
136 if (ps.id() == pageSize.id() && ps.name() == pageSize.name())
137 return ps;
138 }
139 }
140
141 // Next try match on id only if not custom
142 if (pageSize.id() != QPageSize::Custom) {
143 for (const QPageSize &ps : std::as_const(m_pageSizes)) {
144 if (ps.id() == pageSize.id())
145 return ps;
146 }
147 }
148
149 // Next try a match on size, in case it's a custom with a different name
150 return supportedPageSizeMatch(pageSize);
151}
152
153QPageSize QPlatformPrintDevice::supportedPageSize(QPageSize::PageSizeId pageSizeId) const
154{
155 if (!m_havePageSizes)
156 loadPageSizes();
157
158 for (const QPageSize &ps : std::as_const(m_pageSizes)) {
159 if (ps.id() == pageSizeId)
160 return ps;
161 }
162
163 // If no supported page size found, try use a custom size instead if supported
164 return supportedPageSizeMatch(QPageSize(pageSizeId));
165}
166
167QPageSize QPlatformPrintDevice::supportedPageSize(const QString &pageName) const
168{
169 if (!m_havePageSizes)
170 loadPageSizes();
171
172 for (const QPageSize &ps : std::as_const(m_pageSizes)) {
173 if (ps.name() == pageName)
174 return ps;
175 }
176
177 return QPageSize();
178}
179
180QPageSize QPlatformPrintDevice::supportedPageSize(const QSize &sizePoints) const
181{
182 if (!m_havePageSizes)
183 loadPageSizes();
184
185 // Try to find a supported page size based on fuzzy-matched point size
186 return supportedPageSizeMatch(QPageSize(sizePoints));
187}
188
189QPageSize QPlatformPrintDevice::supportedPageSize(const QSizeF &size, QPageSize::Unit units) const
190{
191 if (!m_havePageSizes)
192 loadPageSizes();
193
194 // Try to find a supported page size based on fuzzy-matched unit size
195 return supportedPageSizeMatch(QPageSize(size, units));
196}
197
198QPageSize QPlatformPrintDevice::supportedPageSizeMatch(const QPageSize &pageSize) const
199{
200 // If it's a known page size, just return itself
201 if (m_pageSizes.contains(pageSize))
202 return pageSize;
203
204 // Try to find a supported page size based on point size
205 for (const QPageSize &ps : std::as_const(m_pageSizes)) {
206 if (ps.sizePoints() == pageSize.sizePoints())
207 return ps;
208 }
209 return QPageSize();
210}
211
212bool QPlatformPrintDevice::supportsCustomPageSizes() const
213{
214 return m_supportsCustomPageSizes;
215}
216
217QSize QPlatformPrintDevice::minimumPhysicalPageSize() const
218{
219 return m_minimumPhysicalPageSize;
220}
221
222QSize QPlatformPrintDevice::maximumPhysicalPageSize() const
223{
224 return m_maximumPhysicalPageSize;
225}
226
227QMarginsF QPlatformPrintDevice::printableMargins(const QPageSize &pageSize,
228 QPageLayout::Orientation orientation,
229 int resolution) const
230{
231 Q_UNUSED(pageSize);
232 Q_UNUSED(orientation);
233 Q_UNUSED(resolution);
234 return QMarginsF(0, 0, 0, 0);
235}
236
237void QPlatformPrintDevice::loadResolutions() const
238{
239}
240
241int QPlatformPrintDevice::defaultResolution() const
242{
243 return 0;
244}
245
246QList<int> QPlatformPrintDevice::supportedResolutions() const
247{
248 if (!m_haveResolutions)
249 loadResolutions();
250 return m_resolutions;
251}
252
253void QPlatformPrintDevice::loadInputSlots() const
254{
255}
256
257QPrint::InputSlot QPlatformPrintDevice::defaultInputSlot() const
258{
259 QPrint::InputSlot input;
260 input.key = QByteArrayLiteral("Auto");
261 input.name = QCoreApplication::translate("Print Device Input Slot", "Automatic");
262 input.id = QPrint::Auto;
263 return input;
264}
265
266QList<QPrint::InputSlot> QPlatformPrintDevice::supportedInputSlots() const
267{
268 if (!m_haveInputSlots)
269 loadInputSlots();
270 return m_inputSlots;
271}
272
273void QPlatformPrintDevice::loadOutputBins() const
274{
275}
276
277QPrint::OutputBin QPlatformPrintDevice::defaultOutputBin() const
278{
279 QPrint::OutputBin output;
280 output.key = QByteArrayLiteral("Auto");
281 output.name = QCoreApplication::translate("Print Device Output Bin", "Automatic");
282 output.id = QPrint::AutoOutputBin;
283 return output;
284}
285
286QList<QPrint::OutputBin> QPlatformPrintDevice::supportedOutputBins() const
287{
288 if (!m_haveOutputBins)
289 loadOutputBins();
290 return m_outputBins;
291}
292
293void QPlatformPrintDevice::loadDuplexModes() const
294{
295}
296
297QPrint::DuplexMode QPlatformPrintDevice::defaultDuplexMode() const
298{
299 return QPrint::DuplexNone;
300}
301
302QList<QPrint::DuplexMode> QPlatformPrintDevice::supportedDuplexModes() const
303{
304 if (!m_haveDuplexModes)
305 loadDuplexModes();
306 return m_duplexModes;
307}
308
309void QPlatformPrintDevice::loadColorModes() const
310{
311}
312
313QPrint::ColorMode QPlatformPrintDevice::defaultColorMode() const
314{
315 return QPrint::GrayScale;
316}
317
318QList<QPrint::ColorMode> QPlatformPrintDevice::supportedColorModes() const
319{
320 if (!m_haveColorModes)
321 loadColorModes();
322 return m_colorModes;
323}
324
325#if QT_CONFIG(mimetype)
326void QPlatformPrintDevice::loadMimeTypes() const
327{
328}
329#endif // mimetype
330
331QVariant QPlatformPrintDevice::property(QPrintDevice::PrintDevicePropertyKey key) const
332{
333 Q_UNUSED(key);
334
335 return QVariant();
336}
337
338bool QPlatformPrintDevice::setProperty(QPrintDevice::PrintDevicePropertyKey key, const QVariant &value)
339{
340 Q_UNUSED(key);
341 Q_UNUSED(value);
342
343 return false;
344}
345
346bool QPlatformPrintDevice::isFeatureAvailable(QPrintDevice::PrintDevicePropertyKey key, const QVariant &params) const
347{
348 Q_UNUSED(key);
349 Q_UNUSED(params);
350
351 return false;
352}
353
354#if QT_CONFIG(mimetype)
355QList<QMimeType> QPlatformPrintDevice::supportedMimeTypes() const
356{
357 if (!m_haveMimeTypes)
358 loadMimeTypes();
359 return m_mimeTypes;
360}
361#endif // mimetype
362
363QPageSize QPlatformPrintDevice::createPageSize(const QString &key, const QSize &size, const QString &localizedName)
364{
365 return QPageSize(key, size, localizedName);
366}
367
368QPageSize QPlatformPrintDevice::createPageSize(int windowsId, const QSize &size, const QString &localizedName)
369{
370 return QPageSize(windowsId, size, localizedName);
371}
372
373#endif // QT_NO_PRINTER
374
375QT_END_NAMESPACE