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
qquickscreen.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
5#include <QtQuick/private/qquickscreen_p.h>
6#include <QtQuick/qquickwindow.h>
7
8#include <QtQuick/private/qquickitem_p.h>
9
10#include <QtGui/qguiapplication.h>
11#include <QtGui/qscreen.h>
12
13QT_BEGIN_NAMESPACE
14
15/*!
16 \qmltype Screen
17 \nativetype QQuickScreenAttached
18 \inqmlmodule QtQuick
19 \ingroup qtquick-visual-utility
20 \brief The Screen attached object provides information about the Screen an Item or Window is displayed on.
21
22 The Screen attached object is valid inside Item or Item derived types,
23 after component completion. Inside these items it refers to the screen that
24 the item is currently being displayed on.
25
26 The attached object is also valid inside Window or Window derived types,
27 after component completion. In that case it refers to the screen where the
28 Window was created. It is generally better to access the Screen from the
29 relevant Item instead, because on a multi-screen desktop computer, the user
30 can drag a Window into a position where it spans across multiple screens.
31 In that case some Items will be on one screen, and others on a different
32 screen.
33
34 To use this type, you will need to import the module with the following line:
35 \code
36 import QtQuick.Window 2.2
37 \endcode
38 It is a separate import in order to allow you to have a QML environment
39 without access to window system features.
40
41 Note that the Screen type is not valid at Component.onCompleted, because
42 the Item or Window has not been displayed on a screen by this time.
43
44 \sa {Qt Quick Examples - Window and Screen}
45*/
46
47/*!
48 \qmlattachedproperty string Screen::name
49 \readonly
50 \since 5.1
51
52 The name of the screen.
53*/
54/*!
55 \qmlattachedproperty int Screen::virtualX
56 \readonly
57 \since 5.9
58
59 The x coordinate of the screen within the virtual desktop.
60*/
61/*!
62 \qmlattachedproperty int Screen::virtualY
63 \readonly
64 \since 5.9
65
66 The y coordinate of the screen within the virtual desktop.
67*/
68/*!
69 \qmlattachedproperty string Screen::manufacturer
70 \readonly
71 \since 5.10
72
73 The manufacturer of the screen.
74*/
75/*!
76 \qmlattachedproperty string Screen::model
77 \readonly
78 \since 5.10
79
80 The model of the screen.
81*/
82/*!
83 \qmlattachedproperty string Screen::serialNumber
84 \readonly
85 \since 5.10
86
87 The serial number of the screen.
88*/
89/*!
90 \qmlattachedproperty int Screen::width
91 \readonly
92
93 This contains the width of the screen in pixels.
94*/
95/*!
96 \qmlattachedproperty int Screen::height
97 \readonly
98
99 This contains the height of the screen in pixels.
100*/
101/*!
102 \qmlattachedproperty int Screen::desktopAvailableWidth
103 \readonly
104 \since 5.1
105
106 This contains the available width of the collection of screens which make
107 up the virtual desktop, in pixels, excluding window manager reserved areas
108 such as task bars and system menus. If you want to position a Window at
109 the right of the desktop, you can bind to it like this:
110
111 \code
112 x: Screen.desktopAvailableWidth - width
113 \endcode
114*/
115/*!
116 \qmlattachedproperty int Screen::desktopAvailableHeight
117 \readonly
118 \since 5.1
119
120 This contains the available height of the collection of screens which make
121 up the virtual desktop, in pixels, excluding window manager reserved areas
122 such as task bars and system menus. If you want to position a Window at
123 the bottom of the desktop, you can bind to it like this:
124
125 \code
126 y: Screen.desktopAvailableHeight - height
127 \endcode
128*/
129/*!
130 \qmlattachedproperty real Screen::logicalPixelDensity
131 \readonly
132 \since 5.1
133 \deprecated
134
135 The number of logical pixels per millimeter. This is the effective pixel
136 density provided by the platform to use in image scaling calculations.
137
138 Due to inconsistencies in how logical pixel density is handled across
139 the various platforms Qt supports, it is recommended to
140 use physical pixels instead (via the \c pixelDensity property) for
141 portability.
142
143 \sa pixelDensity
144*/
145/*!
146 \qmlattachedproperty real Screen::pixelDensity
147 \readonly
148 \since 5.2
149
150 The number of physical pixels per millimeter.
151*/
152/*!
153 \qmlattachedproperty real Screen::devicePixelRatio
154 \readonly
155 \since 5.4
156
157 The ratio between physical pixels and device-independent pixels for the screen.
158
159 Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays.
160*/
161/*!
162 \qmlattachedproperty Qt::ScreenOrientation Screen::primaryOrientation
163 \readonly
164
165 This contains the primary orientation of the screen. If the
166 screen's height is greater than its width, then the orientation is
167 Qt.PortraitOrientation; otherwise it is Qt.LandscapeOrientation.
168
169 If you are designing an application which changes its layout depending on
170 device orientation, you probably want to use primaryOrientation to
171 determine the layout. That is because on a desktop computer, you can expect
172 primaryOrientation to change when the user rotates the screen via the
173 operating system's control panel, even if the computer does not contain an
174 accelerometer. Likewise on most handheld computers which do have
175 accelerometers, the operating system will rotate the whole screen
176 automatically, so again you will see the primaryOrientation change.
177*/
178/*!
179 \qmlattachedproperty Qt::ScreenOrientation Screen::orientation
180 \readonly
181
182 This contains the current orientation of the screen from the
183 window system perspective.
184
185 Most mobile devices and tablet computers contain accelerometer sensors.
186 The windowing system may rotate the entire screen automatically
187 based on how it is being held, or manually via settings to rotate a desktop
188 monitor; in that case, this \c orientation property will change.
189
190 \sa primaryOrientation, QWindow::contentOrientation()
191*/
192/*!
193 \qmlattachedmethod int Screen::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
194
195 Returns the rotation angle, in degrees, between the specified screen
196 orientations \a a and \a b.
197*/
198
199QQuickScreenInfo::QQuickScreenInfo(QObject *parent, QScreen *wrappedScreen)
200 : QObject(parent)
201{
202 setWrappedScreen(wrappedScreen);
203}
204
205QString QQuickScreenInfo::name() const
206{
207 if (!m_screen)
208 return QString();
209 return m_screen->name();
210}
211
212QString QQuickScreenInfo::manufacturer() const
213{
214 if (!m_screen)
215 return QString();
216 return m_screen->manufacturer();
217}
218
219QString QQuickScreenInfo::model() const
220{
221 if (!m_screen)
222 return QString();
223 return m_screen->model();
224}
225
226QString QQuickScreenInfo::serialNumber() const
227{
228 if (!m_screen)
229 return QString();
230 return m_screen->serialNumber();
231}
232
233int QQuickScreenInfo::width() const
234{
235 if (!m_screen)
236 return 0;
237 return m_screen->size().width();
238}
239
240int QQuickScreenInfo::height() const
241{
242 if (!m_screen)
243 return 0;
244 return m_screen->size().height();
245}
246
247int QQuickScreenInfo::desktopAvailableWidth() const
248{
249 if (!m_screen)
250 return 0;
251 return m_screen->availableVirtualSize().width();
252}
253
254int QQuickScreenInfo::desktopAvailableHeight() const
255{
256 if (!m_screen)
257 return 0;
258 return m_screen->availableVirtualSize().height();
259}
260
261qreal QQuickScreenInfo::logicalPixelDensity() const
262{
263 if (!m_screen)
264 return 0.0;
265 return m_screen->logicalDotsPerInch() / 25.4;
266}
267
268qreal QQuickScreenInfo::pixelDensity() const
269{
270 if (!m_screen)
271 return 0.0;
272 return m_screen->physicalDotsPerInch() / 25.4;
273}
274
275qreal QQuickScreenInfo::devicePixelRatio() const
276{
277 if (!m_screen)
278 return 1.0;
279 return m_screen->devicePixelRatio();
280}
281
282Qt::ScreenOrientation QQuickScreenInfo::primaryOrientation() const
283{
284 if (!m_screen)
285 return Qt::PrimaryOrientation;
286 return m_screen->primaryOrientation();
287}
288
289Qt::ScreenOrientation QQuickScreenInfo::orientation() const
290{
291 if (!m_screen)
292 return Qt::PrimaryOrientation;
293 return m_screen->orientation();
294}
295
296int QQuickScreenInfo::virtualX() const
297{
298 if (!m_screen)
299 return 0;
300 return m_screen->geometry().topLeft().x();
301}
302
303int QQuickScreenInfo::virtualY() const
304{
305 if (!m_screen)
306 return 0;
307 return m_screen->geometry().topLeft().y();
308}
309
310void QQuickScreenInfo::setWrappedScreen(QScreen *screen)
311{
312 if (screen == m_screen)
313 return;
314
315 QScreen *oldScreen = m_screen;
316 m_screen = screen;
317
318 if (oldScreen)
319 oldScreen->disconnect(this);
320
321 if (!screen) //Don't bother emitting signals, because the new values are garbage anyways
322 return;
323
324 if (!oldScreen || screen->geometry() != oldScreen->geometry()) {
325 emit virtualXChanged();
326 emit virtualYChanged();
327 }
328 if (!oldScreen || screen->size() != oldScreen->size()) {
329 emit widthChanged();
330 emit heightChanged();
331 }
332 if (!oldScreen || screen->name() != oldScreen->name())
333 emit nameChanged();
334 if (!oldScreen || screen->manufacturer() != oldScreen->manufacturer())
335 emit manufacturerChanged();
336 if (!oldScreen || screen->model() != oldScreen->model())
337 emit modelChanged();
338 if (!oldScreen || screen->serialNumber() != oldScreen->serialNumber())
339 emit serialNumberChanged();
340 if (!oldScreen || screen->orientation() != oldScreen->orientation())
341 emit orientationChanged();
342 if (!oldScreen || screen->primaryOrientation() != oldScreen->primaryOrientation())
343 emit primaryOrientationChanged();
344 if (!oldScreen || screen->availableVirtualGeometry() != oldScreen->availableVirtualGeometry())
345 emit desktopGeometryChanged();
346 if (!oldScreen || screen->logicalDotsPerInch() != oldScreen->logicalDotsPerInch())
347 emit logicalPixelDensityChanged();
348 if (!oldScreen || screen->physicalDotsPerInch() != oldScreen->physicalDotsPerInch())
349 emit pixelDensityChanged();
350 if (!oldScreen || screen->devicePixelRatio() != oldScreen->devicePixelRatio())
351 emit devicePixelRatioChanged();
352
353 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
354 this, QQuickScreenInfo, SIGNAL(widthChanged()));
355 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
356 this, QQuickScreenInfo, SIGNAL(heightChanged()));
357 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
358 this, QQuickScreenInfo, SIGNAL(virtualXChanged()));
359 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
360 this, QQuickScreenInfo, SIGNAL(virtualYChanged()));
361 qmlobject_connect(screen, QScreen, SIGNAL(orientationChanged(Qt::ScreenOrientation)),
362 this, QQuickScreenInfo, SIGNAL(orientationChanged()));
363 qmlobject_connect(screen, QScreen, SIGNAL(primaryOrientationChanged(Qt::ScreenOrientation)),
364 this, QQuickScreenInfo, SIGNAL(primaryOrientationChanged()));
365 qmlobject_connect(screen, QScreen, SIGNAL(virtualGeometryChanged(QRect)),
366 this, QQuickScreenInfo, SIGNAL(desktopGeometryChanged()));
367 qmlobject_connect(screen, QScreen, SIGNAL(logicalDotsPerInchChanged(qreal)),
368 this, QQuickScreenInfo, SIGNAL(logicalPixelDensityChanged()));
369 qmlobject_connect(screen, QScreen, SIGNAL(physicalDotsPerInchChanged(qreal)),
370 this, QQuickScreenInfo, SIGNAL(pixelDensityChanged()));
371}
372
373QScreen *QQuickScreenInfo::wrappedScreen() const
374{
375 return m_screen;
376}
377
378QQuickScreenAttached::QQuickScreenAttached(QObject* attachee)
379 : QQuickScreenInfo(attachee)
380{
381 m_attachee = qobject_cast<QQuickItem*>(attachee);
382
383 if (m_attachee) {
384 QQuickItemPrivate::get(m_attachee)->extra.value().screenAttached = this;
385
386 if (m_attachee->window()) //It might not be assigned to a window yet
387 windowChanged(m_attachee->window());
388 } else {
389 QQuickWindow *window = qobject_cast<QQuickWindow*>(attachee);
390 if (window)
391 windowChanged(window);
392 }
393
394 if (!m_screen)
395 screenChanged(QGuiApplication::primaryScreen());
396}
397
398int QQuickScreenAttached::angleBetween(int a, int b)
399{
400 if (!m_screen)
401 return Qt::PrimaryOrientation;
402 return m_screen->angleBetween((Qt::ScreenOrientation)a,(Qt::ScreenOrientation)b);
403}
404
405void QQuickScreenAttached::windowChanged(QQuickWindow* c)
406{
407 if (m_window)
408 qmlobject_disconnect(m_window, QQuickWindow, SIGNAL(screenChanged(QScreen*)), this, QQuickScreenAttached, SLOT(screenChanged(QScreen*)));
409 m_window = c;
410 screenChanged(c ? c->screen() : nullptr);
411 if (c)
412 qmlobject_connect(c, QQuickWindow, SIGNAL(screenChanged(QScreen*)), this, QQuickScreenAttached, SLOT(screenChanged(QScreen*)));
413}
414
415void QQuickScreenAttached::screenChanged(QScreen *screen)
416{
417 //qDebug() << "QQuickScreenAttached::screenChanged" << (screen ? screen->name() : QString::fromLatin1("null"));
418 if (screen != m_screen)
419 setWrappedScreen(screen);
420}
421
422QT_END_NAMESPACE
423
424#include "moc_qquickscreen_p.cpp"