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 ****************************************************************************/
42 var clickHandlers = []
43 var visibleButtons = []
45 var checkingOverallExclusivity = false
48 function create(that) {
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().
56 checkedBtn = self.checkedButton
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)
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()
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
87 function buildItems() {
88 releaseItemConnections()
90 for (var i = 0; i < self.children.length; i++) {
91 var item = self.children[i]
93 // set up item connections
94 clickHandlers[i] = checkExclusive(item)
95 item.clicked.connect(clickHandlers[i])
96 item.visibleChanged.connect(buildItems)
98 // update visibleButtons array
100 visibleButtons.push(item)
102 checkOverallExclusivity()
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)
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
124 self.checkedButton = null
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)
135 checkingOverallExclusivity = false
139 function checkExclusive(item) {
142 if (self.exclusive) {
143 for (var i = 0, ref; (ref = visibleButtons[i]); i++)
144 ref.checked = button === ref
146 self.checkedButton = button
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
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
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
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])
173 return visibleChildren