2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
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.
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.
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
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"
32 #include <QDeclarativeInfo>
38 #include "whitebalance.h"
39 #include "colortone.h"
43 #include "noisereduction.h"
44 #include "flickerreduction.h"
46 #include "autofocus.h"
49 #include "videomute.h"
50 #include "videotorch.h"
51 #include "cameraconfig.h"
53 Camera::Camera(QDeclarativeItem *parent) :
54 QDeclarativeItem(parent),
55 m_cam(new QtCamera(this)),
57 m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
58 m_mode(Camera::UnknownMode),
59 m_notifications(new NotificationsContainer(this)),
70 m_flickerReduction(0),
76 m_config(new CameraConfig(this)) {
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()));
85 if (m_dev->activeMode()) {
86 m_dev->activeMode()->deactivate();
98 delete m_whiteBalance;
103 delete m_noiseReduction;
104 delete m_flickerReduction;
112 void Camera::componentComplete() {
113 QDeclarativeItem::componentComplete();
115 emit deviceCountChanged();
118 int Camera::deviceCount() const {
119 return m_cam ? m_cam->devices().size() : 0;
122 QString Camera::deviceName(int index) const {
123 return m_cam->devices().at(index).first;
126 QVariant Camera::deviceId(int index) const {
127 return m_cam->devices().at(index).second;
130 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
131 if (mode == Camera::UnknownMode) {
132 qmlInfo(this) << "Cannot set mode to unknown";
136 if (!isComponentComplete()) {
137 qmlInfo(this) << "Component is still not ready";
141 QVariant oldId = m_id;
142 Camera::CameraMode oldMode = m_mode;
144 if (setDeviceId(deviceId) && setMode(mode)) {
146 emit deviceIdChanged();
147 emit deviceChanged();
152 if (oldMode != m_mode) {
162 bool Camera::setDeviceId(const QVariant& deviceId) {
163 if (deviceId == m_id) {
167 if (m_dev && m_dev->stop(false)) {
171 qmlInfo(this) << "Failed to stop device";
175 m_dev = m_cam->device(deviceId, this);
179 m_vf->setDevice(m_dev);
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&)));
187 m_notifications->setDevice(m_dev);
192 QVariant Camera::deviceId() const {
196 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
197 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
199 // TODO: seems setting geometry breaks rendering somehow
200 // m_vf->setGeometry(newGeometry);
201 m_vf->resize(newGeometry.size());
204 QtCamDevice *Camera::device() const {
208 bool Camera::setMode(const Camera::CameraMode& mode) {
209 if (m_mode == mode) {
219 if (m_dev->isRunning()) {
226 Camera::CameraMode Camera::mode() {
230 bool Camera::start() {
239 return m_dev->start();
242 bool Camera::stop(bool force) {
244 return m_dev->stop(force);
250 bool Camera::isIdle() {
251 return m_dev ? m_dev->isIdle() : true;
254 bool Camera::isRunning() {
255 return m_dev ? m_dev->isRunning() : false;
258 bool Camera::applyMode() {
259 if (m_mode == Camera::UnknownMode) {
263 if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
264 m_dev->videoMode()->activate();
266 else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
267 m_dev->imageMode()->activate();
273 QString Camera::imageSuffix() const {
274 return m_cam->config()->imageSuffix();
277 QString Camera::videoSuffix() const {
278 return m_cam->config()->videoSuffix();
281 DeclarativeQtCameraNotifications *Camera::notifications() const {
282 return m_notifications->notifications();
285 void Camera::setNotifications(DeclarativeQtCameraNotifications *notifications) {
286 if (m_notifications->setNotifications(notifications)) {
288 if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
289 s->setConfig(m_cam->config());
293 emit notificationsChanged();
297 QRectF Camera::renderArea() const {
298 return m_vf->renderArea();
301 QSizeF Camera::videoResolution() const {
302 return m_vf->videoResolution();
305 void Camera::resetCapabilities() {
306 QtCamDevice *dev = device();
309 m_zoom = new Zoom(dev, this);
313 m_flash = new Flash(dev, this);
317 m_scene = new Scene(dev, this);
321 m_evComp = new EvComp(dev, this);
322 emit evCompChanged();
324 delete m_whiteBalance;
325 m_whiteBalance = new WhiteBalance(dev, this);
326 emit whiteBalanceChanged();
329 m_colorTone = new ColorTone(dev, this);
330 emit colorToneChanged();
333 m_iso = new Iso(dev, this);
337 m_exposure = new Exposure(dev, this);
338 emit exposureChanged();
341 m_aperture = new Aperture(dev, this);
342 emit apertureChanged();
344 delete m_noiseReduction;
345 m_noiseReduction = new NoiseReduction(dev, this);
346 emit noiseReductionChanged();
348 delete m_flickerReduction;
349 m_flickerReduction = new FlickerReduction(dev, this);
350 emit flickerReductionChanged();
353 m_focus = new Focus(dev, this);
357 m_autoFocus = new AutoFocus(dev, this);
358 emit autoFocusChanged();
361 m_roi = new Roi(dev, this);
365 m_videoMute = new VideoMute(dev, this);
366 emit videoMuteChanged();
369 m_videoTorch = new VideoTorch(dev, this);
370 emit videoTorchChanged();
373 Zoom *Camera::zoom() const {
377 Flash *Camera::flash() const {
381 Scene *Camera::scene() const {
385 EvComp *Camera::evComp() const {
389 WhiteBalance *Camera::whiteBalance() const {
390 return m_whiteBalance;
393 ColorTone *Camera::colorTone() const {
397 Iso *Camera::iso() const {
401 Exposure *Camera::exposure() const {
405 Aperture *Camera::aperture() const {
409 NoiseReduction *Camera::noiseReduction() const {
410 return m_noiseReduction;
413 FlickerReduction *Camera::flickerReduction() const {
414 return m_flickerReduction;
417 Focus *Camera::focus() const {
421 AutoFocus *Camera::autoFocus() const {
425 Roi *Camera::roi() const {
429 VideoMute *Camera::videoMute() const {
433 VideoTorch *Camera::videoTorch() const {
437 bool Camera::isRenderingEnabled() const {
438 return m_vf->isRenderingEnabled();
441 void Camera::setRenderingEnabled(bool enabled) {
442 m_vf->setRenderingEnabled(enabled);
445 CameraConfig *Camera::cameraConfig() const {