One cannot set mode and deviceId. Use camera::reset() instead.
authorMohammed Sameer <msameer@foolab.org>
Fri, 7 Dec 2012 23:25:54 +0000 (01:25 +0200)
committerMohammed Sameer <msameer@foolab.org>
Fri, 7 Dec 2012 23:25:54 +0000 (01:25 +0200)
This will simplify the code for QML bindings.

imports/camera.cpp
imports/camera.h
qml/ModeButton.qml
qml/ModeController.qml
qml/main.qml

index 861b401..66d5912 100644 (file)
@@ -29,6 +29,7 @@
 #include "notifications.h"
 #include "notificationscontainer.h"
 #include "sounds.h"
+#include <QDeclarativeInfo>
 
 // TODO: a viewfinder class that inherits QDeclarativeItem
 
@@ -37,7 +38,7 @@ Camera::Camera(QDeclarativeItem *parent) :
   m_cam(new QtCamera(this)),
   m_dev(0),
   m_vf(new QtCamGraphicsViewfinder(m_cam->config(), this)),
-  m_mode(Camera::ImageMode),
+  m_mode(Camera::UnknownMode),
   m_notifications(new NotificationsContainer(this)) {
 
   // TODO:
@@ -56,12 +57,6 @@ Camera::~Camera() {
 void Camera::componentComplete() {
   QDeclarativeItem::componentComplete();
 
-  if (m_id.isValid()) {
-    QVariant oldId = m_id;
-    m_id = QVariant();
-    setDeviceId(oldId);
-  }
-
   emit deviceCountChanged();
 }
 
@@ -77,28 +72,52 @@ QVariant Camera::deviceId(int index) const {
   return m_cam->devices().at(index).second;
 }
 
-void Camera::setDeviceId(const QVariant& id) {
-  if (id == m_id) {
-    return;
+bool Camera::reset(const QVariant& deviceId, const CameraMode& mode) {
+  if (mode == Camera::UnknownMode) {
+    qmlInfo(this) << "Cannot set mode to unknown";
+    return false;
   }
 
   if (!isComponentComplete()) {
-    m_id = id;
-    emit deviceIdChanged();
-    return;
+    qmlInfo(this) << "Component is still not ready";
+    return false;
+  }
+
+  QVariant oldId = m_id;
+  Camera::CameraMode oldMode = m_mode;
+
+  if (setDeviceId(deviceId) && setMode(mode)) {
+    if (oldId != m_id) {
+      emit deviceIdChanged();
+      emit deviceChanged();
+    }
+
+    if (oldMode != m_mode) {
+      emit modeChanged();
+    }
+
+    return true;
+  }
+
+  return false;
+}
+
+bool Camera::setDeviceId(const QVariant& deviceId) {
+  if (deviceId == m_id) {
+    return true;
   }
 
   if (m_dev && m_dev->stop(false)) {
     delete m_dev;
   }
   else if (m_dev) {
-    qWarning() << "Failed to stop device";
-    return;
+    qmlInfo(this) << "Failed to stop device";
+    return false;
   }
 
-  m_dev = m_cam->device(id, this);
+  m_dev = m_cam->device(deviceId, this);
 
-  m_id = id;
+  m_id = deviceId;
 
   m_vf->setDevice(m_dev);
 
@@ -110,8 +129,7 @@ void Camera::setDeviceId(const QVariant& id) {
 
   m_notifications->setDevice(m_dev);
 
-  emit deviceIdChanged();
-  emit deviceChanged();
+  return true;
 }
 
 QVariant Camera::deviceId() const {
@@ -128,22 +146,22 @@ QtCamDevice *Camera::device() const {
   return m_dev;
 }
 
-void Camera::setMode(const Camera::CameraMode& mode) {
+bool Camera::setMode(const Camera::CameraMode& mode) {
   if (m_mode == mode) {
-    return;
+    return true;
   }
 
-  m_mode = mode;
-
   if (!m_dev) {
-    return;
+    return false;
   }
 
+  m_mode = mode;
+
   if (m_dev->isRunning()) {
     applyMode();
   }
 
-  emit modeChanged();
+  return true;
 }
 
 Camera::CameraMode Camera::mode() {
@@ -155,7 +173,9 @@ bool Camera::start() {
     return false;
   }
 
-  applyMode();
+  if (!applyMode()) {
+    return false;
+  }
 
   return m_dev->start();
 }
@@ -176,13 +196,19 @@ bool Camera::isRunning() {
   return m_dev ? m_dev->isRunning() : false;
 }
 
-void Camera::applyMode() {
+bool Camera::applyMode() {
+  if (m_mode == Camera::UnknownMode) {
+    return false;
+  }
+
   if (m_mode == Camera::VideoMode && m_dev->activeMode() != m_dev->videoMode()) {
     m_dev->videoMode()->activate();
   }
   else if (m_mode == Camera::ImageMode && m_dev->activeMode() != m_dev->imageMode()) {
     m_dev->imageMode()->activate();
   }
+
+  return true;
 }
 
 QString Camera::imageSuffix() const {
index 2f072ba..6c3b88b 100644 (file)
@@ -37,8 +37,8 @@ class Camera : public QDeclarativeItem {
   Q_OBJECT
 
   Q_PROPERTY(int deviceCount READ deviceCount NOTIFY deviceCountChanged)
-  Q_PROPERTY(QVariant deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged);
-  Q_PROPERTY(CameraMode mode READ mode WRITE setMode NOTIFY modeChanged);
+  Q_PROPERTY(QVariant deviceId READ deviceId NOTIFY deviceIdChanged);
+  Q_PROPERTY(CameraMode mode READ mode NOTIFY modeChanged);
   Q_PROPERTY(bool idle READ isIdle NOTIFY idleStateChanged);
   Q_PROPERTY(bool running READ isRunning NOTIFY runningStateChanged);
   Q_PROPERTY(QString imageSuffix READ imageSuffix CONSTANT);
@@ -49,6 +49,7 @@ class Camera : public QDeclarativeItem {
 
 public:
   typedef enum {
+    UnknownMode,
     ImageMode,
     VideoMode
   } CameraMode;
@@ -62,10 +63,10 @@ public:
   Q_INVOKABLE QString deviceName(int index) const;
   Q_INVOKABLE QVariant deviceId(int index) const;
 
-  void setDeviceId(const QVariant& id);
+  Q_INVOKABLE bool reset(const QVariant& deviceId, const CameraMode& mode);
+
   QVariant deviceId() const;
 
-  void setMode(const CameraMode& mode);
   CameraMode mode();
 
   QtCamDevice *device() const;
@@ -96,7 +97,9 @@ protected:
   void geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry);
 
 private:
-  void applyMode();
+  bool applyMode();
+  bool setDeviceId(const QVariant& deviceId);
+  bool setMode(const CameraMode& mode);
 
   QtCamera *m_cam;
   QtCamDevice *m_dev;
index 2d9618f..3f4149e 100644 (file)
@@ -44,7 +44,7 @@ Rectangle {
                 height: parent.width
                 color: mouse.pressed ? "lightblue" : "white"
                 radius: parent.width
-                y: mode == 1 ? video.y : image.y
+                y: mode == Camera.VideoMode ? video.y : image.y
         }
 
         Column {
@@ -54,7 +54,7 @@ Rectangle {
                         height: width
                         property string released: "icon-m-viewfinder-camera"
                         property string active: "icon-m-viewfinder-camera-selected"
-                        source: mouse.pressed ? "image://theme/" + released : button.mode == 0 ? "image://theme/" + active : "image://theme/" + released
+                        source: mouse.pressed ? "image://theme/" + released : button.mode == Camera.ImageMode ? "image://theme/" + active : "image://theme/" + released
                 }
 
                 Image {
@@ -63,7 +63,7 @@ Rectangle {
                         height: width
                         property string released: "icon-m-camera-video-record"
                         property string active: "icon-m-camera-video-selected"
-                        source: mouse.pressed ? "image://theme/" + released : button.mode == 1 ? "image://theme/" + active : "image://theme/" + released
+                        source: mouse.pressed ? "image://theme/" + released : button.mode == Camera.VideoMode ? "image://theme/" + active : "image://theme/" + released
                 }
         }
 
@@ -77,10 +77,10 @@ Rectangle {
                 onReleased: {
 
                         if (!drag.active) {
-                                if (mode == 0) {
+                                if (mode == Camera.ImageMode) {
                                         settings.mode = Camera.VideoMode;
                                 }
-                                else {
+                                else if (mode == Camera.VideoMode) {
                                         settings.mode = Camera.ImageMode;
                                 }
 
index 8d0cd58..082a8b0 100644 (file)
@@ -33,10 +33,10 @@ Item {
 
         SequentialAnimation {
                 id: animation
-                property int mode: 0
+                property int mode: Camera.UnknownMode
 
                 function setMode() {
-                        cam.mode = mode;
+                        root.resetCamera(cam.deviceId, mode);
                 }
 
                 NumberAnimation { target: dimmer; property: "opacity"; from: 0; to: 1; duration: 150; alwaysRunToEnd: true }
index 59e1f56..5fdc7f5 100644 (file)
@@ -69,6 +69,12 @@ PageStackWindow {
                 error.show();
         }
 
+        function resetCamera(deviceId, mode) {
+                if (!cam.reset(deviceId, mode)) {
+                        showError(qsTr("Failed to set camera device and mode. Please restart the application."));
+                }
+        }
+
         PositionSource {
                 // NOTE: The source will not reset the position when we lose the signal.
                 // This shouldn't be a big problem as we are course enough.
@@ -239,7 +245,7 @@ PageStackWindow {
                 }
 
                 // TODO: hardcoding device id
-                Component.onCompleted: { cam.deviceId = 0; mode = settings.mode; }
+                Component.onCompleted: { root.resetCamera(0, settings.mode); }
                 Component.onDestruction: cam.stop();
 
                 // TODO: Hack
@@ -263,6 +269,7 @@ PageStackWindow {
                 id: sceneController
                 camera: cam
                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoSceneMode : settings.imageSceneMode : 0
+                onValueChanged: console.log("New scene value " + value);
         }
 
         ColorTone {