Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpaintdevicewindow.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
7#include <QtGui/QGuiApplication>
8#include <QtGui/QScreen>
9
11
12QPaintDeviceWindowPrivate::QPaintDeviceWindowPrivate()
13 = default;
14
15QPaintDeviceWindowPrivate::~QPaintDeviceWindowPrivate()
16 = default;
17
18/*!
19 \class QPaintDeviceWindow
20 \inmodule QtGui
21 \since 5.4
22 \brief Convenience subclass of QWindow that is also a QPaintDevice.
23
24 QPaintDeviceWindow is like a regular QWindow, with the added functionality
25 of being a paint device too. Whenever the content needs to be updated,
26 the virtual paintEvent() function is called. Subclasses, that reimplement
27 this function, can then simply open a QPainter on the window.
28
29 \note This class cannot directly be used in applications. It rather serves
30 as a base for subclasses like QOpenGLWindow.
31
32 \sa QOpenGLWindow
33*/
34
35/*!
36 Marks the entire window as dirty and schedules a repaint.
37
38 \note Subsequent calls to this function before the next paint
39 event will get ignored.
40
41 \note For non-exposed windows the update is deferred until the
42 window becomes exposed again.
43*/
44void QPaintDeviceWindow::update()
45{
46 update(QRect(QPoint(0,0), size()));
47}
48
49/*!
50 Marks the \a rect of the window as dirty and schedules a repaint.
51
52 \note Subsequent calls to this function before the next paint
53 event will get ignored, but \a rect is added to the region to update.
54
55 \note For non-exposed windows the update is deferred until the
56 window becomes exposed again.
57*/
58void QPaintDeviceWindow::update(const QRect &rect)
59{
60 Q_D(QPaintDeviceWindow);
61 d->dirtyRegion += rect;
62 if (isExposed())
63 requestUpdate();
64}
65
66/*!
67 Marks the \a region of the window as dirty and schedules a repaint.
68
69 \note Subsequent calls to this function before the next paint
70 event will get ignored, but \a region is added to the region to update.
71
72 \note For non-exposed windows the update is deferred until the
73 window becomes exposed again.
74*/
75void QPaintDeviceWindow::update(const QRegion &region)
76{
77 Q_D(QPaintDeviceWindow);
78 d->dirtyRegion += region;
79 if (isExposed())
80 requestUpdate();
81}
82
83/*!
84 Handles paint events passed in the \a event parameter.
85
86 The default implementation does nothing. Reimplement this function to
87 perform painting. If necessary, the dirty area is retrievable from
88 the \a event.
89*/
90void QPaintDeviceWindow::paintEvent(QPaintEvent *event)
91{
92 Q_UNUSED(event);
93 // Do nothing
94}
95
96/*!
97 \internal
98 */
99int QPaintDeviceWindow::metric(PaintDeviceMetric metric) const
100{
101 QScreen *screen = this->screen();
102 if (!screen && QGuiApplication::primaryScreen())
103 screen = QGuiApplication::primaryScreen();
104
105 switch (metric) {
106 case PdmWidth:
107 return width();
108 case PdmWidthMM:
109 if (screen)
110 return width() * screen->physicalSize().width() / screen->geometry().width();
111 break;
112 case PdmHeight:
113 return height();
114 case PdmHeightMM:
115 if (screen)
116 return height() * screen->physicalSize().height() / screen->geometry().height();
117 break;
118 case PdmDpiX:
119 if (screen)
120 return qRound(screen->logicalDotsPerInchX());
121 break;
122 case PdmDpiY:
123 if (screen)
124 return qRound(screen->logicalDotsPerInchY());
125 break;
126 case PdmPhysicalDpiX:
127 if (screen)
128 return qRound(screen->physicalDotsPerInchX());
129 break;
130 case PdmPhysicalDpiY:
131 if (screen)
132 return qRound(screen->physicalDotsPerInchY());
133 break;
134 case PdmDevicePixelRatio:
135 return int(QWindow::devicePixelRatio());
136 break;
137 case PdmDevicePixelRatioScaled:
138 return int(QWindow::devicePixelRatio() * devicePixelRatioFScale());
139 break;
140 case PdmDevicePixelRatioF_EncodedA:
141 Q_FALLTHROUGH();
142 case PdmDevicePixelRatioF_EncodedB:
143 return QPaintDevice::encodeMetricF(metric, QWindow::devicePixelRatio());
144 break;
145 default:
146 break;
147 }
148
149 return QPaintDevice::metric(metric);
150}
151
152/*!
153 \internal
154 */
155void QPaintDeviceWindow::exposeEvent(QExposeEvent *exposeEvent)
156{
157 QWindow::exposeEvent(exposeEvent);
158}
159
160/*!
161 \internal
162 */
163bool QPaintDeviceWindow::event(QEvent *event)
164{
165 Q_D(QPaintDeviceWindow);
166
167 if (event->type() == QEvent::UpdateRequest) {
168 if (handle()) // platform window may be gone when the window is closed during app exit
169 d->handleUpdateEvent();
170 return true;
171 } else if (event->type() == QEvent::Paint) {
172 d->markWindowAsDirty();
173 // Do not rely on exposeEvent->region() as it has some issues for the
174 // time being, namely that it is sometimes in local coordinates,
175 // sometimes relative to the parent, depending on the platform plugin.
176 // We require local coords here.
177 auto region = QRect(QPoint(0, 0), size());
178 d->doFlush(region); // Will end up calling paintEvent
179 return true;
180 } else if (event->type() == QEvent::Resize) {
181 d->handleResizeEvent();
182 }
183
184 return QWindow::event(event);
185}
186
187/*!
188 \internal
189 */
190QPaintDeviceWindow::QPaintDeviceWindow(QPaintDeviceWindowPrivate &dd, QWindow *parent)
191 : QWindow(dd, parent)
192{
193}
194
195/*!
196 \internal
197 */
198QPaintEngine *QPaintDeviceWindow::paintEngine() const
199{
200 return nullptr;
201}
202
203QT_END_NAMESPACE
204
205#include "moc_qpaintdevicewindow.cpp"
Combined button and popup list for selecting options.