Added CameraConfig to QtCamera declarative module
[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 "declarativeqtcameranotifications.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_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
58   m_mode(Camera::UnknownMode),
59   m_notifications(new NotificationsContainer(this)),
60   m_zoom(0),
61   m_flash(0),
62   m_scene(0),
63   m_evComp(0),
64   m_whiteBalance(0),
65   m_colorTone(0),
66   m_iso(0),
67   m_exposure(0),
68   m_aperture(0),
69   m_noiseReduction(0),
70   m_flickerReduction(0),
71   m_focus(0),
72   m_autoFocus(0),
73   m_roi(0),
74   m_videoMute(0),
75   m_videoTorch(0),
76   m_config(new CameraConfig(this)) {
77
78   QObject::connect(m_vf, SIGNAL(renderAreaChanged()), this, SIGNAL(renderAreaChanged()));
79   QObject::connect(m_vf, SIGNAL(videoResolutionChanged()), this, SIGNAL(videoResolutionChanged()));
80   QObject::connect(m_vf, SIGNAL(renderingEnabledChanged()), this, SIGNAL(renderingEnabledChanged()));
81 }
82
83 Camera::~Camera() {
84   if (m_dev) {
85     if (m_dev->activeMode()) {
86       m_dev->activeMode()->deactivate();
87     }
88
89     m_dev->stop(true);
90     m_dev->deleteLater();
91     m_dev = 0;
92   }
93
94   delete m_zoom;
95   delete m_flash;
96   delete m_scene;
97   delete m_evComp;
98   delete m_whiteBalance;
99   delete m_colorTone;
100   delete m_iso;
101   delete m_exposure;
102   delete m_aperture;
103   delete m_noiseReduction;
104   delete m_flickerReduction;
105   delete m_focus;
106   delete m_autoFocus;
107   delete m_roi;
108   delete m_videoMute;
109   delete m_videoTorch;
110 }
111
112 void Camera::componentComplete() {
113   QDeclarativeItem::componentComplete();
114
115   emit deviceCountChanged();
116 }
117
118 int Camera::deviceCount() const {
119   return m_cam ? m_cam->devices().size() : 0;
120 }
121
122 QString Camera::deviceName(int index) const {
123   return m_cam->devices().at(index).first;
124 }
125
126 QVariant Camera::deviceId(int index) const {
127   return m_cam->devices().at(index).second;
128 }
129
130 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
131   if (mode == Camera::UnknownMode) {
132     qmlInfo(this) << "Cannot set mode to unknown";
133     return false;
134   }
135
136   if (!isComponentComplete()) {
137     qmlInfo(this) << "Component is still not ready";
138     return false;
139   }
140
141   QVariant oldId = m_id;
142   Camera::CameraMode oldMode = m_mode;
143
144   if (setDeviceId(deviceId) && setMode(mode)) {
145     if (oldId != m_id) {
146       emit deviceIdChanged();
147       emit deviceChanged();
148
149       resetCapabilities();
150     }
151
152     if (oldMode != m_mode) {
153       emit modeChanged();
154     }
155
156     return true;
157   }
158
159   return false;
160 }
161
162 bool Camera::setDeviceId(const QVariant& deviceId) {
163   if (deviceId == m_id) {
164     return true;
165   }
166
167   if (m_dev && m_dev->stop(false)) {
168     delete m_dev;
169   }
170   else if (m_dev) {
171     qmlInfo(this) << "Failed to stop device";
172     return false;
173   }
174
175   m_dev = m_cam->device(deviceId, this);
176
177   m_id = deviceId;
178
179   m_vf->setDevice(m_dev);
180
181   QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
182                       this, SIGNAL(runningStateChanged()));
183   QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
184   QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
185                    this, SIGNAL(error(const QString&, int, const QString&)));
186
187   m_notifications->setDevice(m_dev);
188
189   return true;
190 }
191
192 QVariant Camera::deviceId() const {
193   return m_id;
194 }
195
196 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
197   QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
198
199   // TODO: seems setting geometry breaks rendering somehow
200   //  m_vf->setGeometry(newGeometry);
201   m_vf->resize(newGeometry.size());
202 }
203
204 QtCamDevice *Camera::device() const {
205   return m_dev;
206 }
207
208 bool Camera::setMode(const Camera::CameraMode& mode) {
209   if (m_mode == mode) {
210     return true;
211   }
212
213   if (!m_dev) {
214     return false;
215   }
216
217   m_mode = mode;
218
219   if (m_dev->isRunning()) {
220     applyMode();
221   }
222
223   return true;
224 }
225
226 Camera::CameraMode Camera::mode() {
227   return m_mode;
228 }
229
230 bool Camera::start() {
231   if (!m_dev) {
232     return false;
233   }
234
235   if (!applyMode()) {
236     return false;
237   }
238
239   return m_dev->start();
240 }
241
242 bool Camera::stop(bool force) {
243   if (m_dev) {
244     return m_dev->stop(force);
245   }
246
247   return true;
248 }
249
250 bool Camera::isIdle() {
251   return m_dev ? m_dev->isIdle() : true;
252 }
253
254 bool Camera::isRunning() {
255   return m_dev ? m_dev->isRunning() : false;
256 }
257
258 bool Camera::applyMode() {
259   if (m_mode == Camera::UnknownMode) {
260     return false;
261   }
262
263   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
264     m_dev->videoMode()->activate();
265   }
266   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
267     m_dev->imageMode()->activate();
268   }
269
270   return true;
271 }
272
273 QString Camera::imageSuffix() const {
274   return m_cam->config()->imageSuffix();
275 }
276
277 QString Camera::videoSuffix() const {
278   return m_cam->config()->videoSuffix();
279 }
280
281 DeclarativeQtCameraNotifications *Camera::notifications() const {
282   return m_notifications->notifications();
283 }
284
285 void Camera::setNotifications(DeclarativeQtCameraNotifications *notifications) {
286   if (m_notifications->setNotifications(notifications)) {
287
288     if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
289       s->setConfig(m_cam->config());
290       s->reload();
291     }
292
293     emit notificationsChanged();
294   }
295 }
296
297 QRectF Camera::renderArea() const {
298   return m_vf->renderArea();
299 }
300
301 QSizeF Camera::videoResolution() const {
302   return m_vf->videoResolution();
303 }
304
305 void Camera::resetCapabilities() {
306   QtCamDevice *dev = device();
307
308   delete m_zoom;
309   m_zoom = new Zoom(dev, this);
310   emit zoomChanged();
311
312   delete m_flash;
313   m_flash = new Flash(dev, this);
314   emit flashChanged();
315
316   delete m_scene;
317   m_scene = new Scene(dev, this);
318   emit sceneChanged();
319
320   delete m_evComp;
321   m_evComp = new EvComp(dev, this);
322   emit evCompChanged();
323
324   delete m_whiteBalance;
325   m_whiteBalance = new WhiteBalance(dev, this);
326   emit whiteBalanceChanged();
327
328   delete m_colorTone;
329   m_colorTone = new ColorTone(dev, this);
330   emit colorToneChanged();
331
332   delete m_iso;
333   m_iso = new Iso(dev, this);
334   emit isoChanged();
335
336   delete m_exposure;
337   m_exposure = new Exposure(dev, this);
338   emit exposureChanged();
339
340   delete m_aperture;
341   m_aperture = new Aperture(dev, this);
342   emit apertureChanged();
343
344   delete m_noiseReduction;
345   m_noiseReduction = new NoiseReduction(dev, this);
346   emit noiseReductionChanged();
347
348   delete m_flickerReduction;
349   m_flickerReduction = new FlickerReduction(dev, this);
350   emit flickerReductionChanged();
351
352   delete m_focus;
353   m_focus = new Focus(dev, this);
354   emit focusChanged();
355
356   delete m_autoFocus;
357   m_autoFocus = new AutoFocus(dev, this);
358   emit autoFocusChanged();
359
360   delete m_roi;
361   m_roi = new Roi(dev, this);
362   emit roiChanged();
363
364   delete m_videoMute;
365   m_videoMute = new VideoMute(dev, this);
366   emit videoMuteChanged();
367
368   delete m_videoTorch;
369   m_videoTorch = new VideoTorch(dev, this);
370   emit videoTorchChanged();
371 }
372
373 Zoom *Camera::zoom() const {
374   return m_zoom;
375 }
376
377 Flash *Camera::flash() const {
378   return m_flash;
379 }
380
381 Scene *Camera::scene() const {
382   return m_scene;
383 }
384
385 EvComp *Camera::evComp() const {
386   return m_evComp;
387 }
388
389 WhiteBalance *Camera::whiteBalance() const {
390   return m_whiteBalance;
391 }
392
393 ColorTone *Camera::colorTone() const {
394   return m_colorTone;
395 }
396
397 Iso *Camera::iso() const {
398   return m_iso;
399 }
400
401 Exposure *Camera::exposure() const {
402   return m_exposure;
403 }
404
405 Aperture *Camera::aperture() const {
406   return m_aperture;
407 }
408
409 NoiseReduction *Camera::noiseReduction() const {
410   return m_noiseReduction;
411 }
412
413 FlickerReduction *Camera::flickerReduction() const {
414   return m_flickerReduction;
415 }
416
417 Focus *Camera::focus() const {
418   return m_focus;
419 }
420
421 AutoFocus *Camera::autoFocus() const {
422   return m_autoFocus;
423 }
424
425 Roi *Camera::roi() const {
426   return m_roi;
427 }
428
429 VideoMute *Camera::videoMute() const {
430   return m_videoMute;
431 }
432
433 VideoTorch *Camera::videoTorch() const {
434   return m_videoTorch;
435 }
436
437 bool Camera::isRenderingEnabled() const {
438   return m_vf->isRenderingEnabled();
439 }
440
441 void Camera::setRenderingEnabled(bool enabled) {
442   m_vf->setRenderingEnabled(enabled);
443 }
444
445 CameraConfig *Camera::cameraConfig() const {
446   return m_config;
447 }