Debian Codegraph @

rc/qml/Components/MediaGrid.qml

raw
1 /*
2 * Copyright (C) 2013-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 version 3 as
7 * published by the Free Software Foundation.
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 Lomiri.Components 1.3
20 import Lomiri.Thumbnailer 0.1
21 import Gallery 1.0
22 import "../OrganicView"
23 import "../Utility"
24 import "../Components"
25 import "../../js/Gallery.js" as Gallery
26 import "../../js/GalleryUtility.js" as GalleryUtility
27
28 /*!
29 Shows a grid with media from a given model. The medias can be selected if a
30 proper SelectionSate is given
31 */
32 GridView {
33
34 id: photosGrid
35
36 /// Selection for the grid
37 property SelectionState selection: null
38
39 /// Size of the thumbnails
40 property real thumbnailSize: photosGrid.gridSize
41 /// Size of the requested thumbnails
42 property real thumbnailSourceSize: units.gu(19)
43 /// Minimum space between the tumbnails
44 property real minimumSpace: units.gu(0.6)
45 /// Stores the spacing between 2 images in pixel
46 property real spacing: __calculateSpacing(width)
47
48 /*!
49 Calculates the spacing that should be used, to fit the fotos horizontally nicely
50 */
51 function __calculateSpacing(viewWidth) {
52 var itemSize = thumbnailSize + minimumSpace
53 var itemCount = Math.floor(viewWidth / itemSize)
54 var spareSpace = viewWidth - itemCount * itemSize
55 return (minimumSpace + spareSpace / itemCount)
56 }
57
58 anchors.leftMargin: units.gu(1)
59 anchors.rightMargin: units.gu(1)
60
61 cellWidth: thumbnailSize + spacing
62 cellHeight: thumbnailSize + spacing
63
64 maximumFlickVelocity: units.gu(800)
65 flickDeceleration: units.gu(400)
66
67 // Use this rather than anchors.topMargin to prevent delegates from being
68 // unloaded while scrolling out of view but still partially visible
69 header: Item {
70 width: parent.width
71 height: units.gu(1)
72 }
73
74 delegate: Item {
75 objectName: "allPotosGridPhoto"
76 width: photosGrid.cellWidth
77 height: photosGrid.cellHeight
78
79 LomiriShape {
80 id: roundedThumbnail
81
82 anchors.centerIn: parent
83
84 width: photosGrid.thumbnailSize
85 height: photosGrid.thumbnailSize
86
87 radius: "medium"
88 property bool isLoading: source.status === Image.Loading
89
90 backgroundColor: "black"
91 sourceFillMode: LomiriShape.PreserveAspectCrop
92 source: Image {
93 id: thumbImage
94 source: "image://thumbnailer/" + mediaSource.path + "?at=" + mediaSource.lastModified
95 asynchronous: true
96 fillMode: Image.PreserveAspectCrop
97 sourceSize {
98 width: thumbnailSourceSize
99 height: thumbnailSourceSize
100 }
101 }
102
103 Icon {
104 anchors.centerIn: parent
105 width: units.gu(6)
106 height: width
107 visible: thumbImage.status == Image.Error
108 name: "stock_image"
109 color: "white"
110 opacity: 0.8
111 }
112
113 // Display a play icon if the thumbnail is from a video
114 PlayIcon {
115 anchors.centerIn: parent
116 width: units.gu(5.5)
117 height: width
118 visible: mediaSource.type === MediaSource.Video && thumbImage.status == Image.Ready
119 }
120
121 OrganicItemInteraction {
122 objectName: "photosViewPhoto"
123 selectionItem: model.mediaSource
124 selection: photosGrid.selection ? photosGrid.selection : null
125
126 onPressed: {
127 var rect = GalleryUtility.getRectRelativeTo(roundedThumbnail, photosOverview);
128 photosOverview.mediaSourcePressed(mediaSource, rect);
129 }
130
131 onSelected: photosGrid.positionViewAtIndex(index, GridView.Contain);
132 }
133
134 Component {
135 id: component_loadIndicator
136 ActivityIndicator {
137 id: loadIndicator
138 running: true
139 }
140 }
141 Loader {
142 id: loader_loadIndicator
143 anchors.centerIn: roundedThumbnail
144 sourceComponent: roundedThumbnail.isLoading ? component_loadIndicator : undefined
145 asynchronous: true
146 }
147 }
148 }
149
150 displaced: Transition {
151 NumberAnimation {
152 properties: "x,y"
153 duration: Gallery.FAST_DURATION
154 easing.type: Easing.InQuint
155 }
156 }
157 }