on xenial help sign in
Merged preview widget branch
commit61fb69691771f87acc2224aa4e2cad92c7abbe2d authorAndrea Cimitan <andrea.cimitan@gmail.com> dateFri 31 Jan 2014, 14:12 (13 years ago) parent8beb2491c1a071
3 files changed+82−43
qml/Dash/Previews/AudioPlayer.qml+19−23 qml/Dash/Previews/PreviewWidget.qml+27 tests/qmltests/Dash/Previews/tst_AudioPlayer.qml+36−20
qml/Dash/Previews/AudioPlayer.qml+19−23
@@ -19,16 +19,11 @@ import QtMultimedia 5.0
1919import Ubuntu.Components 0.1
2020import Ubuntu.Components.ListItems 0.1
2121
22−Item {
22+PreviewWidget {
2323 id: root
24−
25− property alias model: trackRepeater.model
26−
2724 implicitHeight: childrenRect.height
2825
29− function stop() {
30− audio.stop()
31− }
26+ onFocusedChanged: if (!focused) audio.stop()
3227
3328 Audio {
3429 id: audio
@@ -42,6 +37,21 @@ Item {
4237 }
4338
4439 onErrorStringChanged: console.warn("Audio player error:", errorString)
40+
41+ function lengthToString(s) {
42+ if (s <= 0 || s === undefined) return ""
43+
44+ var sec = "" + s % 60
45+ if (sec.length == 1) sec = "0" + sec
46+ var hour = Math.floor(s / 3600)
47+ if (hour < 1) {
48+ return Math.floor(s / 60) + ":" + sec
49+ } else {
50+ var min = "" + Math.floor(s / 60) % 60
51+ if (min.length == 1) min = "0" + min
52+ return hour + ":" + min + ":" + sec
53+ }
54+ }
4555 }
4656
4757 Column {
@@ -51,6 +61,7 @@ Item {
5161 Repeater {
5262 id: trackRepeater
5363 objectName: "trackRepeater"
64+ model: root.widgetData["tracks"]
5465
5566 delegate: Item {
5667 id: trackItem
@@ -174,24 +185,9 @@ Item {
174185 color: "white"
175186 fontSize: "small"
176187 horizontalAlignment: Text.AlignRight
177− text: lengthToString(modelData["length"])
188+ text: audio.lengthToString(modelData["length"])
178189 style: Text.Raised
179190 styleColor: "black"
180−
181− function lengthToString(s) {
182− if (s <= 0 || s === undefined) return ""
183−
184− var sec = "" + s % 60
185− if (sec.length == 1) sec = "0" + sec
186− var hour = Math.floor(s / 3600)
187− if (hour < 1) {
188− return Math.floor(s / 60) + ":" + sec
189− } else {
190− var min = "" + Math.floor(s / 60) % 60
191− if (min.length == 1) min = "0" + min
192− return hour + ":" + min + ":" + sec
193− }
194− }
195191 }
196192 }
197193 }
qml/Dash/Previews/PreviewWidget.qmlnew file+27
@@ -0,0 +1,27 @@
1+/*
2+ * Copyright (C) 2014 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.0
18+
19+/*! Interface for preview widgets. */
20+
21+Item {
22+ /// type:bool Specifies the preview widget being focused or not
23+ property bool focused: true
24+
25+ /// Variable used to contain widget's data
26+ property var widgetData
27+}
tests/qmltests/Dash/Previews/tst_AudioPlayer.qml+36−20
@@ -27,23 +27,24 @@ Rectangle {
2727 height: units.gu(80)
2828 color: "grey"
2929
30− property var tracksModel0: [
31− ]
30+ property var tracksModel0: {
31+ "tracks": [ ]
32+ }
3233
33− property var tracksModel1: [
34− { title: "Some track name", length: "30", source: "../../tests/qmltests/Dash/Music/data/testsound1.ogg" }
35− ]
34+ property var tracksModel1: {
35+ "tracks": [ { title: "Some track name", length: "30", source: "../../tests/qmltests/Dash/Music/data/testsound1.ogg" } ]
36+ }
3637
37− property var tracksModel2: [
38− { title: "Some track name", length: "30", source: "../../tests/qmltests/Dash/Music/data/testsound1.ogg" },
39− { title: "Some other track name", subtitle: "Subtitle", length: "83", source: "../../tests/qmltests/Dash/Music/data/testsound2.ogg" },
40− { title: "And another one", length: "7425", source: "../../tests/qmltests/Dash/Music/data/testsound3.ogg" }
41− ]
38+ property var tracksModel2: {
39+ "tracks": [ { title: "Some track name", length: "30", source: "../../tests/qmltests/Dash/Music/data/testsound1.ogg" },
40+ { title: "Some other track name", subtitle: "Subtitle", length: "83", source: "../../tests/qmltests/Dash/Music/data/testsound2.ogg" },
41+ { title: "And another one", length: "7425", source: "../../tests/qmltests/Dash/Music/data/testsound3.ogg" } ]
42+ }
4243
4344 AudioPlayer {
4445 id: audioPlayer
4546 anchors.fill: parent
46− model: tracksModel2
47+ widgetData: tracksModel2
4748 }
4849
4950 UT.UnityTestCase {
@@ -54,6 +55,21 @@ Rectangle {
5455 waitForRendering(audioPlayer);
5556 }
5657
58+ function test_time_formatter_data() {
59+ return [
60+ { tag: "0", value: 0, result: "" },
61+ { tag: "-1", value: -1, result: "" },
62+ { tag: "30", value: 30, result: "0:30" },
63+ { tag: "60", value: 60, result: "1:00" },
64+ { tag: "3600", value: 3600, result: "1:00:00" }
65+ ];
66+ }
67+
68+ function test_time_formatter(data) {
69+ var audio = findInvisibleChild(audioPlayer, "audio");
70+ compare(audio.lengthToString(data.value), data.result)
71+ }
72+
5773 function test_tracks_data() {
5874 return [
5975 {tag: "0 tracks", tracksModel: tracksModel0},
@@ -63,19 +79,19 @@ Rectangle {
6379 }
6480
6581 function test_tracks(data) {
66− audioPlayer.model = data.tracksModel;
82+ audioPlayer.widgetData = data.tracksModel;
6783 waitForRendering(audioPlayer);
6884
6985 var trackRepeater = findChild(audioPlayer, "trackRepeater");
70− compare(trackRepeater.count, data.tracksModel.length)
86+ compare(trackRepeater.count, data.tracksModel["tracks"].length)
7187
72− for (var i = 0; i < data.tracksModel.length; ++i) {
88+ for (var i = 0; i < data.tracksModel["tracks"].length; ++i) {
7389 var trackItem = findChild(audioPlayer, "trackItem" + i);
7490 var titleLabel = findChild(trackItem, "trackTitleLabel");
75− compare(titleLabel.text, data.tracksModel[i]["title"])
91+ compare(titleLabel.text, data.tracksModel["tracks"][i]["title"])
7692 var subtitleLabel = findChild(trackItem, "trackSubtitleLabel");
77− if (data.tracksModel[i]["subtitle"] !== undefined) {
78− compare(subtitleLabel.text, data.tracksModel[i]["subtitle"])
93+ if (data.tracksModel["tracks"][i]["subtitle"] !== undefined) {
94+ compare(subtitleLabel.text, data.tracksModel["tracks"][i]["subtitle"])
7995 } else {
8096 compare(subtitleLabel.visible, false)
8197 }
@@ -84,14 +100,14 @@ Rectangle {
84100 }
85101
86102 function checkPlayerSource(index) {
87− var modelFilename = audioPlayer.model[index]["source"].replace(/^.*[\\\/]/, '');
103+ var modelFilename = audioPlayer.widgetData["tracks"][index]["source"].replace(/^.*[\\\/]/, '');
88104 var playerFilename = findInvisibleChild(audioPlayer, "audio").source.toString().replace(/^.*[\\\/]/, '');
89105
90106 compare(modelFilename, playerFilename, "Player source is not set correctly.");
91107 }
92108
93109 function test_playback() {
94− audioPlayer.model = tracksModel2;
110+ audioPlayer.widgetData = tracksModel2;
95111 waitForRendering(audioPlayer);
96112
97113 var track0Item = findChild(audioPlayer, "trackItem0");
@@ -155,7 +171,7 @@ Rectangle {
155171 tryCompare(track2ProgressBar, "visible", true);
156172
157173 // Calling stop() should make all players shut up!
158− audioPlayer.stop()
174+ audioPlayer.focused = false
159175 tryCompare(audio, "playbackState", Audio.StoppedState);
160176 }
161177 }