added android components
[mardrone] / mardrone / imports / com / nokia / android.1.1 / ButtonGroup.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 var self
42 var clickHandlers = []
43 var visibleButtons = []
44 var resizing = false
45 var checkingOverallExclusivity = false
46 var checkedBtn
47
48 function create(that) {
49     destroy()
50     self = that
51
52     // If the item is not visible at this stage, we store the value of the property
53     // checkedButton to ensure a proper initialization. The value is restored in
54     // initCheckedButton().
55     if (!self.visible)
56         checkedBtn = self.checkedButton
57
58     buildItems()
59     self.childrenChanged.connect(buildItems)
60     self.widthChanged.connect(resizeButtons)
61     self.exclusiveChanged.connect(checkOverallExclusivity)
62     self.checkedButtonChanged.connect(checkOverallExclusivity)
63     self.visibleChanged.connect(initCheckedButton)
64 }
65
66 function destroy() {
67     if (self !== undefined) {
68         self.childrenChanged.disconnect(buildItems)
69         self.widthChanged.disconnect(resizeButtons)
70         self.exclusiveChanged.disconnect(checkOverallExclusivity)
71         self.checkedButtonChanged.disconnect(checkOverallExclusivity)
72         self.visibleChanged.disconnect(initCheckedButton)
73         releaseItemConnections()
74         self = undefined
75     }
76 }
77
78 function initCheckedButton() {
79     // When the item becomes visible, restore the value of checkedButton property
80     // that was stored in the create function.
81     if (self.visible && checkedBtn !== null) {
82         self.checkedButton = checkedBtn
83         checkedBtn = null
84     }
85 }
86
87 function buildItems() {
88     releaseItemConnections()
89     visibleButtons = []
90     for (var i = 0; i < self.children.length; i++) {
91         var item = self.children[i]
92
93         // set up item connections
94         clickHandlers[i] = checkExclusive(item)
95         item.clicked.connect(clickHandlers[i])
96         item.visibleChanged.connect(buildItems)
97
98         // update visibleButtons array
99         if (item.visible)
100             visibleButtons.push(item)
101     }
102     checkOverallExclusivity()
103     resizeButtons()
104 }
105
106 function releaseItemConnections() {
107     for (var i = 0; i < self.children.length; i++) {
108         self.children[i].clicked.disconnect(clickHandlers[i])
109         self.children[i].visibleChanged.disconnect(buildItems)
110     }
111     clickHandlers = []
112 }
113
114 function checkOverallExclusivity() {
115     if (!checkingOverallExclusivity && self.exclusive) {
116         // prevent re-entrant calls
117         checkingOverallExclusivity = true
118         if (visibleButtons.length > 0) {
119             if ((self.checkedButton === null || !self.checkedButton.visible))
120                 self.checkedButton = visibleButtons[0]
121             self.checkedButton.checked = true
122         }
123         else {
124             self.checkedButton = null
125         }
126
127         for (var i = 0; i < self.children.length; i++) {
128             var item = self.children[i]
129             // e.g CheckBox can be added to ButtonGroup but doesn't have "checkable" property
130             if (self.exclusive && item.hasOwnProperty("checkable"))
131                 item.checkable = true
132             if (item !== self.checkedButton)
133                 item.checked = false
134         }
135         checkingOverallExclusivity = false
136     }
137 }
138
139 function checkExclusive(item) {
140     var button = item
141     return function() {
142         if (self.exclusive) {
143             for (var i = 0, ref; (ref = visibleButtons[i]); i++)
144                 ref.checked = button === ref
145         }
146         self.checkedButton = button
147     }
148 }
149
150 function resizeButtons() {
151     if (!resizing && visibleButtons.length && self.privateDirection == Qt.Horizontal) {
152         // if ButtonRow has implicit size, a loop may occur where resizing individual
153         // Button affects ButtonRow size, which triggers again resizing...
154         // therefore we prevent re-entrant resizing attempts
155         resizing = true
156         var buttonWidth = self.width / visibleButtons.length
157         for (var i = 0; i < self.children.length; i++) {
158             self.children[i].width = self.children[i].visible ? buttonWidth : 0
159         }
160         resizing = false
161     }
162 }
163
164 // Binding would not work properly if visibleButtons would just be returned,
165 // it would miss visibility changes. In the long run ButtonGroup.js could be
166 // refactored.
167 function visibleItems(item) {
168     var visibleChildren = []
169     for (var i = 0; i < item.children.length; i++) {
170         if (item.children[i].visible)
171             visibleChildren.push(item.children[i])
172     }
173     return visibleChildren
174 }