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