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)) {
56 void Camera::componentComplete() {
57 QDeclarativeItem::componentComplete();
60 QVariant oldId = m_id;
65 emit deviceCountChanged();
68 int Camera::deviceCount() const {
69 return m_cam ? m_cam->devices().size() : 0;
72 QString Camera::deviceName(int index) const {
73 return m_cam->devices().at(index).first;
76 QVariant Camera::deviceId(int index) const {
77 return m_cam->devices().at(index).second;
80 void Camera::setDeviceId(const QVariant& id) {
85 if (!isComponentComplete()) {
87 emit deviceIdChanged();
91 if (m_dev && m_dev->stop(false)) {
95 qWarning() << "Failed to stop device";
99 m_dev = m_cam->device(id, this);
103 m_vf->setDevice(m_dev);
105 QObject::connect(m_dev, SIGNAL(runningStateChanged(bool)),
106 this, SIGNAL(runningStateChanged()));
107 QObject::connect(m_dev, SIGNAL(idleStateChanged(bool)), this, SIGNAL(idleStateChanged()));
108 QObject::connect(m_dev, SIGNAL(error(const QString&, int, const QString&)),
109 this, SIGNAL(error(const QString&, int, const QString&)));
111 m_notifications->setDevice(m_dev);
113 emit deviceIdChanged();
114 emit deviceChanged();
117 QVariant Camera::deviceId() const {
121 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
122 QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
124 m_vf->setGeometry(newGeometry);
127 QtCamDevice *Camera::device() const {
131 void Camera::setMode(const Camera::CameraMode& mode) {
132 if (m_mode == mode) {
142 if (m_dev->isRunning()) {
149 Camera::CameraMode Camera::mode() {
153 bool Camera::start() {
160 return m_dev->start();
163 bool Camera::stop(bool force) {
165 return m_dev->stop(force);
171 bool Camera::isIdle() {
172 return m_dev ? m_dev->isIdle() : true;
175 bool Camera::isRunning() {
176 return m_dev ? m_dev->isRunning() : false;
179 void Camera::applyMode() {
180 if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
181 m_dev->videoMode()->activate();
183 else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
184 m_dev->imageMode()->activate();
188 QString Camera::imageSuffix() const {
189 return m_cam->config()->imageSuffix();
192 QString Camera::videoSuffix() const {
193 return m_cam->config()->videoSuffix();
196 Notifications *Camera::notifications() const {
197 return m_notifications->notifications();
200 void Camera::setNotifications(Notifications *notifications) {
201 if (m_notifications->setNotifications(notifications)) {
203 if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
204 s->setConfig(m_cam->config());
208 emit notificationsChanged();