Add sound manager initial source code
[staging/soundmanager.git] / sample / radio / app / Radio.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017 Konsulko Group
4  * Copyright (C) 2017 Toyota Motor Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import QtQuick 2.6
20 import QtQuick.Layouts 1.1
21 import QtQuick.Controls 2.0
22 import AGL.Demo.Controls 1.0
23 import 'api' as API
24
25 ApplicationWindow {
26     id: root
27
28     API.Binding {
29         id: radio
30
31         property string title
32
33         onBandChanged: frequency = minimumFrequency
34         onStationFound: title = stationId
35         onFrequencyChanged: {
36             title = ''
37             slider.value = frequency
38         }
39     }
40     property int  sourceID
41     property int  connectionID
42
43     function slotReply(msg){
44         var jstr = JSON.stringify(msg)
45         console.log("radio: Json jstr:" + jstr)
46         var content = JSON.parse(jstr);
47         var verb = content.response.verb
48         var err = content.response.error
49         console.log("radio: verb: " + verb)
50         console.log("radio: content.response.error: " + err)
51         switch(verb)
52         {
53             case "connect":
54                 console.log("radio: replied by connect")
55                 if(err == 0){
56                     connectionID = content.response.mainConnectionID
57                     console.log("radio: mainConnectionID is " + connectionID)
58                 }
59                 break;
60             case "registerSource":
61                 console.log("radio: replied by registerSource")            
62                 if(err == 0){
63                     sourceID = content.response.sourceID
64                 }
65             default:
66                 break;
67         }
68     }
69
70     function slotEvent(event,msg){
71         var jstr = JSON.stringify(msg)
72         var content = JSON.parse(jstr);
73         var eventName = content.event
74         console.log("radio: event qml: event" + eventName + " msg: " + jstr)
75         switch(eventName)
76         {
77             case "soundmanager\/asyncSetSourceState":
78                 console.log("radio: soundmanager\/asyncSetSourceState")
79                 console.log("radio: my soundID:" + sourceID + "handle:" + content.data.handle + ",sourceID:" + content.data.sourceID + ",sourceState:" + content.data.sourceState)
80                 if(sourceID == content.data.sourceID){
81                     console.log("radio: call ackSetSourceState")
82                     var arg = JSON.stringify({handle:content.data.handle, error:0})
83                     smw.call("ackSetSourceState", arg)
84                     switch(content.data.sourceState){
85                         case "on":
86                             radio.start() 
87                             break;
88                         case "off":
89                             radio.stop()
90                             break;
91                         case "paused":
92
93                             radio.stop()
94                             break;
95                     }
96                 }
97                 break;
98             case "soundmanager\/asyncConnect":
99                 // In reality, device shall be opened in this timing
100                 if(connectionID == content.data.connectionID){
101                     //radio.open_device()
102                 }
103                 break;
104             case "soundmanager\/asyncDisconnect":
105                 // In reality, device shall be closed in this timing
106                 if(connectionID == content.data.connectionID){
107                     // radio.close_device()
108                 }
109                 break;
110             default:
111                 break;
112         }     
113     }
114
115     ColumnLayout {
116         anchors.fill: parent
117         Item {
118             Layout.fillWidth: true
119             Layout.fillHeight: true
120             Layout.preferredHeight: 3
121             clip: true
122             Image {
123                 anchors.left: parent.left
124                 anchors.right: parent.right
125                 anchors.bottom: parent.bottom
126                 fillMode: Image.PreserveAspectFit
127                 source: './images/HMI_Radio_Equalizer.svg'
128             }
129             Item {
130                 anchors.left: parent.left
131                 anchors.right: parent.right
132                 anchors.bottom: parent.bottom
133                 height :307
134                 Rectangle {
135                     anchors.fill: parent
136                     color: 'black'
137                     opacity: 0.75
138                 }
139
140                 ColumnLayout {
141                     anchors.fill: parent
142                     anchors.margins: root.width * 0.02
143                     Item {
144                         Layout.fillWidth: true
145                         Layout.fillHeight: true
146                         Row {
147                             spacing: 20
148                             Image {
149                                 source: './images/FM_Icons_FM.svg'
150                             }
151 //                            ToggleButton {
152 //                                offImage: './images/FM_Icons_FM.svg'
153 //                                onImage: './images/FM_Icons_AM.svg'
154 //                                onCheckedChanged: {
155 //                                    radio.band = checked ? radio.amBand : radio.fmBand
156 //                                    radio.frequency = radio.minimumFrequency
157 //                                }
158 //                            }
159                         }
160                         ColumnLayout {
161                             anchors.fill: parent
162                             Label {
163                                 id: label
164                                 Layout.alignment: Layout.Center
165                                 text: radio.freq2str(radio.frequency)
166                                 horizontalAlignment: Label.AlignHCenter
167                                 verticalAlignment: Label.AlignVCenter
168                             }
169                             Label {
170                                 id: artist
171                                 Layout.alignment: Layout.Center
172                                 text: radio.title
173                                 horizontalAlignment: Label.AlignHCenter
174                                 verticalAlignment: Label.AlignVCenter
175                                 font.pixelSize: label.font.pixelSize * 0.6
176                             }
177                         }
178                     }
179                     Slider {
180                         id: slider
181                         Layout.fillWidth: true
182                         from: radio.minimumFrequency
183                         to: radio.maximumFrequency
184                         stepSize: radio.frequencyStep
185                         snapMode: Slider.SnapOnRelease
186                         onValueChanged: radio.frequency = value
187                         Label {
188                             anchors.left: parent.left
189                             anchors.bottom: parent.top
190                             font.pixelSize: 32
191                             text: radio.freq2str(radio.minimumFrequency)
192                         }
193                         Label {
194                             anchors.right: parent.right
195                             anchors.bottom: parent.top
196                             font.pixelSize: 32
197                             text: radio.freq2str(radio.maximumFrequency)
198                         }
199                     }
200                     RowLayout {
201                         Layout.fillHeight: true
202
203                         Label {
204                             text: 'TUNE'
205                         }
206
207                         ImageButton {
208                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
209                             Timer {
210                                 running: parent.pressed
211                                 triggeredOnStart: true
212                                 interval: 100
213                                 repeat: true
214                                 onTriggered: radio.tuneDown()
215                             }
216                         }
217
218                         ImageButton {
219                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
220                             Timer {
221                                 running: parent.pressed
222                                 triggeredOnStart: true
223                                 interval: 100
224                                 repeat: true
225                                 onTriggered: radio.tuneUp()
226                             }
227                         }
228
229                         Item { Layout.fillWidth: true }
230
231                         ImageButton {
232                             id: play
233                             offImage: './images/AGL_MediaPlayer_Player_Play.svg'
234                             onClicked: {
235                                 var JsonArg = JSON.stringify({sourceID:sourceID, sinkID:1})
236                                 console.log(JsonArg)
237                                 smw.call("connect",JsonArg)
238                             }
239                             states: [
240                                 State {
241                                     when: radio.state === radio.activeState
242                                     PropertyChanges {
243                                         target: play
244                                         offImage: './images/AGL_MediaPlayer_Player_Pause.svg'
245                                         onClicked: {
246                                             var JsonDisConArg = JSON.stringify({mainConnectionID:connectionID});
247                                             console.log(JsonDisConArg)
248                                             smw.call("disconnect",JsonDisConArg)
249                                             //radio.stop()
250                                         }
251                                     }
252                                 }
253                             ]
254                         }
255
256                         Item { Layout.fillWidth: true }
257
258                         Label {
259                             //Layout.fillWidth: true
260                             text: 'SCAN'
261                         }
262
263                         ImageButton {
264                             offImage: './images/AGL_MediaPlayer_BackArrow.svg'
265                             Timer {
266                                 running: parent.pressed
267                                 triggeredOnStart: true
268                                 interval: 100
269                                 repeat: true
270                                 onTriggered: radio.scanDown()
271                             }
272                         }
273
274                         ImageButton {
275                             offImage: './images/AGL_MediaPlayer_ForwardArrow.svg'
276                             Timer {
277                                 running: parent.pressed
278                                 triggeredOnStart: true
279                                 interval: 100
280                                 repeat: true
281                                 onTriggered: radio.scanUp()
282                             }
283                         }
284
285                     }
286                 }
287             }
288         }
289         Item {
290             Layout.fillWidth: true
291             Layout.fillHeight: true
292             Layout.preferredHeight: 2
293             ListView {
294                 anchors.fill: parent
295                 anchors.leftMargin: 50
296                 anchors.rightMargin: 50
297                 clip: true
298                 header: Label { text: 'PRESETS'; opacity: 0.5 }
299                 model: presetModel
300
301                 delegate: MouseArea {
302                     width: ListView.view.width
303                     height: ListView.view.height / 4
304
305                     onClicked: {
306                         radio.band = model.modelData.band
307                         radio.frequency = model.modelData.frequency
308                         radio.title = model.modelData.title
309                     }
310
311                     RowLayout {
312                         anchors.fill: parent
313                         Image {
314                             source: './images/Radio_Active_Icon.svg'
315                         }
316                         ColumnLayout {
317                             Layout.fillWidth: true
318                             Label {
319                                 Layout.fillWidth: true
320                                 text: model.title
321                             }
322                             Label {
323                                 Layout.fillWidth: true
324                                 text: radio.freq2str(model.frequency)
325                                 color: '#59FF7F'
326                                 font.pixelSize: 32
327                             }
328                         }
329                         Image {
330                             source: {
331                                 switch (model.modelData.band) {
332                                 case radio.fmBand:
333                                     return './images/FM_Icons_FM.svg'
334                                 case radio.amBand:
335                                     return './images/FM_Icons_AM.svg'
336                                 }
337                                 return null
338                             }
339                         }
340                     }
341                 }
342             }
343         }
344         Component.onCompleted: {
345             var JsonRegArg = JSON.stringify({appname:"radio"})
346             console.log(JsonRegArg)
347             smw.call("registerSource", JsonRegArg)
348         }
349     }
350 }