861b401bfdf6d6bb37341a0763732324d4467a19
[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 #include "notifications.h"
30 #include "notificationscontainer.h"
31 #include "sounds.h"
32
33 // TODO: a viewfinder class that inherits QDeclarativeItem
34
35 Camera::Camera(QDeclarativeItem *parent) :
36   QDeclarativeItem(parent),
37   m_cam(new QtCamera(this)),
38   m_dev(0),
39   m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
40   m_mode(Camera::ImageMode),
41   m_notifications(new NotificationsContainer(this)) {
42
43   // TODO:
44 }
45
46 Camera::~Camera() {
47   if (m_dev) {
48     m_dev->stop(true);
49     m_dev->deleteLater();
50     m_dev = 0;
51   }
52
53   // TODO: cleanup
54 }
55
56 void Camera::componentComplete() {
57   QDeclarativeItem::componentComplete();
58
59   if (m_id.isValid()) {
60     QVariant oldId = m_id;
61     m_id = QVariant();
62     setDeviceId(oldId);
63   }
64
65   emit deviceCountChanged();
66 }
67
68 int Camera::deviceCount() const {
69   return m_cam ? m_cam->devices().size() : 0;
70 }
71
72 QString Camera::deviceName(int index) const {
73   return m_cam->devices().at(index).first;
74 }
75
76 QVariant Camera::deviceId(int index) const {
77   return m_cam->devices().at(index).second;
78 }
79
80 void Camera::setDeviceId(const QVariant& id) {
81   if (id == m_id) {
82     return;
83   }
84
85   if (!isComponentComplete()) {
86     m_id = id;
87     emit deviceIdChanged();
88     return;
89   }
90
91   if (m_dev && m_dev->stop(false)) {
92     delete m_dev;
93   }
94   else if (m_dev) {
95     qWarning() << "Failed to stop device";
96     return;
97   }
98
99   m_dev = m_cam->device(id, this);
100
101   m_id = id;
102
103   m_vf->setDevice(m_dev);
104
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&)));
110
111   m_notifications->setDevice(m_dev);
112
113   emit deviceIdChanged();
114   emit deviceChanged();
115 }
116
117 QVariant Camera::deviceId() const {
118   return m_id;
119 }
120
121 void Camera::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry) {
122   QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
123
124   m_vf->setGeometry(newGeometry);
125 }
126
127 QtCamDevice *Camera::device() const {
128   return m_dev;
129 }
130
131 void Camera::setMode(const Camera::CameraMode& mode) {
132   if (m_mode == mode) {
133     return;
134   }
135
136   m_mode = mode;
137
138   if (!m_dev) {
139     return;
140   }
141
142   if (m_dev->isRunning()) {
143     applyMode();
144   }
145
146   emit modeChanged();
147 }
148
149 Camera::CameraMode Camera::mode() {
150   return m_mode;
151 }
152
153 bool Camera::start() {
154   if (!m_dev) {
155     return false;
156   }
157
158   applyMode();
159
160   return m_dev->start();
161 }
162
163 bool Camera::stop(bool force) {
164   if (m_dev) {
165     return m_dev->stop(force);
166   }
167
168   return true;
169 }
170
171 bool Camera::isIdle() {
172   return m_dev ? m_dev->isIdle() : true;
173 }
174
175 bool Camera::isRunning() {
176   return m_dev ? m_dev->isRunning() : false;
177 }
178
179 void Camera::applyMode() {
180   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
181     m_dev->videoMode()->activate();
182   }
183   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
184     m_dev->imageMode()->activate();
185   }
186 }
187
188 QString Camera::imageSuffix() const {
189   return m_cam->config()->imageSuffix();
190 }
191
192 QString Camera::videoSuffix() const {
193   return m_cam->config()->videoSuffix();
194 }
195
196 Notifications *Camera::notifications() const {
197   return m_notifications->notifications();
198 }
199
200 void Camera::setNotifications(Notifications *notifications) {
201   if (m_notifications->setNotifications(notifications)) {
202
203     if (Sounds *s = dynamic_cast<Sounds *>(notifications)) {
204       s->setConfig(m_cam->config());
205       s->reload();
206     }
207
208     emit notificationsChanged();
209   }
210 }