Unity 8
PreviewInlineVideo.qml
1 /*
2  * Copyright (C) 2016 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 as published by
6  * the Free Software Foundation; version 3.
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 
17 import QtQuick 2.4
18 import QtMultimedia 5.0
19 import Ubuntu.Components 1.3
20 import Ubuntu.Thumbnailer 0.1
21 import "PreviewSingleton"
22 import "../../Components"
23 import "../../Components/MediaServices"
24 
25 /*! \brief Preview widget for video.
26 
27  This widget shows video contained in widgetData["source"],
28  with a placeholder screenshot specified by widgetData["screenshot"].
29  */
30 
31 PreviewWidget {
32  id: root
33  implicitWidth: units.gu(35)
34  implicitHeight: services.height
35 
36  widgetMargins: -units.gu(1)
37  orientationLock: services.fullscreen
38 
39  property alias rootItem: services.rootItem
40  readonly property string widgetExtraDataKey: services.source.toString()
41 
42  onWidgetExtraDataKeyChanged: {
43  root.restorePlaybackState();
44  }
45 
46  function seek() {
47  services.mediaPlayer.seek(services.initialPosition);
48  services.initialPosition = -1;
49  }
50 
51  function storePlaybackState() {
52  if ((services.mediaPlayer.duration - services.mediaPlayer.position) < 1000) {
53  // we're at the end of the video
54  PreviewSingleton.widgetExtraData[widgetExtraDataKey] = 0;
55  } else {
56  PreviewSingleton.widgetExtraData[widgetExtraDataKey] = services.mediaPlayer.position;
57  }
58  }
59 
60  function restorePlaybackState() {
61  if (PreviewSingleton.widgetExtraData[widgetExtraDataKey] > 0) {
62  services.initialPosition = PreviewSingleton.widgetExtraData[widgetExtraDataKey];
63  }
64  }
65 
66  MediaServices {
67  id: services
68  objectName: "services"
69  width: parent.width
70 
71  actions: sharingAction
72  context: "video"
73  sourceData: widgetData
74  fullscreen: false
75  maximumEmbeddedHeight: rootItem.height / 2
76 
77  property int initialPosition: -1
78 
79  readonly property var mediaPlayer: footer.mediaPlayer
80  readonly property url source: mediaPlayer.source
81  readonly property int position: mediaPlayer.position
82 
83  onClose: fullscreen = false
84 
85  onPositionChanged: {
86  if (mediaPlayer.playbackState === MediaPlayer.StoppedState) return;
87  root.storePlaybackState();
88  }
89 
90  Connections {
91  target: services.mediaPlayer
92  ignoreUnknownSignals: true
93  onPlaying: {
94  // at the first playback of the file, do a seek()
95  if (services.initialPosition > 0) root.seek();
96  }
97  }
98 
99  Action {
100  id: sharingAction
101  iconName: "share"
102  visible: sharingPicker.active
103  onTriggered: sharingPicker.showPeerPicker()
104  }
105  }
106 
107  SharingPicker {
108  id: sharingPicker
109  objectName: "sharingPicker"
110  shareData: widgetData["share-data"]
111  }
112 }