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