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

From Maemo Wiki
Jump to navigationJump to search

How to use Maemo 5 specific APIs from Qt application

Accelometer

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 more frequent updates are needed, for example, 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, <source lang="cpp-qt"> 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)));

} </source> processes answers in `mceOrientationSlot', and emits data needed by the application with the following signal: <source lang="cpp-qt"> void deviceOrientation(qreal pitch, qreal roll); </source>

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. <source lang="cpp-qt"> DeviceOrientationNotifier notifier(100); SceneTuner tuner(&scene, TimerInterval); QObject::connect(&notifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal))); </source> Based on that each mouse can take into account the gravity effect, see `Mouse::advance' for details.

Using Zoom In and Zoom Out keys

This section uses the same qt-maemo-gravity-example to explain how to use Zoom In and Out keys in a Qt application. They are used to set the application window to fullscreen and normal modes like in the GTK+ example Maemo Sliders.

First, the keys are released from maemo-status-volume by using XChangeProperty().

hardkeyhandler.cpp: <source lang="cpp-qt"> void HardkeyHandler::grabIncreaseDecreaseKeys(QWidget* window, bool grab) {

   // Tell maemo-status-volume to grab/ungrab increase/decrease keys
   unsigned long val = (grab==true)?1:0;
   Atom atom;
   atom = XInternAtom( QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", 0);
   XChangeProperty (QX11Info::display(),
                    window->winId(),
                    atom,
                    XA_INTEGER,
                    32,
                    PropModeReplace,
                    (unsigned char *) &val,
                    1);

} </source>

Next, a handler for keypresses is created. 73 and 74 are the keycodes for F7/F8 in the SDK and Zoom In/Out buttons in the device. Handler functions emit corresponding signals when called.

hardkeyhandler.cpp: <source lang="cpp-qt"> bool HardkeyHandler::processX11Event(XEvent* event) {

   if ( event->type == KeyPress )
   {
       if (event->xkey.keycode == 73 || event->xkey.keycode == QKeySequence::ZoomOut)
       {
           emit decreaseKeyPressed();
           return true;
       } 
       else if (event->xkey.keycode == 74  || event->xkey.keycode ==  QKeySequence::ZoomIn)
       {
           emit increaseKeyPressed();
           return true;
       }
   }
   return false;

} </source>

Signals need slots, of course. They are located in myapplication.h and myapplication.cpp files. Here changing to fullscreen and back are done by showFullScreen() and showNormal().

myapplication.cpp: <source lang="cpp-qt"> void MyApplication::increaseKeyPressed() {

   qDebug("MyApplication::increaseKeyPressed");
   view.showFullScreen();

}

void MyApplication::decreaseKeyPressed() {

   qDebug("MyApplication::decreaseKeyPressed");
   view.showNormal();

} </source>

The new signals are connected to corresponding slots in main().

main.cpp: <source lang="cpp-qt"> QObject::connect(app.keyHandler(), SIGNAL(decreaseKeyPressed()), &app, SLOT(decreaseKeyPressed())); QObject::connect(app.keyHandler(), SIGNAL(increaseKeyPressed()), &app, SLOT(increaseKeyPressed())); </source>

Finally, gtk-x11 library and the new source and header files (hardkeyhandler.* and myapplication.*) are added to the Qt project file:

collidingmice-gravity.pro:

LIBS += -ldevornot-qt-maemo -L/usr/local/lib -lgtk-x11-2.0

HEADERS += \
    mouse.h \
    hardkeyhandler.h \
    myapplication.h \
    scenetuner.h
SOURCES += \
    main.cpp \
    hardkeyhandler.cpp \
    myapplication.cpp \
    mouse.cpp