components for android added
[mardrone] / mardrone / imports / com / meego / extras / Tumbler.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 __columns = [];
42 var __autoColumnWidth = 0;
43 var __suppressLayoutUpdates = false;
44
45 function initialize() {
46     // check the width requested by fixed width columns
47     var requestedWidth = 0;
48     var requestedCount = 0;
49     var invisibleCount = 0;
50     for (var i = 0; i < columns.length; i++) {
51         if (columns[i].visible) {
52             if (columns[i].width > 0 && !columns[i].privateIsAutoWidth) {
53                 requestedWidth += columns[i].width;
54                 requestedCount++;
55             }
56         } else {
57             invisibleCount++;
58         }
59     }
60
61     // allocate the rest to auto width columns
62     if ((columns.length - requestedCount - invisibleCount) > 0) {
63         __autoColumnWidth = Math.floor((parent.width - requestedWidth) / (columns.length - requestedCount - invisibleCount));
64     }
65
66     for (var i = 0; i < columns.length; i++) {
67         var comp = Qt.createComponent("TumblerTemplate.qml");
68         var newObj = comp.createObject(tumblerRow);
69         if (!columns[i].width || columns[i].privateIsAutoWidth) {
70             columns[i].width = __autoColumnWidth;
71             columns[i].privateIsAutoWidth = true;
72         }
73         if (columns[i].label) {
74             // enable label for the tumbler
75             internal.hasLabel = true;
76         }
77         newObj.height = root.height;
78         newObj.index = i;
79         newObj.tumblerColumn = columns[i];
80         newObj.widthChanged.connect(layout);
81         newObj.visibleChanged.connect(layout);
82         __columns.push(newObj);
83     }
84     privateTemplates = __columns;
85 }
86
87 function clear() {
88     var count = __columns.length;
89     for (var i = 0; i < count; i++) {
90         var tmp = __columns.pop();
91         tmp.destroy();
92     }
93 }
94
95 function forceUpdate() {
96     for (var i = 0; i < columns.length; i++) {
97         columns[i].selectedIndex = __columns[i].view.currentIndex;
98     }
99 }
100
101 function layout() {
102     if (__suppressLayoutUpdates) {
103         // guard against onWidthChanged triggering again during this process
104         return;
105     }
106     var requestedWidth = 0;
107     var requestedCount = 0;
108     var invisibleCount = 0;
109     for (var i = 0; i < columns.length; i++) {
110         if (columns[i].visible) {
111             var w = columns[i].width;
112             var a = columns[i].privateIsAutoWidth;
113             if (!a || (a && w != __autoColumnWidth)) {
114                 requestedWidth += columns[i].width;
115                 requestedCount++;
116                 columns[i].privateIsAutoWidth = false;
117             } else {
118                 columns[i].privateIsAutoWidth = true;
119             }
120         } else {
121             invisibleCount++;
122         }
123     }
124
125     if ((columns.length - requestedCount - invisibleCount) > 0) {
126         __autoColumnWidth = Math.floor((parent.width - requestedWidth) / (columns.length - requestedCount - invisibleCount));
127     }
128
129     // guard against onWidthChanged triggering again during this process
130     __suppressLayoutUpdates = true;
131     for (var i = 0; i < columns.length; i++) {
132         if (columns[i].privateIsAutoWidth) {
133             columns[i].width = __autoColumnWidth;
134         }
135     }
136     __suppressLayoutUpdates = false;
137 }