Added copyright headers and COPYING file.
[harmattan/cameraplus] / lib / qtcamimagemode.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 "qtcamimagemode.h"
22 #include "qtcammode_p.h"
23 #include "qtcamdevice_p.h"
24 #include "qtcamimagesettings.h"
25 #include "qtcamdevice.h"
26
27 class QtCamImageModePrivate : public QtCamModePrivate {
28 public:
29   QtCamImageModePrivate(QtCamDevicePrivate *dev) :
30   QtCamModePrivate(dev),
31   settings(dev->conf->defaultImageSettings()) {
32
33   }
34
35   ~QtCamImageModePrivate() {
36   }
37
38   QtCamImageSettings settings;
39 };
40
41 QtCamImageMode::QtCamImageMode(QtCamDevicePrivate *dev, QObject *parent) :
42   QtCamMode(new QtCamImageModePrivate(dev), "mode-image", "image-done", parent) {
43
44   d = (QtCamImageModePrivate *)d_ptr;
45
46   QString name = d_ptr->dev->conf->imageEncodingProfileName();
47   QString path = d_ptr->dev->conf->imageEncodingProfilePath();
48
49   if (!name.isEmpty() && !path.isEmpty()) {
50     GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
51     if (profile) {
52       setProfile(profile);
53     }
54   }
55 }
56
57 QtCamImageMode::~QtCamImageMode() {
58   d = 0;
59 }
60
61 bool QtCamImageMode::canCapture() {
62   return QtCamMode::canCapture() && d_ptr->dev->isWrapperReady();
63 }
64
65 void QtCamImageMode::applySettings() {
66   bool night = d_ptr->inNightMode();
67
68   int fps = night ? d->settings.nightFrameRate() : d->settings.frameRate();
69
70   d_ptr->setCaps("viewfinder-caps", d->settings.viewfinderResolution(), fps);
71
72   // FIXME:
73   // Ideally, we should query the image-capture-supported-caps and get a proper framerate
74   // as it seems that subdevsrc2 can only capture 15 FPS for at least the highest resolution
75   // we use. For now we will not set any FPS.
76   d_ptr->setCaps("image-capture-caps", d->settings.captureResolution(), -1);
77
78   setPreviewSize(d->settings.previewResolution());
79
80   // TODO: ?
81   // d_ptr->resetCaps("video-capture-caps");
82 }
83
84 void QtCamImageMode::start() {
85   // Nothing
86 }
87
88 void QtCamImageMode::stop() {
89   // Nothing
90 }
91
92 bool QtCamImageMode::capture(const QString& fileName) {
93   if (!canCapture()) {
94     return false;
95   }
96
97   if (fileName.isEmpty()) {
98     return false;
99   }
100
101   setFileName(fileName);
102
103   g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
104   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
105
106   return true;
107 }
108
109 bool QtCamImageMode::setSettings(const QtCamImageSettings& settings) {
110   d->settings = settings;
111
112   if (!d_ptr->dev->q_ptr->isRunning() || !d_ptr->dev->q_ptr->isIdle()) {
113     return false;
114   }
115
116   applySettings();
117
118   return true;
119 }
120
121 void QtCamImageMode::setProfile(GstEncodingProfile *profile) {
122   if (!d_ptr->dev->cameraBin) {
123     gst_encoding_profile_unref(profile);
124     return;
125   }
126
127   g_object_set(d_ptr->dev->cameraBin, "image-profile", profile, NULL);
128 }