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