Added copyright headers and COPYING file.
[harmattan/cameraplus] / imports / camera.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "camera.h"
22 #include "qtcamera.h"
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
30 // TODO: a viewfinder class that inherits QDeclarativeItem
31
32 Camera::Camera(QDeclarativeItem *parent) :
33   QDeclarativeItem(parent),
34   m_cam(new QtCamera(this)),
35   m_dev(0),
36   m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
37   m_mode(Camera::ImageMode) {
38
39   // TODO:
40 }
41
42 Camera::~Camera() {
43   // TODO:
44 }
45
46 void Camera::componentComplete() {
47   QDeclarativeItem::componentComplete();
48
49   if (m_id.isValid()) {
50     QVariant oldId = m_id;
51     m_id = QVariant();
52     setDeviceId(oldId);
53   }
54
55   emit deviceCountChanged();
56 }
57
58 int Camera::deviceCount() const {
59   return m_cam ? m_cam->devices().size() : 0;
60 }
61
62 QString Camera::deviceName(int index) const {
63   return m_cam->devices().at(index).first;
64 }
65
66 QVariant Camera::deviceId(int index) const {
67   return m_cam->devices().at(index).second;
68 }
69
70 void Camera::setDeviceId(const QVariant& id) {
71   if (id == m_id) {
72     return;
73   }
74
75   if (!isComponentComplete()) {
76     m_id = id;
77     emit deviceIdChanged();
78     return;
79   }
80
81   if (m_dev && m_dev->stop()) {
82     delete m_dev;
83   }
84   else if (m_dev) {
85     qWarning() << "Failed to stop device";
86     return;
87   }
88
89   m_dev = m_cam->device(id, this);
90
91   m_id = id;
92
93   m_vf->setDevice(m_dev);
94
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&)));
100
101   emit deviceIdChanged();
102   emit deviceChanged();
103 }
104
105 QVariant Camera::deviceId() const {
106   return m_id;
107 }
108
109 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
110   QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
111
112   m_vf->setGeometry(newGeometry);
113 }
114
115 QtCamDevice *Camera::device() const {
116   return m_dev;
117 }
118
119 void Camera::setMode(const Camera::CameraMode& mode) {
120   if (m_mode == mode) {
121     return;
122   }
123
124   m_mode = mode;
125
126   if (!m_dev) {
127     return;
128   }
129
130   if (m_dev->isRunning()) {
131     applyMode();
132   }
133
134   emit modeChanged();
135 }
136
137 Camera::CameraMode Camera::mode() {
138   return m_mode;
139 }
140
141 bool Camera::start() {
142   if (!m_dev) {
143     return false;
144   }
145
146   applyMode();
147
148   return m_dev->start();
149 }
150
151 bool Camera::stop() {
152   if (m_dev) {
153     return m_dev->stop();
154   }
155
156   return true;
157 }
158
159 bool Camera::isIdle() {
160   return m_dev ? m_dev->isIdle() : true;
161 }
162
163 bool Camera::isRunning() {
164   return m_dev ? m_dev->isRunning() : false;
165 }
166
167 void Camera::applyMode() {
168   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
169     m_dev->videoMode()->activate();
170   }
171   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
172     m_dev->imageMode()->activate();
173   }
174 }
175
176 QString Camera::imageSuffix() const {
177   return m_cam->config()->imageSuffix();
178 }
179
180 QString Camera::videoSuffix() const {
181   return m_cam->config()->videoSuffix();
182 }