Commit 693261e3 authored by Andrew Flegg's avatar Andrew Flegg

Patch from Thomas Perl for exposing visibility notifications to QML.

parent 282e016f
#ifndef _ACTIVEMONITOR_H
#define _ACTIVEMONITOR_H
#include <QObject>
/**
* Simple QObject with a single NOTIFYable, read-only property
* that can be used inside QML to read application active status.
*
* The slot activeChanged(bool) should be connected to the
* activeChanged(bool) signal of the modified QmlApplicationViewer.
**/
class ActiveMonitor : public QObject
{
Q_OBJECT
private:
bool _active;
public:
ActiveMonitor(QObject *parent) : QObject(parent), _active(true) {}
bool active() { return _active; }
Q_PROPERTY(bool active READ active NOTIFY changed)
public slots:
void activeChanged(bool active) {
if (_active != active) {
_active = active;
emit changed();
}
}
signals:
void changed();
};
#endif
......@@ -29,4 +29,4 @@ SOURCES += main.cpp
include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()
HEADERS +=
HEADERS += activemonitor.h
#include <QtGui/QApplication>
#include <QtDeclarative>
#include "qmlapplicationviewer.h"
#include "activemonitor.h"
#include <stdio.h>
int main(int argc, char *argv[])
......@@ -8,6 +9,19 @@ int main(int argc, char *argv[])
QApplication app(argc, argv);
QmlApplicationViewer viewer;
/**
* Qt Quick 1.0 does not have the possibility to tell the QML if
* the application is in the foreground or not. To fix this, we
* monitor the changes manually in the viewer and expose a new
* ActiveMonitor object that has an "active" property to the QML
* context. Inside QML, use "activeMonitor.active" as boolean.
**/
ActiveMonitor *monitor = new ActiveMonitor(&viewer);
QObject::connect(&viewer, SIGNAL(activeChanged(bool)),
monitor, SLOT(activeChanged(bool)));
viewer.rootContext()->setContextProperty("activeMonitor", monitor);
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer.engine()->addImportPath(QString("/opt/qtm11/imports"));
viewer.setWindowTitle("Attitude");
......
......@@ -5,6 +5,12 @@ Item {
width: 800
height: 480
property bool windowActive: activeMonitor.active
onWindowActiveChanged: {
/* TODO: Disable timers, updates, etc.. */
console.log('window active changed:' + activeMonitor.active)
}
/**
* Contains the ground and the sky, and the pivot around which
* we rotate.
......
......@@ -154,3 +154,25 @@ void QmlApplicationViewer::showExpanded()
show();
#endif
}
bool
QmlApplicationViewer::event(QEvent *event)
{
switch (event->type()) {
case QEvent::Leave:
case QEvent::WindowDeactivate:
/* Window was sent to the background */
emit activeChanged(false);
break;
case QEvent::Enter:
case QEvent::WindowActivate:
/* Window is again in the foreground */
emit activeChanged(true);
break;
default:
break;
}
/* Handle all events by the parent class */
return QDeclarativeView::event(event);
}
......@@ -35,8 +35,14 @@ public:
void showExpanded();
bool event(QEvent *event);
private:
class QmlApplicationViewerPrivate *m_d;
signals:
/* Support for application active signals (see ActiveMonitor) */
void activeChanged(bool active);
};
#endif // QMLAPPLICATIONVIEWER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment