Better layouting behavior when window is removed.
[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 var indexes = null;
43
44 function relayout() {
45     if (windowList.length == 0)
46         return;
47
48     var dim = Math.ceil(Math.sqrt(windowList.length));
49     var w = root.width / dim;
50     var h = root.height / dim;
51
52     var cols = dim;
53     var rows = Math.floor(windowList.length / cols);
54     var i;
55     var ix = 0;
56     var iy = 0;
57     var lastDim = 1;
58
59     indexes = new Array(dim * dim);
60
61     for (i = 0; i < windowList.length; ++i) {
62         if (i > 0) {
63             var currentDim = Math.ceil(Math.sqrt(i + 1));
64             if (currentDim == lastDim) {
65                 if (iy < currentDim - 1) {
66                     ++iy;
67                     if (iy == currentDim - 1)
68                         ix = 0;
69                 } else {
70                     ++ix;
71                 }
72             } else {
73                 iy = 0;
74                 ix = currentDim - 1;
75             }
76             lastDim = currentDim;
77         }
78
79         indexes[iy * dim + ix] = i;
80         windowList[i].index = iy * dim + ix;
81
82         var cx = (ix + 0.5) * w;
83         var cy = (iy + 0.5) * h;
84
85         windowList[i].scale = 0.98 * Math.min(w / windowList[i].width, h / windowList[i].height);
86
87         windowList[i].x = (cx - windowList[i].width / 2);
88         windowList[i].y = (cy - windowList[i].height / 2);
89     }
90 }
91
92 function addWindow(window)
93 {
94     if (windowList == null)
95         windowList = new Array(0);
96
97     windowList.push(window);
98     relayout();
99 }
100
101 function removeWindow(window)
102 {
103     var i;
104     for (i = 0; i < windowList.length; ++i) {
105         if (windowList[i] == window)
106             break;
107     }
108
109     var index = windowList[i].index;
110     var dim = Math.ceil(Math.sqrt(windowList.length));
111     var maxY = Math.floor((windowList.length-1) / dim);
112
113     var shrinking = Math.ceil(Math.sqrt(windowList.length - 1)) != dim;
114
115     while (true) {
116         var ix = index % dim;
117         var iy = Math.floor(index / dim);
118
119         console.log("index: " + ix + " " + iy);
120
121         if (shrinking) {
122             if (iy > 0)
123                 --iy;
124             else if (++ix == dim)
125                 break;
126         } else {
127             if (iy < maxY) {
128                 if (ix > 0)
129                     --ix;
130                 else
131                     ++iy;
132             } else {
133                 ++ix;
134             }
135         }
136
137         var next = iy * dim + ix;
138
139         var currentIndex = indexes[index];
140         var nextIndex = indexes[next];
141
142         if (nextIndex == null)
143             break;
144
145         var temp = windowList[currentIndex];
146         windowList[currentIndex] = windowList[nextIndex];
147         windowList[currentIndex].index = currentIndex;
148         windowList[nextIndex] = temp;
149
150         index = next;
151     }
152
153     windowList.splice(indexes[index], 1);
154     relayout();
155 }