d74d790c640aa8e993bccc5a8749510d10a4b7d5
[profile/ivi/qtwayland.git] / examples / qml-compositor / qml / QmlCompositor / compositor.js
1 /****************************************************************************
2 **
3 ** This file is part of QtCompositor**
4 **
5 ** Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies).
6 ** All rights reserved.
7 **
8 ** Contact:  Nokia Corporation qt-info@nokia.com
9 **
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 **
16 ** Redistributions of source code must retain the above copyright
17 ** notice, this list of conditions and the following disclaimer.
18 **
19 ** Redistributions in binary form must reproduce the above copyright
20 ** notice, this list of conditions and the following disclaimer in the
21 ** documentation and/or other materials provided with the distribution.
22 **
23 ** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
24 ** names of its contributors may be used to endorse or promote products
25 ** derived from this software without specific prior written permission.
26 **
27 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 **
39 ****************************************************************************/
40
41 var windowList = null;
42
43 function relayout() {
44     if (windowList.length == 0)
45         return;
46
47     var dim = Math.ceil(Math.sqrt(windowList.length));
48     var w = root.width / dim;
49     var h = root.height / dim;
50
51     var cols = dim;
52     var rows = Math.floor(windowList.length / cols);
53     var i;
54     var ix = 0;
55     var iy = 0;
56     var lastDim = 1;
57
58     for (i = 0; i < windowList.length; ++i) {
59         if (i > 0) {
60             var currentDim = Math.ceil(Math.sqrt(i + 1));
61             if (currentDim == lastDim) {
62                 if (iy < currentDim - 1) {
63                     ++iy;
64                     if (iy == currentDim - 1)
65                         ix = 0;
66                 } else {
67                     ++ix;
68                 }
69             } else {
70                 iy = 0;
71                 ix = currentDim - 1;
72             }
73             lastDim = currentDim;
74         }
75
76         var cx = (ix + 0.5) * w;
77         var cy = (iy + 0.5) * h;
78
79         windowList[i].scale = 0.98 * Math.min(w / windowList[i].width, h / windowList[i].height);
80
81         windowList[i].x = (cx - windowList[i].width / 2);
82         windowList[i].y = (cy - windowList[i].height / 2);
83     }
84 }
85