app/MediaPlayer.qml: Use Window width and height
[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     Connections {
50         target: mediaplayer
51
52         onMetadataChanged: {
53             var track = metadata.track
54
55             if ('status' in metadata) {
56                 player.status = metadata.status
57             }
58
59             if ('connected' in metadata) {
60                 player.av_connected = metadata.connected
61             }
62
63             if (track) {
64                 if ('image' in track)
65                      return
66                 player.title = track.title
67                 player.album = track.album
68                 player.artist = track.artist
69
70                 if ('duration' in track)
71                      player.duration = track.duration
72
73                 if ('index' in track) {
74                      playlistview.currentIndex = track.index
75                 }
76             }
77
78             if ('position' in metadata) {
79                 player.position = metadata.position
80             }
81         }
82     }
83
84     Timer {
85         id: timer
86         interval: 250
87         running: player.av_connected && player.status == "playing"
88         repeat: true
89         onTriggered: {
90             player.position = player.position + 250
91         }
92     }
93
94     Item {
95         id: container
96         anchors.centerIn: parent
97         width: Window.width
98         height: Window.height
99         //scale: screenInfo.scale_factor()
100         scale: 1
101
102     ColumnLayout {
103         anchors.fill: parent
104         Item {
105             Layout.fillWidth: true
106             Layout.fillHeight: true
107             Layout.preferredHeight: 1080
108             clip: true
109             Image {
110                 anchors.left: parent.left
111                 anchors.right: parent.right
112                 anchors.bottom: parent.bottom
113                 height: sourceSize.height * width / sourceSize.width
114                 fillMode: Image.PreserveAspectCrop
115                 source: AlbumArt
116                 visible: player.av_connected === false
117             }
118
119             Item {
120                 anchors.left: parent.left
121                 anchors.right: parent.right
122                 anchors.bottom: parent.bottom
123                 height :307
124                 Rectangle {
125                     anchors.fill: parent
126                     color: 'black'
127                     opacity: 0.75
128                 }
129
130                 ColumnLayout {
131                     anchors.fill: parent
132                     anchors.margins: root.width * 0.02
133                     Item {
134                         Layout.fillWidth: true
135                         Layout.fillHeight: true
136                         Row {
137                             spacing: 20
138                             //ToggleButton {
139                             //    id: random
140                             //    visible: bluetooth.connected == false
141                             //    offImage: './images/AGL_MediaPlayer_Shuffle_Inactive.svg'
142                             //    onImage: './images/AGL_MediaPlayer_Shuffle_Active.svg'
143                             //}
144                             ToggleButton {
145                                 id: loop
146                                 visible: player.av_connected === false
147                                 offImage: './images/AGL_MediaPlayer_Loop_Inactive.svg'
148                                 onImage: './images/AGL_MediaPlayer_Loop_Active.svg'
149                                 onClicked: { mediaplayer.loop(checked ? "playlist" : "off") }
150                             }
151                         }
152                         ColumnLayout {
153                             anchors.fill: parent
154                             Label {
155                                 id: title
156                                 Layout.alignment: Layout.Center
157                                 text: player.title
158                                 horizontalAlignment: Label.AlignHCenter
159                                 verticalAlignment: Label.AlignVCenter
160                             }
161                             Label {
162                                 Layout.alignment: Layout.Center
163                                 text: player.artist
164                                 horizontalAlignment: Label.AlignHCenter
165                                 verticalAlignment: Label.AlignVCenter
166                                 font.pixelSize: title.font.pixelSize * 0.6
167                             }
168                         }
169                     }
170                     Slider {
171                         id: slider
172                         Layout.fillWidth: true
173                         to: player.duration
174                         enabled: player.av_connected === false
175                         value: player.position
176                         Label {
177                             id: position
178                             anchors.left: parent.left
179                             anchors.bottom: parent.top
180                             font.pixelSize: 32
181                             text: player.time2str(player.position)
182                         }
183                         Label {
184                             id: duration
185                             anchors.right: parent.right
186                             anchors.bottom: parent.top
187                             font.pixelSize: 32
188                             text: player.time2str(player.duration)
189                         }
190                         onPressedChanged: mediaplayer.seek(value)
191                     }
192                     RowLayout {
193                         Layout.fillHeight: true
194 //                        Image {
195 //                            source: './images/AGL_MediaPlayer_Playlist_Inactive.svg'
196 //                        }
197 //                        Image {
198 //                            source: './images/AGL_MediaPlayer_CD_Inactive.svg'
199 //                        }
200                         Item { Layout.fillWidth: true }
201                         ImageButton {
202                             id: previous
203                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
204                             onClicked: {
205                                 mediaplayer.previous()
206                             }
207                         }
208                         ImageButton {
209                             id: play
210                             states: [
211                                 State {
212                                     when: player.status == "playing"
213                                     PropertyChanges {
214                                         target: play
215                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
216                                         onClicked: {
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                               checked: player.av_connected
244                               onClicked: {
245                                 if (checked)
246                                         mediaplayer.connect()
247                                 else
248                                         mediaplayer.disconnect()
249                               }
250                               contentItem: Image {
251                                 source: player.av_connected ? './images/AGL_MediaPlayer_Bluetooth_Active.svg' : './images/AGL_MediaPlayer_Bluetooth_Inactive.svg'
252                               }
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 }