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