Added declarative components for compass and orientation.
authorMohammed Sameer <msameer@foolab.org>
Sun, 16 Sep 2012 18:10:27 +0000 (21:10 +0300)
committerMohammed Sameer <msameer@foolab.org>
Sun, 16 Sep 2012 18:10:27 +0000 (21:10 +0300)
QtMobility compass cannot be used because we need to switch on the declination correction

src/compass.cpp [new file with mode: 0644]
src/compass.h [new file with mode: 0644]
src/main.cpp
src/orientation.cpp [new file with mode: 0644]
src/orientation.h [new file with mode: 0644]
src/src.pro

diff --git a/src/compass.cpp b/src/compass.cpp
new file mode 100644 (file)
index 0000000..21b9613
--- /dev/null
@@ -0,0 +1,70 @@
+#include "compass.h"
+#include <qmcompass.h>
+#include <QDebug>
+
+Compass::Compass(QObject *parent) :
+  QObject(parent),
+  m_compass(new MeeGo::QmCompass(this)),
+  m_degree(-1),
+  m_valid(false) {
+
+  m_compass->setUseDeclination(true);
+
+  QObject::connect(m_compass, SIGNAL(dataAvailable(const MeeGo::QmCompassReading&)),
+                  this, SLOT(dataAvailable(const MeeGo::QmCompassReading&)));
+
+  if (m_compass->requestSession(MeeGo::QmSensor::SessionTypeListen)
+      == MeeGo::QmSensor::SessionTypeNone) {
+    qDebug() << "Failed to get listen session:" << m_compass->lastError();
+  }
+}
+
+Compass::~Compass() {
+  m_compass->stop();
+}
+
+bool Compass::isActive() const {
+  return m_compass->isRunning();
+}
+
+void Compass::setActive(bool active) {
+  if (active == isActive()) {
+    return;
+  }
+
+  if (active) {
+    m_compass->start();
+  }
+  else {
+    m_compass->stop();
+
+    m_valid = false;
+    emit directionValidChanged();
+  }
+
+  emit activeChanged();
+}
+
+int Compass::direction() const {
+  return m_degree;
+}
+
+bool Compass::isDirectionValid() const {
+  return m_valid;
+}
+
+void Compass::dataAvailable(const MeeGo::QmCompassReading& value) {
+  bool degreeChanged = (m_degree != value.degrees);
+  bool validityChanged = m_valid != (value.level >= 2);
+
+  m_degree = value.degrees;
+  m_valid = (value.level >= 2);
+
+  if (validityChanged) {
+    emit directionValidChanged();
+  }
+
+  if (degreeChanged) {
+    emit directionChanged();
+  }
+}
diff --git a/src/compass.h b/src/compass.h
new file mode 100644 (file)
index 0000000..dec590a
--- /dev/null
@@ -0,0 +1,44 @@
+// -*- c++ -*-
+
+#ifndef COMPASS_H
+#define COMPASS_H
+
+#include <QObject>
+
+namespace MeeGo {
+  class QmCompass;
+  class QmCompassReading;
+};
+
+class Compass : public QObject {
+  Q_OBJECT
+
+  Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged);
+  Q_PROPERTY(int direction READ direction NOTIFY directionChanged);
+  Q_PROPERTY(bool directionValid READ isDirectionValid NOTIFY directionValidChanged);
+
+public:
+  Compass(QObject *parent = 0);
+  ~Compass();
+
+  bool isActive() const;
+  void setActive(bool active);
+
+  int direction() const;
+  bool isDirectionValid() const;
+
+signals:
+  void activeChanged();
+  void directionChanged();
+  void directionValidChanged();
+
+private slots:
+  void dataAvailable(const MeeGo::QmCompassReading& value);
+
+private:
+  MeeGo::QmCompass *m_compass;
+  int m_degree;
+  bool m_valid;
+};
+
+#endif /* COMPASS_H */
index 816b399..873a11c 100644 (file)
@@ -13,6 +13,8 @@
 #include "displaystate.h"
 #include "fsmonitor.h"
 #include "cameraresources.h"
+#include "compass.h"
+#include "orientation.h"
 
 Q_DECL_EXPORT int main(int argc, char *argv[]) {
   QApplication::setAttribute(Qt::AA_X11InitThreads, true);
@@ -30,6 +32,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) {
   qmlRegisterType<DisplayState>("CameraPlus", 1, 0, "DisplayState");
   qmlRegisterType<FSMonitor>("CameraPlus", 1, 0, "FSMonitor");
   qmlRegisterType<CameraResources>("CameraPlus", 1, 0, "CameraResources");
+  qmlRegisterType<Compass>("CameraPlus", 1, 0, "Compass");
+  qmlRegisterType<Orientation>("CameraPlus", 1, 0, "Orientation");
 
   QUrl sourceUrl = QUrl::fromLocalFile(QDir::currentPath() + "/main.qml");
   view.setSource(sourceUrl);
diff --git a/src/orientation.cpp b/src/orientation.cpp
new file mode 100644 (file)
index 0000000..11bca25
--- /dev/null
@@ -0,0 +1,79 @@
+#include "orientation.h"
+#include <qmorientation.h>
+#include <QDebug>
+
+Orientation::Orientation(QObject *parent) :
+  QObject(parent),
+  m_orientation(new MeeGo::QmOrientation(this)),
+  m_direction(Unknown) {
+
+  QObject::connect(m_orientation, SIGNAL(orientationChanged(const MeeGo::QmOrientationReading&)),
+                  this, SLOT(orientationChanged(const MeeGo::QmOrientationReading&)));
+
+  if (m_orientation->requestSession(MeeGo::QmSensor::SessionTypeListen)
+      == MeeGo::QmSensor::SessionTypeNone) {
+    qDebug() << "Failed to get listen session:" << m_orientation->lastError();
+  }
+}
+
+Orientation::~Orientation() {
+  m_orientation->stop();
+}
+
+bool Orientation::isActive() const {
+  return m_orientation->isRunning();
+}
+
+void Orientation::setActive(bool active) {
+  if (active == isActive()) {
+    return;
+  }
+
+  if (active) {
+    m_orientation->start();
+    orientationChanged(m_orientation->orientation());
+  }
+  else {
+    m_orientation->stop();
+    m_direction = Unknown;
+
+    emit orientationChanged();
+  }
+
+  emit activeChanged();
+}
+
+Orientation::OrientationDirection Orientation::orientation() const {
+  return m_direction;
+}
+
+void Orientation::orientationChanged(const MeeGo::QmOrientationReading& value) {
+  OrientationDirection direction = Unknown;
+
+  switch (value.value) {
+  case MeeGo::QmOrientation::BottomUp:
+    direction = InvertedLandscape;
+    break;
+
+  case MeeGo::QmOrientation::BottomDown:
+    direction = Landscape;
+    break;
+
+  case MeeGo::QmOrientation::LeftUp:
+    direction = Portrait;
+    break;
+
+  case MeeGo::QmOrientation::RightUp:
+    direction = InvertedPortrait;
+    break;
+
+  default:
+    direction = Unknown;
+    break;
+  }
+
+  if (direction != m_direction) {
+    m_direction = direction;
+    emit orientationChanged();
+  }
+}
diff --git a/src/orientation.h b/src/orientation.h
new file mode 100644 (file)
index 0000000..201485e
--- /dev/null
@@ -0,0 +1,51 @@
+// -*- c++ -*-
+
+#ifndef ORIENTATION_H
+#define ORIENTATION_H
+
+#include <QObject>
+
+namespace MeeGo {
+  class QmOrientation;
+  class QmOrientationReading;
+};
+
+class Orientation : public QObject {
+  Q_OBJECT
+
+  Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged);
+  Q_PROPERTY(OrientationDirection orientation READ orientation NOTIFY orientationChanged);
+
+  Q_ENUMS(OrientationDirection);
+
+public:
+  Orientation(QObject *parent = 0);
+  ~Orientation();
+
+  // Make sure they are synced with metadata.
+  typedef enum {
+    Unknown = -1,
+    Landscape = 0,
+    Portrait = 1,
+    InvertedLandscape = 2,
+    InvertedPortrait = 3
+  } OrientationDirection;
+
+  bool isActive() const;
+  void setActive(bool active);
+
+  OrientationDirection orientation() const;
+
+signals:
+  void activeChanged();
+  void orientationChanged();
+
+private slots:
+  void orientationChanged(const MeeGo::QmOrientationReading& value);
+
+private:
+  MeeGo::QmOrientation *m_orientation;
+  OrientationDirection m_direction;
+};
+
+#endif /* ORIENTATION_H */
index b00eac8..51d5cc1 100644 (file)
@@ -14,7 +14,7 @@ PKGCONFIG = gstreamer-0.10 gstreamer-interfaces-0.10 gstreamer-video-0.10 gstrea
 LIBS +=  -L../imports/ -limports -L../lib/ -lqtcamera
 
 SOURCES += main.cpp settings.cpp filenaming.cpp quillitem.cpp displaystate.cpp fsmonitor.cpp \
-           cameraresources.cpp
+           cameraresources.cpp compass.cpp orientation.cpp
 
 HEADERS += settings.h filenaming.h quillitem.h displaystate.h fsmonitor.h \
-           cameraresources.h
+           cameraresources.h compass.h orientation.h