on xenial help sign in
Add a PreviewSingleton module to store some data for previews (LP: #1595235)

Approved by: Albert Astals Cid

commit385fda60c9dea4a3c291a98656bfdfd8e4223f05 authorAndrea Cimitan <andrea.cimitan@gmail.com> dateThu 22 Sep 2016, 07:32 (10 years ago) parent60bee7ce4621ef
7 files changed+119
qml/Dash/GenericScopeView.qml+4 qml/Dash/Previews/PreviewInlineVideo.qml+47 qml/Dash/Previews/PreviewSingleton/PreviewSingleton.qml+22 qml/Dash/Previews/PreviewSingleton/qmldir+1 tests/mocks/QtMultimedia/mediaplayer.cpp+1 tests/mocks/QtMultimedia/mediaplayer.h+1 tests/qmltests/Dash/Previews/tst_PreviewInlineVideo.qml+43
qml/Dash/GenericScopeView.qml+4
@@ -23,6 +23,7 @@ import Unity 0.2
2323import Dash 0.1
2424import "../Components"
2525import "../Components/ListItems" as ListItems
26+import "Previews/PreviewSingleton"
2627
2728FocusScope {
2829 id: scopeView
@@ -824,6 +825,9 @@ FocusScope {
824825 }
825826
826827 function closeSubPage() {
828+ if (subPage == "preview") {
829+ PreviewSingleton.widgetExtraData = new Object();
830+ }
827831 open = false;
828832 }
829833
qml/Dash/Previews/PreviewInlineVideo.qml+47
@@ -18,6 +18,7 @@ import QtQuick 2.4
1818import QtMultimedia 5.0
1919import Ubuntu.Components 1.3
2020import Ubuntu.Thumbnailer 0.1
21+import "PreviewSingleton"
2122import "../../Components"
2223import "../../Components/MediaServices"
2324
@@ -36,9 +37,35 @@ PreviewWidget {
3637 orientationLock: services.fullscreen
3738
3839 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+ }
3965
4066 MediaServices {
4167 id: services
68+ objectName: "services"
4269 width: parent.width
4370
4471 actions: sharingAction
@@ -47,8 +74,28 @@ PreviewWidget {
4774 fullscreen: false
4875 maximumEmbeddedHeight: rootItem.height / 2
4976
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+
5083 onClose: fullscreen = false
5184
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+
5299 Action {
53100 id: sharingAction
54101 iconName: "share"
qml/Dash/Previews/PreviewSingleton/PreviewSingleton.qmlnew file+22
@@ -0,0 +1,22 @@
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+pragma Singleton
18+import QtQuick 2.4
19+
20+QtObject {
21+ property var widgetExtraData: new Object();
22+}
qml/Dash/Previews/PreviewSingleton/qmldirnew file+1
@@ -0,0 +1 @@
1+singleton PreviewSingleton 1.0 PreviewSingleton.qml
tests/mocks/QtMultimedia/mediaplayer.cpp+1
@@ -161,6 +161,7 @@ void MediaPlayer::play()
161161 if (m_playbackState != PlayingState) {
162162 m_playbackState = PlayingState;
163163 Q_EMIT playbackStateChanged(m_playbackState);
164+ Q_EMIT playing();
164165
165166 m_timer.start();
166167 }
tests/mocks/QtMultimedia/mediaplayer.h+1
@@ -131,6 +131,7 @@ public Q_SLOTS:
131131
132132Q_SIGNALS:
133133 void playlistChanged();
134+ void playing();
134135 void sourceChanged(const QUrl &source);
135136 void playbackStateChanged(PlaybackState playbackState);
136137 void positionChanged(int position);
tests/qmltests/Dash/Previews/tst_PreviewInlineVideo.qml+43
@@ -76,6 +76,23 @@ Rectangle {
7676
7777 rootItem: inner
7878 }
79+
80+ Loader {
81+ id: videoPlaybackLoader
82+ anchors {
83+ left: parent.left
84+ right: parent.right
85+ top: videoPlayback.bottom
86+ topMargin: units.gu(2)
87+ }
88+
89+ sourceComponent: PreviewInlineVideo {
90+ width: parent.width
91+ widgetData: widgetData0
92+
93+ rootItem: inner
94+ }
95+ }
7996 }
8097
8198 }
@@ -98,6 +115,11 @@ Rectangle {
98115 }
99116 }
100117
118+ SignalSpy {
119+ id: spy
120+ signalName: "playing"
121+ }
122+
101123 UT.UnityTestCase {
102124 name: "PreviewInlineVideoTest"
103125 when: windowShown
@@ -110,5 +132,26 @@ Rectangle {
110132 var screenshotSource = screenshot.source
111133 compare(screenshotSource.toString(), "file:///test-video2-screenshot");
112134 }
135+
136+ function test_singleton() {
137+ var services = findChild(videoPlaybackLoader, "services");
138+ spy.clear();
139+ spy.target = services.mediaPlayer;
140+ mouseClick(videoPlaybackLoader, videoPlaybackLoader.width / 2, videoPlaybackLoader.height / 2);
141+ tryCompare(spy, "count", 1);
142+ tryCompareFunction(function() { return services.position > 3000; }, true);
143+
144+ // Simulate widget being destroyed and recreated
145+ videoPlaybackLoader.active = false;
146+ videoPlaybackLoader.active = true;
147+
148+ services = findChild(videoPlaybackLoader, "services");
149+ spy.clear();
150+ spy.target = services.mediaPlayer;
151+ compare(services.position, 0);
152+ mouseClick(videoPlaybackLoader, videoPlaybackLoader.width / 2, videoPlaybackLoader.height / 2);
153+ tryCompare(spy, "count", 1);
154+ tryCompareFunction(function() { return services.position > 3000; }, true, 1000);
155+ }
113156 }
114157}