| 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 "qpaintdevice.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | QPaintDevice::QPaintDevice() noexcept |
| 9 | { |
| 10 | painters = 0; |
| 11 | } |
| 12 | |
| 13 | QPaintDevice::~QPaintDevice() |
| 14 | { |
| 15 | if (paintingActive()) |
| 16 | qWarning(msg: "QPaintDevice: Cannot destroy paint device that is being " |
| 17 | "painted"); |
| 18 | } |
| 19 | |
| 20 | /*! |
| 21 | \internal |
| 22 | */ |
| 23 | // ### Qt 7: Replace this workaround mechanism: virtual devicePixelRatio() and virtual metricF() |
| 24 | double QPaintDevice::getDecodedMetricF(PaintDeviceMetric metricA, PaintDeviceMetric metricB) const |
| 25 | { |
| 26 | qint32 buf[2]; |
| 27 | // The Encoded metric enum values come in pairs of one odd and one even value. |
| 28 | // We map those to the 0 and 1 indexes of buf by taking just the least significant bit. |
| 29 | // Same mapping here as in the encodeMetricF() function, to ensure correct order. |
| 30 | buf[metricA & 1] = metric(metric: metricA); |
| 31 | buf[metricB & 1] = metric(metric: metricB); |
| 32 | double res; |
| 33 | memcpy(dest: &res, src: buf, n: sizeof(res)); |
| 34 | return res; |
| 35 | } |
| 36 | |
| 37 | qreal QPaintDevice::devicePixelRatio() const |
| 38 | { |
| 39 | Q_STATIC_ASSERT((PdmDevicePixelRatioF_EncodedA & 1) != (PdmDevicePixelRatioF_EncodedB & 1)); |
| 40 | double res; |
| 41 | int scaledDpr = metric(metric: PdmDevicePixelRatioScaled); |
| 42 | if (scaledDpr == int(devicePixelRatioFScale())) { |
| 43 | res = 1; // Shortcut for common case |
| 44 | } else if (scaledDpr == 2 * int(devicePixelRatioFScale())) { |
| 45 | res = 2; // Shortcut for common case |
| 46 | } else { |
| 47 | res = getDecodedMetricF(metricA: PdmDevicePixelRatioF_EncodedA, metricB: PdmDevicePixelRatioF_EncodedB); |
| 48 | if (res <= 0) // These metrics not implemented, fall back to PdmDevicePixelRatioScaled |
| 49 | res = scaledDpr / devicePixelRatioFScale(); |
| 50 | } |
| 51 | return res; |
| 52 | } |
| 53 | |
| 54 | /*! |
| 55 | \internal |
| 56 | */ |
| 57 | void QPaintDevice::initPainter(QPainter *) const |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | /*! |
| 62 | \internal |
| 63 | */ |
| 64 | QPaintDevice *QPaintDevice::redirected(QPoint *) const |
| 65 | { |
| 66 | return nullptr; |
| 67 | } |
| 68 | |
| 69 | /*! |
| 70 | \internal |
| 71 | */ |
| 72 | QPainter *QPaintDevice::sharedPainter() const |
| 73 | { |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | Q_GUI_EXPORT int qt_paint_device_metric(const QPaintDevice *device, QPaintDevice::PaintDeviceMetric metric) |
| 78 | { |
| 79 | return device->metric(metric); |
| 80 | } |
| 81 | |
| 82 | int QPaintDevice::metric(PaintDeviceMetric m) const |
| 83 | { |
| 84 | // Fallback: A subclass has not implemented PdmDevicePixelRatioScaled but might |
| 85 | // have implemented PdmDevicePixelRatio. |
| 86 | if (m == PdmDevicePixelRatioScaled) |
| 87 | return this->metric(m: PdmDevicePixelRatio) * devicePixelRatioFScale(); |
| 88 | if (m == PdmNumColors) |
| 89 | return 0; |
| 90 | if (m == PdmDevicePixelRatio) |
| 91 | return 1; |
| 92 | |
| 93 | qWarning(msg: "QPaintDevice::metrics: Device has no metric information"); |
| 94 | |
| 95 | switch (m) { |
| 96 | case PdmDevicePixelRatioScaled: |
| 97 | case PdmDevicePixelRatio: |
| 98 | case PdmNumColors: |
| 99 | Q_UNREACHABLE(); |
| 100 | break; |
| 101 | case PdmDpiX: |
| 102 | case PdmDpiY: |
| 103 | return 72; |
| 104 | case PdmDevicePixelRatioF_EncodedA: |
| 105 | case PdmDevicePixelRatioF_EncodedB: |
| 106 | return 0; |
| 107 | case PdmWidth: |
| 108 | case PdmHeight: |
| 109 | case PdmWidthMM: |
| 110 | case PdmHeightMM: |
| 111 | case PdmDepth: |
| 112 | case PdmPhysicalDpiX: |
| 113 | case PdmPhysicalDpiY: |
| 114 | return 0; |
| 115 | } |
| 116 | qDebug(msg: "Unrecognized metric %d!", m); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | QT_END_NAMESPACE |
| 121 |
