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