Debian Codegraph @

rc/qml/MediaViewer/SingleMediaViewer.qml

raw
1 /*
2 * Copyright 2014-2015 Canonical Ltd.
3 * Copyright (C) 2025 UBports Foundation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 import QtQuick 2.9
19 import QtMultimedia 5.0
20 import Lomiri.Components 1.3
21 import Lomiri.Components.ListItems 1.3 as ListItems
22 import Lomiri.Components.Popups 1.3
23 import Lomiri.Content 1.3
24 import Lomiri.Thumbnailer 0.1
25 import Lomiri.Components.Extras 0.2
26 import Gallery 1.0
27 import "../Components"
28
29 Item {
30 id: viewer
31 property bool pinchInProgress: zoomPinchArea.active
32 property var mediaSource
33 property size thumbSize: Qt.size(viewer.width * 1.05, viewer.height * 1.05)
34 property bool showThumbnail: true
35
36 property bool isVideo: mediaSource.type === MediaSource.Video
37 property bool userInteracting: pinchInProgress || flickable.sizeScale != 1.0
38 property bool fullyZoomed: flickable.sizeScale == zoomPinchArea.maximumZoom
39 property bool fullyUnzoomed: flickable.sizeScale == zoomPinchArea.minimumZoom
40 property bool animateMediaOnHeight: false
41 property bool imageReady: image.status == Image.Ready
42
43 property alias paintedHeight: image.paintedHeight
44 property alias paintedWidth: image.paintedWidth
45
46 signal clicked()
47
48 onWidthChanged: {
49 // Only change thumbSize if width increases more than 5%
50 // that way we do not reload image for small resizes
51 if (width > thumbSize.width) {
52 thumbSize = Qt.size(width * 1.05, height * 1.05);
53 }
54 }
55
56 onHeightChanged: {
57 // Only change thumbSize if height increases more than 5%
58 // that way we do not reload image for small resizes
59 if (height > thumbSize.height) {
60 thumbSize = Qt.size(width * 1.05, height * 1.05);
61 }
62 }
63
64 function zoomIn(centerX, centerY, factor) {
65 flickable.scaleCenterX = centerX / (flickable.sizeScale * flickable.width);
66 flickable.scaleCenterY = centerY / (flickable.sizeScale * flickable.height);
67 flickable.sizeScale = factor;
68 }
69
70 function zoomOut() {
71 if (flickable.sizeScale != 1.0) {
72 flickable.scaleCenterX = flickable.contentX / flickable.width / (flickable.sizeScale - 1);
73 flickable.scaleCenterY = flickable.contentY / flickable.height / (flickable.sizeScale - 1);
74 flickable.sizeScale = 1.0;
75 }
76 }
77
78 function reset() {
79 if (!viewer.isVideo)
80 zoomOut()
81 }
82
83 ActivityIndicator {
84 anchors.centerIn: parent
85 visible: running
86 running: image.status != Image.Ready && image.status != Image.Error
87 }
88
89 PinchArea {
90 id: zoomPinchArea
91 anchors.fill: parent
92
93 property real initialZoom
94 property real maximumScale: 3.0
95 property real minimumZoom: 1.0
96 property real maximumZoom: 5.0
97 property bool active: false
98 property var center
99
100 onPinchStarted: {
101 active = true;
102 initialZoom = flickable.sizeScale;
103 center = zoomPinchArea.mapToItem(media, pinch.startCenter.x, pinch.startCenter.y);
104 zoomIn(center.x, center.y, initialZoom);
105 }
106 onPinchUpdated: {
107 var zoomFactor = MathUtils.clamp(initialZoom * pinch.scale, minimumZoom, maximumZoom);
108 flickable.sizeScale = zoomFactor;
109 }
110 onPinchFinished: {
111 active = false;
112 }
113
114 Flickable {
115 id: flickable
116 anchors.fill: parent
117 contentWidth: media.width
118 contentHeight: media.height
119 contentX: (sizeScale - 1) * scaleCenterX * width
120 contentY: (sizeScale - 1) * scaleCenterY * height
121 interactive: !viewer.pinchInProgress
122
123 property real sizeScale: 1.0
124 property real scaleCenterX: 0.0
125 property real scaleCenterY: 0.0
126 Behavior on sizeScale {
127 enabled: !viewer.pinchInProgress
128 LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}
129 }
130 Behavior on scaleCenterX {
131 LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}
132 }
133 Behavior on scaleCenterY {
134 LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}
135 }
136
137 Item {
138 id: media
139
140 width: flickable.width * flickable.sizeScale
141 height: flickable.height * flickable.sizeScale
142
143 Connections {
144 target: mediaSource
145 onDataChanged: {
146 image.source = "";
147 image.source = "image://photo/" + mediaSource.path
148
149 highResolutionImage.source = "";
150 highResolutionImage.source = "image://photo/" + mediaSource.path
151 }
152 }
153
154 Behavior on height {
155 enabled: viewer.animateMediaOnHeight
156 LomiriNumberAnimation {}
157 }
158
159 Image {
160 // Reduce width/hight so background is not visible on the border of camera photos
161 width: image.paintedWidth - 1
162 height: image.paintedHeight - 1
163 anchors.centerIn: image
164 visible: !viewer.isVideo && image.opacity == 1.0
165
166 asynchronous: true
167 cache: true
168 fillMode: Image.Tile
169 horizontalAlignment: Image.AlignLeft
170 verticalAlignment: Image.AlignTop
171 source: "../../img/transparency-bg.png"
172 }
173
174 Image {
175 id: image
176 anchors.fill: parent
177 asynchronous: true
178 cache: false
179 source: {
180 if (viewer.isVideo) {
181 return "image://thumbnailer/" + mediaSource.path
182 } else {
183 return "image://photo/" + mediaSource.path
184 }
185 }
186 sourceSize {
187 width: viewer.thumbSize.width
188 height: viewer.thumbSize.height
189 }
190 fillMode: Image.PreserveAspectFit
191 visible: viewer.showThumbnail
192 opacity: status == Image.Ready ? 1.0 : 0.0
193 Behavior on opacity { LomiriNumberAnimation {duration: LomiriAnimation.FastDuration} }
194
195 }
196
197 Image {
198 id: highResolutionImage
199 anchors.fill: parent
200 asynchronous: true
201 cache: false
202 // Load image using the photo image provider to ensure EXIF orientation
203 source: flickable.sizeScale > 1.0 ? "image://photo/" + mediaSource.path : ""
204 sourceSize {
205 width: width
206 height: height
207 }
208 opacity: status == Image.Ready ? 1.0 : 0.0
209 fillMode: Image.PreserveAspectFit
210 }
211
212 Item {
213 id: mediaLoadingError
214 anchors.centerIn: parent
215 width: parent.width
216 height: mediaLoadingErrorIcon.height + units.gu(5) + mediaLoadingErrorLabel.contentHeight
217 visible: opacity > 0
218 opacity: image.status == Image.Error ? 1.0 : 0.0
219 Behavior on opacity { LomiriNumberAnimation {duration: LomiriAnimation.FastDuration} }
220
221 Icon {
222 id: mediaLoadingErrorIcon
223 anchors.horizontalCenter: parent.horizontalCenter
224 width: units.gu(8)
225 height: width
226 name: "stock_image"
227 color: "white"
228 opacity: 0.8
229 }
230
231 Label {
232 id: mediaLoadingErrorLabel
233 anchors {
234 horizontalCenter: parent.horizontalCenter
235 top: mediaLoadingErrorIcon.bottom
236 topMargin: units.gu(5)
237 }
238
239 width: units.gu(30)
240 wrapMode: Text.WordWrap
241 horizontalAlignment: Text.AlignHCenter
242 text: i18n.tr("An error has occurred attempting to load media")
243 fontSize: "large"
244 color: "lightgrey"
245 }
246 }
247 }
248
249 // If the media is a video, show a 'play' icon
250 PlayIcon {
251 anchors.centerIn: parent
252 width: units.gu(7)
253 height: width
254 visible: viewer.isVideo && image.status == Image.Ready
255 }
256
257 MouseArea {
258 id: viewerMouseArea
259 // Work around being parented under flickable.contentItem
260 parent: flickable
261 anchors.fill: parent
262 property bool eventAccepted: false
263
264 onDoubleClicked: {
265 if (viewerMouseArea.eventAccepted)
266 return;
267
268 clickTimer.stop();
269 viewer.animateMediaOnHeight = false
270
271 if (viewer.ListView.view.moving) {
272 // FIXME: workaround for Qt bug specific to touch:
273 // doubleClicked is received even though the MouseArea
274 // was tapped only once but another MouseArea was also
275 // tapped shortly before.
276 // Ref.: https://bugreports.qt.io/browse/QTBUG-39332
277 return;
278 }
279 if (viewer.isVideo) return;
280
281 if (flickable.sizeScale < zoomPinchArea.maximumZoom) {
282 zoomIn(mouse.x, mouse.y, zoomPinchArea.maximumZoom);
283 } else {
284 zoomOut();
285 }
286 }
287 onClicked: {
288 // For videos, we check for a tap in the center on the
289 // play button icon
290 if (viewer.isVideo
291 && mouse.x > width / 2 - units.gu(5)
292 && mouse.x < width / 2 + units.gu(5)
293 && mouse.y > height / 2 - units.gu(5)
294 && mouse.y < height / 2 + units.gu(5)) {
295 var url = mediaSource.path.toString().replace("file://", "video://");
296 Qt.openUrlExternally(url);
297 } else {
298 viewerMouseArea.eventAccepted = false;
299 clickTimer.start();
300 }
301 }
302
303 Timer {
304 id: clickTimer
305 interval: 200
306 onTriggered: {
307 viewerMouseArea.eventAccepted = true
308 viewer.animateMediaOnHeight = true
309 viewer.clicked()
310 }
311 }
312 }
313 }
314 }
315 }