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"
29 #include "notifications.h"
30 #include "notificationscontainer.h"
33 // TODO: a viewfinder class that inherits QDeclarativeItem
35 Camera::Camera(QDeclarativeItem *parent) :
36 QDeclarativeItem(parent),
37 m_cam(new QtCamera(this)),
39 m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
40 m_mode(Camera::ImageMode),
41 m_notifications(new NotificationsContainer(this)) {
50 void Camera::componentComplete() {
51 QDeclarativeItem::componentComplete();
54 QVariant oldId = m_id;
59 emit deviceCountChanged();
62 int Camera::deviceCount() const {
63 return m_cam ? m_cam->devices().size() : 0;
66 QString Camera::deviceName(int index) const {
67 return m_cam->devices().at(index).first;
70 QVariant Camera::deviceId(int index) const {
71 return m_cam->devices().at(index).second;
74 void Camera::setDeviceId(const QVariant& id) {
79 if (!isComponentComplete()) {
81 emit deviceIdChanged();
85 if (m_dev && m_dev->stop()) {
89 qWarning() << "Failed to stop device";
93 m_dev = m_cam->device(id, this);
97 m_vf->setDevice(m_dev);
99 QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
100 this, SIGNAL(runningStateChanged()));
101 QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
102 QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
103 this, SIGNAL(error(const QString&, int, const QString&)));
105 m_notifications->setDevice(m_dev);
107 emit deviceIdChanged();
108 emit deviceChanged();
111 QVariant Camera::deviceId() const {
115 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
116 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
118 m_vf->setGeometry(newGeometry);
121 QtCamDevice *Camera::device() const {
125 void Camera::setMode(const Camera::CameraMode& mode) {
126 if (m_mode == mode) {
136 if (m_dev->isRunning()) {
143 Camera::CameraMode Camera::mode() {
147 bool Camera::start() {
154 return m_dev->start();
157 bool Camera::stop() {
159 return m_dev->stop();
165 bool Camera::isIdle() {
166 return m_dev ? m_dev->isIdle() : true;
169 bool Camera::isRunning() {
170 return m_dev ? m_dev->isRunning() : false;
173 void Camera::applyMode() {
174 if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
175 m_dev->videoMode()->activate();
177 else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
178 m_dev->imageMode()->activate();
182 QString Camera::imageSuffix() const {
183 return m_cam->config()->imageSuffix();
186 QString Camera::videoSuffix() const {
187 return m_cam->config()->videoSuffix();
190 Notifications *Camera::notifications() const {
191 return m_notifications->notifications();
194 void Camera::setNotifications(Notifications *notifications) {
195 if (m_notifications->setNotifications(notifications)) {
197 if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
198 s->setConfig(m_cam->config());
202 emit notificationsChanged();