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 <QtQuick/private/qquickapplication_p.h>
5
6#include <QtGui/private/qguiapplication_p.h>
7#include <QtGui/qpa/qplatformintegration.h>
8#include <QtGui/qguiapplication.h>
9
10#include <QtQml/private/qqmlglobal_p.h>
11
12#include <QtCore/private/qobject_p.h>
13#include <QtCore/qdebug.h>
14
15QT_BEGIN_NAMESPACE
16
17/*!
18 \qmltype Application
19 \instantiates QQuickApplication
20 \inqmlmodule QtQuick
21 //! once exposed: \inherits CoreApplication?
22 //! TODO: \ingroup ?
23
24 \brief Provides access to global application
25 state properties shared by many QML components.
26
27 The Application singleton exposes a subset of QApplication's properties to
28 QML applications.
29
30 It also provides an aboutToQuit() signal, which is the same as
31 QCoreApplication::aboutToQuit().
32
33 \qml
34 import QtQuick
35
36 Window {
37 id: root
38 visible: true
39 width: 800
40 height: 680
41
42 title: `${Application.name} (${Application.version})`
43
44 Connections {
45 target: Application
46 function onAboutToQuit() {
47 console.log("Bye!")
48 }
49 }
50 }
51 \endqml
52
53 \sa SystemPalette
54*/
55
56/*!
57 \qmlproperty bool Application::active
58 \deprecated [5.2]
59
60 Returns whether the application is active.
61 Use Application.state == Qt.ApplicationActive instead
62*/
63
64/*!
65 \qmlproperty Qt::ApplicationState Application::state
66
67 This property represents the current state of the application.
68
69 \qml
70 Timer {
71 interval: 1000; repeat: true
72 active: Application.state === Qt.Qt.ApplicationActive
73 onTriggered: imageFetcher.fetchLatestImages()
74 }
75 \endqml
76*/
77
78/*!
79 \qmlproperty Qt::LayoutDirection Application::layoutDirection
80
81 This read-only property can be used to query the default layout
82 direction of the application. On system start-up, the default layout
83 direction depends on the application's language. The property has a
84 value of \c Qt.RightToLeft in locales where text and graphic elements
85 are read from right to left, and \c Qt.LeftToRight where the reading
86 direction flows from left to right. You can bind to this property to
87 customize your application layouts to support both layout directions.
88
89 \qml
90 RowLayout {
91 layoutDirection: Application.layoutDirection
92 }
93 \endqml
94*/
95
96/*!
97 \qmlproperty bool Application::supportsMultipleWindows
98
99 Returns \c true if the platform supports multiple windows. Some embedded
100 platforms do not support multiple windows, for example.
101 */
102
103/*!
104 \qmlproperty QFont Application::font
105 Returns the default application font as returned by
106 \l QGuiApplication::font().
107*/
108
109
110/*!
111 \qmlproperty QString Application::displayName
112
113 This property represents the application display name set on the
114 QGuiApplication instance. This property can be written to in order to set
115 the application display name.
116
117 \qml
118 Binding {
119 target: Application
120 property: "displayName"
121 value: "My Awesome Application"
122 }
123 \endqml
124*/
125
126/*!
127 \qmlproperty QQmlListProperty<QQuickScreenInfo> Application::screens
128
129 An array containing the descriptions of all connected screens. The
130 elements of the array are objects with the same properties as the
131 \l{Screen} attached object. In practice the array corresponds to the screen
132 list returned by QGuiApplication::screens(). In addition to examining
133 properties like name, width, height, etc., the array elements can also be
134 assigned to the screen property of Window items, thus serving as an
135 alternative to the C++ side's QWindow::setScreen().
136
137 \sa Screen, Window, {Window::screen}{Window.screen}
138*/
139
140/* The following properties are from QQmlApplication.
141 ### Document those in QQmlApplication instead once it is exposed
142*/
143
144/*!
145 \qmlproperty QStringList Application::arguments
146
147 This is a string list of the arguments the executable was invoked with.
148 */
149
150/*!
151 \qmlproperty QString Application::name
152
153 This is the application name set on the QCoreApplication instance. This
154 property can be written to in order to set the application name.
155 */
156
157/*!
158 \qmlproperty QString Application::version
159
160 This is the application version set on the QCoreApplication instance. This
161 property can be written to in order to set the application version.
162 */
163
164/*!
165 \qmlproperty QString Application::organization
166
167 This is the organization name set on the QCoreApplication instance.
168 This property can be written to in order to set the organization name.
169 */
170
171/*!
172 \qmlproperty QString Application::domain
173
174 This is the organization domain set on the QCoreApplication instance.
175 This property can be written to in order to set the organization domain.
176 */
177
178/*!
179 \qmlproperty StyleHints Application::styleHints
180
181 The \c styleHints property provides platform-specific style hints and settings.
182 See the \l QStyleHints documentation for further details.
183
184 The following example uses \c styleHints to determine whether an
185 item should gain focus on mouse press or touch release:
186 \code
187 import QtQuick
188
189 MouseArea {
190 id: button
191
192 onPressed: {
193 if (!Application.styleHints.setFocusOnTouchRelease)
194 button.forceActiveFocus()
195 }
196 onReleased: {
197 if (Application.styleHints.setFocusOnTouchRelease)
198 button.forceActiveFocus()
199 }
200 }
201 \endcode
202 */
203
204/*!
205 \qmlsignal Application::aboutToQuit()
206
207 This signal is emitted when the application is about to quit the main
208 event loop. The signal is particularly useful if your application has to
209 do some last-second cleanup. User interaction is not possible in this state.
210 For more information, see \l {Window::closing()}{Window.closing}.
211
212 \sa QCoreApplication::aboutToQuit
213*/
214QQuickApplication::QQuickApplication(QObject *parent)
215 : QQmlApplication(parent)
216{
217 QCoreApplication *app = QCoreApplication::instance();
218 if (QGuiApplication *guiApp = qobject_cast<QGuiApplication *>(object: app)) {
219 connect(sender: guiApp, signal: &QGuiApplication::layoutDirectionChanged,
220 context: this, slot: &QQuickApplication::layoutDirectionChanged);
221 connect(sender: guiApp, signal: &QGuiApplication::applicationStateChanged,
222 context: this, slot: &QQuickApplication::stateChanged);
223 connect(sender: guiApp, signal: &QGuiApplication::applicationStateChanged,
224 context: this, slot: &QQuickApplication::activeChanged);
225 connect(sender: guiApp, signal: &QGuiApplication::applicationDisplayNameChanged,
226 context: this, slot: &QQuickApplication::displayNameChanged);
227
228 connect(sender: guiApp, signal: &QGuiApplication::primaryScreenChanged, context: this, slot: &QQuickApplication::updateScreens);
229 connect(sender: guiApp, signal: &QGuiApplication::screenAdded, context: this, slot: &QQuickApplication::updateScreens);
230 connect(sender: guiApp, signal: &QGuiApplication::screenRemoved, context: this, slot: &QQuickApplication::updateScreens);
231 updateScreens();
232 }
233}
234
235QQuickApplication::~QQuickApplication()
236{
237}
238
239bool QQuickApplication::active() const
240{
241 return QGuiApplication::applicationState() == Qt::ApplicationActive;
242}
243
244Qt::LayoutDirection QQuickApplication::layoutDirection() const
245{
246 return QGuiApplication::layoutDirection();
247}
248
249bool QQuickApplication::supportsMultipleWindows() const
250{
251 return QGuiApplicationPrivate::platformIntegration()->hasCapability(cap: QPlatformIntegration::MultipleWindows);
252}
253
254Qt::ApplicationState QQuickApplication::state() const
255{
256 return QGuiApplication::applicationState();
257}
258
259QFont QQuickApplication::font() const
260{
261 return QGuiApplication::font();
262}
263
264QString QQuickApplication::displayName() const
265{
266 return QGuiApplication::applicationDisplayName();
267}
268
269QStyleHints *QQuickApplication::styleHints()
270{
271 return QGuiApplication::styleHints();
272}
273
274void QQuickApplication::setDisplayName(const QString &displayName)
275{
276 return QGuiApplication::setApplicationDisplayName(displayName);
277}
278
279qsizetype screens_count(QQmlListProperty<QQuickScreenInfo> *prop)
280{
281 return static_cast<QVector<QQuickScreenInfo *> *>(prop->data)->size();
282}
283
284QQuickScreenInfo *screens_at(QQmlListProperty<QQuickScreenInfo> *prop, qsizetype idx)
285{
286 return static_cast<QVector<QQuickScreenInfo *> *>(prop->data)->at(i: idx);
287}
288
289QQmlListProperty<QQuickScreenInfo> QQuickApplication::screens()
290{
291 return QQmlListProperty<QQuickScreenInfo>(this,
292 const_cast<QVector<QQuickScreenInfo *> *>(&m_screens), &screens_count, &screens_at);
293}
294
295void QQuickApplication::updateScreens()
296{
297 const QList<QScreen *> screenList = QGuiApplication::screens();
298 m_screens.resize(size: screenList.size());
299 for (int i = 0; i < screenList.size(); ++i) {
300 if (!m_screens[i])
301 m_screens[i] = new QQuickScreenInfo(this);
302 m_screens[i]->setWrappedScreen(screenList[i]);
303 }
304 emit screensChanged();
305}
306
307QT_END_NAMESPACE
308
309#include "moc_qquickapplication_p.cpp"
310

source code of qtdeclarative/src/quick/util/qquickapplication.cpp