| 1 |
/* |
| 2 |
* Copyright (C) 2011-2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License version 3 as |
| 6 |
* published by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Jim Nelson <[email protected]> |
| 18 |
* Lucas Beeler <[email protected]> |
| 19 |
*/ |
| 20 |
|
| 21 |
import QtQuick 2.9 |
| 22 |
import Lomiri.Components 1.3 |
| 23 |
import Gallery 1.0 |
| 24 |
import "../../js/Gallery.js" as Gallery |
| 25 |
import "../../js/GalleryUtility.js" as GalleryUtility |
| 26 |
import "../Components" |
| 27 |
import "../Utility" |
| 28 |
|
| 29 |
/*! |
| 30 |
*/ |
| 31 |
Page { |
| 32 |
id: albumViewer |
| 33 |
|
| 34 |
/// The album that is shown by this viewer |
| 35 |
property Album album |
| 36 |
|
| 37 |
/// This property exists only for the benefit of autopilot tests |
| 38 |
/// which cannot access the album property directy as it's not primitive |
| 39 |
property int albumId: (album) ? album.id : -1 |
| 40 |
|
| 41 |
// Read-only |
| 42 |
/*! |
| 43 |
*/ |
| 44 |
property alias pagesPerSpread: albumSpreadViewer.pagesPerSpread |
| 45 |
/*! |
| 46 |
*/ |
| 47 |
property bool animationRunning: albumSpreadViewer.isFlipping || |
| 48 |
photoViewerLoader.animationRunning |
| 49 |
|
| 50 |
property string mediaCurrentlyInView |
| 51 |
StateSaver.properties: "mediaCurrentlyInView" |
| 52 |
|
| 53 |
/// Indicates if this view is open |
| 54 |
property bool isOpen: false |
| 55 |
/// Origin (rectangle) where this view is animated from when calling open() |
| 56 |
/// And where it is animated to when this view is closed |
| 57 |
property variant origin |
| 58 |
/// The preview item that was used to open the view, or null, if it was opend another way |
| 59 |
/// This item will be hidden when this view is opened. And shown again after closing this view |
| 60 |
property Item previewItem: null |
| 61 |
|
| 62 |
function reopenPicture() { |
| 63 |
if (album && albumViewer.mediaCurrentlyInView !== "") { |
| 64 |
for (var i in album.allMediaSources) { |
| 65 |
var source = album.allMediaSources[i]; |
| 66 |
|
| 67 |
if (source.path == albumViewer.mediaCurrentlyInView) { |
| 68 |
photoViewerLoader.fadeOpen(source); |
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
onVisibleChanged: { |
| 76 |
if (visible) reopenPicture(); |
| 77 |
} |
| 78 |
|
| 79 |
onCloseRequested: { |
| 80 |
if (origin) { |
| 81 |
if (album) |
| 82 |
album.closed = !stayOpen || (viewingPage == 0); |
| 83 |
if (previewItem) |
| 84 |
previewItem.visible = true |
| 85 |
} |
| 86 |
visible = false |
| 87 |
if (!APP.desktopMode) |
| 88 |
setFullScreenAppMode(false); |
| 89 |
overview.popPage(); |
| 90 |
isOpen = false |
| 91 |
} |
| 92 |
onQuickCloseRequested: { |
| 93 |
visible = false |
| 94 |
if (!APP.desktopMode) |
| 95 |
setFullScreenAppMode(false); |
| 96 |
overview.popPage(); |
| 97 |
isOpen = false |
| 98 |
if (previewItem) |
| 99 |
previewItem.visible = true; |
| 100 |
} |
| 101 |
|
| 102 |
// When the user clicks the back button or pages back to the cover. |
| 103 |
signal closeRequested(bool stayOpen, int viewingPage) |
| 104 |
// When the user clicks the back button or pages back to the cover. |
| 105 |
signal quickCloseRequested() |
| 106 |
|
| 107 |
title: i18n.tr("Album") |
| 108 |
|
| 109 |
function closeAlbum() { |
| 110 |
if (photoViewerLoader.item && photoViewerLoader.item.isPoppedUp) { |
| 111 |
photoViewerLoader.item.closePopupPhotoViewer(); |
| 112 |
} |
| 113 |
__close(); |
| 114 |
} |
| 115 |
|
| 116 |
/// Closes the view as stores the page number the page number currently viewed |
| 117 |
function __close() { |
| 118 |
closeRequested(album.containedCount > 0, albumSpreadViewer.viewingPage) |
| 119 |
} |
| 120 |
|
| 121 |
/// Closes the view without animation or storing anything |
| 122 |
function __quickClose() { |
| 123 |
quickCloseRequested() |
| 124 |
} |
| 125 |
|
| 126 |
function resetView(album) { |
| 127 |
albumViewer.album = album; |
| 128 |
|
| 129 |
albumSpreadViewer.visible = true; |
| 130 |
chrome.resetVisibility(false); |
| 131 |
organicView.visible = false; |
| 132 |
|
| 133 |
albumSpreadViewer.viewingPage = album.currentPage; |
| 134 |
} |
| 135 |
|
| 136 |
function showMediaSelector() { |
| 137 |
overview.pushPage(component_mediaSelector); |
| 138 |
} |
| 139 |
|
| 140 |
AlbumSpreadViewer { |
| 141 |
id: albumSpreadViewer |
| 142 |
objectName: "spreadViewer" |
| 143 |
|
| 144 |
anchors.fill: parent |
| 145 |
|
| 146 |
album: albumViewer.album |
| 147 |
load: parent.visible |
| 148 |
|
| 149 |
// Keyboard focus while visible and viewer is not visible |
| 150 |
focus: visible |
| 151 |
|
| 152 |
showCover: true |
| 153 |
|
| 154 |
Keys.onPressed: { |
| 155 |
if (event.key !== Qt.Key_Left && event.key !== Qt.Key_Right) |
| 156 |
return; |
| 157 |
|
| 158 |
var direction = (event.key === Qt.Key_Left ? -1 : 1); |
| 159 |
var destination = albumSpreadViewer.viewingPage + |
| 160 |
direction * albumSpreadViewer.pagesPerSpread; |
| 161 |
|
| 162 |
if (!albumSpreadViewer.isFlipping && |
| 163 |
albumSpreadViewer.isPopulatedContentPage(destination)) { |
| 164 |
chrome.hide(true); |
| 165 |
|
| 166 |
event.accepted = true; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
SwipeArea { |
| 171 |
property real commitTurnFraction: 0.05 |
| 172 |
|
| 173 |
// Should be treated as internal to this SwipeArea. Used for |
| 174 |
// tracking the last swipe direction; if the user moves a page |
| 175 |
// past the 'commit' point, then back, we'll track that here, |
| 176 |
// treat it as a request to release the page and go back to idling. |
| 177 |
// |
| 178 |
// Per the convention used elsewhere, true for right, false for left. |
| 179 |
property bool lastSwipeLeftToRight: true |
| 180 |
property int prevSwipingX: -1 |
| 181 |
property bool canceled: false |
| 182 |
|
| 183 |
// Normal press/click. |
| 184 |
function pressed(x, y) { |
| 185 |
var hit = albumSpreadViewer.hitTestFrame(x, y, parent); |
| 186 |
if (!hit) |
| 187 |
return; |
| 188 |
|
| 189 |
// Handle add button. |
| 190 |
if (hit.objectName === "addButton") |
| 191 |
showMediaSelector(); |
| 192 |
else if (!hit.mediaSource) { |
| 193 |
albumViewer.header.visible = !albumViewer.header.visible; |
| 194 |
if (!APP.desktopMode) |
| 195 |
setFullScreenAppMode(!albumViewer.header.visible); |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
albumViewer.mediaCurrentlyInView = hit.mediaSource.path; |
| 200 |
photoViewerLoader.fadeOpen(hit.mediaSource); |
| 201 |
photoViewerLoader.item.header.visible = false; |
| 202 |
if (!APP.desktopMode) |
| 203 |
setFullScreenAppMode(true); |
| 204 |
} |
| 205 |
|
| 206 |
// Long press/right click. |
| 207 |
function alternativePressed(x, y) { |
| 208 |
var hit = albumSpreadViewer.hitTestFrame(x, y, parent); |
| 209 |
if (!hit || !hit.mediaSource) |
| 210 |
return; |
| 211 |
|
| 212 |
// FIXME show a popover menu for that photo |
| 213 |
} |
| 214 |
|
| 215 |
anchors.fill: parent |
| 216 |
|
| 217 |
enabled: !parent.isRunning |
| 218 |
|
| 219 |
onTapped: { |
| 220 |
if (rightButton) |
| 221 |
alternativePressed(x, y); |
| 222 |
else |
| 223 |
pressed(x, y); |
| 224 |
} |
| 225 |
onLongPressed: alternativePressed(x, y) |
| 226 |
|
| 227 |
onStartSwipe: { |
| 228 |
var direction = (leftToRight ? -1 : 1); |
| 229 |
var destination = albumSpreadViewer.viewingPage + |
| 230 |
direction * albumSpreadViewer.pagesPerSpread; |
| 231 |
|
| 232 |
if ((destination > album.firstValidCurrentPage || |
| 233 |
pagesPerSpread == 2 && destination == album.firstValidCurrentPage) && |
| 234 |
destination <= album.lastPopulatedContentPage) { |
| 235 |
albumSpreadViewer.destinationPage = destination; |
| 236 |
prevSwipingX = mouseX; |
| 237 |
} else { |
| 238 |
canceled = true; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
onSwiping: { |
| 243 |
if (canceled) return; |
| 244 |
lastSwipeLeftToRight = (mouseX > prevSwipingX); |
| 245 |
|
| 246 |
var availableDistance = (leftToRight) ? (width - start) : start; |
| 247 |
// TODO: the 0.999 here is kind of a hack. The AlbumPageFlipper |
| 248 |
// can't tell the difference between its flipFraction being set to 1 |
| 249 |
// from the drag vs. its own animation. So I don't let the drag set it |
| 250 |
// quite all the way to 1. I should somehow fix this shortcoming in |
| 251 |
// the AlbumPageFlipper, but this is fine for now. |
| 252 |
var flipFraction = |
| 253 |
Math.max(0, Math.min(0.999, distance / availableDistance)); |
| 254 |
if (!albumSpreadViewer.isPopulatedContentPage(albumSpreadViewer.destinationPage)) { |
| 255 |
var maxFraction = (isPortrait) ? 0.75 : 0.425 |
| 256 |
flipFraction = Math.min(maxFraction, flipFraction) |
| 257 |
} |
| 258 |
albumSpreadViewer.flipFraction = flipFraction; |
| 259 |
prevSwipingX = mouseX; |
| 260 |
} |
| 261 |
|
| 262 |
onSwiped: { |
| 263 |
if (canceled) { |
| 264 |
canceled = false; |
| 265 |
return; |
| 266 |
} |
| 267 |
var minValidPage = album.firstValidCurrentPage |
| 268 |
var maxValidPage = album.lastValidCurrentPage |
| 269 |
if (isPortrait) { |
| 270 |
minValidPage += 1 |
| 271 |
maxValidPage -= 1 |
| 272 |
} |
| 273 |
if (albumSpreadViewer.flipFraction >= commitTurnFraction && |
| 274 |
leftToRight === lastSwipeLeftToRight) { |
| 275 |
if (albumSpreadViewer.destinationPage >= minValidPage && |
| 276 |
albumSpreadViewer.destinationPage <= maxValidPage) { |
| 277 |
albumSpreadViewer.flip(); |
| 278 |
} else albumSpreadViewer.release(); |
| 279 |
} else albumSpreadViewer.release(); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
Loader { |
| 285 |
id: photoViewerLoader |
| 286 |
anchors.fill: parent |
| 287 |
|
| 288 |
/// Is true while the view is popping up or closing |
| 289 |
property bool animationRunning: item ? item.animationRunning : false |
| 290 |
|
| 291 |
|
| 292 |
/// Opens the view |
| 293 |
function fadeOpen(media) { |
| 294 |
if (status === Loader.Null) |
| 295 |
setSource(Qt.resolvedUrl("../MediaViewer/PopupPhotoViewer.qml"), |
| 296 |
{model: albumCollection, album: albumViewer.album}) |
| 297 |
photoViewerLoader.item.fadeOpen(media) |
| 298 |
} |
| 299 |
|
| 300 |
MediaCollectionModel { |
| 301 |
id: albumCollection |
| 302 |
forCollection: albumViewer.album |
| 303 |
} |
| 304 |
|
| 305 |
Connections{ |
| 306 |
target: photoViewerLoader.item |
| 307 |
onOpening: { |
| 308 |
photoViewerLoader.visible = true; |
| 309 |
} |
| 310 |
|
| 311 |
onOpened: { |
| 312 |
photoViewerLoader.item.title = albumViewer.title; |
| 313 |
overview.pushPage(target); |
| 314 |
} |
| 315 |
onCloseRequested: { |
| 316 |
albumViewer.mediaCurrentlyInView = ""; |
| 317 |
var page = albumViewer.album.getPageForMediaSource(photoViewerLoader.item.photo); |
| 318 |
if (page >= 0) { |
| 319 |
albumViewer.album.currentPage = albumSpreadViewer.getLeftHandPageNumber(page); |
| 320 |
albumSpreadViewer.viewingPage = isPortrait? page : albumViewer.album.currentPage; |
| 321 |
} |
| 322 |
|
| 323 |
photoViewerLoader.item.fadeClosed(); |
| 324 |
albumViewer.header.visible = true; |
| 325 |
} |
| 326 |
onClosed: { |
| 327 |
overview.popPage(); |
| 328 |
} |
| 329 |
|
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
Component { |
| 334 |
id: component_mediaSelector |
| 335 |
MediaSelector { |
| 336 |
id: mediaSelector |
| 337 |
|
| 338 |
onAddClicked: { |
| 339 |
var album = albumViewer.album; |
| 340 |
var firstPhoto = album.addSelectedMediaSources(selection.model.selectedMedias); |
| 341 |
if (firstPhoto && albumViewer.state == "pageView") { |
| 342 |
var firstChangedPage = album.getPageForMediaSource(firstPhoto); |
| 343 |
var firstChangedSpread = albumSpreadViewer.getLeftHandPageNumber(firstChangedPage); |
| 344 |
albumSpreadViewer.flipTo(firstChangedSpread); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
onHidden: { |
| 349 |
overview.popPage(); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
DeleteOrDeleteWithContentsDialog { |
| 355 |
id: albumTrashDialog |
| 356 |
visible: false |
| 357 |
onDeleteClicked: { |
| 358 |
__quickClose(); |
| 359 |
} |
| 360 |
onDeleteWithContentsClicked: { |
| 361 |
__quickClose(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
header: PageHeader { |
| 366 |
objectName: "albumViewerHeader" |
| 367 |
title: albumViewer.title |
| 368 |
/// Contains the actions for the toolbar in the album view |
| 369 |
trailingActionBar.actions: [ |
| 370 |
Action { |
| 371 |
objectName: "addButton" |
| 372 |
text: i18n.tr("Add to album") // text in HUD |
| 373 |
iconName: "add" |
| 374 |
onTriggered: showMediaSelector(); |
| 375 |
}, |
| 376 |
Action { |
| 377 |
objectName: "deleteButton" |
| 378 |
text: i18n.tr("Delete") |
| 379 |
iconName: "delete" |
| 380 |
onTriggered: { |
| 381 |
albumTrashDialog.album = album; |
| 382 |
albumTrashDialog.show(); |
| 383 |
} |
| 384 |
} |
| 385 |
] |
| 386 |
|
| 387 |
leadingActionBar.actions: Action { |
| 388 |
objectName: "backButton" |
| 389 |
iconName: "back" |
| 390 |
onTriggered: __close(); |
| 391 |
} |
| 392 |
} |
| 393 |
} |