Archive for the ‘maemo’ Category

Toying with maemo and rotating apps

Monday, February 1st, 2010

I have been playing a bit around with Maemo and writing Qt apps for n900. I ended up needing a rotation aware QMainWindow a couple of times, so I ended up abstracting it away in my own MaemoMainWindow, which I just wanted to share. It has enough ifdefs to also build against ‘normal’ Qt, outside Maemo.

Have fun. Available under any license.

maemomainwindow.h:

#ifndef MAEMOMAINWINDOW_H
#define MAEMOMAINWINDOW_H                                          

#include <QMainWindow>

class MaemoMainWindow : public QMainWindow
{
  Q_OBJECT
  public:
    MaemoMainWindow (QWidget* parent = 0, Qt::WindowFlags flags = 0);
    virtual ~MaemoMainWindow();
  protected:
    virtual bool event (QEvent* event);
  Q_SIGNALS:
    void orientationChanged(Qt::Orientation newOrientation);
  private Q_SLOTS:
    void orientationChangedSlot(const QString& newOrientation);
  private:
    Qt::Orientation m_orientation;
};                                                                   

#endif // MAEMOMAINWINDOW_H

maemomainwindow.cpp:

#include "maemomainwindow.h"                                         

#ifdef Q_WS_MAEMO_5
#include <mce/mode-names.h>
#include <mce/dbus-names.h>
#endif                     

#ifdef Q_WS_MAEMO_5
#include <QDBusConnection>
#include <QDBusMessage>
#include <QEvent>
#endif

MaemoMainWindow::MaemoMainWindow (QWidget* parent, Qt::WindowFlags flags) : QMainWindow (parent, flags) {
#ifdef Q_WS_MAEMO_5
  QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
                                       MCE_DEVICE_ORIENTATION_SIG,
                                       this,
                                       SLOT(orientationChangedSlot(QString)));
#endif
}

MaemoMainWindow::~MaemoMainWindow() {

}

void MaemoMainWindow::orientationChangedSlot (const QString& newOrientation) {
#ifdef Q_WS_MAEMO_5
  if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT)) {
    setAttribute(Qt::WA_Maemo5ForcePortraitOrientation, true);
    emit orientationChanged(Qt::Vertical);
  } else {
    setAttribute(Qt::WA_Maemo5ForceLandscapeOrientation, true);
    emit orientationChanged(Qt::Horizontal);
  }
#else
Q_UNUSED(newOrientation);
#endif

}

bool MaemoMainWindow::event (QEvent* event) {
#ifdef Q_WS_MAEMO_5
  switch (event->type()) {
    case QEvent::WindowActivate:
      QDBusConnection::systemBus().call(
           QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
                                          MCE_REQUEST_IF,
                                          MCE_ACCELEROMETER_ENABLE_REQ));
      break;
    case QEvent::WindowDeactivate:
      QDBusConnection::systemBus().call(
           QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
                                          MCE_REQUEST_IF,
                                          MCE_ACCELEROMETER_DISABLE_REQ));
      break;
    default:
      break;
  }
#endif
  return QMainWindow::event (event);
}

Update: reformatted code. Thought wordpres and <code> was smart. Thanks Ken.