/*
* 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 .
*/
import QtQuick 2.12
import QtQuick.Window 2.2
import Lomiri.Components 1.3
import Lomiri.Components.Popups 1.3
import QtMultimedia 5.9
import QtPositioning 5.2
import QtSensors 5.0
import CameraApp 0.1
import Qt.labs.settings 1.0
import QtGraphicalEffects 1.0
import "../components"
Item {
id: viewFinderOverlay
property Camera camera
property bool touchAcquired: bottomEdge.pressed || zoomPinchArea.active
property real revealProgress: noSpaceHint.visible ? 1.0 : bottomEdge.progress
property var controls: controls
property var settings: settings
property bool readyForCapture
property int sensorOrientation
// Sometimes the value is FlashVideoLight, sometimes it's FlashTorch
property int videoFlashOnValue: Camera.FlashVideoLight
// Screen's aspect ratio in both major orientations
readonly property string screenAspectRatio: sizeToAspectRatio(Qt.size(Screen.width, Screen.height))
readonly property string screenAspectRatio2: sizeToAspectRatio(Qt.size(Screen.height, Screen.width))
function showFocusRing(x, y) {
focusRing.center = Qt.point(x, y);
focusRing.show();
}
Settings {
id: settings
property int flashMode: Camera.FlashAuto
property bool gpsEnabled: false
property bool hasEXIF: false // Should it even be enabled by default?
property bool hdrEnabled: false
property bool videoFlashOn: false
property int videoQuality: 0 // 0 - Normal, 1 - High
// Left for compatibility
property int videoFlashMode: -1
property int selfTimerDelay: 0
property int encodingQuality: 2 // QMultimedia.NormalQuality
property bool gridEnabled: false
property bool preferRemovableStorage: false
property bool playShutterSound: true
property bool shutterVibration: false
property var photoResolutions
property var videoResolutions
// Left for compatibility
property string videoResolution: ""
property bool dateStampImages: false
property string dateStampFormat: Qt.locale().dateFormat(Locale.ShortFormat)
property color dateStampColor: LomiriColors.orange;
property real dateStampOpacity: 1.0;
property int dateStampAlign : Qt.AlignBottom | Qt.AlignRight;
Component.onCompleted: {
if (!photoResolutions) photoResolutions = {}
if (!videoResolutions) {
videoResolutions = {}
if (videoResolution)
// Migrate old value into default camera
setVideoResolution(videoResolution);
}
if (videoFlashMode != -1) {
videoFlashOn = (videoFlashMode != Camera.FlashOff);
videoFlashMode = -1;
}
}
onPhotoResolutionsChanged: updateViewfinderResolution();
onVideoResolutionsChanged: updateViewfinderResolution();
onGpsEnabledChanged: if(gpsEnabled&&!hasEXIF)hasEXIF = true;
onHasEXIFChanged: if(gpsEnabled&&!hasEXIF)gpsEnabled = false;
onFlashModeChanged: if (flashMode != Camera.FlashOff) hdrEnabled = false;
onHdrEnabledChanged: if (hdrEnabled) flashMode = Camera.FlashOff
}
Binding {
target: camera.flash
property: "mode"
value: settings.flashMode
when: camera.captureMode == Camera.CaptureStillImage
}
Binding {
target: camera.flash
property: "mode"
value: settings.videoFlashOn && viewFinderView.inView ? videoFlashOnValue : Camera.FlashOff
when: camera.captureMode == Camera.CaptureVideo
}
Binding {
target: camera.advanced
property: "hasEXIF"
value: settings.hasEXIF
}
Binding {
target: camera.advanced
property: "hdrEnabled"
value: settings.hdrEnabled
}
Binding {
target: camera.advanced
property: "encodingQuality"
value: settings.encodingQuality
// This makes sure that the correct image quality is initiated
when: camera.cameraStatus == Camera.ActiveStatus
}
Binding {
target: camera.videoRecorder
property: "videoBitRate"
value: getVideoBitrate()
// This makes sure that the correct video bitrate is initiated
when: camera.cameraStatus == Camera.ActiveStatus
}
Binding {
target: camera.videoRecorder
property: "resolution"
value: settings.videoResolutions[camera.deviceId] || Qt.size(-1, -1)
// This makes sure that the correct video resolution is initiated
when: camera.cameraStatus == Camera.LoadedStatus || camera.cameraStatus == Camera.ActiveStatus
}
Binding {
target: camera.imageCapture
property: "resolution"
value: settings.photoResolutions[camera.deviceId] || Qt.size(-1, -1)
}
// FIXME: where's the proper location for this?
function updateViewfinderResolution() {
var EPSILON = 0.02;
var supportedViewfinderResolutions = camera.supportedViewfinderResolutions();
if (supportedViewfinderResolutions.length === 0) {
console.log("updateViewfinderResolution: viewfinder resolutions is not known yet.");
return;
}
var targetResolution = stringToSize(
camera.captureMode === Camera.CaptureStillImage
? settings.photoResolutions[camera.deviceId]
: settings.videoResolutions[camera.deviceId]
);
if (!targetResolution) {
// Resolution has not been selected yet.
return;
}
var targetAspectRatio = targetResolution.width / targetResolution.height;
var selectedResolution;
// Select the highest resolution with matching aspect ratio.
for (var i = 0; i < supportedViewfinderResolutions.length; i++) {
var currentResolution = supportedViewfinderResolutions[i];
var currentAspectRatio = currentResolution.width / currentResolution.height;
if (Math.abs(targetAspectRatio - currentAspectRatio) > EPSILON)
continue;
if (!selectedResolution ||
currentResolution.width > selectedResolution.width) {
selectedResolution = currentResolution;
}
}
if (!selectedResolution) {
// This is strange. Not sure why this would happen.
console.log("updateViewfinderResolution: cannot find suitable viewfinder resolution. Use the default one.");
selectedResolution = supportedViewfinderResolutions[0];
}
console.log("updateViewfinderResolution: For target resolution " +
sizeToString(targetResolution) + ", select " +
sizeToString(selectedResolution) + " for viewfinder resolution.");
camera.viewfinder.resolution =
Qt.size(selectedResolution.width, selectedResolution.height);
}
function updateVideoFlashOnValue() {
if (camera.captureMode != Camera.CaptureVideo) {
// The value can be probed only in video mode.
return;
}
var supportedModes = camera.flash.supportedModes;
for (var i = 0; i < supportedModes.length; i++) {
if (supportedModes[i] === Camera.FlashVideoLight ||
supportedModes[i] === Camera.FlashTorch) {
videoFlashOnValue = supportedModes[i];
return;
}
}
}
Connections {
target: camera.imageCapture
onImageCaptured: {
if(settings.shutterVibration) {
Haptics.play({intensity:0.25,duration:LomiriAnimation.SnapDuration/3});
}
}
}
Connections {
target: camera.flash
onSupportedModesChanged: { updateVideoFlashOnValue(); }
}
Connections {
target: camera
onCaptureModeChanged: {
updateViewfinderResolution();
}
onCameraStatusChanged: {
// Supported viewfinder resolution is guaranteed to be known at
// ActiveStatus, if not earlier.
if (camera.cameraStatus == Camera.LoadedStatus
|| camera.cameraStatus == Camera.ActiveStatus) {
updateViewfinderResolution();
}
}
}
function triggerShoot() {
if (shootButton.enabled) {
if (camera.captureMode == Camera.CaptureVideo && camera.videoRecorder.recorderState == CameraRecorder.RecordingState) {
camera.videoRecorder.stop();
} else {
if (settings.selfTimerDelay > 0) {
controls.timedShoot(settings.selfTimerDelay);
} else {
controls.shoot();
}
}
}
}
function resolutionToLabel(resolution, full=true) {
// takes in a resolution string (e.g. "1920x1080") and returns a nicer
// form of it for display in the UI: "1080p"
const label = resolution.split("x").pop() + "p";
if (full) {
// Include the aspect ratio in full format
const aspectRatio = sizeToAspectRatio(stringToSize(resolution));
let finalAspectRatio = aspectRatio
// Label those that are equal to screen's aspect ratio as "Full"
// except 16:9 since it's common
if ((aspectRatio === viewFinderOverlay.screenAspectRatio
|| aspectRatio === viewFinderOverlay.screenAspectRatio2
)
&& aspectRatio !== "16:9") {
finalAspectRatio = i18n.tr("Full")
}
return "%1 (%2)".arg(label).arg(finalAspectRatio);
}
return "%1".arg(label);
}
function sizeToString(size) {
return size.width + "x" + size.height;
}
function stringToSize(resolution) {
var r = resolution ? resolution.split("x") : null;
return Qt.size(r[0], r[1]);
}
function sizeToAspectRatio(size) {
var ratio = Math.max(size.width, size.height) / Math.min(size.width, size.height);
var maxDenominator = 12;
var epsilon;
var numerator;
var denominator;
var bestDenominator;
var bestEpsilon = 10000;
for (denominator = 2; denominator <= maxDenominator; denominator++) {
numerator = ratio * denominator;
epsilon = Math.abs(Math.round(numerator) - numerator);
if (epsilon < bestEpsilon) {
bestEpsilon = epsilon;
bestDenominator = denominator;
}
}
numerator = Math.round(ratio * bestDenominator);
// Further simplify square aspect ratio to 1:1
if (numerator === bestDenominator) {
numerator = 1;
bestDenominator = 1;
}
return "%1:%2".arg(numerator).arg(bestDenominator);
}
function sizeToMegapixels(size) {
var megapixels = (size.width * size.height) / 1000000;
return parseFloat(megapixels.toFixed(1))
}
function trimNumberToFit(numberStr, digits) {
return (""+numberStr).substr(0,digits).replace(/\.$/,"");
}
function updateVideoResolutionOptions() {
// Clear and refill videoResolutionOptionsModel with available resolutions
// Try to only display well known resolutions: 2160p, 1440p, 1080p, 720p and 480p
videoResolutionOptionsModel.clear();
var supported = camera.advanced.videoSupportedResolutions;
var wellKnown = ["2160", "1440", "1080", "720", "480"];
var supportedFiltered = supported.filter(function (resolution) {
return wellKnown.findIndex(
value => {
const _pattern = "x" + value + "$"
const _regex = new RegExp(_pattern, "g")
return resolution.match(_regex)
}
) !== -1;
});
if (supportedFiltered.length === 0)
supportedFiltered = supported.slice();
// Sort resolutions from low to high, but then insert them into model
// in reverse order (so that highest resolution appear first).
supportedFiltered.sort(function(a, b) {
return a.split("x")[1] - b.split("x")[1];
});
for (var i=0; i 0
&& supportedFiltered.indexOf(settings.videoResolutions[camera.deviceId]) === -1) {
setVideoResolution(supportedFiltered[supportedFiltered.length - 1]);
}
}
function updatePhotoResolutionOptions() {
// Clear and refill photoResolutionOptionsModel with available resolutions
photoResolutionOptionsModel.clear();
//Change to Size object and sort the resolutions by megapixel ( in reverse order so it goes from top high to bottom low )
var sortedResolutions = [];
for(var i in camera.advanced.imageSupportedResolutions) {
sortedResolutions.push( stringToSize(camera.advanced.imageSupportedResolutions[i]) );
}
sortedResolutions.sort(function(a, b) { return sizeToMegapixels(b) - sizeToMegapixels(a) });
for(var i in sortedResolutions) {
var res = sortedResolutions[i];
photoResolutionOptionsModel.insert(i,{"icon": "",
"label": "%1 (%2MP)".arg(sizeToAspectRatio(res))
.arg(sizeToMegapixels(res)),
"value": sizeToString(res)});
}
// If resolution setting is not supported select the resolution automatically
var photoResolution = settings.photoResolutions[camera.deviceId];
if (!isPhotoResolutionAnOption(photoResolution)) {
setPhotoResolution(getAutomaticPhotoResolution());
}
}
// Get the appropriate video bitrate based on the current quality setting
function getVideoBitrate() {
const quality = settings.videoQuality
const resoSize = camera.videoRecorder.resolution
if (quality === 1) { // High Quality
switch(true) {
case resoSize.height >= 2160:
return 48000000 // 48 Mbps
case resoSize.height >= 1440:
return 35000000 // 35 Mbps
case resoSize.height >= 1080:
return 17000000 // 17 Mbps
case resoSize.height >= 720:
return 10000000 // 10 Mbps
case resoSize.height >= 480:
return 6000000 // 6 Mbps
default:
return 5000000 // 5 Mbps
}
} else {
switch(true) {
case resoSize.height >= 2160:
return 35000000 // 35 Mbps
case resoSize.height >= 1440:
return 16000000 // 16 Mbps
case resoSize.height >= 1080:
return 8000000 // 8 Mbps
case resoSize.height >= 720:
return 5000000 // 5 Mbps
case resoSize.height >= 480:
return 2500000 // 2.5 Mbps
default:
return 2000000 // 2 Mbps
}
}
}
function setPhotoResolution(resolution) {
var size = stringToSize(resolution);
if (size.width > 0 && size.height > 0
&& resolution != settings.photoResolutions[camera.deviceId]) {
settings.photoResolutions[camera.deviceId] = resolution;
// FIXME: resetting the value of the property 'photoResolutions' is
// necessary to ensure that a change notification signal is emitted
settings.photoResolutions = settings.photoResolutions;
}
}
function setVideoResolution(resolution) {
if (resolution !== settings.videoResolutions[camera.deviceId]) {
settings.videoResolutions[camera.deviceId] = resolution;
// FIXME: resetting the value of the property 'videoResolutions' is
// necessary to ensure that a change notification signal is emitted
settings.videoResolutions = settings.videoResolutions;
}
}
function getAutomaticPhotoResolution() {
var fittingResolution = sizeToString(camera.advanced.fittingResolution);
var maximumResolution = sizeToString(camera.advanced.maximumResolution);
if (isPhotoResolutionAnOption(fittingResolution)) {
return fittingResolution;
} else {
return maximumResolution;
}
}
function isPhotoResolutionAnOption(resolution) {
for (var i=0; i options: [
ListModel {
id: gpsOptionsModel
property string settingsProperty: "gpsEnabled"
property string icon: "location"
property string label: ""
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(gpsOptionsModel, settings.gpsEnabled)
property bool available: true
property bool visible: true
property bool showInIndicators: true
property bool colorize: !positionSource.isPrecise
ListElement {
icon: ""
label: QT_TR_NOOP("On")
value: true
}
ListElement {
icon: ""
label: QT_TR_NOOP("Off")
value: false
}
},
ListModel {
id: flashOptionsModel
property string settingsProperty: "flashMode"
property string icon: ""
property string label: ""
property bool isToggle: false
property int selectedIndex: bottomEdge.indexForValue(flashOptionsModel, settings.flashMode)
property bool available: camera.advanced.hasFlash
property bool visible: camera.captureMode == Camera.CaptureStillImage
property bool showInIndicators: true
ListElement {
icon: "flash-on"
label: QT_TR_NOOP("On")
value: Camera.FlashOn
}
ListElement {
icon: "flash-auto"
label: QT_TR_NOOP("Auto")
value: Camera.FlashAuto
}
ListElement {
icon: "flash-off"
label: QT_TR_NOOP("Off")
value: Camera.FlashOff
}
},
ListModel {
id: videoFlashOptionsModel
property string settingsProperty: "videoFlashOn"
property string icon: ""
property string label: ""
property bool isToggle: false
property int selectedIndex: bottomEdge.indexForValue(videoFlashOptionsModel, settings.videoFlashOn)
property bool available: camera.advanced.hasFlash
property bool visible: camera.captureMode == Camera.CaptureVideo
property bool showInIndicators: true
ListElement {
icon: "torch-on"
label: QT_TR_NOOP("On")
value: true
}
ListElement {
icon: "torch-off"
label: QT_TR_NOOP("Off")
value: false
}
},
ListModel {
id: hdrOptionsModel
property string settingsProperty: "hdrEnabled"
property string icon: ""
property string label: i18n.tr("HDR")
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(hdrOptionsModel, settings.hdrEnabled)
property bool available: camera.advanced.hasHdr
property bool visible: camera.captureMode === Camera.CaptureStillImage
property bool showInIndicators: true
ListElement {
icon: ""
label: QT_TR_NOOP("On")
value: true
}
ListElement {
icon: ""
label: QT_TR_NOOP("Off")
value: false
}
},
ListModel {
id: selfTimerOptionsModel
property string settingsProperty: "selfTimerDelay"
property string icon: ""
property string iconSource: "../../assets/self_timer.svg"
property string label: ""
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(selfTimerOptionsModel, settings.selfTimerDelay)
property bool available: true
property bool visible: true
property bool showInIndicators: true
ListElement {
icon: ""
label: QT_TR_NOOP("Off")
value: 0
}
ListElement {
icon: ""
label: QT_TR_NOOP("5 seconds")
value: 5
}
ListElement {
icon: ""
label: QT_TR_NOOP("15 seconds")
value: 15
}
},
ListModel {
id: encodingQualityOptionsModel
property string settingsProperty: "encodingQuality"
property string icon: "stock_image"
property string label: ""
property bool isToggle: false
property int selectedIndex: bottomEdge.indexForValue(encodingQualityOptionsModel, settings.encodingQuality)
property bool available: true
property bool visible: camera.captureMode == Camera.CaptureStillImage
property bool showInIndicators: false
ListElement {
icon: ""
label: QT_TR_NOOP("Fine Quality")
value: 4 // QMultimedia.VeryHighQuality
}
ListElement {
icon: ""
label: QT_TR_NOOP("High Quality")
value: 3 // QMultimedia.HighQuality
}
ListElement {
icon: ""
label: QT_TR_NOOP("Normal Quality")
value: 2 // QMultimedia.NormalQuality
}
ListElement {
icon: ""
label: QT_TR_NOOP("Basic Quality")
value: 1 // QMultimedia.LowQuality
}
},
ListModel {
id: videoQualityOptionsModel
property string settingsProperty: "videoQuality"
property string icon: "stock_video"
property string label: ""
property bool isToggle: false
property int selectedIndex: bottomEdge.indexForValue(videoQualityOptionsModel, settings.videoQuality)
property bool available: true
property bool visible: camera.captureMode == Camera.CaptureVideo
property bool showInIndicators: false
ListElement {
icon: ""
label: QT_TR_NOOP("High Quality")
value: 1
}
ListElement {
icon: ""
label: QT_TR_NOOP("Normal Quality")
value: 0
}
},
ListModel {
id: videoResolutionOptionsModel
function setSettingProperty(value) {
setVideoResolution(value);
}
property string icon: ""
property string label: resolutionToLabel(settings.videoResolutions[camera.deviceId], false)
property bool isToggle: false
property int selectedIndex: bottomEdge.indexForValue(videoResolutionOptionsModel,
settings.videoResolutions[camera.deviceId])
property bool available: true
property bool visible: camera.captureMode == Camera.CaptureVideo
property bool showInIndicators: false
},
ListModel {
id: gridOptionsModel
property string settingsProperty: "gridEnabled"
property string icon: ""
property string iconSource: "../../assets/grid_lines.svg"
property string label: ""
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(gridOptionsModel, settings.gridEnabled)
property bool available: true
property bool visible: true
ListElement {
icon: ""
label: QT_TR_NOOP("On")
value: true
}
ListElement {
icon: ""
label: QT_TR_NOOP("Off")
value: false
}
},
ListModel {
id: removableStorageOptionsModel
property string settingsProperty: "preferRemovableStorage"
property string icon: ""
// TRANSLATORS: this will be displayed on an small button so for it to fit it should be less then 3 characters long.
property string label: i18n.tr("SD")
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(removableStorageOptionsModel, settings.preferRemovableStorage)
property bool available: StorageLocations.removableStoragePresent
property bool visible: available
ListElement {
icon: ""
label: QT_TR_NOOP("Save to SD Card")
value: true
}
ListElement {
icon: ""
label: QT_TR_NOOP("Save internally")
value: false
}
},
ListModel {
id: shutterSoundOptionsModel
function setSettingProperty(value) {
settings.shutterVibration = value & 0x1;
settings.playShutterSound = value & 0x2;
}
property string settingsProperty: "playShutterSound"
property string icon: ""
property string label: ""
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(shutterSoundOptionsModel, 2 * settings.playShutterSound + settings.shutterVibration)
property bool available: true
property bool visible: camera.captureMode === Camera.CaptureStillImage
property bool showInIndicators: false
ListElement {
icon: "audio-volume-high"
label: QT_TR_NOOP("On")
value: 2
}
ListElement {
iconSource: "../../assets/vibrate.png"
label: QT_TR_NOOP("Vibrate")
value: 1
}
ListElement {
icon: "audio-volume-muted"
label: QT_TR_NOOP("Off")
value: 0
}
},
ListModel {
id: photoResolutionOptionsModel
function setSettingProperty(value) {
setPhotoResolution(value);
}
property string icon: ""
property string label: "%1MP".arg(trimNumberToFit(sizeToMegapixels(stringToSize(settings.photoResolutions[camera.deviceId])),3))
property bool isToggle: false
property int selectedIndex: bottomEdge.indexForValue(photoResolutionOptionsModel, settings.photoResolutions[camera.deviceId])
property bool available: true
property bool visible: camera.captureMode == Camera.CaptureStillImage
property bool showInIndicators: false
},
ListModel {
id: photoEXIFSettingsModel
function setSettingProperty(value) {
settings.hasEXIF = value;
}
property string settingsProperty: "hasEXIF"
property string icon: ""
property string label: ""
property bool isToggle: true
property int selectedIndex: bottomEdge.indexForValue(photoEXIFSettingsModel, settings.hasEXIF)
property bool available: true
property bool visible: camera.captureMode === Camera.CaptureStillImage
property bool showInIndicators: false
ListElement {
icon: "private-browsing-exit"
label: QT_TR_NOOP("Save device info")
value: true
}
ListElement {
icon: "private-browsing"
label: QT_TR_NOOP("No device info")
value: false
}
}
]
/* FIXME: StorageLocations.removableStoragePresent is not updated dynamically.
Workaround that by reading it when the bottom edge is opened/closed.
*/
Connections {
target: bottomEdge
onOpenedChanged: StorageLocations.updateRemovableStorageInfo()
}
function indexForValue(model, value) {
var i;
var element;
for (i=0; i 0
opacity:tintOpacity
color: LomiriColors.jet
z:-1
}
}