2 * This file is part of CameraPlus.
4 * Copyright (C) 2012 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"
30 // TODO: a viewfinder class that inherits QDeclarativeItem
32 Camera::Camera(QDeclarativeItem *parent) :
33 QDeclarativeItem(parent),
34 m_cam(new QtCamera(this)),
36 m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
37 m_mode(Camera::ImageMode) {
46 void Camera::componentComplete() {
47 QDeclarativeItem::componentComplete();
50 QVariant oldId = m_id;
55 emit deviceCountChanged();
58 int Camera::deviceCount() const {
59 return m_cam ? m_cam->devices().size() : 0;
62 QString Camera::deviceName(int index) const {
63 return m_cam->devices().at(index).first;
66 QVariant Camera::deviceId(int index) const {
67 return m_cam->devices().at(index).second;
70 void Camera::setDeviceId(const QVariant& id) {
75 if (!isComponentComplete()) {
77 emit deviceIdChanged();
81 if (m_dev && m_dev->stop()) {
85 qWarning() << "Failed to stop device";
89 m_dev = m_cam->device(id, this);
93 m_vf->setDevice(m_dev);
95 QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
96 this, SIGNAL(runningStateChanged()));
97 QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
98 QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
99 this, SIGNAL(error(const QString&, int, const QString&)));
101 emit deviceIdChanged();
102 emit deviceChanged();
105 QVariant Camera::deviceId() const {
109 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
110 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
112 m_vf->setGeometry(newGeometry);
115 QtCamDevice *Camera::device() const {
119 void Camera::setMode(const Camera::CameraMode& mode) {
120 if (m_mode == mode) {
130 if (m_dev->isRunning()) {
137 Camera::CameraMode Camera::mode() {
141 bool Camera::start() {
148 return m_dev->start();
151 bool Camera::stop() {
153 return m_dev->stop();
159 bool Camera::isIdle() {
160 return m_dev ? m_dev->isIdle() : true;
163 bool Camera::isRunning() {
164 return m_dev ? m_dev->isRunning() : false;
167 void Camera::applyMode() {
168 if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
169 m_dev->videoMode()->activate();
171 else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
172 m_dev->imageMode()->activate();
176 QString Camera::imageSuffix() const {
177 return m_cam->config()->imageSuffix();
180 QString Camera::videoSuffix() const {
181 return m_cam->config()->videoSuffix();