1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Components project.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
41 // This file contains the implementation of CheckableGroup.qml functionality,
42 // and CheckableGroup root element is identified by 'root'.
45 var selectedItem = null;
46 var completed = false;
50 // Add the item, and if it's the case, check it as well.
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.
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
65 item.exclusiveGroupChanged.connect(item, onItemExclusiveGroupChange);
66 item.checkedChanged.connect(item, onItemCheckedChange);
69 function onItemExclusiveGroupChange() {
71 if (item.checkGroup !== root) {
76 function onItemCheckedChange() {
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)) {
86 // A non-selected item was checked, so we do the real check.
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.
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.
98 // The selected item was unchecked, choose another one to check.
99 var idx = (items.indexOf(item) + 1) % items.length
104 function remove(item) {
105 // Remove the item from the list
106 var idx = items.indexOf(item);
108 console.log("Error: removing an invalid item.");
111 items.splice(idx, 1);
113 item.exclusiveGroupChanged.disconnect(item, onItemExclusiveGroupChange);
114 item.checkedChanged.disconnect(item, onItemCheckedChange);
116 // If we are removing the selected item, we need to find a
117 // replacement to be selected...
118 if (selectedItem === item) {
120 if (items.length > 0) {
121 var otherIdx = idx % items.length;
122 check(items[otherIdx]);
124 // ...or simply having no selectedValue.
125 root.selectedValue = null;
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
135 var oldSelectedItem = selectedItem
138 oldSelectedItem.checked = false;
140 root.selectedValue = item.value;
143 function onCompleted() {
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.
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
154 if (!selectedItem && items.length > 0) {
160 var itemsLength = items.length;
161 for (var i = 0; i < itemsLength; i++) {
163 item.exclusiveGroupChanged.disconnect(item, onItemExclusiveGroupChange);
164 item.checkedChanged.disconnect(item, onItemCheckedChange);
165 item.exclusiveGroup = null;
170 root.selectedValue = null;