| 1 |
/* |
| 2 |
* Copyright 2014 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.12 |
| 19 |
import Lomiri.Components 1.3 |
| 20 |
import Lomiri.Components.Popups 1.3 |
| 21 |
import Lomiri.Thumbnailer 0.1 |
| 22 |
import Lomiri.Content 1.3 |
| 23 |
import Qt.labs.settings 1.0 |
| 24 |
import CameraApp 0.1 |
| 25 |
import "MimeTypeMapper.js" as MimeTypeMapper |
| 26 |
|
| 27 |
FocusScope { |
| 28 |
id: photogridView |
| 29 |
|
| 30 |
property var model |
| 31 |
signal photoClicked(int index) |
| 32 |
signal photoPressAndHold(int index) |
| 33 |
signal photoSelectionAreaClicked(int index) |
| 34 |
signal exitUserSelectionMode |
| 35 |
signal toggleHeader() |
| 36 |
property real headerHeight |
| 37 |
property bool inView |
| 38 |
property bool inSelectionMode |
| 39 |
property bool userSelectionMode: false |
| 40 |
property var actions: userSelectionMode ? userSelectionActions : regularModeActions |
| 41 |
property list<Action> userSelectionActions: [ |
| 42 |
Action { |
| 43 |
text: i18n.tr("Share") |
| 44 |
iconName: "share" |
| 45 |
enabled: model.selectedFiles.length > 0 |
| 46 |
onTriggered: { |
| 47 |
// Display a warning message if we are attempting to share mixed |
| 48 |
// content, as the framework does not properly support this |
| 49 |
if (selectionContainsMixedMedia()) { |
| 50 |
PopupUtils.open(unableShareDialogComponent).parent = photogridView; |
| 51 |
} else { |
| 52 |
PopupUtils.open(sharePopoverComponent).parent = photogridView; |
| 53 |
} |
| 54 |
} |
| 55 |
}, |
| 56 |
Action { |
| 57 |
text: i18n.tr("Delete") |
| 58 |
iconName: "delete" |
| 59 |
onTriggered: { |
| 60 |
if (model.selectedFiles.length > 0) { |
| 61 |
var dialog = PopupUtils.open(deleteDialogComponent) |
| 62 |
dialog.parent = photogridView |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
] |
| 67 |
|
| 68 |
property list<Action> regularModeActions: [ |
| 69 |
Action { |
| 70 |
text: i18n.tr("Settings") |
| 71 |
objectName: "openSettingsPage" |
| 72 |
iconName: "settings" |
| 73 |
onTriggered: { |
| 74 |
galleryPageStack.clear(); |
| 75 |
galleryPageStack.push(advancedOptionsComponent); |
| 76 |
} |
| 77 |
}, |
| 78 |
Action { |
| 79 |
text: i18n.tr("About") |
| 80 |
objectName: "openAboutPage" |
| 81 |
iconName: "info" |
| 82 |
onTriggered: { |
| 83 |
galleryPageStack.clear(); |
| 84 |
galleryPageStack.push(infoPageComponent); |
| 85 |
} |
| 86 |
} |
| 87 |
] |
| 88 |
|
| 89 |
function selectionContainsMixedMedia() { |
| 90 |
var selection = model.selectedFiles; |
| 91 |
var lastType = model.get(selection[0], "fileType"); |
| 92 |
for (var i = 1; i < selection.length; i++) { |
| 93 |
var type = model.get(selection[i], "fileType"); |
| 94 |
if (type !== lastType) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
lastType = type; |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
function showPhotoAtIndex(index) { |
| 103 |
gridView.positionViewAtIndex(index, GridView.Center); |
| 104 |
} |
| 105 |
|
| 106 |
function exit() { |
| 107 |
showPhotoAtIndex(0); |
| 108 |
} |
| 109 |
|
| 110 |
Settings { |
| 111 |
id:galleryGridViewSettings |
| 112 |
property var delegateWidth: gridView.baseDelegateWidth |
| 113 |
property var delegateHeight: gridView.baseDelegateHeight |
| 114 |
property real currentScale: 1 |
| 115 |
} |
| 116 |
|
| 117 |
PinchArea { |
| 118 |
id:galleryViewPinch |
| 119 |
anchors.fill: parent |
| 120 |
|
| 121 |
property real currentScale: galleryGridViewSettings.currentScale |
| 122 |
pinch.dragAxis: Pinch.NoDrag |
| 123 |
pinch.minimumScale: 0.5 |
| 124 |
pinch.maximumScale: 1.5 |
| 125 |
//FIXME This is a a workaround that remember the scale between pinches |
| 126 |
pinch.target: Item { |
| 127 |
width: galleryGridViewSettings.delegateWidth |
| 128 |
height: galleryGridViewSettings.delegateHeight |
| 129 |
scale:galleryGridViewSettings.currentScale |
| 130 |
visible:false |
| 131 |
} |
| 132 |
|
| 133 |
onPinchUpdated: { |
| 134 |
if( pinch.scale ) { |
| 135 |
galleryGridViewSettings.currentScale = Math.min(2,Math.max(0.33,galleryViewPinch.pinch.target.scale)) ; |
| 136 |
galleryGridViewSettings.delegateHeight = Math.floor(gridView.baseDelegateHeight * currentScale); |
| 137 |
galleryGridViewSettings.delegateWidth = Math.floor(gridView.baseDelegateWidth * currentScale); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
ResponsiveGridView { |
| 142 |
id: gridView |
| 143 |
anchors.fill: parent |
| 144 |
// FIXME: prevent the header from overlapping the beginning of the grid |
| 145 |
// when Qt 5.3 is landed, use property 'displayMarginBeginning' instead |
| 146 |
// cf. http://qt-project.org/doc/qt-5/qml-qtquick-gridview.html#displayMarginBeginning-prop |
| 147 |
header: Item { |
| 148 |
width: gridView.width |
| 149 |
height: headerHeight |
| 150 |
} |
| 151 |
|
| 152 |
property var baseDelegateWidth: Math.min(photogridView.width,photogridView.height)/2/2 |
| 153 |
property var baseDelegateHeight: baseDelegateWidth |
| 154 |
|
| 155 |
Component.onCompleted: { |
| 156 |
// FIXME: workaround for qtubuntu not returning values depending on the grid unit definition |
| 157 |
// for Flickable.maximumFlickVelocity and Flickable.flickDeceleration |
| 158 |
var scaleFactor = units.gridUnit / 8; |
| 159 |
maximumFlickVelocity = maximumFlickVelocity * scaleFactor; |
| 160 |
flickDeceleration = flickDeceleration * scaleFactor; |
| 161 |
} |
| 162 |
|
| 163 |
minimumHorizontalSpacing: units.dp(2) |
| 164 |
maximumNumberOfColumns: 100 |
| 165 |
delegateWidth: galleryGridViewSettings.delegateWidth |
| 166 |
delegateHeight: galleryGridViewSettings.delegateHeight |
| 167 |
StateSaver.properties:"delegateWidth,delegateHeight" |
| 168 |
|
| 169 |
model: photogridView.model |
| 170 |
delegate: Item { |
| 171 |
z:10 |
| 172 |
id: cellDelegate |
| 173 |
objectName: "mediaItem" + index |
| 174 |
|
| 175 |
width: gridView.cellWidth |
| 176 |
height: gridView.cellHeight |
| 177 |
|
| 178 |
Behavior on height { LomiriNumberAnimation {} } |
| 179 |
Behavior on width { LomiriNumberAnimation {} } |
| 180 |
|
| 181 |
property bool isVideo: MimeTypeMapper.mimeTypeToContentType(fileType) === ContentType.Videos |
| 182 |
|
| 183 |
Image { |
| 184 |
id: thumbnail |
| 185 |
property real margin: units.dp(2) |
| 186 |
anchors { |
| 187 |
top: parent.top |
| 188 |
topMargin: index < photogridView.columns ? 0 : margin/2 |
| 189 |
bottom: parent.bottom |
| 190 |
bottomMargin: margin/2 |
| 191 |
left: parent.left |
| 192 |
leftMargin: index % photogridView.columns == 0 ? 0 : margin/2 |
| 193 |
right: parent.right |
| 194 |
rightMargin: index % photogridView.columns == photogridView.columns - 1 ? 0 : margin/2 |
| 195 |
} |
| 196 |
|
| 197 |
asynchronous: true |
| 198 |
cache: photogridView.inView |
| 199 |
// The thumbnailer does not seem to check when an image has been changed on disk, |
| 200 |
// so we use this hack to force it to check and refresh if necessary. |
| 201 |
source: photogridView.inView ? "image://thumbnailer/" + fileURL.toString() + "?at=" + Date.now() : "" |
| 202 |
sourceSize { |
| 203 |
width: gridView.baseDelegateWidth |
| 204 |
height: gridView.baseDelegateHeight |
| 205 |
} |
| 206 |
fillMode: Image.PreserveAspectCrop |
| 207 |
opacity: status == Image.Ready ? 1.0 : 0.0 |
| 208 |
Behavior on opacity { LomiriNumberAnimation {duration: LomiriAnimation.FastDuration} } |
| 209 |
} |
| 210 |
|
| 211 |
PlayIcon { |
| 212 |
anchors.centerIn: parent |
| 213 |
width: units.gu(5.5) |
| 214 |
height: width |
| 215 |
visible: isVideo |
| 216 |
} |
| 217 |
|
| 218 |
Icon { |
| 219 |
objectName: "thumbnailLoadingErrorIcon" |
| 220 |
anchors.centerIn: parent |
| 221 |
width: units.gu(6) |
| 222 |
height: width |
| 223 |
name: cellDelegate.isVideo ? "stock_video" : "stock_image" |
| 224 |
color: "white" |
| 225 |
opacity: thumbnail.status == Image.Error ? 1.0 : 0.0 |
| 226 |
asynchronous: true |
| 227 |
} |
| 228 |
|
| 229 |
MouseArea { |
| 230 |
anchors.fill: parent |
| 231 |
onClicked: photogridView.photoClicked(index) |
| 232 |
onPressAndHold: photogridView.photoPressAndHold(index) |
| 233 |
} |
| 234 |
|
| 235 |
Rectangle { |
| 236 |
anchors { |
| 237 |
top: parent.top |
| 238 |
right: parent.right |
| 239 |
topMargin: units.gu(0.5) |
| 240 |
rightMargin: units.gu(0.5) |
| 241 |
} |
| 242 |
width: units.gu(4) |
| 243 |
height: units.gu(4) |
| 244 |
color: selected ? theme.palette.normal.positive : LomiriColors.inkstone |
| 245 |
radius: 10 |
| 246 |
opacity: selected ? 0.8 : 0.6 |
| 247 |
visible: inSelectionMode |
| 248 |
|
| 249 |
Icon { |
| 250 |
objectName: "mediaItemCheckBox" |
| 251 |
anchors.centerIn: parent |
| 252 |
width: parent.width * 0.8 |
| 253 |
height: parent.height * 0.8 |
| 254 |
name: "ok" |
| 255 |
color: "white" |
| 256 |
visible: selected |
| 257 |
asynchronous: true |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
MouseArea { |
| 263 |
anchors { |
| 264 |
top: parent.top |
| 265 |
right: parent.right |
| 266 |
} |
| 267 |
width: parent.width * 0.5 |
| 268 |
height: parent.height * 0.5 |
| 269 |
enabled: inSelectionMode |
| 270 |
|
| 271 |
onClicked: { |
| 272 |
mouse.accepted = true; |
| 273 |
photogridView.photoSelectionAreaClicked(index) |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
Component { |
| 281 |
id: contentItemComp |
| 282 |
ContentItem {} |
| 283 |
} |
| 284 |
|
| 285 |
Component { |
| 286 |
id: sharePopoverComponent |
| 287 |
|
| 288 |
SharePopover { |
| 289 |
id: sharePopover |
| 290 |
|
| 291 |
onContentPeerSelected: photogridView.exitUserSelectionMode(); |
| 292 |
onVisibleChanged: photogridView.toggleHeader() |
| 293 |
|
| 294 |
transferContentType: MimeTypeMapper.mimeTypeToContentType(model.get(model.selectedFiles[0], "fileType")); |
| 295 |
transferItems: model.selectedFiles.map(function(row) { |
| 296 |
return contentItemComp.createObject(parent, {"url": model.get(row, "filePath")}); |
| 297 |
}) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
Component { |
| 302 |
id: deleteDialogComponent |
| 303 |
|
| 304 |
DeleteDialog { |
| 305 |
id: deleteDialog |
| 306 |
|
| 307 |
FileOperations { |
| 308 |
id: fileOperations |
| 309 |
} |
| 310 |
|
| 311 |
onDeleteFiles: { |
| 312 |
model.deleteSelectedFiles(); |
| 313 |
photogridView.exitUserSelectionMode(); |
| 314 |
} |
| 315 |
onVisibleChanged: photogridView.toggleHeader() |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
Component { |
| 320 |
id: unableShareDialogComponent |
| 321 |
UnableShareDialog { |
| 322 |
objectName: "unableShareDialog" |
| 323 |
onVisibleChanged: photogridView.toggleHeader() |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
} |