File: SlideshowView.qml

package info (click to toggle)
lomiri-camera-app 4.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,092 kB
  • sloc: cpp: 1,671; javascript: 27; makefile: 16; sh: 12
file content (508 lines) | stat: -rw-r--r-- 19,208 bytes parent folder | download
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
500
501
502
503
504
505
506
507
508
/*
 * 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.12
import Lomiri.Components 1.3
import Lomiri.Components.ListItems 1.3 as ListItems
import Lomiri.Components.Popups 1.3
import Lomiri.Content 1.3
import Lomiri.Thumbnailer 0.1
import QtGraphicalEffects 1.0

import CameraApp 0.1
import "MimeTypeMapper.js" as MimeTypeMapper
import "qml/components"

FocusScope {
    id: slideshowView

    property var model
    property int currentIndex: listView.currentIndex
    property bool touchAcquired: listView.currentItem ? listView.currentItem.pinchInProgress ||
                                                        editor.active || photoBottomEdge.dragProgress != 0 : false
    property bool inView
    property bool editingAvailable: false
    property bool inSelectionMode: false
    signal toggleHeader
    signal toggleSelection
    signal bottomEdgeCommit
    property var actions: inSelectionMode ? slideShowSelectionActions : slideShowActions

    property list<Action> slideShowSelectionActions: [
        Action {
            text: i18n.tr("Select")
            iconName: listView.currentItem.isSelected ? "close" : "ok"
            onTriggered: slideshowView.toggleSelection()
        }
    ]

    property list<Action> slideShowActions: [
        Action {
            text: i18n.tr("Share")
            iconName: "share"
            onTriggered: {
                var dialog = PopupUtils.open(sharePopoverComponent)
                dialog.parent = slideshowView
            }
        },
        Action {
            text: i18n.tr("Image Info")
            iconName: "info"
            onTriggered: {
                infoPopover.show()
            }
        },
        Action {
            text: i18n.tr("Delete")
            iconName: "delete"
            onTriggered: {
                var dialog = PopupUtils.open(deleteDialogComponent)
                dialog.parent = slideshowView
            }
        },
        Action {
            text: i18n.tr("Settings")
            objectName: "openSettingsPage"
            iconName: "settings"
            onTriggered: {
                galleryPageStack.clear();
                galleryPageStack.push(advancedOptionsComponent)
            }
        },
        Action {
            text: i18n.tr("About")
            objectName: "openAboutPage"
            iconName: "info"
            onTriggered: {
                galleryPageStack.clear();
                galleryPageStack.push(infoPageComponent);
            }
        }
    ]

    Action {
        id: editAction
        text: i18n.tr("Edit")
        iconName: "edit"
        onTriggered: editor.start(listView.currentItem.url)
        enabled: listView.currentItem && !listView.currentItem.isVideo
    }

    Component.onCompleted: {
        // The PhotoEditor is only available in Lomiri.Components.Extras 0.2
        // If we succeed here we add the edit button to the list of actions.
        try { Qt.createQmlObject('import QtQuick 2.12; import Lomiri.Components.Extras 0.2; Item {}', slideshowView) }
        catch (e) { return; }

        editingAvailable = true;
        var newActions = [];
        for (var i = 0; i < slideShowActions.length; i++) newActions.push(slideShowActions[i]);
        newActions.unshift(editAction);
        slideShowActions = newActions;
    }

    function showPhotoAtIndex(index) {
        listView.positionViewAtIndex(index, ListView.Contain);
    }

    function showLastPhotoTaken() {
        listView.currentIndex = 0;
    }

    function exit() {
        if (listView.currentItem) {
            listView.currentItem.zoomOut();
        }
    }

    ListView {
        id: listView
        Component.onCompleted: {
            // FIXME: workaround for qtubuntu not returning values depending on the grid unit definition
            // for Flickable.maximumFlickVelocity and Flickable.flickDeceleration
            var scaleFactor = units.gridUnit / 8;
            maximumFlickVelocity = maximumFlickVelocity * scaleFactor;
            flickDeceleration = flickDeceleration * scaleFactor;
        }

        anchors.fill: parent
        model: slideshowView.model
        focus: true
        orientation: ListView.Horizontal
        boundsBehavior: Flickable.StopAtBounds
        cacheBuffer: width
        highlightRangeMode: ListView.StrictlyEnforceRange
        highlightMoveDuration: LomiriAnimation.FastDuration
        snapMode: ListView.SnapOneItem
        onCountChanged: {
            // currentIndex is -1 by default and stays so until manually set to something else
            if (currentIndex == -1 && count != 0) {
                currentIndex = 0;
            }
        }
        spacing: units.gu(1)
        interactive: currentItem ? !currentItem.pinchInProgress : true
        property real maxDimension: Math.max(width, height)

        removeDisplaced: Transition {
            LomiriNumberAnimation { property: "x" }
        }
        remove: Transition {
            ParallelAnimation {
                LomiriNumberAnimation { property: "opacity"; to: 0 }
            }
        }
        delegate: Item {
            id: delegate
            objectName: "mediaItem" + index
            property bool pinchInProgress: zoomPinchArea.active
            property string url: fileURL
            property bool isSelected: selected
            property alias isVideo: media.isVideo

            function zoomIn(centerX, centerY, factor) {
                flickable.scaleCenterX = centerX / (flickable.sizeScale * flickable.width);
                flickable.scaleCenterY = centerY / (flickable.sizeScale * flickable.height);
                flickable.sizeScale = factor;
            }

            function zoomOut() {
                if (flickable.sizeScale != 1.0) {
                    flickable.scaleCenterX = flickable.contentX / flickable.width / (flickable.sizeScale - 1);
                    flickable.scaleCenterY = flickable.contentY / flickable.height / (flickable.sizeScale - 1);
                    flickable.sizeScale = 1.0;
                }
            }

            function reload() {
                reloadImage(image);
                reloadImage(highResolutionImage);
            }

            function getMedia() {
                return  isVideo ? null : media;
            }

            width: ListView.view.width
            height: ListView.view.height

            ActivityIndicator {
                anchors.centerIn: parent
                visible: running
                running: image.status == Image.Loading
            }

            PinchArea {
                id: zoomPinchArea
                anchors.fill: parent

                property real initialZoom
                property real maximumScale: 3.0
                property real minimumZoom: 1.0
                property real maximumZoom: 3.0
                property bool active: false
                property var center
                enabled: !media.isVideo

                onPinchStarted: {
                    active = true;
                    initialZoom = flickable.sizeScale;
                    center = zoomPinchArea.mapToItem(media, pinch.startCenter.x, pinch.startCenter.y);
                    zoomIn(center.x, center.y, initialZoom);
                }
                onPinchUpdated: {
                    var zoomFactor = MathUtils.clamp(initialZoom * pinch.scale, minimumZoom, maximumZoom);
                    flickable.sizeScale = zoomFactor;
                }
                onPinchFinished: {
                    active = false;
                }

                Flickable {
                    id: flickable
                    anchors.fill: parent
                    contentWidth: media.width
                    contentHeight: media.height
                    contentX: (sizeScale - 1) * scaleCenterX * width
                    contentY: (sizeScale - 1) * scaleCenterY * height
                    interactive: !delegate.pinchInProgress

                    property real sizeScale: 1.0
                    property real scaleCenterX: 0.0
                    property real scaleCenterY: 0.0
                    Behavior on sizeScale {
                        enabled: !delegate.pinchInProgress
                        LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}
                    }
                    Behavior on scaleCenterX {
                        LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}
                    }
                    Behavior on scaleCenterY {
                        LomiriNumberAnimation {duration: LomiriAnimation.FastDuration}
                    }

                    Item {
                        id: media
                        scale:1 - (photoBottomEdge.dragProgress*0.05)
                        width: flickable.width * flickable.sizeScale
                        height: flickable.height * flickable.sizeScale

                        property bool isVideo: MimeTypeMapper.mimeTypeToContentType(fileType) === ContentType.Videos
                        property string photoUrl: editingAvailable ? "image://photo/%1".arg(fileURL.toString()) : fileURL.toString()
                        property string url: fileURL.toString().replace("file://", "");
                        Image {
                            id: image
                            anchors.fill: parent
                            asynchronous: true
                            cache: false
                            source: slideshowView.inView ? (media.isVideo ? "image://thumbnailer/%1".arg(fileURL.toString())
                                                                          : media.photoUrl) : ""
                            sourceSize {
                                width: listView.maxDimension
                                height: listView.maxDimension
                            }
                            fillMode: Image.PreserveAspectFit
                            opacity: status == Image.Ready ? 1.0 : 0.0
                            Behavior on opacity { LomiriNumberAnimation {duration: LomiriAnimation.FastDuration} }
                        }

                        Image {
                            id: highResolutionImage
                            anchors.fill: parent
                            asynchronous: true
                            cache: false
                            source: slideshowView.inView && (flickable.sizeScale > 1.0 ) ?
                                        media.photoUrl :
                                        ""
                            sourceSize {
                                width: listView.maxDimension * (zoomPinchArea.maximumZoom / 2)
                                height: listView.maxDimension  * (zoomPinchArea.maximumZoom / 2)
                            }
                            fillMode: Image.PreserveAspectFit
                        }

                        Icon {
                            objectName: "thumbnailLoadingErrorIcon"
                            anchors.centerIn: parent
                            width: units.gu(30)
                            height: width
                            name: media.isVideo ? "stock_video" : "stock_image"
                            color: "white"
                            opacity: image.status == Image.Error ? 1.0 : 0.0
                            asynchronous: true
                         }
                    }

                    Icon {
                        width: units.gu(5)
                        height: units.gu(5)
                        anchors.centerIn: parent
                        name: "media-playback-start"
                        color: "white"
                        opacity: 0.8
                        visible: media.isVideo
                        asynchronous: true
                    }

                    MouseArea {
                        // Work around being parented under flickable.contentItem
                        parent: flickable
                        anchors.fill: parent
                        onClicked: {
                            // For videos, we check for a tap in the center
                            // on the play button icon
                            if (media.isVideo
                                && mouse.x > width / 2 - units.gu(5)
                                && mouse.x < width / 2 + units.gu(5)
                                && mouse.y > height / 2 - units.gu(5)
                                && mouse.y < height / 2 + units.gu(5)) {
                                var url = fileURL.toString().replace("file://", "video://");
                                Qt.openUrlExternally(url);
                            } else {
                                slideshowView.toggleHeader();
                                mouse.accepted = false;
                            }
                        }
                        onDoubleClicked: {
                            if (listView.moving) {
                                // FIXME: workaround for Qt bug specific to touch:
                                // doubleClicked is received even though the MouseArea
                                // was tapped only once but another MouseArea was also
                                // tapped shortly before.
                                // Ref.: https://bugreports.qt.io/browse/QTBUG-39332
                                return;
                            }

                            if (media.isVideo) {
                                return;
                            }

                            if (flickable.sizeScale < zoomPinchArea.maximumZoom) {
                                zoomIn(mouse.x, mouse.y, zoomPinchArea.maximumZoom);
                            } else {
                                zoomOut();
                            }
                        }
                    }
                }
            }
        }
    }

   Component {
        id: sharePopoverComponent

        SharePopover {
            id: sharePopover

            ContentItem {
                id: contentItem
            }

            Component.onCompleted: {
                contentItem.url = slideshowView.model.get(slideshowView.currentIndex, "filePath");
                transferItems = [contentItem];
            }
            onVisibleChanged: toggleHeader()

            transferContentType: MimeTypeMapper.mimeTypeToContentType(slideshowView.model.get(slideshowView.currentIndex, "fileType"));
        }
    }

   MediaInfoPopover {
        id: infoPopover
        contentWidth:slideshowView.width > units.gu(45) ? units.gu(40) : slideshowView.width*0.85
        currentMedia: listView.currentItem.getMedia()
        model:{
            "fileName": slideshowView.model.get(slideshowView.currentIndex, "fileName"),
            "fileType": slideshowView.model.get(slideshowView.currentIndex, "fileType"),
        }
     }

    Component {
        id: deleteDialogComponent

        DeleteDialog {
            id: deleteDialog

            FileOperations {
                id: fileOperations
            }

            onDeleteFiles: {
                // FIXME: workaround bug in ListView with snapMode: ListView.SnapOneItem
                // whereby after deleting the last item in the list the first
                // item would be shown even though the currentIndex was not set to 0
                var toBeDeleted = listView.currentIndex;
                if (listView.currentIndex == listView.count - 1) {
                    listView.currentIndex = listView.currentIndex - 1;
                }

                var currentFilePath = slideshowView.model.get(toBeDeleted, "filePath");
                fileOperations.remove(currentFilePath);
            }
            onVisibleChanged: toggleHeader()
        }
    }

    Binding { target: header; property: "editMode"; value: editor.active }
    Binding { target: header; property: "editModeActions"; value: editor.item.actions; when: editor.active && editor.item }

    function reloadImage(image) {
        var async = image.asynchronous;
        var source = image.source;
        image.asynchronous = false;
        image.source = "";
        image.asynchronous = async;
        image.source = source;
    }

    Loader {
        id: editor
        source: "PhotoEditorLoader.qml"
        active: false
        anchors.fill: parent

        function start(url) {
            editor.active = true;
            editor.item.start(url);
        }

        Connections {
            target: editor.item
            onClosed: {
                editor.active = false;
                if (photoWasModified) listView.currentItem.reload();
            }
        }
    }

    OverlayPanel {
        id:bottomimageBlur

        overlayItem: photoBottomEdge
        anchorTo: photoBottomEdge

        visible: photoBottomEdge.status !== BottomEdge.Hidden
        transform: Translate {
            id:beTransalte
            y: photoBottomEdge.height - (photoBottomEdge.height*photoBottomEdge.dragProgress)
            Behavior on y { LomiriNumberAnimation {duration:LomiriAnimation.FastDuration}}
        }

        blur.visible: appSettings.blurEffects && !appSettings.blurEffectsPreviewOnly
        blur.backgroundItem:  listView
        blur.transparentBorder:false
        blur.offset: Qt.point(photoBottomEdge.x,beTransalte.y)
    }

    BottomEdge {
        id: photoBottomEdge
        enabled: !editor.active
        visible: enabled
        height:units.gu(8)
        hint.text: i18n.tr("Back to Photo roll");
        hint.iconName: "go-up"
        hint.visible:enabled
        hint.opacity: 1.0 - photoBottomEdge.dragProgress

        contentComponent: Page {
            id:bottomReturn
            opacity: photoBottomEdge.dragProgress
            header: PageHeader { opacity: 0 }

            Icon {
                id:bottomEdgeGoUpIcon
                height:units.gu(3)
                width:units.gu(3)
                name:"go-up"
                color: theme.palette.normal.backgroundText
                anchors.top:parent.top
                anchors.horizontalCenter:parent.horizontalCenter
            }
            Label {
                anchors.horizontalCenter:parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                height:photoBottomEdge.height
                text: photoBottomEdge.hint.text
                color: theme.palette.normal.backgroundText
                fontSize: "x-large"
            }
        }

        onCommitCompleted:  { bottomEdgeCommit(); photoBottomEdge.collapse(); }
    }
}