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 \qmlattachedproperty rect Screen::geometry
194 \readonly
195 \since 6.13
196
197 This property holds the screen's geometry in pixels.
198*/
199/*!
200 \qmlattachedproperty rect Screen::availableGeometry
201 \readonly
202 \since 6.13
203
204 This property holds the screen's available geometry in pixels, excluding
205 window manager reserved areas such as task bars and system menus.
206*/
207/*!
208 \qmlattachedproperty rect Screen::desktopGeometry
209 \readonly
210 \since 6.13
211
212 This property holds the geometry of the virtual desktop in pixels.
213*/
214/*!
215 \qmlattachedproperty rect Screen::availableDesktopGeometry
216 \readonly
217 \since 6.13
218
219 This property holds the available geometry of the virtual desktop in
220 pixels, excluding window manager reserved areas such as task bars and
221 system menus.
222*/
223/*!
224 \qmlattachedmethod int Screen::angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b)
225
226 Returns the rotation angle, in degrees, between the specified screen
227 orientations \a a and \a b.
228*/
229
230QQuickScreenInfo::QQuickScreenInfo(QObject *parent, QScreen *wrappedScreen)
231 : QObject(parent)
232{
233 setWrappedScreen(wrappedScreen);
234}
235
236QString QQuickScreenInfo::name() const
237{
238 if (!m_screen)
239 return QString();
240 return m_screen->name();
241}
242
243QString QQuickScreenInfo::manufacturer() const
244{
245 if (!m_screen)
246 return QString();
247 return m_screen->manufacturer();
248}
249
250QString QQuickScreenInfo::model() const
251{
252 if (!m_screen)
253 return QString();
254 return m_screen->model();
255}
256
257QString QQuickScreenInfo::serialNumber() const
258{
259 if (!m_screen)
260 return QString();
261 return m_screen->serialNumber();
262}
263
264int QQuickScreenInfo::width() const
265{
266 if (!m_screen)
267 return 0;
268 return m_screen->size().width();
269}
270
271int QQuickScreenInfo::height() const
272{
273 if (!m_screen)
274 return 0;
275 return m_screen->size().height();
276}
277
278int QQuickScreenInfo::desktopAvailableWidth() const
279{
280 if (!m_screen)
281 return 0;
282 return m_screen->availableVirtualSize().width();
283}
284
285int QQuickScreenInfo::desktopAvailableHeight() const
286{
287 if (!m_screen)
288 return 0;
289 return m_screen->availableVirtualSize().height();
290}
291
292qreal QQuickScreenInfo::logicalPixelDensity() const
293{
294 if (!m_screen)
295 return 0.0;
296 return m_screen->logicalDotsPerInch() / 25.4;
297}
298
299qreal QQuickScreenInfo::pixelDensity() const
300{
301 if (!m_screen)
302 return 0.0;
303 return m_screen->physicalDotsPerInch() / 25.4;
304}
305
306qreal QQuickScreenInfo::devicePixelRatio() const
307{
308 if (!m_screen)
309 return 1.0;
310 return m_screen->devicePixelRatio();
311}
312
313Qt::ScreenOrientation QQuickScreenInfo::primaryOrientation() const
314{
315 if (!m_screen)
316 return Qt::PrimaryOrientation;
317 return m_screen->primaryOrientation();
318}
319
320Qt::ScreenOrientation QQuickScreenInfo::orientation() const
321{
322 if (!m_screen)
323 return Qt::PrimaryOrientation;
324 return m_screen->orientation();
325}
326
327int QQuickScreenInfo::virtualX() const
328{
329 if (!m_screen)
330 return 0;
331 return m_screen->geometry().topLeft().x();
332}
333
334int QQuickScreenInfo::virtualY() const
335{
336 if (!m_screen)
337 return 0;
338 return m_screen->geometry().topLeft().y();
339}
340
341QRect QQuickScreenInfo::geometry() const
342{
343 if (!m_screen)
344 return {};
345 return m_screen->geometry();
346}
347
348QRect QQuickScreenInfo::availableGeometry() const
349{
350 if (!m_screen)
351 return {};
352 return m_screen->availableGeometry();
353}
354
355QRect QQuickScreenInfo::desktopGeometry() const
356{
357 if (!m_screen)
358 return {};
359 return m_screen->virtualGeometry();
360}
361
362QRect QQuickScreenInfo::availableDesktopGeometry() const
363{
364 if (!m_screen)
365 return {};
366 return m_screen->availableVirtualGeometry();
367}
368
369void QQuickScreenInfo::setWrappedScreen(QScreen *screen)
370{
371 if (screen == m_screen)
372 return;
373
374 QScreen *oldScreen = m_screen;
375 m_screen = screen;
376
377 if (oldScreen)
378 oldScreen->disconnect(this);
379
380 if (!screen) //Don't bother emitting signals, because the new values are garbage anyways
381 return;
382
383 if (!oldScreen || screen->geometry() != oldScreen->geometry()) {
384 emit virtualXChanged();
385 emit virtualYChanged();
386 emit geometryChanged();
387 }
388 if (!oldScreen || screen->size() != oldScreen->size()) {
389 emit widthChanged();
390 emit heightChanged();
391 }
392 if (!oldScreen || screen->name() != oldScreen->name())
393 emit nameChanged();
394 if (!oldScreen || screen->manufacturer() != oldScreen->manufacturer())
395 emit manufacturerChanged();
396 if (!oldScreen || screen->model() != oldScreen->model())
397 emit modelChanged();
398 if (!oldScreen || screen->serialNumber() != oldScreen->serialNumber())
399 emit serialNumberChanged();
400 if (!oldScreen || screen->orientation() != oldScreen->orientation())
401 emit orientationChanged();
402 if (!oldScreen || screen->primaryOrientation() != oldScreen->primaryOrientation())
403 emit primaryOrientationChanged();
404 if (!oldScreen || screen->availableGeometry() != oldScreen->availableGeometry())
405 emit availableGeometryChanged();
406 if (!oldScreen
407 || screen->virtualGeometry() != oldScreen->virtualGeometry()
408 || screen->availableVirtualGeometry() != oldScreen->availableVirtualGeometry()) {
409 emit desktopGeometryChanged();
410 }
411 if (!oldScreen || screen->availableVirtualGeometry() != oldScreen->availableVirtualGeometry())
412 emit availableDesktopGeometryChanged();
413 if (!oldScreen || screen->logicalDotsPerInch() != oldScreen->logicalDotsPerInch())
414 emit logicalPixelDensityChanged();
415 if (!oldScreen || screen->physicalDotsPerInch() != oldScreen->physicalDotsPerInch())
416 emit pixelDensityChanged();
417 if (!oldScreen || screen->devicePixelRatio() != oldScreen->devicePixelRatio())
418 emit devicePixelRatioChanged();
419 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
420 this, QQuickScreenInfo, SIGNAL(widthChanged()));
421 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
422 this, QQuickScreenInfo, SIGNAL(heightChanged()));
423 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
424 this, QQuickScreenInfo, SIGNAL(virtualXChanged()));
425 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
426 this, QQuickScreenInfo, SIGNAL(virtualYChanged()));
427 qmlobject_connect(screen, QScreen, SIGNAL(geometryChanged(QRect)),
428 this, QQuickScreenInfo, SIGNAL(geometryChanged()));
429 qmlobject_connect(screen, QScreen, SIGNAL(orientationChanged(Qt::ScreenOrientation)),
430 this, QQuickScreenInfo, SIGNAL(orientationChanged()));
431 qmlobject_connect(screen, QScreen, SIGNAL(primaryOrientationChanged(Qt::ScreenOrientation)),
432 this, QQuickScreenInfo, SIGNAL(primaryOrientationChanged()));
433 qmlobject_connect(screen, QScreen, SIGNAL(virtualGeometryChanged(QRect)),
434 this, QQuickScreenInfo, SIGNAL(desktopGeometryChanged()));
435 qmlobject_connect(screen, QScreen, SIGNAL(virtualGeometryChanged(QRect)),
436 this, QQuickScreenInfo, SIGNAL(availableDesktopGeometryChanged()));
437 qmlobject_connect(screen, QScreen, SIGNAL(logicalDotsPerInchChanged(qreal)),
438 this, QQuickScreenInfo, SIGNAL(logicalPixelDensityChanged()));
439 qmlobject_connect(screen, QScreen, SIGNAL(physicalDotsPerInchChanged(qreal)),
440 this, QQuickScreenInfo, SIGNAL(pixelDensityChanged()));
441 qmlobject_connect(screen, QScreen, SIGNAL(availableGeometryChanged(QRect)),
442 this, QQuickScreenInfo, SIGNAL(availableGeometryChanged()));
443}
444
445QScreen *QQuickScreenInfo::wrappedScreen() const
446{
447 return m_screen;
448}
449
450QQuickScreenAttached::QQuickScreenAttached(QObject* attachee)
451 : QQuickScreenInfo(attachee)
452{
453 m_attachee = qobject_cast<QQuickItem*>(attachee);
454
455 if (m_attachee) {
456 QQuickItemPrivate::get(m_attachee)->extra.value().screenAttached = this;
457
458 if (m_attachee->window()) //It might not be assigned to a window yet
459 windowChanged(m_attachee->window());
460 } else {
461 QQuickWindow *window = qobject_cast<QQuickWindow*>(attachee);
462 if (window)
463 windowChanged(window);
464 }
465
466 if (!m_screen)
467 screenChanged(QGuiApplication::primaryScreen());
468}
469
470int QQuickScreenAttached::angleBetween(int a, int b)
471{
472 if (!m_screen)
473 return Qt::PrimaryOrientation;
474 return m_screen->angleBetween((Qt::ScreenOrientation)a,(Qt::ScreenOrientation)b);
475}
476
477void QQuickScreenAttached::windowChanged(QQuickWindow* c)
478{
479 if (m_window)
480 qmlobject_disconnect(m_window, QQuickWindow, SIGNAL(screenChanged(QScreen*)), this, QQuickScreenAttached, SLOT(screenChanged(QScreen*)));
481 m_window = c;
482 screenChanged(c ? c->screen() : nullptr);
483 if (c)
484 qmlobject_connect(c, QQuickWindow, SIGNAL(screenChanged(QScreen*)), this, QQuickScreenAttached, SLOT(screenChanged(QScreen*)));
485}
486
487void QQuickScreenAttached::screenChanged(QScreen *screen)
488{
489 //qDebug() << "QQuickScreenAttached::screenChanged" << (screen ? screen->name() : QString::fromLatin1("null"));
490 if (screen != m_screen)
491 setWrappedScreen(screen);
492}
493
494QT_END_NAMESPACE
495
496#include "moc_qquickscreen_p.cpp"