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