diff options
author | Yoann Lopes <yoann.lopes@digia.com> | 2013-08-28 19:16:41 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-02 16:26:21 +0200 |
commit | 040fa04882b2050e73e74459d8ca06f72e6f95c5 (patch) | |
tree | 6d5724bb0735178dbed8cb096cb0f063bdad87c1 | |
parent | e040a2733184a00ca4951269e0a3e3733ffaa377 (diff) |
Added applicationState() to QGuiApplication.
We only had the QApplicationStateChangeEvent to know about the
application state. Added a function and a signal to offer a more
convenient way to query the current state.
Change-Id: I926aac0b3b53bd285df5825aff5b4c37ae863d03
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
-rw-r--r-- | src/corelib/global/qnamespace.h | 1 | ||||
-rw-r--r-- | src/gui/kernel/qguiapplication.cpp | 87 | ||||
-rw-r--r-- | src/gui/kernel/qguiapplication.h | 3 | ||||
-rw-r--r-- | src/gui/kernel/qguiapplication_p.h | 4 |
4 files changed, 61 insertions, 34 deletions
diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h index 9b63166474..81329fd5ba 100644 --- a/src/corelib/global/qnamespace.h +++ b/src/corelib/global/qnamespace.h @@ -89,6 +89,7 @@ Qt { Q_ENUMS(ScreenOrientation) Q_FLAGS(ScreenOrientations) Q_ENUMS(ConnectionType) + Q_ENUMS(ApplicationState) #ifndef QT_NO_GESTURES Q_ENUMS(GestureState) Q_ENUMS(GestureType) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index eed1c41bfa..4c2adef6de 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1418,7 +1418,7 @@ void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePriv QGuiApplicationPrivate::processWindowScreenChangedEvent(static_cast<QWindowSystemInterfacePrivate::WindowScreenChangedEvent *>(e)); break; case QWindowSystemInterfacePrivate::ApplicationStateChanged: - QGuiApplicationPrivate::processApplicationStateChangedEvent(static_cast<QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *>(e)); + QGuiApplicationPrivate::setApplicationState(static_cast<QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *>(e)->newState); break; case QWindowSystemInterfacePrivate::FlushEvents: QWindowSystemInterface::deferredFlushWindowSystemEvents(); @@ -1748,10 +1748,7 @@ void QGuiApplicationPrivate::processActivatedEvent(QWindowSystemInterfacePrivate QObject::disconnect(previous, SIGNAL(focusObjectChanged(QObject*)), qApp, SLOT(_q_updateFocusObject(QObject*))); } else if (!platformIntegration()->hasCapability(QPlatformIntegration::ApplicationState)) { - QEvent appActivate(QEvent::ApplicationActivate); - qApp->sendSpontaneousEvent(qApp, &appActivate); - QApplicationStateChangeEvent appState(Qt::ApplicationActive); - qApp->sendSpontaneousEvent(qApp, &appState); + setApplicationState(Qt::ApplicationActive); } if (QGuiApplicationPrivate::focus_window) { @@ -1760,10 +1757,7 @@ void QGuiApplicationPrivate::processActivatedEvent(QWindowSystemInterfacePrivate QObject::connect(QGuiApplicationPrivate::focus_window, SIGNAL(focusObjectChanged(QObject*)), qApp, SLOT(_q_updateFocusObject(QObject*))); } else if (!platformIntegration()->hasCapability(QPlatformIntegration::ApplicationState)) { - QEvent appActivate(QEvent::ApplicationDeactivate); - qApp->sendSpontaneousEvent(qApp, &appActivate); - QApplicationStateChangeEvent appState(Qt::ApplicationInactive); - qApp->sendSpontaneousEvent(qApp, &appState); + setApplicationState(Qt::ApplicationInactive); } if (self) { @@ -1799,29 +1793,6 @@ void QGuiApplicationPrivate::processWindowScreenChangedEvent(QWindowSystemInterf } } -void QGuiApplicationPrivate::processApplicationStateChangedEvent(QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *e) -{ - if (e->newState == applicationState) - return; - applicationState = e->newState; - - switch (e->newState) { - case Qt::ApplicationActive: { - QEvent appActivate(QEvent::ApplicationActivate); - qApp->sendSpontaneousEvent(qApp, &appActivate); - break; } - case Qt::ApplicationInactive: { - QEvent appDeactivate(QEvent::ApplicationDeactivate); - qApp->sendSpontaneousEvent(qApp, &appDeactivate); - break; } - default: - break; - } - - QApplicationStateChangeEvent event(applicationState); - qApp->sendSpontaneousEvent(qApp, &event); -} - void QGuiApplicationPrivate::processThemeChanged(QWindowSystemInterfacePrivate::ThemeChangeEvent *tce) { if (self) @@ -2632,6 +2603,58 @@ bool QGuiApplicationPrivate::shouldQuitInternal(const QWindowList &processedWind } /*! + \since 5.2 + \fn Qt::ApplicationState QGuiApplication::applicationState() + + + Returns the current state of the application. + + You can react to application state changes to perform actions such as + stopping/resuming CPU-intensive tasks, freeing/loading resources or + saving/restoring application data. + */ + +Qt::ApplicationState QGuiApplication::applicationState() +{ + return QGuiApplicationPrivate::applicationState; +} + +/*! + \since 5.2 + \fn void QGuiApplication::applicationStateChanged(Qt::ApplicationState state) + + This signal is emitted when the \a state of the application changes. + + \sa applicationState() +*/ + +void QGuiApplicationPrivate::setApplicationState(Qt::ApplicationState state) +{ + if (applicationState == state) + return; + + applicationState = state; + + switch (state) { + case Qt::ApplicationActive: { + QEvent appActivate(QEvent::ApplicationActivate); + QCoreApplication::sendSpontaneousEvent(qApp, &appActivate); + break; } + case Qt::ApplicationInactive: { + QEvent appDeactivate(QEvent::ApplicationDeactivate); + QCoreApplication::sendSpontaneousEvent(qApp, &appDeactivate); + break; } + default: + break; + } + + QApplicationStateChangeEvent event(applicationState); + QCoreApplication::sendSpontaneousEvent(qApp, &event); + + emit qApp->applicationStateChanged(applicationState); +} + +/*! \since 4.2 \fn void QGuiApplication::commitDataRequest(QSessionManager &manager) diff --git a/src/gui/kernel/qguiapplication.h b/src/gui/kernel/qguiapplication.h index 0e9d6f2336..a0aef83eed 100644 --- a/src/gui/kernel/qguiapplication.h +++ b/src/gui/kernel/qguiapplication.h @@ -142,6 +142,8 @@ public: static void setQuitOnLastWindowClosed(bool quit); static bool quitOnLastWindowClosed(); + static Qt::ApplicationState applicationState(); + static int exec(); bool notify(QObject *, QEvent *); @@ -159,6 +161,7 @@ Q_SIGNALS: void lastWindowClosed(); void focusObjectChanged(QObject *focusObject); void focusWindowChanged(QWindow *focusWindow); + void applicationStateChanged(Qt::ApplicationState state); #ifndef QT_NO_SESSIONMANAGER void commitDataRequest(QSessionManager &sessionManager); void saveStateRequest(QSessionManager &sessionManager); diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index 11f68ba2ff..79e826f330 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -130,8 +130,6 @@ public: static void processWindowStateChangedEvent(QWindowSystemInterfacePrivate::WindowStateChangedEvent *e); static void processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e); - static void processApplicationStateChangedEvent(QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *e); - static void processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *e); static void updateFilteredScreenOrientation(QScreen *screen); @@ -277,6 +275,8 @@ public: static QRect applyWindowGeometrySpecification(const QRect &windowGeometry, const QWindow *window); + static void setApplicationState(Qt::ApplicationState state); + protected: virtual void notifyThemeChanged(); #ifndef QT_NO_DRAGANDDROP |