WIP better handling for slider creation
[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 // BUG: ValueChanged event is raised by sliders when you are moving the caret, should be raised only when you release it.
18 // TODO: Call mixer.setVolume(sliderName, Value) on value change
19 // TODO: Call mixer.getVolume(sliderName) on load
20
21 import QtQuick 2.6
22 import QtQuick.Layouts 1.1
23 import QtQuick.Controls 2.0
24 import AGL.Demo.Controls 1.0
25 import Mixer 1.0
26
27 ApplicationWindow {
28     property Component volumeSlider
29
30     id: root
31
32     Mixer {
33         id: mixer
34         Component.onCompleted: {
35             root.volumeSlider = Qt.createComponent("VolumeSlider.qml");
36             if (root.VolumeSlider.status !== Component.Ready) {
37                 console.log("Failed to load the VolumeSlider.qml component: " + root.volumeSlider.errorString());
38             }
39             mixer.open(bindingAddress);
40         }
41         onRolesChanged: {
42             // Remove existing sliders
43             for(var i = sliders.children.length; i > 0 ; --i) {
44                 console.log("destroying: " + i);
45                 sliders.children[i-1].destroy();
46             }
47
48             // Add slider for each role
49             for(var j = 0; j < mixer.roles.length; ++j) {
50                 addSlider(mixer.roles[j]);
51             }
52         }
53
54         onVolumeChanged: {
55             for(var i = 0; i < sliders.children.length ; i++) {
56                 console.log("Slider found: " + i);
57                 //if (sliders[i].role === name) {
58                 //    sliders[i].value = value;
59                 //}
60             }
61         }
62
63         function addSlider(name) {
64             var sld = root.volumeSlider.createObject(sliders);
65             sld.role = name;
66             mixer.getVolume(name); // Update volume
67         }
68
69         function deleteChilds(item) {
70             for(var i = item.children.length; i > 0 ; i--) {
71                 deleteChilds(item.children[i-1]);
72             }
73             item.destroy();
74         }
75     }
76
77     Label {
78         id: title
79         font.pixelSize: 48
80         text: "Mixer"
81         anchors.horizontalCenter: parent.horizontalCenter
82     }
83
84     ColumnLayout {
85         id: sliders
86         anchors.margins: 80
87         anchors.top: title.bottom
88         anchors.left: parent.left
89         anchors.right: parent.right
90     }
91 }
92