Added resource policy support (Still needs more testing) and refactored the pipeline...
[harmattan/cameraplus] / imports / camera.cpp
1 #include "camera.h"
2 #include "qtcamera.h"
3 #include "qtcamdevice.h"
4 #include "qtcammode.h"
5 #include "qtcamimagemode.h"
6 #include "qtcamvideomode.h"
7 #include "qtcamgraphicsviewfinder.h"
8 #include "qtcamconfig.h"
9
10 // TODO: a viewfinder class that inherits QDeclarativeItem
11
12 Camera::Camera(QDeclarativeItem *parent) :
13   QDeclarativeItem(parent),
14   m_cam(new QtCamera(this)),
15   m_dev(0),
16   m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
17   m_mode(Camera::ImageMode) {
18
19   // TODO:
20 }
21
22 Camera::~Camera() {
23   // TODO:
24 }
25
26 void Camera::componentComplete() {
27   QDeclarativeItem::componentComplete();
28
29   if (m_id.isValid()) {
30     QVariant oldId = m_id;
31     m_id = QVariant();
32     setDeviceId(oldId);
33   }
34
35   emit deviceCountChanged();
36 }
37
38 int Camera::deviceCount() const {
39   return m_cam ? m_cam->devices().size() : 0;
40 }
41
42 QString Camera::deviceName(int index) const {
43   return m_cam->devices().at(index).first;
44 }
45
46 QVariant Camera::deviceId(int index) const {
47   return m_cam->devices().at(index).second;
48 }
49
50 void Camera::setDeviceId(const QVariant& id) {
51   if (id == m_id) {
52     return;
53   }
54
55   if (!isComponentComplete()) {
56     m_id = id;
57     emit deviceIdChanged();
58     return;
59   }
60
61   if (m_dev && m_dev->stop()) {
62     delete m_dev;
63   }
64   else if (m_dev) {
65     qWarning() << "Failed to stop device";
66     return;
67   }
68
69   m_dev = m_cam->device(id, this);
70
71   m_id = id;
72
73   m_vf->setDevice(m_dev);
74
75   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
76                       this, SIGNAL(runningStateChanged()));
77   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
78
79   emit deviceIdChanged();
80   emit deviceChanged();
81 }
82
83 QVariant Camera::deviceId() const {
84   return m_id;
85 }
86
87 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
88   QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
89
90   m_vf->setGeometry(newGeometry);
91 }
92
93 QtCamDevice *Camera::device() const {
94   return m_dev;
95 }
96
97 void Camera::setMode(const Camera::CameraMode& mode) {
98   if (m_mode == mode) {
99     return;
100   }
101
102   m_mode = mode;
103
104   if (!m_dev) {
105     return;
106   }
107
108   if (m_dev->isRunning()) {
109     applyMode();
110   }
111
112   emit modeChanged();
113 }
114
115 Camera::CameraMode Camera::mode() {
116   return m_mode;
117 }
118
119 bool Camera::start() {
120   if (!m_dev) {
121     return false;
122   }
123
124   applyMode();
125
126   return m_dev->start();
127 }
128
129 bool Camera::stop() {
130   if (m_dev) {
131     return m_dev->stop();
132   }
133
134   return true;
135 }
136
137 bool Camera::isIdle() {
138   return m_dev ? m_dev->isIdle() : true;
139 }
140
141 bool Camera::isRunning() {
142   return m_dev ? m_dev->isRunning() : false;
143 }
144
145 void Camera::applyMode() {
146   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
147     m_dev->videoMode()->activate();
148   }
149   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
150     m_dev->imageMode()->activate();
151   }
152 }
153
154 QString Camera::imageSuffix() const {
155   return m_cam->config()->imageSuffix();
156 }
157
158 QString Camera::videoSuffix() const {
159   return m_cam->config()->videoSuffix();
160 }