summaryrefslogtreecommitdiffstats
path: root/src/printsupport/kernel/qprinterinfo.cpp
blob: b1321bf57a86b719faf120fdbcf203c03b9aeeb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qprinterinfo.h"
#include "qprinterinfo_p.h"
#include "qprintdevice_p.h"

#ifndef QT_NO_PRINTER

#include <qpa/qplatformprintplugin.h>
#include <qpa/qplatformprintersupport.h>

QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QPrinterInfoPrivate, shared_null);

class QPrinterInfoPrivateDeleter
{
public:
    static inline void cleanup(QPrinterInfoPrivate *d)
    {
        if (d != shared_null)
            delete d;
    }
};

QPrinterInfoPrivate::QPrinterInfoPrivate(const QString &id)
{
    if (!id.isEmpty()) {
        QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
        if (ps)
            m_printDevice = ps->createPrintDevice(id);
    }
}

QPrinterInfoPrivate::~QPrinterInfoPrivate()
{
}

/*!
    \class QPrinterInfo

    \brief The QPrinterInfo class gives access to information about
    existing printers.

    \ingroup printing
    \inmodule QtPrintSupport

    Use the static functions to generate a list of QPrinterInfo
    objects. Each QPrinterInfo object in the list represents a single
    printer and can be queried for name, supported paper sizes, and
    whether or not it is the default printer.

    \since 4.4
*/

/*!
    Constructs an empty QPrinterInfo object.

    \sa isNull()
*/
QPrinterInfo::QPrinterInfo()
    : d_ptr(shared_null)
{
}

/*!
    Constructs a copy of \a other.
*/
QPrinterInfo::QPrinterInfo(const QPrinterInfo &other)
    : d_ptr((other.d_ptr.data() == shared_null) ? shared_null : new QPrinterInfoPrivate(*other.d_ptr))
{
}

/*!
    Constructs a QPrinterInfo object from \a printer.
*/
QPrinterInfo::QPrinterInfo(const QPrinter &printer)
    : d_ptr(shared_null)
{
    QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
    if (ps) {
        QPrinterInfo pi(printer.printerName());
        if (pi.d_ptr.data() == shared_null)
            d_ptr.reset(shared_null);
        else
            d_ptr.reset(new QPrinterInfoPrivate(*pi.d_ptr));
    }
}

/*!
    \internal
*/
QPrinterInfo::QPrinterInfo(const QString &name)
    : d_ptr(new QPrinterInfoPrivate(name))
{
}

/*!
    Destroys the QPrinterInfo object. References to the values in the
    object become invalid.
*/
QPrinterInfo::~QPrinterInfo()
{
}

/*!
    Sets the QPrinterInfo object to be equal to \a other.
*/
QPrinterInfo &QPrinterInfo::operator=(const QPrinterInfo &other)
{
    Q_ASSERT(d_ptr);
    if (other.d_ptr.data() == shared_null)
        d_ptr.reset(shared_null);
    else
        d_ptr.reset(new QPrinterInfoPrivate(*other.d_ptr));
    return *this;
}

/*!
    Returns the name of the printer.

    This is a unique id to identify the printer and may not be human-readable.

    \sa QPrinterInfo::description()
    \sa QPrinter::setPrinterName()
*/
QString QPrinterInfo::printerName() const
{
    const Q_D(QPrinterInfo);
    return d->m_printDevice.id();
}

/*!
    Returns the human-readable description of the printer.

    \since 5.0
    \sa QPrinterInfo::printerName()
*/
QString QPrinterInfo::description() const
{
    const Q_D(QPrinterInfo);
    return d->m_printDevice.name();
}

/*!
    Returns the human-readable location of the printer.

    \since 5.0
*/
QString QPrinterInfo::location() const
{
    const Q_D(QPrinterInfo);
    return d->m_printDevice.location();
}

/*!
    Returns the human-readable make and model of the printer.

    \since 5.0
*/
QString QPrinterInfo::makeAndModel() const
{
    const Q_D(QPrinterInfo);
    return d->m_printDevice.makeAndModel();
}

/*!
    Returns whether this QPrinterInfo object holds a printer definition.

    An empty QPrinterInfo object could result for example from calling
    defaultPrinter() when there are no printers on the system.
*/
bool QPrinterInfo::isNull() const
{
    Q_D(const QPrinterInfo);
    return d == shared_null || !d->m_printDevice.isValid();
}

/*!
    Returns whether this printer is currently the default printer.
*/
bool QPrinterInfo::isDefault() const
{
    Q_D(const QPrinterInfo);
    return d->m_printDevice.isDefault();
}

/*!
    Returns whether this printer is a remote network printer.

    \since 5.3
*/
bool QPrinterInfo::isRemote() const
{
    Q_D(const QPrinterInfo);
    return d->m_printDevice.isRemote();
}

/*!
    Returns the current state of this printer.

    This state may not always be accurate, depending on the platform, printer
    driver, or printer itself.

    \since 5.3
*/
QPrinter::PrinterState QPrinterInfo::state() const
{
    Q_D(const QPrinterInfo);
    return QPrinter::PrinterState(d->m_printDevice.state());
}

/*!
    Returns a list of Page Sizes supported by this printer.

    \since 5.3
*/

QList<QPageSize> QPrinterInfo::supportedPageSizes() const
{
    Q_D(const QPrinterInfo);
    return d->m_printDevice.supportedPageSizes();
}

/*!
    Returns the current default Page Size for this printer.

    \since 5.3
*/

QPageSize QPrinterInfo::defaultPageSize() const
{
    Q_D(const QPrinterInfo);
    return d->m_printDevice.defaultPageSize();
}

/*!
    Returns whether this printer supports custom page sizes.

    \since 5.3
*/

bool QPrinterInfo::supportsCustomPageSizes() const
{
    Q_D(const QPrinterInfo);
    return d->m_printDevice.supportsCustomPageSizes();
}

/*!
    Returns the minimum physical page size supported by this printer.

    \sa maximumPhysicalPageSize()

    \since 5.3
*/

QPageSize QPrinterInfo::minimumPhysicalPageSize() const
{
    Q_D(const QPrinterInfo);
    return QPageSize(d->m_printDevice.minimumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
}

/*!
    Returns the maximum physical page size supported by this printer.

    \sa minimumPhysicalPageSize()

    \since 5.3
*/

QPageSize QPrinterInfo::maximumPhysicalPageSize() const
{
    Q_D(const QPrinterInfo);
    return QPageSize(d->m_printDevice.maximumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
}

#if QT_DEPRECATED_SINCE(5,3)
/*!
    \obsolete Use supportedPageSizes() instead.

    Returns a list of supported paper sizes by the printer.

    Not all printer drivers support this query, so the list may be empty.

    \since 4.4
*/

QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
{
    Q_D(const QPrinterInfo);
    QList<QPrinter::PaperSize> list;
    foreach (const QPageSize &pageSize, d->m_printDevice.supportedPageSizes())
        list.append(QPrinter::PaperSize(pageSize.id()));
    return list;
}

/*!
    \obsolete Use supportedPageSizes() instead.

    Returns a list of all the paper names supported by the driver with the
    corresponding size in millimeters.

    Not all printer drivers support this query, so the list may be empty.

    \since 5.1
*/

QList<QPair<QString, QSizeF> > QPrinterInfo::supportedSizesWithNames() const
{
    Q_D(const QPrinterInfo);
    QList<QPair<QString, QSizeF> > list;
    foreach (const QPageSize &pageSize, d->m_printDevice.supportedPageSizes())
        list.append(qMakePair(pageSize.name(), pageSize.size(QPageSize::Millimeter)));
    return list;
}
#endif // QT_DEPRECATED_SINCE(5,3)

/*!
    Returns a list of resolutions supported by this printer.

    \since 5.3
*/

QList<int> QPrinterInfo::supportedResolutions() const
{
    Q_D(const QPrinterInfo);
    return d->m_printDevice.supportedResolutions();
}

/*!
    Returns a list of all the available Printer Names on this system.

    It is recommended to use this instead of availablePrinters() as
    it will be faster on most systems.

    Note that the list may become outdated if changes are made on the local
    system or remote print server. Only instantiate required QPrinterInfo
    instances when needed, and always check for validity before calling.

    \since 5.3
*/
QStringList QPrinterInfo::availablePrinterNames()
{
    QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
    if (ps)
        return ps->availablePrintDeviceIds();
    return QStringList();
}

/*!
    Returns a list of QPrinterInfo objects for all the available printers
    on this system.

    It is NOT recommended to use this as creating each printer instance may
    take a long time, especially if there are remote networked printers, and
    retained instances may become outdated if changes are made on the local
    system or remote print server. Use availablePrinterNames() instead and
    only instantiate printer instances as you need them.
*/
QList<QPrinterInfo> QPrinterInfo::availablePrinters()
{
    QList<QPrinterInfo> list;
    QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
    if (ps) {
        foreach (const QString &id, ps->availablePrintDeviceIds())
            list.append(QPrinterInfo(id));
    }
    return list;
}

/*!
    Returns the current default printer name.

    \since 5.3
*/
QString QPrinterInfo::defaultPrinterName()
{
    QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
    if (ps)
        return ps->defaultPrintDeviceId();
    return QString();
}

/*!
    Returns the default printer on the system.

    The return value should be checked using isNull() before being
    used, in case there is no default printer.

    On some systems it is possible for there to be available printers
    but none of them set to be the default printer.

    \sa isNull()
    \sa isDefault()
    \sa availablePrinters()
*/

QPrinterInfo QPrinterInfo::defaultPrinter()
{
    QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
    if (ps)
        return QPrinterInfo(ps->defaultPrintDeviceId());
    return QPrinterInfo();
}

/*!
    Returns the printer \a printerName.

    The return value should be checked using isNull() before being
    used, in case the named printer does not exist.

    \since 5.0
    \sa isNull()
*/
QPrinterInfo QPrinterInfo::printerInfo(const QString &printerName)
{
    return QPrinterInfo(printerName);
}

QT_END_NAMESPACE

#endif // QT_NO_PRINTER