c04741b94bb9740eee26537da0d4e27f78158a33
[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(QObject *parent) :
54   QObject(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 int Camera::deviceCount() const {
110   return m_cam ? m_cam->devices().size() : 0;
111 }
112
113 QString Camera::deviceName(int index) const {
114   return m_cam->devices().at(index).first;
115 }
116
117 QVariant Camera::deviceId(int index) const {
118   return m_cam->devices().at(index).second;
119 }
120
121 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
122   if (mode == Camera::UnknownMode) {
123     qmlInfo(this) << "Cannot set mode to unknown";
124     return false;
125   }
126
127   QVariant oldId = m_id;
128   Camera::CameraMode oldMode = m_mode;
129
130   if (setDeviceId(deviceId) && setMode(mode)) {
131     if (oldId != m_id) {
132       emit deviceIdChanged();
133       emit deviceChanged();
134
135       resetCapabilities();
136     }
137
138     if (oldMode != m_mode) {
139       emit modeChanged();
140     }
141
142     return true;
143   }
144
145   return false;
146 }
147
148 bool Camera::setDeviceId(const QVariant& deviceId) {
149   if (deviceId == m_id) {
150     return true;
151   }
152
153   if (m_dev && m_dev->stop(false)) {
154     delete m_dev;
155   }
156   else if (m_dev) {
157     qmlInfo(this) << "Failed to stop device";
158     return false;
159   }
160
161   m_dev = m_cam->device(deviceId, this);
162
163   m_id = deviceId;
164
165   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
166                       this, SIGNAL(runningStateChanged()));
167   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
168   QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
169                    this, SIGNAL(error(const QString&, int, const QString&)));
170
171   m_notifications->setDevice(m_dev);
172
173   return true;
174 }
175
176 QVariant Camera::deviceId() const {
177   return m_id;
178 }
179
180 QtCamDevice *Camera::device() const {
181   return m_dev;
182 }
183
184 bool Camera::setMode(const Camera::CameraMode& mode) {
185   if (m_mode == mode) {
186     return true;
187   }
188
189   if (!m_dev) {
190     return false;
191   }
192
193   m_mode = mode;
194
195   if (m_dev->isRunning()) {
196     applyMode();
197   }
198
199   return true;
200 }
201
202 Camera::CameraMode Camera::mode() {
203   return m_mode;
204 }
205
206 bool Camera::start() {
207   if (!m_dev) {
208     return false;
209   }
210
211   if (!applyMode()) {
212     return false;
213   }
214
215   return m_dev->start();
216 }
217
218 bool Camera::stop(bool force) {
219   if (m_dev) {
220     return m_dev->stop(force);
221   }
222
223   return true;
224 }
225
226 bool Camera::isIdle() {
227   return m_dev ? m_dev->isIdle() : true;
228 }
229
230 bool Camera::isRunning() {
231   return m_dev ? m_dev->isRunning() : false;
232 }
233
234 bool Camera::applyMode() {
235   if (m_mode == Camera::UnknownMode) {
236     return false;
237   }
238
239   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
240     m_dev->videoMode()->activate();
241   }
242   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
243     m_dev->imageMode()->activate();
244   }
245
246   return true;
247 }
248
249 QString Camera::imageSuffix() const {
250   return m_cam->config()->imageSuffix();
251 }
252
253 QString Camera::videoSuffix() const {
254   return m_cam->config()->videoSuffix();
255 }
256
257 Sounds *Camera::sounds() const {
258   return m_notifications->sounds();
259 }
260 void Camera::setSounds(Sounds *sounds) {
261   if (m_notifications->setSounds(sounds)) {
262     sounds->reload();
263     emit soundsChanged();
264   }
265 }
266
267 void Camera::resetCapabilities() {
268   QtCamDevice *dev = device();
269
270   delete m_zoom;
271   m_zoom = new Zoom(dev, this);
272   emit zoomChanged();
273
274   delete m_flash;
275   m_flash = new Flash(dev, this);
276   emit flashChanged();
277
278   delete m_scene;
279   m_scene = new Scene(dev, this);
280   emit sceneChanged();
281
282   delete m_evComp;
283   m_evComp = new EvComp(dev, this);
284   emit evCompChanged();
285
286   delete m_whiteBalance;
287   m_whiteBalance = new WhiteBalance(dev, this);
288   emit whiteBalanceChanged();
289
290   delete m_colorTone;
291   m_colorTone = new ColorTone(dev, this);
292   emit colorToneChanged();
293
294   delete m_iso;
295   m_iso = new Iso(dev, this);
296   emit isoChanged();
297
298   delete m_exposure;
299   m_exposure = new Exposure(dev, this);
300   emit exposureChanged();
301
302   delete m_aperture;
303   m_aperture = new Aperture(dev, this);
304   emit apertureChanged();
305
306   delete m_noiseReduction;
307   m_noiseReduction = new NoiseReduction(dev, this);
308   emit noiseReductionChanged();
309
310   delete m_flickerReduction;
311   m_flickerReduction = new FlickerReduction(dev, this);
312   emit flickerReductionChanged();
313
314   delete m_focus;
315   m_focus = new Focus(dev, this);
316   emit focusChanged();
317
318   delete m_autoFocus;
319   m_autoFocus = new AutoFocus(dev, this);
320   emit autoFocusChanged();
321
322   delete m_roi;
323   m_roi = new Roi(dev, this);
324   emit roiChanged();
325
326   delete m_videoMute;
327   m_videoMute = new VideoMute(dev, this);
328   emit videoMuteChanged();
329
330   delete m_videoTorch;
331   m_videoTorch = new VideoTorch(dev, this);
332   emit videoTorchChanged();
333 }
334
335 Zoom *Camera::zoom() const {
336   return m_zoom;
337 }
338
339 Flash *Camera::flash() const {
340   return m_flash;
341 }
342
343 Scene *Camera::scene() const {
344   return m_scene;
345 }
346
347 EvComp *Camera::evComp() const {
348   return m_evComp;
349 }
350
351 WhiteBalance *Camera::whiteBalance() const {
352   return m_whiteBalance;
353 }
354
355 ColorTone *Camera::colorTone() const {
356   return m_colorTone;
357 }
358
359 Iso *Camera::iso() const {
360   return m_iso;
361 }
362
363 Exposure *Camera::exposure() const {
364   return m_exposure;
365 }
366
367 Aperture *Camera::aperture() const {
368   return m_aperture;
369 }
370
371 NoiseReduction *Camera::noiseReduction() const {
372   return m_noiseReduction;
373 }
374
375 FlickerReduction *Camera::flickerReduction() const {
376   return m_flickerReduction;
377 }
378
379 Focus *Camera::focus() const {
380   return m_focus;
381 }
382
383 AutoFocus *Camera::autoFocus() const {
384   return m_autoFocus;
385 }
386
387 Roi *Camera::roi() const {
388   return m_roi;
389 }
390
391 VideoMute *Camera::videoMute() const {
392   return m_videoMute;
393 }
394
395 VideoTorch *Camera::videoTorch() const {
396   return m_videoTorch;
397 }
398
399 CameraConfig *Camera::cameraConfig() const {
400   return m_config;
401 }