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
6QT_BEGIN_NAMESPACE
7
8QPaintDevice::QPaintDevice() noexcept
9{
10 painters = 0;
11}
12
13QPaintDevice::~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()
24double 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
37qreal 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*/
57void QPaintDevice::initPainter(QPainter *) const
58{
59}
60
61/*!
62 \internal
63*/
64QPaintDevice *QPaintDevice::redirected(QPoint *) const
65{
66 return nullptr;
67}
68
69/*!
70 \internal
71*/
72QPainter *QPaintDevice::sharedPainter() const
73{
74 return nullptr;
75}
76
77Q_GUI_EXPORT int qt_paint_device_metric(const QPaintDevice *device, QPaintDevice::PaintDeviceMetric metric)
78{
79 return device->metric(metric);
80}
81
82int 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
89 qWarning(msg: "QPaintDevice::metrics: Device has no metric information");
90
91 if (m == PdmDpiX) {
92 return 72;
93 } else if (m == PdmDpiY) {
94 return 72;
95 } else if (m == PdmNumColors) {
96 // FIXME: does this need to be a real value?
97 return 256;
98 } else if (m == PdmDevicePixelRatio) {
99 return 1;
100 } else if (m == PdmDevicePixelRatioF_EncodedA || m == PdmDevicePixelRatioF_EncodedB) {
101 return 0;
102 } else {
103 qDebug(msg: "Unrecognised metric %d!",m);
104 return 0;
105 }
106}
107
108QT_END_NAMESPACE
109

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/gui/painting/qpaintdevice.cpp