85328a916800d0a115a6033a8c672c590920d508
[apps/mediaplayer.git] / app / MediaPlayer.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 import QtQuick 2.6
18 import QtQuick.Layouts 1.1
19 import QtQuick.Controls 2.0
20 import AGL.Demo.Controls 1.0
21
22 ApplicationWindow {
23     id: root
24
25     width: container.width * container.scale
26     height: container.height * container.scale
27
28     Item {
29         id: player
30
31         property string title: ""
32         property string album: ""
33         property string artist: ""
34
35         property bool av_connected: false
36
37         property int duration: 0
38         property int position: 0
39
40         property string status: "stopped"
41
42         function time2str(value) {
43             return Qt.formatTime(new Date(value), (value > 3600000) ? 'hh:mm:ss' : 'mm:ss')
44         }
45     }
46
47     Connections {
48         target: mediaplayer
49
50         onMetadataChanged: {
51             var track = metadata.track
52
53             if ('status' in metadata) {
54                 player.status = metadata.status
55             }
56
57             if ('connected' in metadata) {
58                 player.av_connected = metadata.connected
59             }
60
61             if (track) {
62                 player.title = track.title
63                 player.album = track.album
64                 player.artist = track.artist
65                 player.duration = track.duration
66
67                 if ('index' in track) {
68                      playlistview.currentIndex = track.index
69                 }
70             }
71
72             if ('position' in metadata) {
73                 player.position = metadata.position
74             }
75         }
76     }
77
78     Timer {
79         id: timer
80         interval: 250
81         running: player.av_connected && player.status == "playing"
82         repeat: true
83         onTriggered: {
84             player.position = player.position + 250
85         }
86     }
87
88     Item {
89         id: container
90         anchors.centerIn: parent
91         width: 1080
92         height: 1487
93         scale: screenInfo.scale_factor()
94
95     ColumnLayout {
96         anchors.fill: parent
97         Item {
98             Layout.fillWidth: true
99             Layout.fillHeight: true
100             Layout.preferredHeight: 1080
101             clip: true
102             Image {
103                 anchors.left: parent.left
104                 anchors.right: parent.right
105                 anchors.bottom: parent.bottom
106                 height: sourceSize.height * width / sourceSize.width
107                 fillMode: Image.PreserveAspectCrop
108                 source: AlbumArt
109                 visible: player.av_connected === false
110             }
111
112             Item {
113                 anchors.left: parent.left
114                 anchors.right: parent.right
115                 anchors.bottom: parent.bottom
116                 height :307
117                 Rectangle {
118                     anchors.fill: parent
119                     color: 'black'
120                     opacity: 0.75
121                 }
122
123                 ColumnLayout {
124                     anchors.fill: parent
125                     anchors.margins: root.width * 0.02
126                     Item {
127                         Layout.fillWidth: true
128                         Layout.fillHeight: true
129                         Row {
130                             spacing: 20
131                             //ToggleButton {
132                             //    id: random
133                             //    visible: bluetooth.connected == false
134                             //    offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
135                             //    onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
136                             //}
137                             ToggleButton {
138                                 id: loop
139                                 visible: player.av_connected === false
140                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
141                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
142                                 onClicked: { mediaplayer.loop(checked ? "playlist" : "off") }
143                             }
144                         }
145                         ColumnLayout {
146                             anchors.fill: parent
147                             Label {
148                                 id: title
149                                 Layout.alignment: Layout.Center
150                                 text: player.title
151                                 horizontalAlignment: Label.AlignHCenter
152                                 verticalAlignment: Label.AlignVCenter
153                             }
154                             Label {
155                                 Layout.alignment: Layout.Center
156                                 text: player.artist
157                                 horizontalAlignment: Label.AlignHCenter
158                                 verticalAlignment: Label.AlignVCenter
159                                 font.pixelSize: title.font.pixelSize * 0.6
160                             }
161                         }
162                     }
163                     Slider {
164                         id: slider
165                         Layout.fillWidth: true
166                         to: player.duration
167                         enabled: player.av_connected === false
168                         value: player.position
169                         Label {
170                             id: position
171                             anchors.left: parent.left
172                             anchors.bottom: parent.top
173                             font.pixelSize: 32
174                             text: player.time2str(player.position)
175                         }
176                         Label {
177                             id: duration
178                             anchors.right: parent.right
179                             anchors.bottom: parent.top
180                             font.pixelSize: 32
181                             text: player.time2str(player.duration)
182                         }
183                         onPressedChanged: mediaplayer.seek(value)
184                     }
185                     RowLayout {
186                         Layout.fillHeight: true
187 //                        Image {
188 //                            source: './images/AGL_MediaPlayer_Playlist_Inactive.svg'
189 //                        }
190 //                        Image {
191 //                            source: './images/AGL_MediaPlayer_CD_Inactive.svg'
192 //                        }
193                         Item { Layout.fillWidth: true }
194                         ImageButton {
195                             id: previous
196                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
197                             onClicked: {
198                                 mediaplayer.previous()
199                             }
200                         }
201                         ImageButton {
202                             id: play
203                             states: [
204                                 State {
205                                     when: player.status == "playing"
206                                     PropertyChanges {
207                                         target: play
208                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
209                                         onClicked: {
210                                             mediaplayer.pause()
211                                         }
212                                     }
213                                 },
214                                 State {
215                                     when: player.status != "playing"
216                                     PropertyChanges {
217                                         target: play
218                                         offImage: './images/AGL_MediaPlayer_Player_Play.svg'
219                                         onClicked: mediaplayer.play()
220                                     }
221                                 }
222                             ]
223                         }
224                         ImageButton {
225                             id: forward
226                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
227                             onClicked: {
228                                 mediaplayer.next()
229                             }
230                         }
231
232                         Item { Layout.fillWidth: true }
233  
234                         ToggleButton {
235                               visible: true
236                               checked: player.av_connected
237                               onClicked: {
238                                 if (checked)
239                                         mediaplayer.connect()
240                                 else
241                                         mediaplayer.disconnect()
242                               }
243                               contentItem: Image {
244                                 source: player.av_connected ? './images/AGL_MediaPlayer_Bluetooth_Active.svg' : './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
245                               }
246                         }
247                     }
248                 }
249             }
250         }
251         Item {
252             Layout.fillWidth: true
253             Layout.fillHeight: true
254             Layout.preferredHeight: 407
255
256             ListView {
257                 anchors.fill: parent
258                 id: playlistview
259                 visible: player.av_connected === false
260                 clip: true
261                 header: Label {
262                     x: 50
263                     text: 'PLAYLIST'
264                     opacity: 0.5
265                 }
266                 model: MediaplayerModel
267                 currentIndex: -1
268
269                 delegate: MouseArea {
270                     id: delegate
271                     width: ListView.view.width
272                     height: ListView.view.height / 4
273                     RowLayout {
274                         anchors.fill: parent
275                         anchors.leftMargin: 50
276                         anchors.rightMargin: 50
277                         ColumnLayout {
278                             Layout.fillWidth: true
279                             Label {
280                                 Layout.fillWidth: true
281                                 text: model.title
282                             }
283                             Label {
284                                 Layout.fillWidth: true
285                                 text: model.artist
286                                 color: '#00ADDC'
287                                 font.pixelSize: 32
288                             }
289                         }
290                         //Label {
291                         //    text: player.time2str(model.duration)
292                         //    color: '#00ADDC'
293                         //    font.pixelSize: 32
294                         //}
295                     }
296                     onClicked: {
297                         mediaplayer.picktrack(playlistview.model[index].index)
298                     }
299                 }
300
301                 highlight: Rectangle {
302                     color: 'white'
303                     opacity: 0.25
304                 }
305             }
306         }
307     }
308 }
309 }