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
qprinterinfo.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// Qt-Security score:significant reason:default
4
5#include "qprinterinfo.h"
8
9#ifndef QT_NO_PRINTER
10
11#include <QtCore/qdebug.h>
12
13#include <qpa/qplatformprintplugin.h>
14#include <qpa/qplatformprintersupport.h>
15
17
18Q_GLOBAL_STATIC(QPrinterInfoPrivate, shared_null);
19
21{
22public:
23 static inline void cleanup(QPrinterInfoPrivate *d)
24 {
25 if (d != &*shared_null)
26 delete d;
27 }
28};
29
31{
32 if (!id.isEmpty()) {
33 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
34 if (ps)
35 m_printDevice = ps->createPrintDevice(id);
36 }
37}
38
39/*!
40 \class QPrinterInfo
41
42 \brief The QPrinterInfo class gives access to information about
43 existing printers.
44
45 \ingroup printing
46 \inmodule QtPrintSupport
47
48 Use the static functions to generate a list of QPrinterInfo
49 objects. Each QPrinterInfo object in the list represents a single
50 printer and can be queried for name, supported paper sizes, and
51 whether or not it is the default printer.
52
53 \since 4.4
54*/
55
56/*!
57 Constructs an empty QPrinterInfo object.
58
59 \sa isNull()
60*/
61QPrinterInfo::QPrinterInfo()
62 : d_ptr(shared_null)
63{
64}
65
66/*!
67 Constructs a copy of \a other.
68*/
69QPrinterInfo::QPrinterInfo(const QPrinterInfo &other)
70 : d_ptr((other.d_ptr.data() == shared_null) ? &*shared_null : new QPrinterInfoPrivate(*other.d_ptr))
71{
72}
73
74/*!
75 Constructs a QPrinterInfo object from \a printer.
76*/
77QPrinterInfo::QPrinterInfo(const QPrinter &printer)
78 : d_ptr(shared_null)
79{
80 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
81 if (ps) {
82 QPrinterInfo pi(printer.printerName());
83 if (pi.d_ptr.data() == shared_null)
84 d_ptr.reset(shared_null);
85 else
86 d_ptr.reset(new QPrinterInfoPrivate(*pi.d_ptr));
87 }
88}
89
90/*!
91 \internal
92*/
93QPrinterInfo::QPrinterInfo(const QString &name)
94 : d_ptr(new QPrinterInfoPrivate(name))
95{
96}
97
98/*!
99 Destroys the QPrinterInfo object. References to the values in the
100 object become invalid.
101*/
102QPrinterInfo::~QPrinterInfo()
103{
104}
105
106/*!
107 Sets the QPrinterInfo object to be equal to \a other.
108*/
109QPrinterInfo &QPrinterInfo::operator=(const QPrinterInfo &other)
110{
111 Q_ASSERT(d_ptr);
112 if (other.d_ptr.data() == shared_null)
113 d_ptr.reset(shared_null);
114 else
115 d_ptr.reset(new QPrinterInfoPrivate(*other.d_ptr));
116 return *this;
117}
118
119/*!
120 Returns the name of the printer.
121
122 This is a unique id to identify the printer and may not be human-readable.
123
124 \sa QPrinterInfo::description()
125 \sa QPrinter::setPrinterName()
126*/
127QString QPrinterInfo::printerName() const
128{
129 const Q_D(QPrinterInfo);
130 return d->m_printDevice.id();
131}
132
133/*!
134 Returns the human-readable description of the printer.
135
136 \since 5.0
137 \sa QPrinterInfo::printerName()
138*/
139QString QPrinterInfo::description() const
140{
141 const Q_D(QPrinterInfo);
142 return d->m_printDevice.name();
143}
144
145/*!
146 Returns the human-readable location of the printer.
147
148 \since 5.0
149*/
150QString QPrinterInfo::location() const
151{
152 const Q_D(QPrinterInfo);
153 return d->m_printDevice.location();
154}
155
156/*!
157 Returns the human-readable make and model of the printer.
158
159 \since 5.0
160*/
161QString QPrinterInfo::makeAndModel() const
162{
163 const Q_D(QPrinterInfo);
164 return d->m_printDevice.makeAndModel();
165}
166
167/*!
168 Returns whether this QPrinterInfo object holds a printer definition.
169
170 An empty QPrinterInfo object could result for example from calling
171 defaultPrinter() when there are no printers on the system.
172*/
173bool QPrinterInfo::isNull() const
174{
175 Q_D(const QPrinterInfo);
176 return d == shared_null || !d->m_printDevice.isValid();
177}
178
179/*!
180 Returns whether this printer is currently the default printer.
181*/
182bool QPrinterInfo::isDefault() const
183{
184 Q_D(const QPrinterInfo);
185 return d->m_printDevice.isDefault();
186}
187
188/*!
189 Returns whether this printer is a remote network printer.
190
191 \since 5.3
192*/
193bool QPrinterInfo::isRemote() const
194{
195 Q_D(const QPrinterInfo);
196 return d->m_printDevice.isRemote();
197}
198
199/*!
200 Returns the current state of this printer.
201
202 This state may not always be accurate, depending on the platform, printer
203 driver, or printer itself.
204
205 \since 5.3
206*/
207QPrinter::PrinterState QPrinterInfo::state() const
208{
209 Q_D(const QPrinterInfo);
210 return QPrinter::PrinterState(d->m_printDevice.state());
211}
212
213/*!
214 Returns a list of Page Sizes supported by this printer.
215
216 \since 5.3
217*/
218
219QList<QPageSize> QPrinterInfo::supportedPageSizes() const
220{
221 Q_D(const QPrinterInfo);
222 return d->m_printDevice.supportedPageSizes();
223}
224
225/*!
226 Returns the current default Page Size for this printer.
227
228 \since 5.3
229*/
230
231QPageSize QPrinterInfo::defaultPageSize() const
232{
233 Q_D(const QPrinterInfo);
234 return d->m_printDevice.defaultPageSize();
235}
236
237/*!
238 Returns whether this printer supports custom page sizes.
239
240 \since 5.3
241*/
242
243bool QPrinterInfo::supportsCustomPageSizes() const
244{
245 Q_D(const QPrinterInfo);
246 return d->m_printDevice.supportsCustomPageSizes();
247}
248
249/*!
250 Returns the minimum physical page size supported by this printer.
251
252 \sa maximumPhysicalPageSize()
253
254 \since 5.3
255*/
256
257QPageSize QPrinterInfo::minimumPhysicalPageSize() const
258{
259 Q_D(const QPrinterInfo);
260 return QPageSize(d->m_printDevice.minimumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
261}
262
263/*!
264 Returns the maximum physical page size supported by this printer.
265
266 \sa minimumPhysicalPageSize()
267
268 \since 5.3
269*/
270
271QPageSize QPrinterInfo::maximumPhysicalPageSize() const
272{
273 Q_D(const QPrinterInfo);
274 return QPageSize(d->m_printDevice.maximumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
275}
276
277/*!
278 Returns a list of resolutions supported by this printer.
279
280 \since 5.3
281*/
282
283QList<int> QPrinterInfo::supportedResolutions() const
284{
285 Q_D(const QPrinterInfo);
286 return d->m_printDevice.supportedResolutions();
287}
288
289/*!
290 Returns the default duplex mode of this printer.
291
292 \since 5.4
293*/
294
295QPrinter::DuplexMode QPrinterInfo::defaultDuplexMode() const
296{
297 Q_D(const QPrinterInfo);
298 return QPrinter::DuplexMode(d->m_printDevice.defaultDuplexMode());
299}
300
301/*!
302 Returns a list of duplex modes supported by this printer.
303
304 \since 5.4
305*/
306
307QList<QPrinter::DuplexMode> QPrinterInfo::supportedDuplexModes() const
308{
309 Q_D(const QPrinterInfo);
310 QList<QPrinter::DuplexMode> list;
311 const auto supportedDuplexModes = d->m_printDevice.supportedDuplexModes();
312 list.reserve(supportedDuplexModes.size());
313 for (QPrint::DuplexMode mode : supportedDuplexModes)
314 list << QPrinter::DuplexMode(mode);
315 return list;
316}
317
318/*!
319 Returns the default color mode of this printer.
320
321 \since 5.13
322*/
323
324QPrinter::ColorMode QPrinterInfo::defaultColorMode() const
325{
326 Q_D(const QPrinterInfo);
327 return QPrinter::ColorMode(d->m_printDevice.defaultColorMode());
328}
329
330/*!
331 Returns the supported color modes of this printer.
332
333 \since 5.13
334*/
335
336QList<QPrinter::ColorMode> QPrinterInfo::supportedColorModes() const
337{
338 Q_D(const QPrinterInfo);
339 QList<QPrinter::ColorMode> list;
340 const auto supportedColorModes = d->m_printDevice.supportedColorModes();
341 list.reserve(supportedColorModes.size());
342 for (QPrint::ColorMode mode : supportedColorModes)
343 list << QPrinter::ColorMode(mode);
344 return list;
345}
346
347/*!
348 Returns a list of all the available Printer Names on this system.
349
350 It is recommended to use this instead of availablePrinters() as
351 it will be faster on most systems.
352
353 Note that the list may become outdated if changes are made on the local
354 system or remote print server. Only instantiate required QPrinterInfo
355 instances when needed, and always check for validity before calling.
356
357 \since 5.3
358*/
359QStringList QPrinterInfo::availablePrinterNames()
360{
361 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
362 if (ps)
363 return ps->availablePrintDeviceIds();
364 return QStringList();
365}
366
367/*!
368 Returns a list of QPrinterInfo objects for all the available printers
369 on this system.
370
371 It is NOT recommended to use this as creating each printer instance may
372 take a long time, especially if there are remote networked printers, and
373 retained instances may become outdated if changes are made on the local
374 system or remote print server. Use availablePrinterNames() instead and
375 only instantiate printer instances as you need them.
376*/
377QList<QPrinterInfo> QPrinterInfo::availablePrinters()
378{
379 QList<QPrinterInfo> list;
380 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
381 if (ps) {
382 const QStringList availablePrintDeviceIds = ps->availablePrintDeviceIds();
383 list.reserve(availablePrintDeviceIds.size());
384 for (const QString &id : availablePrintDeviceIds)
385 list.append(QPrinterInfo(id));
386 }
387 return list;
388}
389
390/*!
391 Returns the current default printer name.
392
393 \since 5.3
394*/
395QString QPrinterInfo::defaultPrinterName()
396{
397 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
398 if (ps)
399 return ps->defaultPrintDeviceId();
400 return QString();
401}
402
403/*!
404 Returns the default printer on the system.
405
406 The return value should be checked using isNull() before being
407 used, in case there is no default printer.
408
409 On some systems it is possible for there to be available printers
410 but none of them set to be the default printer.
411
412 \sa isNull()
413 \sa isDefault()
414 \sa availablePrinters()
415*/
416
417QPrinterInfo QPrinterInfo::defaultPrinter()
418{
419 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
420 if (ps)
421 return QPrinterInfo(ps->defaultPrintDeviceId());
422 return QPrinterInfo();
423}
424
425/*!
426 Returns the printer \a printerName.
427
428 The return value should be checked using isNull() before being
429 used, in case the named printer does not exist.
430
431 \since 5.0
432 \sa isNull()
433*/
434QPrinterInfo QPrinterInfo::printerInfo(const QString &printerName)
435{
436 return QPrinterInfo(printerName);
437}
438
439# ifndef QT_NO_DEBUG_STREAM
440QDebug operator<<(QDebug debug, const QPrinterInfo &p)
441{
442 QDebugStateSaver saver(debug);
443 debug.nospace();
444 debug << "QPrinterInfo(";
445 if (p.isNull())
446 debug << "null";
447 else
448 p.d_ptr->m_printDevice.format(debug);
449 debug << ')';
450 return debug;
451}
452# endif // !QT_NO_DEBUG_STREAM
453
454QT_END_NAMESPACE
455
456#endif // QT_NO_PRINTER
static void cleanup(QPrinterInfoPrivate *d)
QPrinterInfoPrivate(const QString &id=QString())