Debian Codegraph @

qml/Viewfinder/ViewFinderView.qml

raw
1 /*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.12
18 import QtQuick.Window 2.2
19 import Lomiri.Components 1.3
20 import Lomiri.Components.Popups 1.3
21 import QtMultimedia 5.9
22 import CameraApp 0.1
23 //import QtGraphicalEffects 1.0
24 import Lomiri.Content 1.3
25
26 import "../components"
27
28 FocusScope {
29 id: viewFinderView
30
31 property bool overlayVisible: true
32 property bool optionValueSelectorVisible: false
33 property bool touchAcquired: viewFinderOverlay.touchAcquired || camera.videoRecorder.recorderState == CameraRecorder.RecordingState
34 property bool inView
35 property alias captureMode: camera.captureMode
36 property alias finderOverlay: viewFinderOverlay
37 property real aspectRatio: viewFinder.sourceRect.height != 0 ? viewFinder.sourceRect.width / viewFinder.sourceRect.height : 1.0
38 property bool readOnly : false
39 property var cameraFilters : []
40 property string recentlyScannedTag: ""
41 signal photoTaken(string filePath)
42 signal videoShot(string filePath)
43
44 Connections {
45 target: viewFinderOverlay
46 onStatusChanged: decideCameraState()
47 }
48 Connections {
49 target: Qt.application
50 onActiveChanged: if (Qt.application.active && camera.failedToConnect) decideCameraState()
51 }
52
53 function decideCameraState() {
54 if (viewFinderOverlay.status == Loader.Ready) {
55 camera.cameraState = Camera.ActiveState;
56 }
57 }
58
59 property Camera camera: Camera {
60 id: camera
61 captureMode: Camera.CaptureStillImage
62 cameraState: Camera.UnloadedState
63 StateSaver.properties: "captureMode, position"
64 property bool failedToConnect: false
65
66 function manualFocus(x, y) {
67 var normalizedPoint = viewFinder.mapPointToSourceNormalized(Qt.point(x, y - viewFinder.y));
68 if (normalizedPoint.x >= 0.0 && normalizedPoint.x <= 1.0 &&
69 normalizedPoint.y >= 0.0 && normalizedPoint.y <= 1.0) {
70 viewFinderOverlay.showFocusRing(x, y);
71 autoFocusTimer.restart();
72 focus.focusMode = Camera.FocusAuto;
73 focus.customFocusPoint = normalizedPoint;
74 focus.focusPointMode = Camera.FocusPointCustom;
75
76 searchAndLock();
77 }
78 }
79
80 function autoFocus() {
81 unlock();
82
83 focus.focusMode = Camera.FocusContinuous;
84 focus.focusPointMode = Camera.FocusPointAuto;
85 }
86
87 property var autoFocusTimer: Timer {
88 interval: 5000
89 onTriggered: camera.autoFocus();
90 }
91
92 focus {
93 focusMode: Camera.FocusContinuous
94 focusPointMode: Camera.FocusPointAuto
95 }
96
97 property AdvancedCameraSettings advanced: AdvancedCameraSettings {
98 id: advancedCamera
99 camera: camera
100 }
101
102 /* Use only digital zoom for now as it's what phone cameras mostly use.
103 TODO: if optical zoom is available, maximumZoom should be the combined
104 range of optical and digital zoom and currentZoom should adjust the two
105 transparently based on the value. */
106 property alias currentZoom: camera.digitalZoom
107 property alias maximumZoom: camera.maximumDigitalZoom
108 property bool switchInProgress: false
109 property bool photoCaptureInProgress: false
110 property bool timedCaptureInProgress: false
111
112 onPhotoCaptureInProgressChanged: {
113 if (main.contentExportMode && camera.photoCaptureInProgress) {
114 viewFinderExportConfirmation.photoCaptureStarted();
115 }
116 }
117
118 imageCapture {
119 onImageCaptured: {
120 if (camera.photoCaptureInProgress) {
121 if (photoRollHint.necessary && !main.transfer) photoRollHint.enable();
122 camera.photoCaptureInProgress = false;
123 }
124 }
125
126 onCaptureFailed: {
127 console.log("Image capture failed for request " + requestId + ": " + message);
128 camera.photoCaptureInProgress = false;
129 viewFinderOverlay.visible = true;
130 PopupUtils.open(captureFailedDialogComponent);
131 }
132
133 onImageSaved: {
134 if(!viewFinderOverlay.settings.hasEXIF) {
135 // TODO: make this operation asynchronous.
136 postProcessOperations.deleteEXIFdata(path);
137 }
138
139 if(viewFinderOverlay.settings.dateStampImages && !main.contentExportMode) {
140 postProcessOperations.addDateStamp(path,
141 viewFinderOverlay.settings.dateStampFormat,
142 viewFinderOverlay.settings.dateStampColor,
143 viewFinderOverlay.settings.dateStampOpacity,
144 viewFinderOverlay.settings.dateStampAlign);
145 }
146
147 // TODO: make it so that asynchronous operations signals to us
148 // on finished and only then proceed to the code below.
149
150 if (main.contentExportMode)
151 viewFinderExportConfirmation.mediaPath = path;
152
153 viewFinderView.photoTaken(path);
154 metricPhotos.increment();
155 console.log("Picture saved as " + path);
156 }
157 }
158
159 videoRecorder {
160 onRecorderStateChanged: {
161 if (videoRecorder.recorderState === CameraRecorder.StoppedState) {
162 metricVideos.increment()
163 // "actualLocation" is a URL string. Get only path from it
164 // before passing on.
165 var videoPath = videoRecorder.actualLocation.replace("file://", "");
166 viewFinderView.videoShot(videoPath);
167 if (main.contentExportMode) {
168 viewFinderExportConfirmation.mediaPath = videoRecorder.actualLocation
169 } else if (photoRollHint.necessary) {
170 photoRollHint.enable();
171 }
172 }
173 }
174 onErrorCodeChanged: {
175 if (videoRecorder.errorCode !== CameraRecorder.NoError) {
176 PopupUtils.open(captureFailedDialogComponent);
177 }
178 }
179 }
180 }
181
182 Item {
183 id: viewFinderSwitcher
184 anchors.fill: parent
185 visible: true //!viewFinderSwitcherBlurred.visible
186
187 ShaderEffectSource {
188 id: viewFinderGrab
189 live: false
190 sourceItem: viewFinder
191
192 onScheduledUpdateCompleted: {
193 if (camera.switchInProgress) {
194 // FIXME: hack to make viewFinder invisible
195 // 'viewFinder.visible = false' prevents the camera switching
196 viewFinder.width = 1;
197 viewFinder.height = 1;
198 camera.cameraState = Camera.LoadedState;
199 camera.position = (camera.position === Camera.FrontFace) ? Camera.BackFace : Camera.FrontFace;
200 decideCameraState();
201 viewFinderSwitcherRotation.angle = 180;
202 }
203 }
204 transform: Rotation {
205 origin.x: viewFinderGrab.width/2
206 origin.y: viewFinderGrab.height/2
207 axis.x: 0; axis.y: 1; axis.z: 0
208 angle: 180
209 }
210 }
211
212 transform: [
213 Scale {
214 id: viewFinderSwitcherScale
215 origin.x: viewFinderSwitcher.width/2
216 origin.y: viewFinderSwitcher.height/2
217 xScale: 1
218 yScale: xScale
219 },
220 Rotation {
221 id: viewFinderSwitcherRotation
222 origin.x: viewFinderSwitcher.width/2
223 origin.y: viewFinderSwitcher.height/2
224 axis.x: 0; axis.y: 1; axis.z: 0
225 angle: 0
226 }
227 ]
228
229
230 SequentialAnimation {
231 id: viewFinderSwitcherAnimation
232
233 SequentialAnimation {
234 ParallelAnimation {
235 LomiriNumberAnimation {target: viewFinderSwitcherScale; property: "xScale"; from: 1.0; to: 0.8; duration: LomiriAnimation.BriskDuration ; easing: LomiriAnimation.StandardEasing}
236 LomiriNumberAnimation {
237 target: viewFinderSwitcherRotation
238 property: "angle"
239 from: 180
240 to: 90
241 duration: LomiriAnimation.BriskDuration
242 easing: LomiriAnimation.StandardEasing
243 }
244 }
245 ScriptAction {
246 script: {
247 viewFinder.width = Qt.binding(function () {return viewFinderSwitcher.width;});
248 viewFinder.height = Qt.binding(function () {return viewFinderSwitcher.height;});
249 }
250 }
251 PropertyAction { target: viewFinderGrab; property: "visible"; value: false }
252 ParallelAnimation {
253 LomiriNumberAnimation {target: viewFinderSwitcherScale; property: "xScale"; from: 0.8; to: 1.0; duration: LomiriAnimation.BriskDuration; easing: LomiriAnimation.StandardEasingReverse}
254 LomiriNumberAnimation {
255 target: viewFinderSwitcherRotation
256 property: "angle"
257 from: 90
258 to: 0
259 duration: LomiriAnimation.BriskDuration
260 easing: LomiriAnimation.StandardEasingReverse
261 }
262 }
263 }
264 }
265
266 VideoOutput {
267 id: viewFinder
268
269 x: 0
270 y: -viewFinderGeometry.y
271 width: parent.width
272 height: parent.height
273 source: camera
274 filters: cameraFilters
275 opacity: ((main.contentExportMode && viewFinderExportConfirmation.waitingForPictureCapture) ||
276 (!main.contentExportMode && camera.photoCaptureInProgress && !camera.imageCapture.ready))
277 ? 0.1 : 1.0
278
279 orientation: {
280 // Camera-app uses X-Lomiri-Rotates-Window-Contents, which means we're always in
281 // native orientation. Thus, Screen.orientation isn't used here.
282
283 if (camera.position === Camera.FrontFace) {
284 // Front facing cameras are flipped horizontally, compensate the mirror
285 return (360 - camera.orientation) % 360;
286 } else {
287 return camera.orientation;
288 }
289 }
290
291 transform: Rotation {
292 origin.x: viewFinder.width / 2
293 origin.y: viewFinder.height / 2
294 axis.x: 0; axis.y: 1; axis.z: 0
295 angle: application.desktopMode ? 180 : 0
296 }
297 }
298
299 /* Convenience item tracking the real position and size of the real video feed.
300 Having this helps since these values depend on a lot of rules:
301 - the feed is automatically scaled to fit the viewfinder
302 - the viewfinder might apply a rotation to the feed, depending on device orientation
303 - the resolution and aspect ratio of the feed changes depending on the active camera
304 The item is also separated in a component so it can be unit tested.
305 */
306 ViewFinderGeometry {
307 id: viewFinderGeometry
308 anchors.centerIn: parent
309
310 cameraResolution: camera.viewfinder.resolution
311 viewFinderHeight: viewFinder.height
312 viewFinderWidth: viewFinder.width
313 viewFinderOrientation: viewFinder.orientation
314 }
315
316 Loader {
317 anchors.horizontalCenter: parent.horizontalCenter
318 width: viewFinderGeometry.width
319 height: viewFinderGeometry.height
320 visible: viewFinderOverlay.settings != undefined && viewFinderOverlay.settings.gridEnabled
321 source: "GridLines.qml"
322 asynchronous: true
323 }
324
325 Connections {
326 target: viewFinderView
327 onInViewChanged: if (!viewFinderView.inView) viewFinderOverlay.controls.cancelTimedShoot()
328 }
329
330 TimedShootFeedback {
331 id: timedShootFeedback
332 anchors.fill: parent
333 automaticOrientation: false
334 customRotation: main.staticRotationAngle
335 }
336
337 ShootFeedback {
338 id: shootFeedback
339 anchors.fill: parent
340 }
341 }
342
343
344 PhotoRollHint {
345 id: photoRollHint
346 objectName: "photoRollHint"
347 anchors.fill: parent
348 visible: enabled
349
350 Connections {
351 target: viewFinderView
352 onInViewChanged: if (!viewFinderView.inView) photoRollHint.disable()
353 }
354 }
355
356
357 ViewFinderOverlayLoader {
358 id: viewFinderOverlay
359
360 anchors.fill: parent
361 asynchronous: true
362 camera: camera
363 readOnly: viewFinderView.readOnly
364 sensorOrientation: camera.orientation
365 opacity: status == Loader.Ready && overlayVisible && !photoRollHint.enabled ? 1.0 : 0.0
366 readyForCapture: main.contentExportMode &&
367 viewFinderExportConfirmation.waitingForPictureCapture ? false : camera.imageCapture.ready
368
369 Behavior on opacity {
370 enabled: !photoRollHint.enabled
371 LomiriNumberAnimation {duration: LomiriAnimation.SnapDuration}
372 }
373
374 // Tapping anywhere on the screen should not trigger any camera
375 // controls while PhotoRoll hint is visible
376 MouseArea {
377 anchors.fill: parent
378 enabled: photoRollHint.visible
379 }
380 }
381
382 Loader {
383 id: viewFinderExportConfirmationLoader
384 asynchronous: true
385 anchors.fill: parent
386 sourceComponent: viewFinderExportConfirmationComp
387 }
388
389 PostProcessOperations {
390 id: postProcessOperations
391 }
392
393 property alias viewFinderExportConfirmation: viewFinderExportConfirmationLoader.item
394
395 Component {
396 id: viewFinderExportConfirmationComp
397 ViewFinderExportConfirmation {
398 id: viewFinderExportConfirmation
399 anchors.fill: parent
400
401 isVideo: main.transfer && main.transfer.contentType == ContentType.Videos
402 geometry: viewFinderGeometry
403
404 onShowRequested: {
405 viewFinder.visible = false;
406 viewFinderOverlay.visible = false;
407 visible = true;
408 }
409
410 onHideRequested: {
411 viewFinder.visible = true;
412 viewFinderOverlay.visible = true;
413 visible = false;
414 }
415 }
416 }
417
418 Component {
419 id: captureFailedDialogComponent
420 Dialog {
421 id: captureFailedDialog
422 objectName: "captureFailedDialog"
423 title: i18n.tr("Capture failed")
424
425 // If we are capturing to an SD card the problem can be a broken card, otherwise it is probably a
426 // crash in the driver and a reboot might fix things.
427 text: StorageLocations.removableStorageLocation && viewFinderOverlay.settings.preferRemovableStorage ?
428 i18n.tr("Replacing your external media, formatting it, or restarting the device might fix the problem.") :
429 i18n.tr("Restarting your device might fix the problem.")
430
431 Button {
432 text: i18n.tr("Cancel")
433 onClicked: PopupUtils.close(captureFailedDialog)
434 }
435 }
436 }
437 }