0058c133f7764f9d9445465d43164e7647494f7f
[harmattan/cameraplus] / declarative / camera.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "camera.h"
22 #include "qtcamera.h"
23 #include "qtcamdevice.h"
24 #include "qtcammode.h"
25 #include "qtcamimagemode.h"
26 #include "qtcamvideomode.h"
27 #include "qtcamgraphicsviewfinder.h"
28 #include "qtcamconfig.h"
29 #include "sounds.h"
30 #include "notificationscontainer.h"
31 #include "sounds.h"
32 #include <QDeclarativeInfo>
33
34 #include "zoom.h"
35 #include "flash.h"
36 #include "scene.h"
37 #include "evcomp.h"
38 #include "whitebalance.h"
39 #include "colortone.h"
40 #include "iso.h"
41 #include "exposure.h"
42 #include "aperture.h"
43 #include "noisereduction.h"
44 #include "flickerreduction.h"
45 #include "focus.h"
46 #include "autofocus.h"
47 #include "roi.h"
48
49 #include "videomute.h"
50 #include "videotorch.h"
51 #include "cameraconfig.h"
52
53 Camera::Camera(QDeclarativeItem *parent) :
54   QDeclarativeItem(parent),
55   m_cam(new QtCamera(this)),
56   m_dev(0),
57   m_mode(Camera::UnknownMode),
58   m_notifications(new NotificationsContainer(this)),
59   m_zoom(0),
60   m_flash(0),
61   m_scene(0),
62   m_evComp(0),
63   m_whiteBalance(0),
64   m_colorTone(0),
65   m_iso(0),
66   m_exposure(0),
67   m_aperture(0),
68   m_noiseReduction(0),
69   m_flickerReduction(0),
70   m_focus(0),
71   m_autoFocus(0),
72   m_roi(0),
73   m_videoMute(0),
74   m_videoTorch(0),
75   m_config(new CameraConfig(this)) {
76
77   m_config->componentComplete();
78 }
79
80 Camera::~Camera() {
81   if (m_dev) {
82     if (m_dev->activeMode()) {
83       m_dev->activeMode()->deactivate();
84     }
85
86     m_dev->stop(true);
87     m_dev->deleteLater();
88     m_dev = 0;
89   }
90
91   delete m_zoom;
92   delete m_flash;
93   delete m_scene;
94   delete m_evComp;
95   delete m_whiteBalance;
96   delete m_colorTone;
97   delete m_iso;
98   delete m_exposure;
99   delete m_aperture;
100   delete m_noiseReduction;
101   delete m_flickerReduction;
102   delete m_focus;
103   delete m_autoFocus;
104   delete m_roi;
105   delete m_videoMute;
106   delete m_videoTorch;
107 }
108
109 void Camera::componentComplete() {
110   QDeclarativeItem::componentComplete();
111
112   emit deviceCountChanged();
113 }
114
115 int Camera::deviceCount() const {
116   return m_cam ? m_cam->devices().size() : 0;
117 }
118
119 QString Camera::deviceName(int index) const {
120   return m_cam->devices().at(index).first;
121 }
122
123 QVariant Camera::deviceId(int index) const {
124   return m_cam->devices().at(index).second;
125 }
126
127 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
128   if (mode == Camera::UnknownMode) {
129     qmlInfo(this) << "Cannot set mode to unknown";
130     return false;
131   }
132
133   if (!isComponentComplete()) {
134     qmlInfo(this) << "Component is still not ready";
135     return false;
136   }
137
138   QVariant oldId = m_id;
139   Camera::CameraMode oldMode = m_mode;
140
141   if (setDeviceId(deviceId) && setMode(mode)) {
142     if (oldId != m_id) {
143       emit deviceIdChanged();
144       emit deviceChanged();
145
146       resetCapabilities();
147     }
148
149     if (oldMode != m_mode) {
150       emit modeChanged();
151     }
152
153     return true;
154   }
155
156   return false;
157 }
158
159 bool Camera::setDeviceId(const QVariant& deviceId) {
160   if (deviceId == m_id) {
161     return true;
162   }
163
164   if (m_dev && m_dev->stop(false)) {
165     delete m_dev;
166   }
167   else if (m_dev) {
168     qmlInfo(this) << "Failed to stop device";
169     return false;
170   }
171
172   m_dev = m_cam->device(deviceId, this);
173
174   m_id = deviceId;
175
176   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
177                       this, SIGNAL(runningStateChanged()));
178   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
179   QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
180                    this, SIGNAL(error(const QString&, int, const QString&)));
181
182   m_notifications->setDevice(m_dev);
183
184   return true;
185 }
186
187 QVariant Camera::deviceId() const {
188   return m_id;
189 }
190
191 QtCamDevice *Camera::device() const {
192   return m_dev;
193 }
194
195 bool Camera::setMode(const Camera::CameraMode& mode) {
196   if (m_mode == mode) {
197     return true;
198   }
199
200   if (!m_dev) {
201     return false;
202   }
203
204   m_mode = mode;
205
206   if (m_dev->isRunning()) {
207     applyMode();
208   }
209
210   return true;
211 }
212
213 Camera::CameraMode Camera::mode() {
214   return m_mode;
215 }
216
217 bool Camera::start() {
218   if (!m_dev) {
219     return false;
220   }
221
222   if (!applyMode()) {
223     return false;
224   }
225
226   return m_dev->start();
227 }
228
229 bool Camera::stop(bool force) {
230   if (m_dev) {
231     return m_dev->stop(force);
232   }
233
234   return true;
235 }
236
237 bool Camera::isIdle() {
238   return m_dev ? m_dev->isIdle() : true;
239 }
240
241 bool Camera::isRunning() {
242   return m_dev ? m_dev->isRunning() : false;
243 }
244
245 bool Camera::applyMode() {
246   if (m_mode == Camera::UnknownMode) {
247     return false;
248   }
249
250   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
251     m_dev->videoMode()->activate();
252   }
253   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
254     m_dev->imageMode()->activate();
255   }
256
257   return true;
258 }
259
260 QString Camera::imageSuffix() const {
261   return m_cam->config()->imageSuffix();
262 }
263
264 QString Camera::videoSuffix() const {
265   return m_cam->config()->videoSuffix();
266 }
267
268 Sounds *Camera::sounds() const {
269   return m_notifications->sounds();
270 }
271 void Camera::setSounds(Sounds *sounds) {
272   if (m_notifications->setSounds(sounds)) {
273     sounds->reload();
274     emit soundsChanged();
275   }
276 }
277
278 void Camera::resetCapabilities() {
279   QtCamDevice *dev = device();
280
281   delete m_zoom;
282   m_zoom = new Zoom(dev, this);
283   emit zoomChanged();
284
285   delete m_flash;
286   m_flash = new Flash(dev, this);
287   emit flashChanged();
288
289   delete m_scene;
290   m_scene = new Scene(dev, this);
291   emit sceneChanged();
292
293   delete m_evComp;
294   m_evComp = new EvComp(dev, this);
295   emit evCompChanged();
296
297   delete m_whiteBalance;
298   m_whiteBalance = new WhiteBalance(dev, this);
299   emit whiteBalanceChanged();
300
301   delete m_colorTone;
302   m_colorTone = new ColorTone(dev, this);
303   emit colorToneChanged();
304
305   delete m_iso;
306   m_iso = new Iso(dev, this);
307   emit isoChanged();
308
309   delete m_exposure;
310   m_exposure = new Exposure(dev, this);
311   emit exposureChanged();
312
313   delete m_aperture;
314   m_aperture = new Aperture(dev, this);
315   emit apertureChanged();
316
317   delete m_noiseReduction;
318   m_noiseReduction = new NoiseReduction(dev, this);
319   emit noiseReductionChanged();
320
321   delete m_flickerReduction;
322   m_flickerReduction = new FlickerReduction(dev, this);
323   emit flickerReductionChanged();
324
325   delete m_focus;
326   m_focus = new Focus(dev, this);
327   emit focusChanged();
328
329   delete m_autoFocus;
330   m_autoFocus = new AutoFocus(dev, this);
331   emit autoFocusChanged();
332
333   delete m_roi;
334   m_roi = new Roi(dev, this);
335   emit roiChanged();
336
337   delete m_videoMute;
338   m_videoMute = new VideoMute(dev, this);
339   emit videoMuteChanged();
340
341   delete m_videoTorch;
342   m_videoTorch = new VideoTorch(dev, this);
343   emit videoTorchChanged();
344 }
345
346 Zoom *Camera::zoom() const {
347   return m_zoom;
348 }
349
350 Flash *Camera::flash() const {
351   return m_flash;
352 }
353
354 Scene *Camera::scene() const {
355   return m_scene;
356 }
357
358 EvComp *Camera::evComp() const {
359   return m_evComp;
360 }
361
362 WhiteBalance *Camera::whiteBalance() const {
363   return m_whiteBalance;
364 }
365
366 ColorTone *Camera::colorTone() const {
367   return m_colorTone;
368 }
369
370 Iso *Camera::iso() const {
371   return m_iso;
372 }
373
374 Exposure *Camera::exposure() const {
375   return m_exposure;
376 }
377
378 Aperture *Camera::aperture() const {
379   return m_aperture;
380 }
381
382 NoiseReduction *Camera::noiseReduction() const {
383   return m_noiseReduction;
384 }
385
386 FlickerReduction *Camera::flickerReduction() const {
387   return m_flickerReduction;
388 }
389
390 Focus *Camera::focus() const {
391   return m_focus;
392 }
393
394 AutoFocus *Camera::autoFocus() const {
395   return m_autoFocus;
396 }
397
398 Roi *Camera::roi() const {
399   return m_roi;
400 }
401
402 VideoMute *Camera::videoMute() const {
403   return m_videoMute;
404 }
405
406 VideoTorch *Camera::videoTorch() const {
407   return m_videoTorch;
408 }
409
410 CameraConfig *Camera::cameraConfig() const {
411   return m_config;
412 }