| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 John Layt <jlayt@kde.org> |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtPrintSupport module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qplatformprintdevice.h" |
| 41 | |
| 42 | #include "qprintdevice_p.h" |
| 43 | |
| 44 | #include <QtCore/qcoreapplication.h> |
| 45 | #include <QtGui/qpagelayout.h> |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | #ifndef QT_NO_PRINTER |
| 50 | |
| 51 | QPlatformPrintDevice::QPlatformPrintDevice(const QString &id) |
| 52 | : m_id(id), |
| 53 | m_isRemote(false), |
| 54 | m_supportsMultipleCopies(false), |
| 55 | m_supportsCollateCopies(false), |
| 56 | m_havePageSizes(false), |
| 57 | m_supportsCustomPageSizes(false), |
| 58 | m_haveResolutions(false), |
| 59 | m_haveInputSlots(false), |
| 60 | m_haveOutputBins(false), |
| 61 | m_haveDuplexModes(false), |
| 62 | m_haveColorModes(false) |
| 63 | #if QT_CONFIG(mimetype) |
| 64 | , m_haveMimeTypes(false) |
| 65 | #endif |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | QPlatformPrintDevice::~QPlatformPrintDevice() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | QString QPlatformPrintDevice::id() const |
| 74 | { |
| 75 | return m_id; |
| 76 | } |
| 77 | |
| 78 | QString QPlatformPrintDevice::name() const |
| 79 | { |
| 80 | return m_name; |
| 81 | } |
| 82 | |
| 83 | QString QPlatformPrintDevice::location() const |
| 84 | { |
| 85 | return m_location; |
| 86 | } |
| 87 | |
| 88 | QString QPlatformPrintDevice::makeAndModel() const |
| 89 | { |
| 90 | return m_makeAndModel; |
| 91 | } |
| 92 | |
| 93 | bool QPlatformPrintDevice::isValid() const |
| 94 | { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | bool QPlatformPrintDevice::isDefault() const |
| 99 | { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | bool QPlatformPrintDevice::isRemote() const |
| 104 | { |
| 105 | return m_isRemote; |
| 106 | } |
| 107 | |
| 108 | bool QPlatformPrintDevice::isValidPageLayout(const QPageLayout &layout, int resolution) const |
| 109 | { |
| 110 | // Check the page size is supported |
| 111 | if (!supportedPageSize(pageSize: layout.pageSize()).isValid()) |
| 112 | return false; |
| 113 | |
| 114 | // Check the margins are valid |
| 115 | QMarginsF pointMargins = layout.margins(units: QPageLayout::Point); |
| 116 | QMarginsF printMargins = printableMargins(pageSize: layout.pageSize(), orientation: layout.orientation(), resolution); |
| 117 | return pointMargins.left() >= printMargins.left() |
| 118 | && pointMargins.right() >= printMargins.right() |
| 119 | && pointMargins.top() >= printMargins.top() |
| 120 | && pointMargins.bottom() >= printMargins.bottom(); |
| 121 | } |
| 122 | |
| 123 | QPrint::DeviceState QPlatformPrintDevice::state() const |
| 124 | { |
| 125 | return QPrint::Error; |
| 126 | } |
| 127 | |
| 128 | bool QPlatformPrintDevice::supportsMultipleCopies() const |
| 129 | { |
| 130 | return m_supportsMultipleCopies; |
| 131 | } |
| 132 | |
| 133 | bool QPlatformPrintDevice::supportsCollateCopies() const |
| 134 | { |
| 135 | return m_supportsCollateCopies; |
| 136 | } |
| 137 | |
| 138 | void QPlatformPrintDevice::loadPageSizes() const |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | QPageSize QPlatformPrintDevice::defaultPageSize() const |
| 143 | { |
| 144 | return QPageSize(); |
| 145 | } |
| 146 | |
| 147 | QList<QPageSize> QPlatformPrintDevice::supportedPageSizes() const |
| 148 | { |
| 149 | if (!m_havePageSizes) |
| 150 | loadPageSizes(); |
| 151 | return m_pageSizes; |
| 152 | } |
| 153 | |
| 154 | QPageSize QPlatformPrintDevice::supportedPageSize(const QPageSize &pageSize) const |
| 155 | { |
| 156 | if (!pageSize.isValid()) |
| 157 | return QPageSize(); |
| 158 | |
| 159 | if (!m_havePageSizes) |
| 160 | loadPageSizes(); |
| 161 | |
| 162 | // First try match on name and id for case where printer defines same size twice with different names |
| 163 | // e.g. Windows defines DMPAPER_11X17 and DMPAPER_TABLOID with names "11x17" and "Tabloid", but both |
| 164 | // map to QPageSize::Tabloid / PPD Key "Tabloid" / ANSI B Tabloid |
| 165 | if (pageSize.id() != QPageSize::Custom) { |
| 166 | for (const QPageSize &ps : qAsConst(t&: m_pageSizes)) { |
| 167 | if (ps.id() == pageSize.id() && ps.name() == pageSize.name()) |
| 168 | return ps; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Next try match on id only if not custom |
| 173 | if (pageSize.id() != QPageSize::Custom) { |
| 174 | for (const QPageSize &ps : qAsConst(t&: m_pageSizes)) { |
| 175 | if (ps.id() == pageSize.id()) |
| 176 | return ps; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Next try a match on size, in case it's a custom with a different name |
| 181 | return supportedPageSizeMatch(pageSize); |
| 182 | } |
| 183 | |
| 184 | QPageSize QPlatformPrintDevice::supportedPageSize(QPageSize::PageSizeId pageSizeId) const |
| 185 | { |
| 186 | if (!m_havePageSizes) |
| 187 | loadPageSizes(); |
| 188 | |
| 189 | for (const QPageSize &ps : qAsConst(t&: m_pageSizes)) { |
| 190 | if (ps.id() == pageSizeId) |
| 191 | return ps; |
| 192 | } |
| 193 | |
| 194 | // If no supported page size found, try use a custom size instead if supported |
| 195 | return supportedPageSizeMatch(pageSize: QPageSize(pageSizeId)); |
| 196 | } |
| 197 | |
| 198 | QPageSize QPlatformPrintDevice::supportedPageSize(const QString &pageName) const |
| 199 | { |
| 200 | if (!m_havePageSizes) |
| 201 | loadPageSizes(); |
| 202 | |
| 203 | for (const QPageSize &ps : qAsConst(t&: m_pageSizes)) { |
| 204 | if (ps.name() == pageName) |
| 205 | return ps; |
| 206 | } |
| 207 | |
| 208 | return QPageSize(); |
| 209 | } |
| 210 | |
| 211 | QPageSize QPlatformPrintDevice::supportedPageSize(const QSize &sizePoints) const |
| 212 | { |
| 213 | if (!m_havePageSizes) |
| 214 | loadPageSizes(); |
| 215 | |
| 216 | // Try to find a supported page size based on fuzzy-matched point size |
| 217 | return supportedPageSizeMatch(pageSize: QPageSize(sizePoints)); |
| 218 | } |
| 219 | |
| 220 | QPageSize QPlatformPrintDevice::supportedPageSize(const QSizeF &size, QPageSize::Unit units) const |
| 221 | { |
| 222 | if (!m_havePageSizes) |
| 223 | loadPageSizes(); |
| 224 | |
| 225 | // Try to find a supported page size based on fuzzy-matched unit size |
| 226 | return supportedPageSizeMatch(pageSize: QPageSize(size, units)); |
| 227 | } |
| 228 | |
| 229 | QPageSize QPlatformPrintDevice::supportedPageSizeMatch(const QPageSize &pageSize) const |
| 230 | { |
| 231 | // If it's a known page size, just return itself |
| 232 | if (m_pageSizes.contains(t: pageSize)) |
| 233 | return pageSize; |
| 234 | |
| 235 | // Try to find a supported page size based on point size |
| 236 | for (const QPageSize &ps : qAsConst(t&: m_pageSizes)) { |
| 237 | if (ps.sizePoints() == pageSize.sizePoints()) |
| 238 | return ps; |
| 239 | } |
| 240 | return QPageSize(); |
| 241 | } |
| 242 | |
| 243 | bool QPlatformPrintDevice::supportsCustomPageSizes() const |
| 244 | { |
| 245 | return m_supportsCustomPageSizes; |
| 246 | } |
| 247 | |
| 248 | QSize QPlatformPrintDevice::minimumPhysicalPageSize() const |
| 249 | { |
| 250 | return m_minimumPhysicalPageSize; |
| 251 | } |
| 252 | |
| 253 | QSize QPlatformPrintDevice::maximumPhysicalPageSize() const |
| 254 | { |
| 255 | return m_maximumPhysicalPageSize; |
| 256 | } |
| 257 | |
| 258 | QMarginsF QPlatformPrintDevice::printableMargins(const QPageSize &pageSize, |
| 259 | QPageLayout::Orientation orientation, |
| 260 | int resolution) const |
| 261 | { |
| 262 | Q_UNUSED(pageSize) |
| 263 | Q_UNUSED(orientation) |
| 264 | Q_UNUSED(resolution) |
| 265 | return QMarginsF(0, 0, 0, 0); |
| 266 | } |
| 267 | |
| 268 | void QPlatformPrintDevice::loadResolutions() const |
| 269 | { |
| 270 | } |
| 271 | |
| 272 | int QPlatformPrintDevice::defaultResolution() const |
| 273 | { |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | QList<int> QPlatformPrintDevice::supportedResolutions() const |
| 278 | { |
| 279 | if (!m_haveResolutions) |
| 280 | loadResolutions(); |
| 281 | return m_resolutions; |
| 282 | } |
| 283 | |
| 284 | void QPlatformPrintDevice::loadInputSlots() const |
| 285 | { |
| 286 | } |
| 287 | |
| 288 | QPrint::InputSlot QPlatformPrintDevice::defaultInputSlot() const |
| 289 | { |
| 290 | QPrint::InputSlot input; |
| 291 | input.key = QByteArrayLiteral("Auto" ); |
| 292 | input.name = QCoreApplication::translate(context: "Print Device Input Slot" , key: "Automatic" ); |
| 293 | input.id = QPrint::Auto; |
| 294 | return input; |
| 295 | } |
| 296 | |
| 297 | QVector<QPrint::InputSlot> QPlatformPrintDevice::supportedInputSlots() const |
| 298 | { |
| 299 | if (!m_haveInputSlots) |
| 300 | loadInputSlots(); |
| 301 | return m_inputSlots; |
| 302 | } |
| 303 | |
| 304 | void QPlatformPrintDevice::loadOutputBins() const |
| 305 | { |
| 306 | } |
| 307 | |
| 308 | QPrint::OutputBin QPlatformPrintDevice::defaultOutputBin() const |
| 309 | { |
| 310 | QPrint::OutputBin output; |
| 311 | output.key = QByteArrayLiteral("Auto" ); |
| 312 | output.name = QCoreApplication::translate(context: "Print Device Output Bin" , key: "Automatic" ); |
| 313 | output.id = QPrint::AutoOutputBin; |
| 314 | return output; |
| 315 | } |
| 316 | |
| 317 | QVector<QPrint::OutputBin> QPlatformPrintDevice::supportedOutputBins() const |
| 318 | { |
| 319 | if (!m_haveOutputBins) |
| 320 | loadOutputBins(); |
| 321 | return m_outputBins; |
| 322 | } |
| 323 | |
| 324 | void QPlatformPrintDevice::loadDuplexModes() const |
| 325 | { |
| 326 | } |
| 327 | |
| 328 | QPrint::DuplexMode QPlatformPrintDevice::defaultDuplexMode() const |
| 329 | { |
| 330 | return QPrint::DuplexNone; |
| 331 | } |
| 332 | |
| 333 | QVector<QPrint::DuplexMode> QPlatformPrintDevice::supportedDuplexModes() const |
| 334 | { |
| 335 | if (!m_haveDuplexModes) |
| 336 | loadDuplexModes(); |
| 337 | return m_duplexModes; |
| 338 | } |
| 339 | |
| 340 | void QPlatformPrintDevice::loadColorModes() const |
| 341 | { |
| 342 | } |
| 343 | |
| 344 | QPrint::ColorMode QPlatformPrintDevice::defaultColorMode() const |
| 345 | { |
| 346 | return QPrint::GrayScale; |
| 347 | } |
| 348 | |
| 349 | QVector<QPrint::ColorMode> QPlatformPrintDevice::supportedColorModes() const |
| 350 | { |
| 351 | if (!m_haveColorModes) |
| 352 | loadColorModes(); |
| 353 | return m_colorModes; |
| 354 | } |
| 355 | |
| 356 | #if QT_CONFIG(mimetype) |
| 357 | void QPlatformPrintDevice::loadMimeTypes() const |
| 358 | { |
| 359 | } |
| 360 | #endif // mimetype |
| 361 | |
| 362 | QVariant QPlatformPrintDevice::property(QPrintDevice::PrintDevicePropertyKey key) const |
| 363 | { |
| 364 | Q_UNUSED(key) |
| 365 | |
| 366 | return QVariant(); |
| 367 | } |
| 368 | |
| 369 | bool QPlatformPrintDevice::setProperty(QPrintDevice::PrintDevicePropertyKey key, const QVariant &value) |
| 370 | { |
| 371 | Q_UNUSED(key) |
| 372 | Q_UNUSED(value) |
| 373 | |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | bool QPlatformPrintDevice::isFeatureAvailable(QPrintDevice::PrintDevicePropertyKey key, const QVariant ¶ms) const |
| 378 | { |
| 379 | Q_UNUSED(key) |
| 380 | Q_UNUSED(params) |
| 381 | |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | #if QT_CONFIG(mimetype) |
| 386 | QList<QMimeType> QPlatformPrintDevice::supportedMimeTypes() const |
| 387 | { |
| 388 | if (!m_haveMimeTypes) |
| 389 | loadMimeTypes(); |
| 390 | return m_mimeTypes; |
| 391 | } |
| 392 | #endif // mimetype |
| 393 | |
| 394 | QPageSize QPlatformPrintDevice::createPageSize(const QString &key, const QSize &size, const QString &localizedName) |
| 395 | { |
| 396 | return QPageSize(key, size, localizedName); |
| 397 | } |
| 398 | |
| 399 | QPageSize QPlatformPrintDevice::createPageSize(int windowsId, const QSize &size, const QString &localizedName) |
| 400 | { |
| 401 | return QPageSize(windowsId, size, localizedName); |
| 402 | } |
| 403 | |
| 404 | #endif // QT_NO_PRINTER |
| 405 | |
| 406 | QT_END_NAMESPACE |
| 407 | |