components for android added
[mardrone] / mardrone / imports / Qt / labs / components.1.1 / CheckableGroup.js
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Components project.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 // This file contains the implementation of CheckableGroup.qml functionality,
42 // and CheckableGroup root element is identified by 'root'.
43
44 var items = [];
45 var selectedItem = null;
46 var completed = false;
47
48
49 function add(item) {
50     // Add the item, and if it's the case, check it as well.
51     items.push(item);
52     if (item.checked) {
53         check(item);
54     } else if (items.length == 1 && completed) {
55         // If we are already completed, this enforces the invariant of
56         // having always one item checked in the group. For exception
57         // case see onCompleted() function.
58         check(item);
59     }
60
61     // Connect so we know when items left the group or are requesting
62     // a different state (check or uncheck). Note that we register
63     // passing the item to be the 'this' value when the function
64     // is called.
65     item.exclusiveGroupChanged.connect(item, onItemExclusiveGroupChange);
66     item.checkedChanged.connect(item, onItemCheckedChange);
67 }
68
69 function onItemExclusiveGroupChange() {
70     var item = this;
71     if (item.checkGroup !== root) {
72         remove(item);
73     }
74 }
75
76 function onItemCheckedChange() {
77     var item = this;
78
79     // In those two cases we know the change was caused by the group, so we can ignore it.
80     if ((item === selectedItem && item.checked) ||
81         (item !== selectedItem && !item.checked)) {
82         return;
83     }
84
85     if (item.checked) {
86         // A non-selected item was checked, so we do the real check.
87         check(item);
88     } else if (items.length == 1) {
89         // Only one item, and it was unchecked. We have to rollback
90         // because it would break the group invariant.
91         //
92         // NOTE: This cause a glitch in the item.checked variable that
93         // goes false then true, however the CheckableGroup.selectedValue
94         // remains unchanged. We could avoid this with a custom setter
95         // in the 'checked' property of an item.
96         item.checked = true;
97     } else {
98         // The selected item was unchecked, choose another one to check.
99         var idx = (items.indexOf(item) + 1) % items.length
100         check(items[idx]);
101     }
102 }
103
104 function remove(item) {
105     // Remove the item from the list
106     var idx = items.indexOf(item);
107     if (idx == -1) {
108         console.log("Error: removing an invalid item.");
109         return;
110     }
111     items.splice(idx, 1);
112
113     item.exclusiveGroupChanged.disconnect(item, onItemExclusiveGroupChange);
114     item.checkedChanged.disconnect(item, onItemCheckedChange);
115
116     // If we are removing the selected item, we need to find a
117     // replacement to be selected...
118     if (selectedItem === item) {
119         selectedItem = null;
120         if (items.length > 0) {
121             var otherIdx = idx % items.length;
122             check(items[otherIdx]);
123         } else {
124             // ...or simply having no selectedValue.
125             root.selectedValue = null;
126         }
127     }
128 }
129
130 /// Select one item, setting the selectedValue in the group
131 function check(item) {
132     // It is important to have the right item in the 'selectedItem'
133     // before the changes since we use that to identify that the change
134     // was made by us.
135     var oldSelectedItem = selectedItem
136     selectedItem = item;
137     if (oldSelectedItem)
138         oldSelectedItem.checked = false;
139     item.checked = true;
140     root.selectedValue = item.value;
141 }
142
143 function onCompleted() {
144     completed = true;
145
146     // When the QML file is loaded, and no items on it had 'checkable: true' (which
147     // would be considered in the add() function), then we simply select one of them
148     // to enforce the rule of having one item selected.
149     //
150     // The delayed selection is useful because it allow to declare in the QML file
151     // one item as 'checked' and don't see a "glitch" in the selectedValue because
152     // another item was added before to the group (and could be selected if this was
153     // not delayed).
154     if (!selectedItem && items.length > 0) {
155         check(items[0]);
156     }
157 }
158
159 function clear() {
160     var itemsLength = items.length;
161     for (var i = 0; i < itemsLength; i++) {
162         var item = items[i];
163         item.exclusiveGroupChanged.disconnect(item, onItemExclusiveGroupChange);
164         item.checkedChanged.disconnect(item, onItemCheckedChange);
165         item.exclusiveGroup = null;
166     }
167
168     items = [];
169     selectedItem = null;
170     root.selectedValue = null;
171 }