Show an error if we fail to start the camera pipeline
[harmattan/cameraplus] / qml / main.qml
1 // -*- qml -*-
2 import QtQuick 1.1
3 import com.nokia.meego 1.1
4 import com.nokia.extras 1.1
5 import QtCamera 1.0
6 import CameraPlus 1.0
7
8 // TODO: metadata creator name, gps, geotags
9 // TODO: resolutions and aspect ratios
10 // TODO: postcapture
11 // TODO: battery low state
12 // TODO: disk space
13 // TODO: flash not ready
14 // TODO: focus, caf, ...
15 // TODO: indicators
16 // TODO: portrait/landscape
17 // TODO: record in a hidden directory and then copy the video to avoid tracker indexing it.
18
19 PageStackWindow {
20         id: root
21
22         property alias dimmer: camDimmer
23
24         showStatusBar: false
25
26         Component.onCompleted: {
27                 theme.inverted = true;
28                 if (settings.mode == 0) {
29                         openFile("ImagePage.qml");
30                 }
31                 else {
32                         openFile("VideoPage.qml");
33                 }
34         }
35
36         function showError(msg) {
37                 error.text = msg;
38                 error.show();
39         }
40
41         InfoBanner {
42                 id: error
43         }
44
45         Settings {
46                 id: settings
47         }
48
49         FileNaming {
50                 id: fileNaming
51                 imageSuffix: cam.imageSuffix
52                 videoSuffix: cam.videoSuffix
53         }
54
55         // Stolen from https://qt.gitorious.org/qt-components/qt-components/blobs/master/examples/meego/QmlComponentGallery/qml/ListPage.qml
56         function replacePage(file) {
57                 var component = Qt.createComponent(file)
58
59                 if (component.status == Component.Ready) {
60                         pageStack.replace(component, {cam: cam}, true);
61                 }
62                 else {
63                         console.log("Error loading component:", component.errorString());
64                 }
65         }
66
67         function openFile(file) {
68                 var component = Qt.createComponent(file)
69
70                 if (component.status == Component.Ready) {
71                         pageStack.push(component, {cam: cam});
72                 }
73                 else {
74                         console.log("Error loading component:", component.errorString());
75                 }
76         }
77
78         platformStyle: PageStackWindowStyle {
79                 // TODO: Hack
80                 background: " "
81         }
82
83         Connections {
84                 target: platformWindow
85                 onActiveChanged: {
86                         if (platformWindow.active) {
87                                 if (!cam.start()) {
88                                         showError("Camera failed to start. Please restart the camera.");
89                                 }
90                         }
91                         else {
92                                 // This is a noop if camera is not idle so calling it will not hurt
93                                 cam.stop();
94                         }
95                 }
96         }
97
98         Camera {
99                 onIdleChanged: {
100                         if (idle && !platformWindow.active) {
101                                 stop();
102                         }
103                 }
104
105                 onDeviceIdChanged: {
106                         if (platformWindow.active) {
107                                 cam.start();
108                         }
109                 }
110
111                 id: cam
112                 anchors.fill: parent
113
114                 // TODO: hardcoding device id
115                 Component.onCompleted: { cam.deviceId = 0; mode = settings.mode; }
116
117                 // TODO: Hack
118                 z: -1
119
120                 Rectangle {
121                         id: camDimmer
122                         z: 1
123                         anchors.fill: parent
124                         opacity: 0
125                         color: "black"
126                 }
127         }
128
129         Scene {
130                 id: sceneController
131                 camera: cam
132                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoSceneMode : settings.imageSceneMode : 0
133         }
134
135         ColorTone {
136                 id: colorToneController
137                 camera: cam
138                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoColorFilter : settings.imageColorFilter : 0
139         }
140
141         WhiteBalance {
142                 id: whiteBalanceController
143                 camera: cam
144                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoWhiteBalance : settings.imageWhiteBalance : 0
145         }
146
147         ModeController {
148                 id: cameraMode
149                 cam: cam
150                 dimmer: root.dimmer
151         }
152
153         Connections {
154                 target: cam
155                 onModeChanged: {
156                         if (cam.mode == Camera.VideoMode) {
157                                 replacePage("VideoPage.qml");
158                         }
159                         else {
160                                 replacePage("ImagePage.qml");
161                         }
162                 }
163         }
164 }