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