2 * This file is part of CameraPlus.
4 * Copyright (C) 2012-2013 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
21 #include "qtcamconfig.h"
22 #include "qtcamimagesettings.h"
23 #include "qtcamvideosettings.h"
24 #include "qtcamquirks.h"
26 #include <QStringList>
30 #define CONFIGURATION_FILE DATA_DIR"/qtcamera.ini"
32 class QtCamConfigPrivate {
34 QString element(const QString& name) const {
35 return conf->value(QString("%1/element").arg(name)).toString();
38 QSize readResolution(const QString key) {
39 QList<QString> parts = conf->value(key).toString().trimmed().split("x");
40 return QSize(parts[0].toInt(), parts[1].toInt());
43 QVariant readWithFallback(const QString& generic, const QString& specific, const QString& key) {
44 QString genericKey = QString("%1/%2").arg(generic).arg(key);
45 QString specificKey = QString("%1/%2").arg(specific).arg(key);
47 QVariant var = conf->value(genericKey);
49 return conf->value(specificKey, var);
54 QList<QtCamImageSettings> imageSettings;
55 QList<QtCamVideoSettings> videoSettings;
58 QtCamConfig::QtCamConfig(QObject *parent) :
59 QObject(parent), d_ptr(new QtCamConfigPrivate) {
61 d_ptr->conf = new QSettings(CONFIGURATION_FILE, QSettings::IniFormat, this);
64 QtCamConfig::QtCamConfig(const QString& configPath, QObject *parent) :
65 QObject(parent), d_ptr(new QtCamConfigPrivate) {
67 d_ptr->conf = new QSettings(configPath, QSettings::IniFormat, this);
70 QtCamConfig::~QtCamConfig() {
74 QString QtCamConfig::deviceScannerType() const {
75 return d_ptr->conf->value("devices/scanner").toString();
78 QString QtCamConfig::deviceScannerProperty() const {
79 return d_ptr->conf->value("devices/property").toString();
82 QString QtCamConfig::videoSource() const {
83 return d_ptr->element("video-source");
86 QString QtCamConfig::viewfinderSink() const {
87 return d_ptr->element("viewfinder-sink");
90 QString QtCamConfig::viewfinderRenderer() const {
91 return d_ptr->conf->value("viewfinder-sink/renderer").toString();
94 bool QtCamConfig::viewfinderUseFence() const {
95 return d_ptr->conf->value("viewfinder-sink/use-fence").toBool();
98 QString QtCamConfig::audioSource() const {
99 return d_ptr->element("audio-source");
102 QString QtCamConfig::wrapperVideoSource() const {
103 return d_ptr->element("wrapper-video-source");
106 QString QtCamConfig::wrapperVideoSourceProperty() const {
107 return d_ptr->conf->value("wrapper-video-source/property").toString();
110 QtCamImageSettings *QtCamConfig::imageSettings(const QVariant& id) {
111 QString generic = "image";
112 QString specific = QString("%1-%2").arg(generic).arg(id.toString());
114 QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
115 QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
116 QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
117 QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
119 QList<QtCamImageResolution> resolutions;
121 foreach (const QString& preset, presets) {
122 d_ptr->conf->beginGroup(preset);
125 QString name = d_ptr->conf->value("name").toString();
126 QSize capture = d_ptr->readResolution("capture");
127 QSize preview = d_ptr->readResolution("preview");
128 QSize viewfinder = d_ptr->readResolution("viewfinder");
129 int fps = d_ptr->conf->value("fps").toInt();
130 int nightFps = d_ptr->conf->value("night").toInt();
131 int megaPixels = d_ptr->conf->value("megapixels").toInt();
132 QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
134 d_ptr->conf->endGroup();
136 resolutions << QtCamImageResolution(id, name, capture, preview, viewfinder,
137 fps, nightFps, megaPixels, aspectRatio);
140 return new QtCamImageSettings(id.toString(), suffix, profileName, profilePath, resolutions);
143 QtCamVideoSettings *QtCamConfig::videoSettings(const QVariant& id) {
144 QString generic = "video";
145 QString specific = QString("%1-%2").arg(generic).arg(id.toString());
147 QString profileName = d_ptr->readWithFallback(generic, specific, "profile-name").toString();
148 QString profilePath = d_ptr->readWithFallback(generic, specific, "profile-path").toString();
149 QString suffix = d_ptr->readWithFallback(generic, specific, "extension").toString();
150 QStringList presets = d_ptr->readWithFallback(generic, specific, "presets").toStringList();
152 QList<QtCamVideoResolution> resolutions;
154 foreach (const QString& preset, presets) {
155 d_ptr->conf->beginGroup(preset);
158 QString name = d_ptr->conf->value("name").toString();
159 QSize capture = d_ptr->readResolution("capture");
160 QSize preview = d_ptr->readResolution("preview");
161 int fps = d_ptr->conf->value("fps").toInt();
162 int nightFps = d_ptr->conf->value("night").toInt();
163 QString aspectRatio = d_ptr->conf->value("aspectratio").toString();
164 QString resolution = d_ptr->conf->value("resolution").toString();
166 d_ptr->conf->endGroup();
168 resolutions << QtCamVideoResolution(id, name, capture, preview,
169 fps, nightFps, aspectRatio, resolution);
172 return new QtCamVideoSettings(id.toString(), suffix, profileName, profilePath, resolutions);
175 QtCamQuirks *QtCamConfig::quirks(const QVariant& id) {
176 QString group = QString("quirks-%1").arg(id.toString());
177 d_ptr->conf->beginGroup(group);
178 QStringList quirks = d_ptr->conf->value("quirks").toStringList();
179 d_ptr->conf->endGroup();
181 QMetaObject m = QtCamQuirks::staticMetaObject;
182 int index = m.indexOfEnumerator("QuirkType");
184 qCritical() << "Failed to obtain QuirkType index";
188 QMetaEnum e = m.enumerator(index);
192 foreach (const QString& quirk, quirks) {
193 int val = e.keyToValue(quirk.toLatin1().constData());
196 qWarning() << "Unknown quirk" << quirk;
203 QtCamQuirks::QuirkTypes types = (QtCamQuirks::QuirkTypes)value;
205 return new QtCamQuirks(types);
208 QString QtCamConfig::imageEncodingProfileName() const {
209 return d_ptr->conf->value("image/profile-name").toString();
212 QString QtCamConfig::imageEncodingProfilePath() const {
213 return d_ptr->conf->value("image/profile-path").toString();
216 QString QtCamConfig::videoEncodingProfileName() const {
217 return d_ptr->conf->value("video/profile-name").toString();
220 QString QtCamConfig::videoEncodingProfilePath() const {
221 return d_ptr->conf->value("video/profile-path").toString();
224 QString QtCamConfig::audioCaptureCaps() const {
225 return d_ptr->conf->value("audio-capture-caps/caps").toString();
228 QString QtCamConfig::imageSuffix() const {
229 return d_ptr->conf->value("image/extension").toString();
232 QString QtCamConfig::videoSuffix() const {
233 return d_ptr->conf->value("video/extension").toString();
236 QStringList QtCamConfig::viewfinderFilters() const {
237 return d_ptr->conf->value("viewfinder-filters/elements").toStringList();
240 QString QtCamConfig::roiElement() const {
241 return d_ptr->conf->value("roi/element").toString();
244 QString QtCamConfig::roiMessageName() const {
245 return d_ptr->conf->value("roi/message").toString();
248 QString QtCamConfig::roiEnableProperty() const {
249 return d_ptr->conf->value("roi/enable").toString();
252 QString QtCamConfig::roiMessage() const {
253 return d_ptr->conf->value("roi/message").toString();
256 bool QtCamConfig::isPreviewSupported() const {
257 return d_ptr->conf->value("general/preview-supported").toBool();