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 /// Helper code that is needed by ToolBarLayout.
43 var connectedItems = [];
45 // Find item in an array
46 function contains(container, obj) {
47 for (var i = 0 ; i < container.length; i++) {
48 if (container[i] == obj)
54 // Remove item from an array
55 function remove(container, obj)
57 for (var i = 0 ; i < container.length ; i++ )
58 if (container[i] == obj)
59 container.splice(i,1);
62 // Helper function to give us the sender id on slots
63 // This is needed to remove connectens on a reparent
64 Function.prototype.bind = function() {
66 var thisObject = arguments[0];
67 var args = Array.prototype.slice.call(arguments, 1);
69 return func.apply(thisObject, args);
73 // Called whenever a child is added or removed in the toolbar
74 function childrenChanged() {
75 for (var i = 0; i < children.length; i++) {
76 if (!contains(connectedItems, children[i])) {
77 connectedItems.push(children[i]);
78 children[i].visibleChanged.connect(layout);
79 children[i].parentChanged.connect(cleanup.bind(children[i]));
84 // Disconnects signals connected by this layout
86 remove(connectedItems, this);
87 this.visibleChanged.disconnect(layout);
88 this.parentChanged.disconnect(arguments.callee);
91 // Main layout function
94 if (parent === null || width === 0)
98 var items = new Array(); // Keep track of visible items
99 var expandingItems = new Array(); // Keep track of expandingItems for tabs
102 for (i = 0; i < children.length; i++) {
103 if (children[i].visible) {
104 items.push(children[i])
106 // Center all items vertically
107 items[0].y = (function() {return height / 2 - items[0].height / 2})
108 // Find out which items are expanding
109 if (children[i].__expanding) {
110 expandingItems.push(children[i])
112 // Calculate the space that fixed size items take
113 widthOthers += children[i].width;
118 if (items.length === 0)
121 // Extra padding is applied if the leftMost or rightmost widget is expanding (note** removed on new design)
125 // In LandScape mode we add extra margin to keep contents centered
126 // for two basic cases
127 if (items.length == 2 && screen.currentOrientation == Screen.Landscape) {
128 // expanding item on left
129 if (expandingItems.length > 0 && items[0].__expanding && !items[items.length-1].__expanding)
130 leftPadding += items[items.length-1].width
132 // expanding item is on right
133 if (expandingItems.length > 0 && items[items.length-1].__expanding && !items[0].__expanding)
134 rightPadding += items[0].width
137 var width = toolbarLayout.width - leftPadding - rightPadding
139 // Calc expandingItems and tabrows
140 for (i = 0; i < expandingItems.length; i++)
141 expandingItems[i].width = (width - widthOthers) / expandingItems.length
143 var lastItem = items[items.length-1] ? items[items.length-1] : undefined;
145 // Space to be divided between first and last items
146 var toolBox = width - (items[0] ? items[0].width : 0) -
147 (lastItem ? lastItem.width : 0);
150 var spacingBetween = toolBox;
151 for (i = 1; i < items.length - 1; i++)
152 spacingBetween -= items[i].width;
153 items[0].x = leftPadding
155 // Calculate spacing between items
156 spacingBetween /= items.length - 1;
158 // Starting after first item
159 var dX = items[0].width + spacingBetween;
160 for (i = 1; i < items.length; i++) {
161 items[i].x = dX + leftPadding;
162 dX += spacingBetween + items[i].width;