Changes to night mode handling:
[harmattan/cameraplus] / imports / mode.cpp
1 #include "mode.h"
2 #include "qtcammode.h"
3 #include "camera.h"
4 #include "qtcamdevice.h"
5 #include "previewprovider.h"
6
7 Mode::Mode(QObject *parent) :
8   QObject(parent),
9   m_cam(0),
10   m_mode(0),
11   m_seq(0) {
12
13 }
14
15 Mode::~Mode() {
16   m_cam = 0;
17   m_mode = 0;
18 }
19
20 Camera *Mode::camera() {
21   return m_cam;
22 }
23
24 void Mode::setCamera(Camera *camera) {
25   if (camera == m_cam) {
26     return;
27   }
28
29   if (m_cam) {
30     QObject::disconnect(m_cam, SIGNAL(deviceChanged()), this, SLOT(deviceChanged()));
31     QObject::disconnect(m_cam, SIGNAL(deviceChanged()), this, SIGNAL(isReadyChanged()));
32   }
33
34   m_cam = camera;
35
36   if (m_cam) {
37     QObject::connect(m_cam, SIGNAL(deviceChanged()), this, SLOT(deviceChanged()));
38     QObject::connect(m_cam, SIGNAL(deviceChanged()), this, SIGNAL(isReadyChanged()));
39   }
40
41   emit cameraChanged();
42
43   deviceChanged();
44
45   emit isReadyChanged();
46 }
47
48 bool Mode::isActive() {
49   return m_mode ? m_mode->isActive() : false;
50 }
51
52 bool Mode::canCapture() {
53   return m_mode ? m_mode->canCapture() : false;
54 }
55
56 void Mode::deviceChanged() {
57   if (m_mode) {
58     QObject::disconnect(m_mode, SIGNAL(canCaptureChanged()), this, SIGNAL(canCaptureChanged()));
59     QObject::disconnect(m_mode, SIGNAL(saved(const QString&)),
60                         this, SIGNAL(saved(const QString&)));
61     QObject::disconnect(m_mode, SIGNAL(previewAvailable(const QImage&, const QString&)),
62                         this, SLOT(gotPreview(const QImage&, const QString&)));
63     QObject::disconnect(m_mode, SIGNAL(activeChanged()), this, SIGNAL(activeChanged()));
64     QObject::disconnect(m_mode, SIGNAL(activeChanged()), this, SIGNAL(canCaptureChanged()));
65     QObject::disconnect(m_cam->device(), SIGNAL(idleStateChanged(bool)),
66                         this, SIGNAL(canCaptureChanged()));
67     QObject::disconnect(m_cam->device(), SIGNAL(runningStateChanged(bool)),
68                         this, SIGNAL(canCaptureChanged()));
69
70     preChangeMode();
71   }
72
73   if (!m_cam || !m_cam->device()) {
74     return;
75   }
76
77   changeMode();
78
79   if (m_mode) {
80     QObject::connect(m_mode, SIGNAL(canCaptureChanged()), this, SIGNAL(canCaptureChanged()));
81     QObject::connect(m_mode, SIGNAL(saved(const QString&)), this, SIGNAL(saved(const QString&)));
82     QObject::connect(m_mode, SIGNAL(previewAvailable(const QImage&, const QString&)),
83                      this, SLOT(gotPreview(const QImage&, const QString&)));
84     QObject::connect(m_mode, SIGNAL(activeChanged()), this, SIGNAL(activeChanged()));
85     QObject::connect(m_mode, SIGNAL(activeChanged()), this, SIGNAL(canCaptureChanged()));
86     QObject::connect(m_cam->device(), SIGNAL(idleStateChanged(bool)),
87                      this, SIGNAL(canCaptureChanged()));
88     QObject::connect(m_cam->device(), SIGNAL(runningStateChanged(bool)),
89                      this, SIGNAL(canCaptureChanged()));
90
91     postChangeMode();
92   }
93
94   emit canCaptureChanged();
95   emit activeChanged();
96 }
97
98 void Mode::gotPreview(const QImage& image, const QString& fileName) {
99   PreviewProvider::instance()->setPreview(image);
100
101   // HACK: QML insists on caching the images.
102   QString url = QString("image://preview/%1").arg(m_seq);
103   ++m_seq;
104
105   emit previewAvailable(url, fileName);
106 }
107
108 bool Mode::isReady() const {
109   return m_mode;
110 }