Fix issue with QML variable declaration
[apps/mixer.git] / app / Mixer.qml
1 /*
2  * Copyright 2016 Konsulko Group
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 import Mixer 1.0
22
23 ApplicationWindow {
24     // ----- Signals
25
26     // ----- Properties
27     property Component volumeSlider
28
29     // ----- Setup
30     id: root
31
32     // ----- Childs
33     Mixer {
34         // ----- Signals
35         signal sliderVolumeChanged(string role, int value)
36
37         // ----- Properties
38
39         // ----- Setup
40         id: mixer
41
42         onSliderVolumeChanged: {
43             console.log("======role: " + role + ", volume: " + value);
44             mixer.setVolume(role, value);
45         }
46
47         Component.onCompleted: {
48             var vs = Qt.createComponent("VolumeSlider.qml");
49             if (vs.status !== Component.Ready) {
50                 console.log("Failed to load the VolumeSlider.qml component: " + vs.errorString());
51             }
52             root.volumeSlider = vs
53             mixer.open(bindingAddress);
54         }
55
56         onRolesChanged: {
57             // Remove existing sliders
58             for(var i = sliders.children.length; i > 0 ; --i) {
59                 console.log("destroying: " + i);
60                 sliders.children[i-1].destroy();
61             }
62
63             // Add slider for each role
64             for(var j = 0; j < mixer.roles.length; ++j) {
65                 addSlider(mixer.roles[j]);
66             }
67         }
68
69         onVolumeChanged: {
70             console.log("onVolumeChanged(\"" + name + "\", " + value + ")");
71             for(var i = 0; i < sliders.children.length ; i++) {
72                 var sld = sliders.children[i];
73                 console.log(i + " - Slider found:" + sld + "[\"" + sld.role + "\"] = " + sld.value);
74                 if (sld.role === name) {
75                     sld.value = value;
76                 }
77             }
78         }
79
80         // ----- Functions
81         function addSlider(name) {
82             var sld = root.volumeSlider.createObject(sliders)
83             sld.role = name
84             sld.onSliderValueChanged.connect(mixer.sliderVolumeChanged)
85             mixer.getVolume(name); // Update volume
86         }
87
88         function deleteChilds(item) {
89             for(var i = item.children.length; i > 0 ; i--) {
90                 deleteChilds(item.children[i-1]);
91             }
92             item.destroy();
93         }
94     }
95
96     Label {
97         id: title
98         font.pixelSize: 48
99         text: "Mixer"
100         anchors.horizontalCenter: parent.horizontalCenter
101     }
102
103     ColumnLayout {
104         id: sliders
105         anchors.margins: 80
106         anchors.top: title.bottom
107         anchors.left: parent.left
108         anchors.right: parent.right
109     }
110 }
111