improved desktop UI
[mardrone] / mardrone / imports / com / nokia / meego / ToolBarLayout.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 /// Helper code that is needed by ToolBarLayout.
42
43 var connectedItems = [];
44
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)
49         return true;
50   }
51   return false
52 }
53
54 // Remove item from an array
55 function remove(container, obj)
56 {
57     for (var i = 0 ; i < container.length ; i++ )
58         if (container[i] == obj)
59             container.splice(i,1);
60 }
61
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() {
65     var func = this;
66     var thisObject = arguments[0];
67     var args = Array.prototype.slice.call(arguments, 1);
68     return function() {
69         return func.apply(thisObject, args);
70     }
71 }
72
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]));
80         }
81     }
82 }
83
84 // Disconnects signals connected by this layout
85 function cleanup() {
86     remove(connectedItems, this);
87     this.visibleChanged.disconnect(layout);
88     this.parentChanged.disconnect(arguments.callee);
89 }
90
91 // Main layout function
92 function layout() {
93
94     if (parent === null || width === 0)
95         return;
96
97     var i;
98     var items = new Array();          // Keep track of visible items
99     var expandingItems = new Array(); // Keep track of expandingItems for tabs
100     var widthOthers = 0;
101
102     for (i = 0; i < children.length; i++) {
103         if (children[i].visible) {
104             items.push(children[i])
105
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])
111             } else {
112                 // Calculate the space that fixed size items take
113                 widthOthers += children[i].width;
114             }
115         }
116     }
117
118     if (items.length === 0)
119         return;
120
121     // Extra padding is applied if the leftMost or rightmost widget is expanding (note** removed on new design)
122     var leftPadding = 0
123     var rightPadding = 0 
124
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
131
132         // expanding item is on right
133         if (expandingItems.length > 0 && items[items.length-1].__expanding && !items[0].__expanding)
134             rightPadding += items[0].width
135     }
136
137     var width = toolbarLayout.width - leftPadding - rightPadding
138
139     // Calc expandingItems and tabrows
140     for (i = 0; i < expandingItems.length; i++)
141         expandingItems[i].width = (width - widthOthers) / expandingItems.length
142
143     var lastItem = items[items.length-1] ? items[items.length-1] : undefined;
144
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);
148
149     // |X  X  X| etc.
150     var spacingBetween = toolBox;
151     for (i = 1; i < items.length - 1; i++)
152         spacingBetween -= items[i].width;
153     items[0].x = leftPadding
154
155     // Calculate spacing between items
156     spacingBetween /= items.length - 1;
157
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;
163     }
164 }