Documentation/Maemo 5 Developer Guide/Development Environment/Maemo Programming Environments/Using Maemo 5 specific APIs in Qt application

(synced with qt-maemo-gravity-example r309)
m (changed code indentation, added some articles, and changed some prepositions)
Line 1: Line 1:
== Introduction ==
== Introduction ==
-
This section explains how to use [[Accelerometers | accelerometer]] D-Bus interface in Maemo 5 Qt application.
+
This section explains how to use the [[Accelerometers | accelerometer]] D-Bus interface in a Maemo 5 Qt application.
The following code examples are used in this section:
The following code examples are used in this section:
Line 8: Line 8:
== D-Bus interface ==
== D-Bus interface ==
-
It's up to each application how to organize interactions with [http://maemo.org/api_refs/5.0/beta/mce-dev/ MCE] orientation interface. In [[Documentation/Maemo 5 Developer Guide/Porting Software/Porting Existing GTK+ Application to Maemo 5#Portrait Mode | some cases]] catching orientation signal can be enough, but if e.g. more frequent updates are needed you might want to use method calls, that demonstrated in our example.
+
It is up to each application how to organize interactions with the [http://maemo.org/api_refs/5.0/beta/mce-dev/ MCE] orientation interface. In [[Documentation/Maemo 5 Developer Guide/Porting Software/Porting Existing GTK+ Application to Maemo 5#Portrait Mode | some cases]] catching orientation signal can be enough, but if, e.g., more frequent updates are needed, you might want to use method calls, as demonstrated in our example.
-
We've moved D-Bus related functionality and orientation based calculations to separate library, that implements `DeviceOrientationNotifier' class.  
+
We've moved D-Bus related functionality and orientation based calculations to a separate library that implements `DeviceOrientationNotifier' class.  
-
Our notifier asks MCE about orientation parameters with certain period,  
+
Our notifier asks MCE about orientation parameters with a certain period,  
  void DeviceOrientationNotifier::timerEvent(QTimerEvent *)
  void DeviceOrientationNotifier::timerEvent(QTimerEvent *)
  {
  {
-
   QDBusMessage msg = QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET);
+
   QDBusMessage msg = QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF,  
-
   bool sent = QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(mceOrientationSlot(QString, QString, QString, int, int, int)));
+
                                                    MCE_DEVICE_ORIENTATION_GET);
 +
   bool sent = QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(mceOrientationSlot(QString, QString, QString,  
 +
                                                                                                int, int, int)));
  }
  }
-
processes answers in `mceOrientationSlot' and emits data needed by application with following signal.
+
processes answers in `mceOrientationSlot', and emits data needed by the application with the following signal.
  void deviceOrientation(qreal pitch, qreal roll);
  void deviceOrientation(qreal pitch, qreal roll);
Line 22: Line 24:
We've modified [http://qt.nokia.com/doc/4.5/graphicsview-collidingmice.html one of Qt4 Examples] in order to show how device orientation can be used.
We've modified [http://qt.nokia.com/doc/4.5/graphicsview-collidingmice.html one of Qt4 Examples] in order to show how device orientation can be used.
-
In our case pitch and roll are scene's characteristics, so we've implemented `SceneTuner', that's slot can be connected to appropriate signal in order to setup these scene's properties.
+
In our case pitch and roll are scene's characteristics, so we've implemented a `SceneTuner', whose slot can be connected to an appropriate signal in order to setup these scene properties.
  DeviceOrientationNotifier notifier(100);
  DeviceOrientationNotifier notifier(100);
  SceneTuner tuner(&scene, TimerInterval);
  SceneTuner tuner(&scene, TimerInterval);
  QObject::connect(&notifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal)));
  QObject::connect(&notifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal)));
-
Based on that each mouse can take into account gravity effect, see `Mouse::advance' for details.
+
Based on that each mouse can take into account the gravity effect, see `Mouse::advance' for details.

Revision as of 10:42, 9 September 2009

Introduction

This section explains how to use the accelerometer D-Bus interface in a Maemo 5 Qt application.

The following code examples are used in this section:

D-Bus interface

It is up to each application how to organize interactions with the MCE orientation interface. In some cases catching orientation signal can be enough, but if, e.g., more frequent updates are needed, you might want to use method calls, as demonstrated in our example. We've moved D-Bus related functionality and orientation based calculations to a separate library that implements `DeviceOrientationNotifier' class. Our notifier asks MCE about orientation parameters with a certain period,

void DeviceOrientationNotifier::timerEvent(QTimerEvent *)
{
  QDBusMessage msg = QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, 
                                                    MCE_DEVICE_ORIENTATION_GET);
  bool sent = QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(mceOrientationSlot(QString, QString, QString, 
                                                                                               int, int, int)));
}

processes answers in `mceOrientationSlot', and emits data needed by the application with the following signal.

void deviceOrientation(qreal pitch, qreal roll);

Example application

We've modified one of Qt4 Examples in order to show how device orientation can be used. In our case pitch and roll are scene's characteristics, so we've implemented a `SceneTuner', whose slot can be connected to an appropriate signal in order to setup these scene properties.

DeviceOrientationNotifier notifier(100);
SceneTuner tuner(&scene, TimerInterval);
QObject::connect(&notifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal)));

Based on that each mouse can take into account the gravity effect, see `Mouse::advance' for details.