Implemented video resolution setting
authorMohammed Sameer <msameer@foolab.org>
Sat, 22 Sep 2012 22:41:24 +0000 (01:41 +0300)
committerMohammed Sameer <msameer@foolab.org>
Sat, 22 Sep 2012 22:41:24 +0000 (01:41 +0300)
imports/imports.pro
imports/plugin.cpp
imports/videoresolutionmodel.cpp [new file with mode: 0644]
imports/videoresolutionmodel.h [new file with mode: 0644]
imports/videosettings.cpp [new file with mode: 0644]
imports/videosettings.h [new file with mode: 0644]
qml/VideoSettingsPage.qml
qml/main.qml
src/settings.cpp
src/settings.h

index 4a366b7..095a37a 100644 (file)
@@ -13,9 +13,11 @@ QT += declarative
 HEADERS += plugin.h previewprovider.h camera.h mode.h imagemode.h videomode.h \
            capability.h zoom.h flash.h scene.h evcomp.h videotorch.h whitebalance.h \
            colortone.h exposure.h aperture.h iso.h noisereduction.h \
-           flickerreduction.h mute.h metadata.h imagesettings.h imageresolutionmodel.h
+           flickerreduction.h mute.h metadata.h imagesettings.h imageresolutionmodel.h \
+           videosettings.h videoresolutionmodel.h
 
 SOURCES += plugin.cpp previewprovider.cpp camera.cpp mode.cpp imagemode.cpp videomode.cpp \
            capability.cpp zoom.cpp flash.cpp scene.cpp evcomp.cpp videotorch.cpp whitebalance.cpp \
            colortone.cpp exposure.cpp aperture.cpp iso.cpp noisereduction.cpp \
-           flickerreduction.cpp mute.cpp metadata.cpp imagesettings.cpp imageresolutionmodel.cpp
+           flickerreduction.cpp mute.cpp metadata.cpp imagesettings.cpp imageresolutionmodel.cpp \
+           videosettings.cpp videoresolutionmodel.cpp
index e7a30d9..a08107c 100644 (file)
@@ -40,6 +40,8 @@
 #include "metadata.h"
 #include "imagesettings.h"
 #include "imageresolutionmodel.h"
+#include "videosettings.h"
+#include "videoresolutionmodel.h"
 
 #include <QtDeclarative>
 
@@ -66,9 +68,13 @@ void Plugin::registerTypes(QDeclarativeEngine *engine) {
   qmlRegisterType<Mute>(URI, MAJOR, MINOR, "Mute");
   qmlRegisterType<MetaData>(URI, MAJOR, MINOR, "MetaData");
   qmlRegisterType<ImageSettings>(URI, MAJOR, MINOR, "ImageSettings");
+  qmlRegisterType<VideoSettings>(URI, MAJOR, MINOR, "VideoSettings");
 
   qmlRegisterUncreatableType<ImageResolutionModel>(URI, MAJOR, MINOR, "ImageResolutionModel",
                          "ImageResolutionModel can be obtained from ImageSettings");
+  qmlRegisterUncreatableType<VideoResolutionModel>(URI, MAJOR, MINOR, "VideoResolutionModel",
+                         "VideoResolutionModel can be obtained from VideoSettings");
+
   qmlRegisterType<Mode>();
   qmlRegisterType<Capability>();
 
diff --git a/imports/videoresolutionmodel.cpp b/imports/videoresolutionmodel.cpp
new file mode 100644 (file)
index 0000000..0817077
--- /dev/null
@@ -0,0 +1,109 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "videoresolutionmodel.h"
+#include "qtcamvideosettings.h"
+#include <QDebug>
+
+VideoResolutionModel::VideoResolutionModel(QtCamVideoSettings *settings, QObject *parent) :
+  QAbstractListModel(parent), m_settings(settings) {
+
+  QHash<int, QByteArray> roles;
+  roles[IdRole] = "resolutionId";
+  roles[NameRole] = "resolutionName";
+  roles[CaptureRole] = "captureResolution";
+  roles[PreviewRole] = "previewResolution";
+  roles[FpsRole] = "frameRate";
+  roles[NightFpsRole] = "nightFrameRate";
+  roles[ResolutionRole] = "resolution";
+  roles[AspectRatioRole] = "resolutionAspectRatio";
+
+  setRoleNames(roles);
+
+  m_resolutions = m_settings->resolutions(m_aspectRatio);
+}
+
+VideoResolutionModel::~VideoResolutionModel() {
+  m_settings = 0;
+}
+
+int VideoResolutionModel::rowCount(const QModelIndex& parent) const {
+  if (!parent.isValid()) {
+    return m_resolutions.size();
+  }
+
+  return 0;
+}
+
+QVariant VideoResolutionModel::data(const QModelIndex& index, int role) const {
+  if (index.row() < 0 || index.row() > m_resolutions.size()) {
+    return QVariant();
+  }
+
+  const QtCamVideoResolution& res = m_resolutions[index.row()];
+
+  switch (role) {
+  case IdRole:
+    return res.id();
+
+  case NameRole:
+    return res.name();
+
+  case CaptureRole:
+    return res.captureResolution();
+
+  case PreviewRole:
+    return res.previewResolution();
+
+  case FpsRole:
+    return res.frameRate();
+
+  case NightFpsRole:
+    return res.nightFrameRate();
+
+  case ResolutionRole:
+    return res.resolution();
+
+  case AspectRatioRole:
+    return res.aspectRatio();
+
+  default:
+    return QVariant();
+  }
+}
+
+QString VideoResolutionModel::aspectRatio() const {
+  return m_aspectRatio;
+}
+
+void VideoResolutionModel::setAspectRatio(const QString& aspectRatio) {
+  if (aspectRatio != m_aspectRatio) {
+
+    m_aspectRatio = aspectRatio;
+
+    emit aspectRatioChanged();
+
+    beginResetModel();
+
+    m_resolutions = m_settings->resolutions(m_aspectRatio);
+
+    endResetModel();
+  }
+}
diff --git a/imports/videoresolutionmodel.h b/imports/videoresolutionmodel.h
new file mode 100644 (file)
index 0000000..c461210
--- /dev/null
@@ -0,0 +1,70 @@
+// -*- c++ -*-
+
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef VIDEO_RESOLUTION_MODEL_H
+#define VIDEO_RESOLUTION_MODEL_H
+
+#include <QAbstractListModel>
+
+class QtCamVideoSettings;
+class QtCamVideoResolution;
+
+class VideoResolutionModel : public QAbstractListModel {
+  Q_OBJECT
+
+  Q_PROPERTY(QString aspectRatio READ aspectRatio WRITE setAspectRatio NOTIFY aspectRatioChanged);
+
+public:
+
+  enum ResolutionRoles {
+    IdRole = Qt::UserRole + 1,
+    NameRole,
+    CaptureRole,
+    PreviewRole,
+    FpsRole,
+    NightFpsRole,
+    ResolutionRole,
+    AspectRatioRole,
+  };
+
+  VideoResolutionModel(QtCamVideoSettings *settings, QObject *parent = 0);
+  ~VideoResolutionModel();
+
+  int rowCount(const QModelIndex& parent = QModelIndex()) const;
+
+  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
+
+  QString aspectRatio() const;
+  void setAspectRatio(const QString& aspectRatio);
+
+signals:
+  void aspectRatioChanged();
+
+private:
+  QString m_aspectRatio;
+
+  QtCamVideoSettings *m_settings;
+
+  QList<QtCamVideoResolution> m_resolutions;
+};
+
+#endif /* VIDEO_RESOLUTION_MODEL_H */
diff --git a/imports/videosettings.cpp b/imports/videosettings.cpp
new file mode 100644 (file)
index 0000000..cc06728
--- /dev/null
@@ -0,0 +1,116 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "videosettings.h"
+#include "qtcamvideosettings.h"
+#include "camera.h"
+#include "qtcamdevice.h"
+#include "qtcamvideomode.h"
+#include "videoresolutionmodel.h"
+#include <QDebug>
+
+VideoSettings::VideoSettings(QObject *parent) :
+  QObject(parent), m_cam(0), m_settings(0), m_resolutions(0) {
+
+}
+
+VideoSettings::~VideoSettings() {
+  m_settings = 0;
+}
+
+QString VideoSettings::suffix() const {
+  return m_settings ? m_settings->suffix() : QString();
+}
+
+QStringList VideoSettings::aspectRatios() const {
+  return m_settings ? m_settings->aspectRatios() : QStringList();
+}
+
+Camera *VideoSettings::camera() {
+  return m_cam;
+}
+
+void VideoSettings::setCamera(Camera *camera) {
+  if (camera == m_cam) {
+    return;
+  }
+
+  if (m_cam) {
+    QObject::disconnect(m_cam, SIGNAL(deviceChanged()), this, SLOT(deviceChanged()));
+  }
+
+  m_cam = camera;
+
+  if (m_cam) {
+    QObject::connect(m_cam, SIGNAL(deviceChanged()), this, SLOT(deviceChanged()));
+  }
+
+  emit cameraChanged();
+
+  if (m_cam->device()) {
+    deviceChanged();
+  }
+}
+
+void VideoSettings::deviceChanged() {
+  m_settings = m_cam->device()->videoMode()->settings();
+
+  emit settingsChanged();
+
+  emit readyChanged();
+
+  delete m_resolutions;
+  m_resolutions = 0;
+
+  emit resolutionsChanged();
+}
+
+VideoResolutionModel *VideoSettings::resolutions() {
+  if (!m_settings) {
+    return 0;
+  }
+
+
+  if (!m_resolutions) {
+    m_resolutions = new VideoResolutionModel(m_settings, this);
+  }
+
+  return m_resolutions;
+}
+
+bool VideoSettings::isReady() const {
+  return m_settings;
+}
+
+bool VideoSettings::setResolution(const QString& aspectRatio, const QString& resolution) {
+  if (!isReady()) {
+    return false;
+  }
+
+  QList<QtCamVideoResolution> res = m_settings->resolutions(aspectRatio);
+
+  foreach (const QtCamVideoResolution& r, res) {
+    if (r.name() == resolution) {
+      return m_cam->device()->videoMode()->setResolution(r);
+    }
+  }
+
+  return false;
+}
diff --git a/imports/videosettings.h b/imports/videosettings.h
new file mode 100644 (file)
index 0000000..0bc1767
--- /dev/null
@@ -0,0 +1,73 @@
+// -*- c++ -*-
+
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef VIDEO_SETTINGS_H
+#define VIDEO_SETTINGS_H
+
+#include <QObject>
+#include <QStringList>
+
+class Camera;
+class QtCamVideoSettings;
+class VideoResolutionModel;
+
+class VideoSettings : public QObject {
+  Q_OBJECT
+
+  Q_PROPERTY(Camera* camera READ camera WRITE setCamera NOTIFY cameraChanged);
+  Q_PROPERTY(QString suffix READ suffix NOTIFY settingsChanged);
+  Q_PROPERTY(QStringList aspectRatios READ aspectRatios NOTIFY settingsChanged);
+  Q_PROPERTY(VideoResolutionModel *resolutions READ resolutions NOTIFY resolutionsChanged);
+  Q_PROPERTY(bool ready READ isReady NOTIFY readyChanged);
+
+public:
+  VideoSettings(QObject *parent = 0);
+  ~VideoSettings();
+
+  QString suffix() const;
+  QStringList aspectRatios() const;
+
+  Camera *camera();
+  void setCamera(Camera *camera);
+
+  VideoResolutionModel *resolutions();
+
+  bool isReady() const;
+
+  Q_INVOKABLE bool setResolution(const QString& aspectRatio, const QString& resolution);
+
+signals:
+  void settingsChanged();
+  void cameraChanged();
+  void resolutionsChanged();
+  void readyChanged();
+
+private slots:
+  void deviceChanged();
+
+private:
+  Camera *m_cam;
+  QtCamVideoSettings *m_settings;
+  VideoResolutionModel *m_resolutions;
+};
+
+#endif /* VIDEO_SETTINGS_H */
index d737402..42d487c 100644 (file)
@@ -79,6 +79,38 @@ CameraPage {
                                 }
                         }
 
+                        SectionHeader {
+                                text: qsTr("Resolution");
+                        }
+
+                        ButtonRow {
+                                anchors.horizontalCenter: parent.horizontalCenter
+                                exclusive: false
+
+                                Repeater {
+                                        id: resolutions
+
+                                        model: videoSettings.resolutions
+
+                                        function name(name, res) {
+                                                return name.charAt(0).toUpperCase() + name.slice(1) + " " + res;
+                                        }
+
+                                        delegate: Button {
+                                                text: resolutions.name(resolutionName, resolution);
+                                                checked: settings.videoResolution == resolutionName;
+                                                onClicked: {
+                                                        if (!cam.idle) {
+                                                                showError(qsTr("Camera is busy saving."));
+                                                                return;
+                                                        }
+
+                                                        settings.videoResolution = resolutionName;
+                                                }
+                                        }
+                                }
+                        }
+
                         CameraSettings {
                                 anchors.horizontalCenter: parent.horizontalCenter
                         }
index 4688994..c0a3067 100644 (file)
@@ -179,8 +179,26 @@ PageStackWindow {
                 }
         }
 
+        VideoSettings {
+                id: videoSettings
+                camera: cam
+
+                function setVideoResolution() {
+                        if (!videoSettings.setResolution(settings.videoAspectRatio, settings.videoResolution)) {
+                                showError(qsTr("Failed to set required resolution"));
+                        }
+                }
+
+                onReadyChanged: {
+                        if (ready) {
+                                videoSettings.setVideoResolution();
+                        }
+                }
+        }
+
         Connections {
                 target: settings
+
                 onImageAspectRatioChanged: {
                         imageSettings.setImageResolution();
                 }
@@ -188,6 +206,10 @@ PageStackWindow {
                 onImageResolutionChanged: {
                         imageSettings.setImageResolution();
                 }
+
+                onVideoResolutionChanged: {
+                        videoSettings.setVideoResolution();
+                }
         }
 
         Camera {
@@ -210,6 +232,9 @@ PageStackWindow {
 
                 // TODO: hardcoding device id
                 Component.onCompleted: { cam.deviceId = 0; mode = settings.mode; }
+                Component.onDestruction: {
+                console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
+                }
 
                 // TODO: Hack
                 z: -1
index 9eb2b49..0cecbef 100644 (file)
@@ -36,6 +36,7 @@
 #define DEFAULT_IMAGE_ISO          0
 #define DEFAULT_IMAGE_RESOLUTION   "high"
 #define DEFAULT_IMAGE_ASPECT_RATIO "16:9"
+#define DEFAULT_VIDEO_RESOLUTION   "high"
 
 Settings::Settings(QObject *parent) :
   QObject(parent),
@@ -247,3 +248,27 @@ void Settings::setImageResolution(const QString& resolution) {
     emit imageResolutionChanged();
   }
 }
+
+QString Settings::videoAspectRatio() const {
+  // This is not used for anything so we will return an empty string for now
+  // which will make the backend return all resolutions.
+
+  return QString();
+}
+
+void Settings::setVideoAspectRatio(const QString& aspectRatio) {
+  Q_UNUSED(aspectRatio);
+
+  // This is not used for anything so we will just ignore it.
+}
+
+QString Settings::videoResolution() const {
+  return m_settings->value("video/resolution", DEFAULT_VIDEO_RESOLUTION).toString();
+}
+
+void Settings::setVideoResolution(const QString& resolution) {
+  if (resolution != videoResolution()) {
+    m_settings->setValue("video/resolution", resolution);
+    emit videoResolutionChanged();
+  }
+}
index 15f3663..f2a0fd0 100644 (file)
@@ -51,6 +51,9 @@ class Settings : public QObject {
   Q_PROPERTY(QString imageAspectRatio READ imageAspectRatio WRITE setImageAspectRatio NOTIFY imageAspectRatioChanged);
   Q_PROPERTY(QString imageResolution READ imageResolution WRITE setImageResolution NOTIFY imageResolutionChanged);
 
+  Q_PROPERTY(QString videoAspectRatio READ videoAspectRatio WRITE setVideoAspectRatio NOTIFY videoAspectRatioChanged);
+  Q_PROPERTY(QString videoResolution READ videoResolution WRITE setVideoResolution NOTIFY videoResolutionChanged);
+
 public:
   Settings(QObject *parent = 0);
   ~Settings();
@@ -106,6 +109,12 @@ public:
   QString imageResolution() const;
   void setImageResolution(const QString& resolution);
 
+  QString videoAspectRatio() const;
+  void setVideoAspectRatio(const QString& aspectRatio);
+
+  QString videoResolution() const;
+  void setVideoResolution(const QString& resolution);
+
 signals:
   void modeChanged();
   void creatorNameChanged();
@@ -124,6 +133,8 @@ signals:
   void imageIsoChanged();
   void imageAspectRatioChanged();
   void imageResolutionChanged();
+  void videoAspectRatioChanged();
+  void videoResolutionChanged();
 
 private:
   QSettings *m_settings;