This will simplify the code for QML bindings.
#include "notifications.h"
#include "notificationscontainer.h"
#include "sounds.h"
+#include <QDeclarativeInfo>
// TODO: a viewfinder class that inherits QDeclarativeItem
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:
void Camera::componentComplete() {
QDeclarativeItem::componentComplete();
- if (m_id.isValid()) {
- QVariant oldId = m_id;
- m_id = QVariant();
- setDeviceId(oldId);
- }
-
emit deviceCountChanged();
}
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);
m_notifications->setDevice(m_dev);
- emit deviceIdChanged();
- emit deviceChanged();
+ return true;
}
QVariant Camera::deviceId() 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() {
return false;
}
- applyMode();
+ if (!applyMode()) {
+ return false;
+ }
return m_dev->start();
}
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 {
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);
public:
typedef enum {
+ UnknownMode,
ImageMode,
VideoMode
} CameraMode;
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;
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;
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 {
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 {
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
}
}
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;
}
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 }
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.
}
// 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
id: sceneController
camera: cam
value: ready ? camera.mode == Camera.VideoMode ? settings.videoSceneMode : settings.imageSceneMode : 0
+ onValueChanged: console.log("New scene value " + value);
}
ColorTone {