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