{"id":148,"date":"2010-02-01T01:29:50","date_gmt":"2010-02-01T00:29:50","guid":{"rendered":"http:\/\/pusling.com\/blog\/?p=148"},"modified":"2016-07-10T14:35:22","modified_gmt":"2016-07-10T13:35:22","slug":"toying-with-maemo-and-rotating-apps","status":"publish","type":"post","link":"https:\/\/pusling.com\/blog\/?p=148","title":{"rendered":"Toying with maemo and rotating apps"},"content":{"rendered":"<p>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 &#8216;normal&#8217; Qt, outside Maemo.<\/p>\n<p>Have fun. Available under any license.<\/p>\n<p>maemomainwindow.h:<\/p>\n<pre>#ifndef MAEMOMAINWINDOW_H                                          \r\n#define MAEMOMAINWINDOW_H                                          \r\n\r\n#include <QMainWindow>\r\n\r\nclass MaemoMainWindow : public QMainWindow\r\n{                                         \r\n  Q_OBJECT                                \r\n  public:                                 \r\n    MaemoMainWindow (QWidget* parent = 0, Qt::WindowFlags flags = 0);\r\n    virtual ~MaemoMainWindow();                                      \r\n  protected:                                                         \r\n    virtual bool event (QEvent* event);                              \r\n  Q_SIGNALS:                                                         \r\n    void orientationChanged(Qt::Orientation newOrientation);         \r\n  private Q_SLOTS:                                                   \r\n    void orientationChangedSlot(const QString& newOrientation);      \r\n  private:                                                           \r\n    Qt::Orientation m_orientation;                                   \r\n};                                                                   \r\n\r\n#endif \/\/ MAEMOMAINWINDOW_H\r\n<\/pre>\n<p>maemomainwindow.cpp:<\/p>\n<pre>#include \"maemomainwindow.h\"                                         \r\n\r\n#ifdef Q_WS_MAEMO_5\r\n#include <mce\/mode-names.h>\r\n#include <mce\/dbus-names.h>\r\n#endif                     \r\n\r\n#ifdef Q_WS_MAEMO_5\r\n#include <QDBusConnection>\r\n#include <QDBusMessage>\r\n#include <QEvent>\r\n#endif\r\n\r\n\r\n\r\nMaemoMainWindow::MaemoMainWindow (QWidget* parent, Qt::WindowFlags flags) : QMainWindow (parent, flags) {\r\n#ifdef Q_WS_MAEMO_5\r\n  QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF,\r\n                                       MCE_DEVICE_ORIENTATION_SIG,\r\n                                       this,\r\n                                       SLOT(orientationChangedSlot(QString)));\r\n#endif\r\n}\r\n\r\n\r\nMaemoMainWindow::~MaemoMainWindow() {\r\n\r\n}\r\n\r\n\r\nvoid MaemoMainWindow::orientationChangedSlot (const QString& newOrientation) {\r\n#ifdef Q_WS_MAEMO_5\r\n  if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT)) {\r\n    setAttribute(Qt::WA_Maemo5ForcePortraitOrientation, true);\r\n    emit orientationChanged(Qt::Vertical);\r\n  } else {\r\n    setAttribute(Qt::WA_Maemo5ForceLandscapeOrientation, true);\r\n    emit orientationChanged(Qt::Horizontal);\r\n  }\r\n#else\r\nQ_UNUSED(newOrientation);\r\n#endif\r\n\r\n}\r\n\r\n\r\n\r\n\r\nbool MaemoMainWindow::event (QEvent* event) {\r\n#ifdef Q_WS_MAEMO_5\r\n  switch (event->type()) {\r\n    case QEvent::WindowActivate:\r\n      QDBusConnection::systemBus().call(\r\n           QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,\r\n                                          MCE_REQUEST_IF,\r\n                                          MCE_ACCELEROMETER_ENABLE_REQ));\r\n      break;\r\n    case QEvent::WindowDeactivate:\r\n      QDBusConnection::systemBus().call(\r\n           QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,\r\n                                          MCE_REQUEST_IF,\r\n                                          MCE_ACCELEROMETER_DISABLE_REQ));\r\n      break;\r\n    default:\r\n      break;\r\n  }\r\n#endif\r\n  return QMainWindow::event (event);\r\n}\r\n<\/pre>\n<p>Update: reformatted code. Thought wordpres and &lt;code&gt; was smart. Thanks Ken.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8216;normal&#8217; Qt, outside [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,8],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-english","category-kde","category-maemo"],"_links":{"self":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=148"}],"version-history":[{"count":4,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":437,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions\/437"}],"wp:attachment":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}