438ff20e8774e5e7a11d9371e2c2f3a3dcc2a639
[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 QtMultimedia 5.6
21 import AGL.Demo.Controls 1.0
22 import 'api' as API
23
24 ApplicationWindow {
25     id: root
26
27     Item {
28         id: player
29
30         property string title: ""
31         property string album: ""
32         property string artist: ""
33
34         property int duration: 0
35         property int position: 0
36
37         property string cover_art: ""
38         property string status: ""
39
40         function time2str(value) {
41             return Qt.formatTime(new Date(value), 'mm:ss')
42         }
43     }
44
45     Connections {
46         target: mediaplayer
47
48         onPlaylistChanged: {
49             playlist.clear()
50
51             for (var i = 0; i < playlist.list.length; i++) {
52                 var item = playlist.list[i]
53
54                 playlist_model.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
55
56                 if (item.selected) {
57                     playlistview.currentIndex = i
58                 }
59             }
60         }
61
62         onMetadataChanged: {
63             player.title = metadata.title
64             player.album = metadata.album
65             player.artist = metadata.artist
66
67             if (metadata.duration) {
68                 player.duration = metadata.duration
69             }
70
71             if (metadata.position) {
72                 player.position = metadata.position
73             }
74
75             if (metadata.status) {
76                 player.status = metadata.status
77             }
78
79             if (metadata.image) {
80                 player.cover_art = metadata.image
81             }
82
83             playlistview.currentIndex = metadata.index
84         }
85     }
86
87     API.BluetoothManager {
88         id: bluetooth
89         url: bindingAddress
90     }
91
92     Timer {
93         id: timer
94         interval: 250
95         running: (bluetooth.av_connected && bluetooth.state == "playing")
96         repeat: true
97         onTriggered: {
98             bluetooth.position = bluetooth.position + 250
99         }
100     }
101
102     ListModel {
103         id: playlist_model
104     }
105
106     ColumnLayout {
107         anchors.fill: parent
108         Item {
109             Layout.fillWidth: true
110             Layout.fillHeight: true
111             Layout.preferredHeight: 1080
112             clip: true
113             Image {
114                 id: albumart
115                 anchors.left: parent.left
116                 anchors.right: parent.right
117                 anchors.bottom: parent.bottom
118                 height: sourceSize.height * width / sourceSize.width
119                 fillMode: Image.PreserveAspectCrop
120                 source: player.cover_art ? player.cover_art : ''
121                 visible: bluetooth.av_connected == false
122             }
123
124             Item {
125                 anchors.left: parent.left
126                 anchors.right: parent.right
127                 anchors.bottom: parent.bottom
128                 height :307
129                 Rectangle {
130                     anchors.fill: parent
131                     color: 'black'
132                     opacity: 0.75
133                 }
134
135                 ColumnLayout {
136                     anchors.fill: parent
137                     anchors.margins: root.width * 0.02
138                     Item {
139                         Layout.fillWidth: true
140                         Layout.fillHeight: true
141                         Row {
142                             spacing: 20
143                             //ToggleButton {
144                             //    id: random
145                             //    visible: bluetooth.connected == false
146                             //    offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
147                             //    onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
148                             //}
149                             ToggleButton {
150                                 id: loop
151                                 visible: bluetooth.connected == false
152                                 //checked: player.loop_state
153                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
154                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
155                                 onClicked: { mediaplayer.loop(checked) }
156                             }
157                         }
158                         ColumnLayout {
159                             anchors.fill: parent
160                             Label {
161                                 id: title
162                                 Layout.alignment: Layout.Center
163                                 text: bluetooth.av_connected ? bluetooth.title : (player.title ? player.title : '')
164                                 horizontalAlignment: Label.AlignHCenter
165                                 verticalAlignment: Label.AlignVCenter
166                             }
167                             Label {
168                                 Layout.alignment: Layout.Center
169                                 text: bluetooth.av_connected ? bluetooth.artist : (player.artist ? player.artist : '')
170                                 horizontalAlignment: Label.AlignHCenter
171                                 verticalAlignment: Label.AlignVCenter
172                                 font.pixelSize: title.font.pixelSize * 0.6
173                             }
174                         }
175                     }
176                     Slider {
177                         id: slider
178                         Layout.fillWidth: true
179                         to: bluetooth.av_connected ? bluetooth.duration : player.duration
180                         enabled: bluetooth.av_connected == false
181                         value: bluetooth.av_connected ? bluetooth.position : player.position
182                         function getPosition() {
183                             if (bluetooth.av_connected) {
184                                 return player.time2str(bluetooth.position)
185                             }
186                             return player.time2str(player.position)
187                         }
188                         Label {
189                             id: position
190                             anchors.left: parent.left
191                             anchors.bottom: parent.top
192                             font.pixelSize: 32
193                             text: slider.getPosition()
194                         }
195                         Label {
196                             id: duration
197                             anchors.right: parent.right
198                             anchors.bottom: parent.top
199                             font.pixelSize: 32
200                             text: bluetooth.av_connected ? player.time2str(bluetooth.duration) : player.time2str(player.duration)
201                         }
202                         onPressedChanged: mediaplayer.seek(value)
203                     }
204                     RowLayout {
205                         Layout.fillHeight: true
206 //                        Image {
207 //                            source: './images/AGL_MediaPlayer_Playlist_Inactive.svg'
208 //                        }
209 //                        Image {
210 //                            source: './images/AGL_MediaPlayer_CD_Inactive.svg'
211 //                        }
212                         Item { Layout.fillWidth: true }
213                         ImageButton {
214                             id: previous
215                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
216                             onClicked: {
217                                 if (bluetooth.av_connected) {
218                                     bluetooth.sendMediaCommand("Previous")
219                                     bluetooth.position = 0
220                                 } else {
221                                     mediaplayer.previous()
222                                 }
223                             }
224                         }
225                         ImageButton {
226                             id: play
227                             offImage: './images/AGL_MediaPlayer_Player_Play.svg'
228                             onClicked: {
229                                 if (bluetooth.av_connected) {
230                                     bluetooth.sendMediaCommand("Play")
231                                 } else {
232                                     mediaplayer.play()
233                                 }
234                             }
235                             states: [
236                                 State {
237                                     when: player.status == "playing"
238                                     PropertyChanges {
239                                         target: play
240                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
241                                         onClicked: {
242                                             player.status = ""
243                                             mediaplayer.pause()
244                                         }
245                                     }
246                                 },
247                                 State {
248                                     when: bluetooth.av_connected && bluetooth.state == "playing"
249                                     PropertyChanges {
250                                         target: play
251                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
252                                         onClicked: bluetooth.sendMediaCommand("Pause")
253                                     }
254                                 }
255
256                             ]
257                         }
258                         ImageButton {
259                             id: forward
260                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
261                             onClicked: {
262                                 if (bluetooth.av_connected) {
263                                     bluetooth.sendMediaCommand("Next")
264                                 } else {
265                                     mediaplayer.next()
266                                 }
267                             }
268                         }
269
270                         Item { Layout.fillWidth: true }
271  
272                         ToggleButton {
273                               visible: bluetooth.connected
274                               checked: bluetooth.av_connected
275                               offImage: './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
276                               onImage: './images/AGL_MediaPlayer_Bluetooth_Active.svg'
277
278                               onClicked: {
279                                   if (bluetooth.av_connected) {
280                                       bluetooth.disconnect_profiles()
281                                   } else {
282                                       bluetooth.connect_profiles()
283                                   }
284                               }
285                         }
286                     }
287                 }
288             }
289         }
290         Item {
291             Layout.fillWidth: true
292             Layout.fillHeight: true
293             Layout.preferredHeight: 407
294
295             ListView {
296                 anchors.fill: parent
297                 id: playlistview
298                 visible: bluetooth.av_connected == false
299                 clip: true
300                 header: Label {
301                     x: 50
302                     text: 'PLAYLIST'
303                     opacity: 0.5
304                 }
305                 model: playlist_model
306                 currentIndex: -1
307
308                 delegate: MouseArea {
309                     id: delegate
310                     width: ListView.view.width
311                     height: ListView.view.height / 4
312                     RowLayout {
313                         anchors.fill: parent
314                         anchors.leftMargin: 50
315                         anchors.rightMargin: 50
316                         ColumnLayout {
317                             Layout.fillWidth: true
318                             Label {
319                                 Layout.fillWidth: true
320                                 text: model.title
321                             }
322                             Label {
323                                 Layout.fillWidth: true
324                                 text: model.artist
325                                 color: '#00ADDC'
326                                 font.pixelSize: 32
327                             }
328                         }
329                         //Label {
330                         //    text: player.time2str(model.duration)
331                         //    color: '#00ADDC'
332                         //    font.pixelSize: 32
333                         //}
334                     }
335                     onClicked: {
336                         mediaplayer.picktrack(playlistview.model.get(index).index)
337                     }
338                 }
339
340                 highlight: Rectangle {
341                     color: 'white'
342                     opacity: 0.25
343                 }
344             }
345         }
346     }
347 }