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