Debian Codegraph @

rc/qml/MediaViewer/MediaViewer.qml

raw
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 * Emanuele Sorce <[email protected]>
21 */
22
23 import QtQuick 2.9
24 import QtQuick.Layouts 1.1
25 import Gallery 1.0
26 import Lomiri.Components 1.3
27 import Lomiri.Components.Popups 1.3
28 import Lomiri.Components.ListItems 1.3 as ListItem
29 import Lomiri.Content 1.3
30 import "../Components"
31 import "../Utility"
32 import "../../js/Gallery.js" as Gallery
33
34 /*!
35 */
36 Item {
37 id: viewerWrapper
38
39 /*!
40 */
41 property alias media: galleryPhotoViewer.media
42 /*!
43 */
44 property alias model: galleryPhotoViewer.model
45 /*!
46 */
47 property alias index: galleryPhotoViewer.index
48 /*!
49 */
50 property alias currentIndexForHighlight:
51 galleryPhotoViewer.currentIndexForHighlight
52
53 // Set this when entering from an album.
54 property variant album
55
56 // Read-only
57 // Set to true when an image is loaded and displayed.
58 //
59 // NOTE: The empty-model check does perform a useful function here and should NOT be
60 // removed; for whatever reason, it's possible to get here and have what would have
61 // been the current item be deleted, but not be null, and what it actually points to
62 // is no longer valid and will result in an immediate segfault if dereferenced.
63 //
64 // Since there is no current item if there are no more photo objects left in the model,
65 // the check catches this before we can inadvertently follow a stale pointer.
66 property bool isReady: model != null && model.count > 0 && galleryPhotoViewer.currentItem
67
68 // tooolbar actions for the full view
69 property variant actions: (media && !sharePicker.visible) ? d.mediaActions : []
70
71 property variant backAction: d.backAction
72
73 /*!
74 */
75 signal closeRequested()
76 /*!
77 */
78 signal editRequested(variant photo)
79
80 signal setHeaderVisibilityRequested(bool visibility)
81 signal toggleHeaderVisibilityRequested()
82
83 /*!
84 */
85 function setCurrentIndex(index) {
86 galleryPhotoViewer.setCurrentIndex(index);
87 }
88
89 /*!
90 */
91 function setCurrentPhoto(photo) {
92 galleryPhotoViewer.setCurrentPhoto(photo);
93 }
94
95 /*!
96 */
97 function goBack() {
98 galleryPhotoViewer.goBack();
99 }
100
101 /*!
102 */
103 function goForward() {
104 galleryPhotoViewer.goForward();
105 }
106
107 function closeMediaViewer()
108 {
109 galleryPhotoViewer.currentItem.reset();
110 closeRequested();
111 }
112
113 Rectangle{
114 color: "black"
115 anchors.fill: parent
116 }
117
118 MediaListView {
119 id: galleryPhotoViewer
120 objectName: "mediaListView"
121
122 // When the user clicks the back button.
123 signal closeRequested()
124 signal editRequested(variant photo) // The user wants to edit this photo.
125
126 // NOTE: These properties should be treated as read-only, as setting them
127 // individually can lead to bogus results. Use setCurrentPhoto() or
128 // setCurrentIndex() to initialize the view.
129 property variant media: null
130
131 function setCurrentPhoto(photo) {
132 setCurrentIndex(model.indexOf(photo));
133 }
134
135 function goBack() {
136 galleryPhotoViewer.currentItem.reset();
137 pageBack();
138 }
139
140 function goForward() {
141 galleryPhotoViewer.currentItem.reset();
142 pageForward();
143 }
144
145 anchors.fill: parent
146
147 onCurrentIndexChanged: {
148 if (model)
149 media = model.getAt(currentIndex);
150 }
151
152 delegate: SingleMediaViewer {
153 id: media
154 objectName: "openedMedia" + index
155 mediaSource: model.mediaSource
156
157 width: galleryPhotoViewer.width
158 height: galleryPhotoViewer.height
159
160 // Needed as ListView.isCurrentItem can't be used directly in a change handler
161 property bool isActive: ListView.isCurrentItem
162 onIsActiveChanged: if (!isActive) reset();
163
164 onClicked: viewerWrapper.toggleHeaderVisibilityRequested()
165 }
166
167 // Don't allow flicking while the chrome is actively displaying a popup
168 // menu, or the image is zoomed. When images are zoomed,
169 // mouse drags should pan, not flick.
170 interactive: currentItem != null &&
171 !currentItem.userInteracting // FIXME: disable when editing ??
172
173 Component {
174 id: contentItemComp
175 ContentItem {}
176 }
177
178 Page {
179 id: sharePicker
180 visible: false
181
182 title: i18n.tr("Share to")
183
184 onVisibleChanged: viewerWrapper.setHeaderVisibilityRequested(!visible)
185
186 ContentPeerPicker {
187 objectName: "sharePicker"
188 showTitle: false
189 anchors.fill: parent
190 contentType: galleryPhotoViewer.media.type === MediaSource.Video ? ContentType.Videos : ContentType.Pictures
191 handler: ContentHandler.Share
192
193 onPeerSelected: {
194 overview.popPage();
195 sharePicker.visible = false;
196 var curTransfer = peer.request();
197 if (curTransfer.state === ContentTransfer.InProgress)
198 {
199 curTransfer.items = [ contentItemComp.createObject(parent, {"url": viewerWrapper.media.path}) ];
200 curTransfer.state = ContentTransfer.Charged;
201 }
202 }
203 onCancelPressed: {
204 overview.popPage();
205 sharePicker.visible = false;
206 }
207 }
208 }
209
210 Component {
211 id: deleteDialog
212 Dialog {
213 id: dialogue
214 objectName: "deletePhotoDialog"
215 title: (galleryPhotoViewer.media.type === MediaSource.Photo) ? i18n.tr("Delete a photo") : i18n.tr("Delete a video")
216
217 function finishRemove() {
218 if (!album === undefined)
219 return;
220 if (model.count === 0)
221 galleryPhotoViewer.closeRequested();
222 }
223
224 Button {
225 objectName: "deletePhotoDialogYes"
226 text: i18n.tr("Delete")
227 color: theme.palette.normal.negative
228 onClicked: {
229 PopupUtils.close(dialogue)
230 viewerWrapper.model.destroyMedia(galleryPhotoViewer.media, true);
231 galleryPhotoViewer.currentIndexChanged();
232 dialogue.finishRemove();
233 }
234 }
235
236 Button {
237 objectName: "deletePhotoDialogNo"
238 text: i18n.tr("Cancel")
239 onClicked: PopupUtils.close(dialogue)
240 }
241 }
242 }
243
244 Component {
245 id: removeFromAlbumDialog
246 Dialog {
247 id: dialogue
248 objectName: "removePhotoFromAlbumDialog"
249 title: (galleryPhotoViewer.media.type === MediaSource.Photo) ? i18n.tr("Remove a photo from album") : i18n.tr("Remove a video from album")
250
251 function finishRemove() {
252 if (model.count === 0)
253 galleryPhotoViewer.closeRequested();
254 }
255
256 Button {
257 objectName: "removeFromAlbumButton"
258 text: i18n.tr("Remove from Album")
259 color: theme.palette.normal.negative
260 onClicked: {
261 PopupUtils.close(dialogue)
262 viewerWrapper.model.removeMediaFromAlbum(album, galleryPhotoViewer.media);
263 dialogue.finishRemove();
264 }
265 }
266
267 Button {
268 objectName: "removeFromAlbumAndDeleteButton"
269 text: i18n.tr("Remove from Album and Delete")
270 onClicked: {
271 PopupUtils.close(dialogue)
272 viewerWrapper.model.destroyMedia(galleryPhotoViewer.media, true);
273 dialogue.finishRemove();
274 }
275 }
276
277 Button {
278 objectName: "removeFromAlbumCancelButton"
279 text: i18n.tr("Cancel")
280 onClicked: PopupUtils.close(dialogue)
281 }
282 }
283
284 }
285
286 onCloseRequested: viewerWrapper.closeRequested()
287 onEditRequested: viewerWrapper.editRequested(media)
288 }
289
290 property int __pickerContentHeight: galleryPhotoViewer.height - units.gu(10)
291 property PopupAlbumPicker __albumPicker
292 Connections {
293 target: __albumPicker
294 onAlbumPicked: {
295 album.addMediaSource(galleryPhotoViewer.media);
296 }
297 onNewAlbumPicked: {
298 viewerWrapper.closeRequested();
299 }
300 }
301
302 ActivityIndicator {
303 id: busySpinner
304 objectName: "busySpinner"
305 anchors.centerIn: parent
306 visible: media ? media.busy : false
307 running: visible
308 }
309
310 Item {
311 id: d
312
313 property list<Action> mediaActions: [
314 Action {
315 objectName: "editButton"
316 text: i18n.tr("Edit")
317 iconName: "edit"
318 visible: galleryPhotoViewer.media.type === MediaSource.Photo && galleryPhotoViewer.media.canBeEdited
319 onTriggered: {
320 var path = galleryPhotoViewer.media.path.toString();
321 path = path.replace("file://", "")
322 var editor;
323 try {
324 Qt.createQmlObject('import QtQuick 2.9; import Lomiri.Components.Extras 0.2; Item {}', viewerWrapper);
325 editor = overview.pushPage(Qt.resolvedUrl("PhotoEditorPage.qml"), { photo: path });
326 } catch (e) {
327 console.log("WARNING: Unable to load PhotoEditor from Lomiri.Components.Extras");
328 return;
329 }
330 editor.done.connect(function(photoWasModified) {
331 if (photoWasModified) {
332 galleryPhotoViewer.media.refresh();
333 galleryPhotoViewer.media.dataChanged();
334 }
335 overview.popPage();
336 });
337 }
338 },
339 Action {
340 objectName: "addButton"
341 text: i18n.tr("Add to album")
342 iconName: "add"
343 onTriggered: {
344 __albumPicker = PopupUtils.open(Qt.resolvedUrl("../Components/PopupAlbumPicker.qml"),
345 null,
346 {contentHeight: viewerWrapper.__pickerContentHeight});
347 }
348 },
349 Action {
350 objectName: "deleteButton"
351 text: i18n.tr("Delete")
352 iconName: "delete"
353 onTriggered: {
354 if (album)
355 PopupUtils.open(removeFromAlbumDialog, null);
356 else
357 PopupUtils.open(deleteDialog, null);
358 }
359 },
360 Action {
361 objectName: "shareButton"
362 text: i18n.tr("Share")
363 iconName: "share"
364 visible: !APP.desktopMode
365 onTriggered: {
366 overview.pushPage(sharePicker)
367 sharePicker.visible = true;
368 }
369 },
370 Action {
371 objectName: "infoButton"
372 text: i18n.tr("Info")
373 iconName: "info"
374 onTriggered: {
375 PopupUtils.open(mediaInfo)
376 }
377 }
378 ]
379
380 property Action backAction: Action {
381 objectName: "backButton"
382 iconName: "back"
383 onTriggered: {
384 galleryPhotoViewer.currentItem.reset();
385 closeRequested();
386 }
387 }
388 }
389
390 Component {
391 id: mediaInfo
392
393 Dialog {
394 id: mediaInfoDialog
395 title: i18n.tr("Information")
396
397 Label {
398 text: i18n.tr("Media type: ") +
399 ((galleryPhotoViewer.media.type === MediaSource.Photo) ? i18n.tr("photo") : i18n.tr("video")) + "<br><br>" +
400 i18n.tr("Media name: ") + galleryPhotoViewer.media.path.toString() + "<br><br>" +
401 i18n.tr("Date: ") + galleryPhotoViewer.media.exposureDate.toLocaleString(Qt.locale(), "ddd MMM d yyyy") + "<br><br>" +
402 i18n.tr("Time: ") + galleryPhotoViewer.media.exposureTimeOfDay.toLocaleTimeString(Qt.locale(), "h:mm:ss a t")
403 horizontalAlignment: Text.AlignHCenter
404 wrapMode: Text.WordWrap
405 }
406
407 Button {
408 text: i18n.tr("ok")
409 color: theme.palette.normal.focus
410 onClicked: PopupUtils.close(mediaInfoDialog)
411 }
412 }
413 }
414 }