Disable auto focus when front camera is active
[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 "qtcamconfig.h"
28 #include "sounds.h"
29 #include "notificationscontainer.h"
30 #include "sounds.h"
31 #if defined(QT4)
32 #include <QDeclarativeInfo>
33 #elif defined(QT5)
34 #include <QQmlInfo>
35 #endif
36 #include "zoom.h"
37 #include "flash.h"
38 #include "scene.h"
39 #include "evcomp.h"
40 #include "whitebalance.h"
41 #include "colortone.h"
42 #include "iso.h"
43 #include "exposure.h"
44 #include "aperture.h"
45 #include "noisereduction.h"
46 #include "flickerreduction.h"
47 #include "focus.h"
48 #include "autofocus.h"
49 #include "roi.h"
50
51 #include "videomute.h"
52 #include "videotorch.h"
53 #include "cameraconfig.h"
54
55 Camera::Camera(QObject *parent) :
56   QObject(parent),
57   m_cam(new QtCamera(this)),
58   m_dev(0),
59   m_mode(Camera::UnknownMode),
60   m_notifications(new NotificationsContainer(this)),
61   m_zoom(0),
62   m_flash(0),
63   m_scene(0),
64   m_evComp(0),
65   m_whiteBalance(0),
66   m_colorTone(0),
67   m_iso(0),
68   m_exposure(0),
69   m_aperture(0),
70   m_noiseReduction(0),
71   m_flickerReduction(0),
72   m_focus(0),
73   m_autoFocus(0),
74   m_roi(0),
75   m_videoMute(0),
76   m_videoTorch(0),
77   m_config(new CameraConfig(this)) {
78
79   m_config->componentComplete();
80 }
81
82 Camera::~Camera() {
83   if (m_dev) {
84     if (m_dev->activeMode()) {
85       m_dev->activeMode()->deactivate();
86     }
87
88     m_dev->stop(true);
89     m_dev->deleteLater();
90     m_dev = 0;
91   }
92
93   delete m_zoom;
94   delete m_flash;
95   delete m_scene;
96   delete m_evComp;
97   delete m_whiteBalance;
98   delete m_colorTone;
99   delete m_iso;
100   delete m_exposure;
101   delete m_aperture;
102   delete m_noiseReduction;
103   delete m_flickerReduction;
104   delete m_focus;
105   delete m_autoFocus;
106   delete m_roi;
107   delete m_videoMute;
108   delete m_videoTorch;
109 }
110
111 int Camera::deviceCount() const {
112   return m_cam ? m_cam->devices().size() : 0;
113 }
114
115 QString Camera::deviceName(int index) const {
116   return m_cam->devices().at(index).first;
117 }
118
119 QVariant Camera::deviceId(int index) const {
120   return m_cam->devices().at(index).second;
121 }
122
123 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
124   if (mode == Camera::UnknownMode) {
125     qmlInfo(this) << "Cannot set mode to unknown";
126     return false;
127   }
128
129   QVariant oldId = m_id;
130   Camera::CameraMode oldMode = m_mode;
131
132   if (setDeviceId(deviceId) && setMode(mode)) {
133     if (oldId != m_id) {
134       emit deviceIdChanged();
135       emit deviceChanged();
136
137       resetCapabilities();
138     }
139
140     if (oldMode != m_mode) {
141       emit modeChanged();
142     }
143
144     return true;
145   }
146
147   return false;
148 }
149
150 bool Camera::setDeviceId(const QVariant& deviceId) {
151   if (deviceId == m_id) {
152     return true;
153   }
154
155   if (m_dev && m_dev->stop(false)) {
156     emit prepareForDeviceChange();
157     delete m_dev;
158   }
159   else if (m_dev) {
160     qmlInfo(this) << "Failed to stop device";
161     return false;
162   }
163
164   m_dev = m_cam->device(deviceId, this);
165
166   m_id = deviceId;
167
168   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
169                       this, SIGNAL(runningStateChanged()));
170   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
171   QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
172                    this, SIGNAL(error(const QString&, int, const QString&)));
173
174   m_notifications->setDevice(m_dev);
175
176   return true;
177 }
178
179 QVariant Camera::deviceId() const {
180   return m_id;
181 }
182
183 QtCamDevice *Camera::device() const {
184   return m_dev;
185 }
186
187 bool Camera::setMode(const Camera::CameraMode& mode) {
188   if (m_mode == mode) {
189     return true;
190   }
191
192   if (!m_dev) {
193     return false;
194   }
195
196   m_mode = mode;
197
198   if (m_dev->isRunning()) {
199     applyMode();
200   }
201
202   return true;
203 }
204
205 Camera::CameraMode Camera::mode() {
206   return m_mode;
207 }
208
209 bool Camera::start() {
210   if (!m_dev) {
211     return false;
212   }
213
214   if (!applyMode()) {
215     return false;
216   }
217
218   return m_dev->start();
219 }
220
221 bool Camera::stop(bool force) {
222   if (m_dev) {
223     return m_dev->stop(force);
224   }
225
226   return true;
227 }
228
229 bool Camera::isIdle() {
230   return m_dev ? m_dev->isIdle() : true;
231 }
232
233 bool Camera::isRunning() {
234   return m_dev ? m_dev->isRunning() : false;
235 }
236
237 bool Camera::applyMode() {
238   if (m_mode == Camera::UnknownMode) {
239     return false;
240   }
241
242   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
243     m_dev->videoMode()->activate();
244   }
245   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
246     m_dev->imageMode()->activate();
247   }
248
249   return true;
250 }
251
252 QString Camera::imageSuffix() const {
253   return m_cam->config()->imageSuffix();
254 }
255
256 QString Camera::videoSuffix() const {
257   return m_cam->config()->videoSuffix();
258 }
259
260 Sounds *Camera::sounds() const {
261   return m_notifications->sounds();
262 }
263 void Camera::setSounds(Sounds *sounds) {
264   if (m_notifications->setSounds(sounds)) {
265     sounds->reload();
266     emit soundsChanged();
267   }
268 }
269
270 void Camera::resetCapabilities() {
271   QtCamDevice *dev = device();
272
273   delete m_zoom;
274   m_zoom = new Zoom(dev, this);
275   emit zoomChanged();
276
277   delete m_flash;
278   m_flash = new Flash(dev, this);
279   emit flashChanged();
280
281   delete m_scene;
282   m_scene = new Scene(dev, this);
283   emit sceneChanged();
284
285   delete m_evComp;
286   m_evComp = new EvComp(dev, this);
287   emit evCompChanged();
288
289   delete m_whiteBalance;
290   m_whiteBalance = new WhiteBalance(dev, this);
291   emit whiteBalanceChanged();
292
293   delete m_colorTone;
294   m_colorTone = new ColorTone(dev, this);
295   emit colorToneChanged();
296
297   delete m_iso;
298   m_iso = new Iso(dev, this);
299   emit isoChanged();
300
301   delete m_exposure;
302   m_exposure = new Exposure(dev, this);
303   emit exposureChanged();
304
305   delete m_aperture;
306   m_aperture = new Aperture(dev, this);
307   emit apertureChanged();
308
309   delete m_noiseReduction;
310   m_noiseReduction = new NoiseReduction(dev, this);
311   emit noiseReductionChanged();
312
313   delete m_flickerReduction;
314   m_flickerReduction = new FlickerReduction(dev, this);
315   emit flickerReductionChanged();
316
317   delete m_focus;
318   m_focus = new Focus(dev, this);
319   emit focusChanged();
320
321   delete m_autoFocus;
322   m_autoFocus = new AutoFocus(dev, this);
323   emit autoFocusChanged();
324
325   delete m_roi;
326   m_roi = new Roi(dev, this);
327   emit roiChanged();
328
329   delete m_videoMute;
330   m_videoMute = new VideoMute(dev, this);
331   emit videoMuteChanged();
332
333   delete m_videoTorch;
334   m_videoTorch = new VideoTorch(dev, this);
335   emit videoTorchChanged();
336 }
337
338 Zoom *Camera::zoom() const {
339   return m_zoom;
340 }
341
342 Flash *Camera::flash() const {
343   return m_flash;
344 }
345
346 Scene *Camera::scene() const {
347   return m_scene;
348 }
349
350 EvComp *Camera::evComp() const {
351   return m_evComp;
352 }
353
354 WhiteBalance *Camera::whiteBalance() const {
355   return m_whiteBalance;
356 }
357
358 ColorTone *Camera::colorTone() const {
359   return m_colorTone;
360 }
361
362 Iso *Camera::iso() const {
363   return m_iso;
364 }
365
366 Exposure *Camera::exposure() const {
367   return m_exposure;
368 }
369
370 Aperture *Camera::aperture() const {
371   return m_aperture;
372 }
373
374 NoiseReduction *Camera::noiseReduction() const {
375   return m_noiseReduction;
376 }
377
378 FlickerReduction *Camera::flickerReduction() const {
379   return m_flickerReduction;
380 }
381
382 Focus *Camera::focus() const {
383   return m_focus;
384 }
385
386 AutoFocus *Camera::autoFocus() const {
387   return m_autoFocus;
388 }
389
390 Roi *Camera::roi() const {
391   return m_roi;
392 }
393
394 VideoMute *Camera::videoMute() const {
395   return m_videoMute;
396 }
397
398 VideoTorch *Camera::videoTorch() const {
399   return m_videoTorch;
400 }
401
402 CameraConfig *Camera::cameraConfig() const {
403   return m_config;
404 }