Added declarative components for compass and orientation.
[harmattan/cameraplus] / src / compass.cpp
1 #include "compass.h"
2 #include <qmcompass.h>
3 #include <QDebug>
4
5 Compass::Compass(QObject *parent) :
6   QObject(parent),
7   m_compass(new MeeGo::QmCompass(this)),
8   m_degree(-1),
9   m_valid(false) {
10
11   m_compass->setUseDeclination(true);
12
13   QObject::connect(m_compass, SIGNAL(dataAvailable(const MeeGo::QmCompassReading&)),
14                    this, SLOT(dataAvailable(const MeeGo::QmCompassReading&)));
15
16   if (m_compass->requestSession(MeeGo::QmSensor::SessionTypeListen)
17       == MeeGo::QmSensor::SessionTypeNone) {
18     qDebug() << "Failed to get listen session:" << m_compass->lastError();
19   }
20 }
21
22 Compass::~Compass() {
23   m_compass->stop();
24 }
25
26 bool Compass::isActive() const {
27   return m_compass->isRunning();
28 }
29
30 void Compass::setActive(bool active) {
31   if (active == isActive()) {
32     return;
33   }
34
35   if (active) {
36     m_compass->start();
37   }
38   else {
39     m_compass->stop();
40
41     m_valid = false;
42     emit directionValidChanged();
43   }
44
45   emit activeChanged();
46 }
47
48 int Compass::direction() const {
49   return m_degree;
50 }
51
52 bool Compass::isDirectionValid() const {
53   return m_valid;
54 }
55
56 void Compass::dataAvailable(const MeeGo::QmCompassReading& value) {
57   bool degreeChanged = (m_degree != value.degrees);
58   bool validityChanged = m_valid != (value.level >= 2);
59
60   m_degree = value.degrees;
61   m_valid = (value.level >= 2);
62
63   if (validityChanged) {
64     emit directionValidChanged();
65   }
66
67   if (degreeChanged) {
68     emit directionChanged();
69   }
70 }