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 m_config->componentComplete();
80 QObject::connect(m_vf, SIGNAL(renderAreaChanged()), this, SIGNAL(renderAreaChanged()));
81 QObject::connect(m_vf, SIGNAL(videoResolutionChanged()), this, SIGNAL(videoResolutionChanged()));
82 QObject::connect(m_vf, SIGNAL(renderingEnabledChanged()), this, SIGNAL(renderingEnabledChanged()));
87 if (m_dev->activeMode()) {
88 m_dev->activeMode()->deactivate();
100 delete m_whiteBalance;
105 delete m_noiseReduction;
106 delete m_flickerReduction;
114 void Camera::componentComplete() {
115 QDeclarativeItem::componentComplete();
117 emit deviceCountChanged();
120 int Camera::deviceCount() const {
121 return m_cam ? m_cam->devices().size() : 0;
124 QString Camera::deviceName(int index) const {
125 return m_cam->devices().at(index).first;
128 QVariant Camera::deviceId(int index) const {
129 return m_cam->devices().at(index).second;
132 bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
133 if (mode == Camera::UnknownMode) {
134 qmlInfo(this) << "Cannot set mode to unknown";
138 if (!isComponentComplete()) {
139 qmlInfo(this) << "Component is still not ready";
143 QVariant oldId = m_id;
144 Camera::CameraMode oldMode = m_mode;
146 if (setDeviceId(deviceId) && setMode(mode)) {
148 emit deviceIdChanged();
149 emit deviceChanged();
154 if (oldMode != m_mode) {
164 bool Camera::setDeviceId(const QVariant& deviceId) {
165 if (deviceId == m_id) {
169 if (m_dev && m_dev->stop(false)) {
173 qmlInfo(this) << "Failed to stop device";
177 m_dev = m_cam->device(deviceId, this);
181 m_vf->setDevice(m_dev);
183 QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
184 this, SIGNAL(runningStateChanged()));
185 QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
186 QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
187 this, SIGNAL(error(const QString&, int, const QString&)));
189 m_notifications->setDevice(m_dev);
194 QVariant Camera::deviceId() const {
198 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
199 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
201 // TODO: seems setting geometry breaks rendering somehow
202 // m_vf->setGeometry(newGeometry);
203 m_vf->resize(newGeometry.size());
206 QtCamDevice *Camera::device() const {
210 bool Camera::setMode(const Camera::CameraMode& mode) {
211 if (m_mode == mode) {
221 if (m_dev->isRunning()) {
228 Camera::CameraMode Camera::mode() {
232 bool Camera::start() {
241 return m_dev->start();
244 bool Camera::stop(bool force) {
246 return m_dev->stop(force);
252 bool Camera::isIdle() {
253 return m_dev ? m_dev->isIdle() : true;
256 bool Camera::isRunning() {
257 return m_dev ? m_dev->isRunning() : false;
260 bool Camera::applyMode() {
261 if (m_mode == Camera::UnknownMode) {
265 if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
266 m_dev->videoMode()->activate();
268 else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
269 m_dev->imageMode()->activate();
275 QString Camera::imageSuffix() const {
276 return m_cam->config()->imageSuffix();
279 QString Camera::videoSuffix() const {
280 return m_cam->config()->videoSuffix();
283 DeclarativeQtCameraNotifications *Camera::notifications() const {
284 return m_notifications->notifications();
287 void Camera::setNotifications(DeclarativeQtCameraNotifications *notifications) {
288 if (m_notifications->setNotifications(notifications)) {
290 if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
291 s->setConfig(m_cam->config());
295 emit notificationsChanged();
299 QRectF Camera::renderArea() const {
300 return m_vf->renderArea();
303 QSizeF Camera::videoResolution() const {
304 return m_vf->videoResolution();
307 void Camera::resetCapabilities() {
308 QtCamDevice *dev = device();
311 m_zoom = new Zoom(dev, this);
315 m_flash = new Flash(dev, this);
319 m_scene = new Scene(dev, this);
323 m_evComp = new EvComp(dev, this);
324 emit evCompChanged();
326 delete m_whiteBalance;
327 m_whiteBalance = new WhiteBalance(dev, this);
328 emit whiteBalanceChanged();
331 m_colorTone = new ColorTone(dev, this);
332 emit colorToneChanged();
335 m_iso = new Iso(dev, this);
339 m_exposure = new Exposure(dev, this);
340 emit exposureChanged();
343 m_aperture = new Aperture(dev, this);
344 emit apertureChanged();
346 delete m_noiseReduction;
347 m_noiseReduction = new NoiseReduction(dev, this);
348 emit noiseReductionChanged();
350 delete m_flickerReduction;
351 m_flickerReduction = new FlickerReduction(dev, this);
352 emit flickerReductionChanged();
355 m_focus = new Focus(dev, this);
359 m_autoFocus = new AutoFocus(dev, this);
360 emit autoFocusChanged();
363 m_roi = new Roi(dev, this);
367 m_videoMute = new VideoMute(dev, this);
368 emit videoMuteChanged();
371 m_videoTorch = new VideoTorch(dev, this);
372 emit videoTorchChanged();
375 Zoom *Camera::zoom() const {
379 Flash *Camera::flash() const {
383 Scene *Camera::scene() const {
387 EvComp *Camera::evComp() const {
391 WhiteBalance *Camera::whiteBalance() const {
392 return m_whiteBalance;
395 ColorTone *Camera::colorTone() const {
399 Iso *Camera::iso() const {
403 Exposure *Camera::exposure() const {
407 Aperture *Camera::aperture() const {
411 NoiseReduction *Camera::noiseReduction() const {
412 return m_noiseReduction;
415 FlickerReduction *Camera::flickerReduction() const {
416 return m_flickerReduction;
419 Focus *Camera::focus() const {
423 AutoFocus *Camera::autoFocus() const {
427 Roi *Camera::roi() const {
431 VideoMute *Camera::videoMute() const {
435 VideoTorch *Camera::videoTorch() const {
439 bool Camera::isRenderingEnabled() const {
440 return m_vf->isRenderingEnabled();
443 void Camera::setRenderingEnabled(bool enabled) {
444 m_vf->setRenderingEnabled(enabled);
447 CameraConfig *Camera::cameraConfig() const {