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

source code of qtbase/src/printsupport/kernel/qprinterinfo.cpp