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