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