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>
bb10
Yoann Lopes 2013-08-28 19:16:41 +02:00 committed by The Qt Project
parent e040a27331
commit 040fa04882
4 changed files with 61 additions and 34 deletions

View File

@ -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)

View File

@ -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)
@ -2631,6 +2602,58 @@ bool QGuiApplicationPrivate::shouldQuitInternal(const QWindowList &processedWind
return true;
}
/*!
\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)

View File

@ -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);

View File

@ -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