forked from ubports/camera-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewFinderView.qml
499 lines (436 loc) · 18.3 KB
/
ViewFinderView.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Copyright 2014 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.4
import QtQuick.Window 2.2
import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import QtMultimedia 5.0
import CameraApp 0.1
import QtGraphicalEffects 1.0
import Ubuntu.Content 1.3
FocusScope {
id: viewFinderView
property bool overlayVisible: true
property bool overlayPageVisible: false
property bool optionValueSelectorVisible: false
property bool touchAcquired: viewFinderOverlay.touchAcquired || camera.videoRecorder.recorderState == CameraRecorder.RecordingState
property bool inView
property alias captureMode: camera.captureMode
property real aspectRatio: viewFinder.sourceRect.height != 0 ? viewFinder.sourceRect.width / viewFinder.sourceRect.height : 1.0
signal photoTaken(string filePath)
signal videoShot(string filePath)
Connections {
target: viewFinderOverlay
onStatusChanged: decideCameraState()
}
Connections {
target: Qt.application
onActiveChanged: if (Qt.application.active && camera.failedToConnect) decideCameraState()
}
function decideCameraState() {
if (viewFinderOverlay.status == Loader.Ready) {
camera.cameraState = Camera.LoadedState;
viewFinderOverlay.updateResolutionOptions();
camera.cameraState = Camera.ActiveState;
}
}
property Camera camera: Camera {
id: camera
captureMode: Camera.CaptureStillImage
cameraState: Camera.UnloadedState
StateSaver.properties: "captureMode, position"
property bool failedToConnect: false
function manualFocus(x, y) {
var normalizedPoint = viewFinder.mapPointToSourceNormalized(Qt.point(x, y - viewFinder.y));
if (normalizedPoint.x >= 0.0 && normalizedPoint.x <= 1.0 &&
normalizedPoint.y >= 0.0 && normalizedPoint.y <= 1.0) {
viewFinderOverlay.showFocusRing(x, y);
autoFocusTimer.restart();
focus.focusMode = Camera.FocusAuto;
focus.customFocusPoint = normalizedPoint;
focus.focusPointMode = Camera.FocusPointCustom;
}
}
function autoFocus() {
focus.focusMode = Camera.FocusContinuous;
focus.focusPointMode = Camera.FocusPointAuto;
}
property var autoFocusTimer: Timer {
interval: 5000
onTriggered: camera.autoFocus();
}
focus {
focusMode: Camera.FocusContinuous
focusPointMode: Camera.FocusPointAuto
}
property AdvancedCameraSettings advanced: AdvancedCameraSettings {
id: advancedCamera
camera: camera
}
/* Use only digital zoom for now as it's what phone cameras mostly use.
TODO: if optical zoom is available, maximumZoom should be the combined
range of optical and digital zoom and currentZoom should adjust the two
transparently based on the value. */
property alias currentZoom: camera.digitalZoom
property alias maximumZoom: camera.maximumDigitalZoom
property bool switchInProgress: false
property bool photoCaptureInProgress: false
property bool timedCaptureInProgress: false
onPhotoCaptureInProgressChanged: {
if (main.contentExportMode && camera.photoCaptureInProgress) {
viewFinderExportConfirmation.photoCaptureStarted();
}
}
imageCapture {
onImageCaptured: {
if (camera.photoCaptureInProgress) {
if (photoRollHint.necessary && !main.transfer) photoRollHint.enable();
camera.photoCaptureInProgress = false;
}
}
onCaptureFailed: {
console.log("Image capture failed for request " + requestId + ": " + message);
camera.photoCaptureInProgress = false;
viewFinderOverlay.visible = true;
PopupUtils.open(captureFailedDialogComponent);
}
onImageSaved: {
if (main.contentExportMode) viewFinderExportConfirmation.mediaPath = path;
viewFinderView.photoTaken(path);
metricPhotos.increment();
console.log("Picture saved as " + path);
}
}
videoRecorder {
onRecorderStateChanged: {
if (videoRecorder.recorderState === CameraRecorder.StoppedState) {
metricVideos.increment()
viewFinderView.videoShot(videoRecorder.actualLocation);
if (main.contentExportMode) {
viewFinderExportConfirmation.mediaPath = videoRecorder.actualLocation
} else if (photoRollHint.necessary) {
photoRollHint.enable();
}
}
}
onErrorCodeChanged: {
if (videoRecorder.errorCode !== CameraRecorder.NoError) {
PopupUtils.open(captureFailedDialogComponent);
}
}
}
}
Item {
id: viewFinderSwitcher
anchors.fill: parent
visible: !viewFinderSwitcherBlurred.visible
ShaderEffectSource {
id: viewFinderGrab
live: false
sourceItem: viewFinder
onScheduledUpdateCompleted: {
if (camera.switchInProgress) {
// FIXME: hack to make viewFinder invisible
// 'viewFinder.visible = false' prevents the camera switching
viewFinder.width = 1;
viewFinder.height = 1;
camera.cameraState = Camera.LoadedState;
camera.position = (camera.position === Camera.FrontFace) ? Camera.BackFace : Camera.FrontFace;
decideCameraState();
viewFinderSwitcherRotation.angle = 180;
}
}
transform: Rotation {
origin.x: viewFinderGrab.width/2
origin.y: viewFinderGrab.height/2
axis.x: 0; axis.y: 1; axis.z: 0
angle: 180
}
}
transform: [
Scale {
id: viewFinderSwitcherScale
origin.x: viewFinderSwitcher.width/2
origin.y: viewFinderSwitcher.height/2
xScale: 1
yScale: xScale
},
Rotation {
id: viewFinderSwitcherRotation
origin.x: viewFinderSwitcher.width/2
origin.y: viewFinderSwitcher.height/2
axis.x: 0; axis.y: 1; axis.z: 0
angle: 0
}
]
SequentialAnimation {
id: viewFinderSwitcherAnimation
SequentialAnimation {
ParallelAnimation {
UbuntuNumberAnimation {target: viewFinderSwitcherScale; property: "xScale"; from: 1.0; to: 0.8; duration: UbuntuAnimation.BriskDuration ; easing: UbuntuAnimation.StandardEasing}
UbuntuNumberAnimation {
target: viewFinderSwitcherRotation
property: "angle"
from: 180
to: 90
duration: UbuntuAnimation.BriskDuration
easing: UbuntuAnimation.StandardEasing
}
}
ScriptAction {
script: {
viewFinder.width = Qt.binding(function () {return viewFinderSwitcher.width;});
viewFinder.height = Qt.binding(function () {return viewFinderSwitcher.height;});
}
}
PropertyAction { target: viewFinderGrab; property: "visible"; value: false }
ParallelAnimation {
UbuntuNumberAnimation {target: viewFinderSwitcherScale; property: "xScale"; from: 0.8; to: 1.0; duration: UbuntuAnimation.BriskDuration; easing: UbuntuAnimation.StandardEasingReverse}
UbuntuNumberAnimation {
target: viewFinderSwitcherRotation
property: "angle"
from: 90
to: 0
duration: UbuntuAnimation.BriskDuration
easing: UbuntuAnimation.StandardEasingReverse
}
}
}
}
VideoOutput {
id: viewFinder
x: 0
y: -viewFinderGeometry.y
width: parent.width
height: parent.height
source: camera
opacity: ((main.contentExportMode && viewFinderExportConfirmation.waitingForPictureCapture) ||
(!main.contentExportMode && camera.photoCaptureInProgress && !camera.imageCapture.ready))
? 0.1 : 1.0
/* This rotation need to be applied since the camera hardware in the
Galaxy Nexus phone is mounted at an angle inside the device, so the video
feed is rotated too.
FIXME: This should come from a system configuration option so that we
don't have to have a different codebase for each different device we want
to run on. Android has that information and QML has an API to reflect it:
the camera.orientation property. Unfortunately it is not hooked up yet.
Ref.: http://doc.qt.io/qt-5/qml-qtmultimedia-camera.html#orientation-prop
http://doc.qt.io/qt-5/qcamerainfocontrol.html#cameraOrientation
http://developer.android.com/reference/android/hardware/Camera.CameraInfo.html#orientation
*/
Component.onCompleted: {
// Set orientation only at startup because later on Screen.primaryOrientation
// may change.
orientation = Screen.primaryOrientation === Qt.PortraitOrientation ? -90 : 0;
viewFinderOverlay.sensorOrientation = orientation;
}
transform: Rotation {
origin.x: viewFinder.width / 2
origin.y: viewFinder.height / 2
axis.x: 0; axis.y: 1; axis.z: 0
angle: application.desktopMode ? 180 : 0
}
}
/* Convenience item tracking the real position and size of the real video feed.
Having this helps since these values depend on a lot of rules:
- the feed is automatically scaled to fit the viewfinder
- the viewfinder might apply a rotation to the feed, depending on device orientation
- the resolution and aspect ratio of the feed changes depending on the active camera
The item is also separated in a component so it can be unit tested.
*/
ViewFinderGeometry {
id: viewFinderGeometry
anchors.centerIn: parent
cameraResolution: camera.viewfinder.resolution
viewFinderHeight: viewFinder.height
viewFinderWidth: viewFinder.width
viewFinderOrientation: viewFinder.orientation
}
Item {
id: gridlines
objectName: "gridlines"
anchors.horizontalCenter: parent.horizontalCenter
width: viewFinderGeometry.width
height: viewFinderGeometry.height
visible: viewFinderOverlay.settings != undefined && viewFinderOverlay.settings.gridEnabled
property color color: Qt.rgba(0.8, 0.8, 0.8, 0.8)
property real thickness: units.dp(1)
Rectangle {
y: parent.height / 3
width: parent.width
height: gridlines.thickness
color: gridlines.color
}
Rectangle {
y: 2 * parent.height / 3
width: parent.width
height: gridlines.thickness
color: gridlines.color
}
Rectangle {
x: parent.width / 3
width: gridlines.thickness
height: parent.height
color: gridlines.color
}
Rectangle {
x: 2 * parent.width / 3
width: gridlines.thickness
height: parent.height
color: gridlines.color
}
}
Connections {
target: viewFinderView
onInViewChanged: if (!viewFinderView.inView) viewFinderOverlay.controls.cancelTimedShoot()
}
OrientationHelper {
id: timedShootFeedback
anchors.fill: parent
function start() {
}
function stop() {
remainingSecsLabel.text = "";
}
function showRemainingSecs(secs) {
remainingSecsLabel.text = secs;
remainingSecsLabel.opacity = 1.0;
remainingSecsLabelAnimation.restart();
}
Label {
id: remainingSecsLabel
anchors.fill: parent
font.pixelSize: units.gu(6)
font.bold: true
color: "white"
style: Text.Outline;
styleColor: "black"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
visible: opacity != 0.0
opacity: 0.0
OpacityAnimator {
id: remainingSecsLabelAnimation
target: remainingSecsLabel
from: 1.0
to: 0.0
duration: 750
easing: UbuntuAnimation.StandardEasing
}
}
// tapping anywhere on the screen while a timed shoot is ongoing cancels it
MouseArea {
anchors.fill: parent
onClicked: viewFinderOverlay.controls.cancelTimedShoot()
enabled: remainingSecsLabel.visible
}
}
Rectangle {
id: shootFeedback
anchors.fill: parent
color: "black"
visible: opacity != 0.0
opacity: 0.0
function start() {
shootFeedback.opacity = 1.0;
shootFeedbackAnimation.restart();
}
OpacityAnimator {
id: shootFeedbackAnimation
target: shootFeedback
from: 1.0
to: 0.0
duration: UbuntuAnimation.SnapDuration
easing: UbuntuAnimation.StandardEasing
}
}
}
FastBlur {
id: viewFinderSwitcherBlurred
anchors.fill: viewFinderSwitcher
property real finalRadius: 64
property real finalOpacity: 0.7
radius: photoRollHint.visible ? finalRadius : viewFinderOverlay.revealProgress * finalRadius
opacity: photoRollHint.visible ? finalOpacity : (1.0 - viewFinderOverlay.revealProgress) * finalOpacity + finalOpacity
source: viewFinderSwitcher !== null ? viewFinderSwitcher : null
visible: radius !== 0
Behavior on radius { UbuntuNumberAnimation { duration: UbuntuAnimation.SnapDuration} }
}
PhotoRollHint {
id: photoRollHint
objectName: "photoRollHint"
anchors.fill: parent
visible: enabled
Connections {
target: viewFinderView
onInViewChanged: if (!viewFinderView.inView) photoRollHint.disable()
}
}
ViewFinderOverlayLoader {
id: viewFinderOverlay
anchors.fill: parent
camera: camera
opacity: status == Loader.Ready && overlayVisible && !photoRollHint.enabled ? 1.0 : 0.0
readyForCapture: main.contentExportMode &&
viewFinderExportConfirmation.waitingForPictureCapture ? false : camera.imageCapture.ready
Behavior on opacity {
enabled: !photoRollHint.enabled
UbuntuNumberAnimation {duration: UbuntuAnimation.SnapDuration}
}
// Tapping anywhere on the screen should not trigger any camera
// controls while PhotoRoll hint is visible
MouseArea {
anchors.fill: parent
enabled: photoRollHint.visible
}
onLoaded: {
viewFinderView.overlayPageVisible = Qt.binding(function() { return item.overlayPageVisible;})
}
}
ViewFinderExportConfirmation {
id: viewFinderExportConfirmation
anchors.fill: parent
isVideo: main.transfer && main.transfer.contentType == ContentType.Videos
viewFinderGeometry: viewFinderGeometry
onShowRequested: {
viewFinder.visible = false;
viewFinderOverlay.visible = false;
visible = true;
}
onHideRequested: {
viewFinder.visible = true;
viewFinderOverlay.visible = true;
visible = false;
}
}
Component {
id: captureFailedDialogComponent
Dialog {
id: captureFailedDialog
objectName: "captureFailedDialog"
title: i18n.tr("Capture failed")
// If we are capturing to an SD card the problem can be a broken card, otherwise it is probably a
// crash in the driver and a reboot might fix things.
text: StorageLocations.removableStorageLocation && viewFinderOverlay.settings.preferRemovableStorage ?
i18n.tr("Replacing your external media, formatting it, or restarting the device might fix the problem.") :
i18n.tr("Restarting your device might fix the problem.")
Button {
text: i18n.tr("Cancel")
onClicked: PopupUtils.close(captureFailedDialog)
}
}
}
}