-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutEngine.cc
More file actions
297 lines (271 loc) · 10.7 KB
/
Copy pathLayoutEngine.cc
File metadata and controls
297 lines (271 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
The MIT License (MIT)
Copyright (c) 2011 Gene Z. Ragan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Self
#include "LayoutEngine.h"
// Qt
#include <QList>
#include <QVector>
#include <QtDebug>
typedef qint64 Fixed64;
static inline Fixed64 toFixed(int i) { return (Fixed64)i * 256; }
static inline int fRound(Fixed64 i) {
return (i % 256 < 128) ? i / 256 : 1 + i / 256;
}
void qGeomCalc(QVector<LayoutStruct> &chain, int start, int count,
int pos, int space, int spacer)
{
int cHint = 0;
int cMin = 0;
int cMax = 0;
int sumStretch = 0;
int sumSpacing = 0;
bool wannaGrow = false; // anyone who really wants to grow?
// bool canShrink = false; // anyone who could be persuaded to shrink?
bool allEmptyNonstretch = true;
int pendingSpacing = -1;
int spacerCount = 0;
int i;
for (i = start; i < start + count; i++) {
LayoutStruct *data = &chain[i];
data->done = false;
cHint += data->smartSizeHint();
cMin += data->minimumSize;
cMax += data->maximumSize;
sumStretch += data->stretch;
if (!data->empty) {
/*
Using pendingSpacing, we ensure that the spacing for the last
(non-empty) item is ignored.
*/
if (pendingSpacing >= 0) {
sumSpacing += pendingSpacing;
++spacerCount;
}
pendingSpacing = data->effectiveSpacer(spacer);
}
wannaGrow = wannaGrow || data->expansive || data->stretch > 0;
allEmptyNonstretch = allEmptyNonstretch && !wannaGrow && data->empty;
}
int extraspace = 0;
if (space < cMin + sumSpacing) {
/*
Less space than minimumSize; take from the biggest first
*/
int minSize = cMin + sumSpacing;
// shrink the spacers proportionally
if (spacer >= 0) {
spacer = minSize > 0 ? spacer * space / minSize : 0;
sumSpacing = spacer * spacerCount;
}
QList<int> list;
for (i = start; i < start + count; i++)
list << chain.at(i).minimumSize;
qSort(list);
int space_left = space - sumSpacing;
int sum = 0;
int idx = 0;
int space_used=0;
int current = 0;
while (idx < count && space_used < space_left) {
current = list.at(idx);
space_used = sum + current * (count - idx);
sum += current;
++idx;
}
--idx;
int deficit = space_used - space_left;
int items = count - idx;
/*
* If we truncate all items to "current", we would get "deficit" too many pixels. Therefore, we have to remove
* deficit/items from each item bigger than maxval. The actual value to remove is deficitPerItem + remainder/items
* "rest" is the accumulated error from using integer arithmetic.
*/
int deficitPerItem = deficit/items;
int remainder = deficit % items;
int maxval = current - deficitPerItem;
int rest = 0;
for (i = start; i < start + count; i++) {
int maxv = maxval;
rest += remainder;
if (rest >= items) {
maxv--;
rest-=items;
}
LayoutStruct *data = &chain[i];
data->size = qMin(data->minimumSize, maxv);
data->done = true;
}
} else if (space < cHint + sumSpacing) {
/*
Less space than smartSizeHint(), but more than minimumSize.
Currently take space equally from each, as in Qt 2.x.
Commented-out lines will give more space to stretchier
items.
*/
int n = count;
int space_left = space - sumSpacing;
int overdraft = cHint - space_left;
// first give to the fixed ones:
for (i = start; i < start + count; i++) {
LayoutStruct *data = &chain[i];
if (!data->done
&& data->minimumSize >= data->smartSizeHint()) {
data->size = data->smartSizeHint();
data->done = true;
space_left -= data->smartSizeHint();
// sumStretch -= data->stretch;
n--;
}
}
bool finished = n == 0;
while (!finished) {
finished = true;
Fixed64 fp_over = toFixed(overdraft);
Fixed64 fp_w = 0;
for (i = start; i < start+count; i++) {
LayoutStruct *data = &chain[i];
if (data->done)
continue;
// if (sumStretch <= 0)
fp_w += fp_over / n;
// else
// fp_w += (fp_over * data->stretch) / sumStretch;
int w = fRound(fp_w);
data->size = data->smartSizeHint() - w;
fp_w -= toFixed(w); // give the difference to the next
if (data->size < data->minimumSize) {
data->done = true;
data->size = data->minimumSize;
finished = false;
overdraft -= data->smartSizeHint() - data->minimumSize;
// sumStretch -= data->stretch;
n--;
break;
}
}
}
} else { // extra space
int n = count;
int space_left = space - sumSpacing;
// first give to the fixed ones, and handle non-expansiveness
for (i = start; i < start + count; i++) {
LayoutStruct *data = &chain[i];
if (!data->done
&& (data->maximumSize <= data->smartSizeHint()
|| (wannaGrow && !data->expansive && data->stretch == 0)
|| (!allEmptyNonstretch && data->empty &&
!data->expansive && data->stretch == 0))) {
data->size = data->smartSizeHint();
data->done = true;
space_left -= data->size;
sumStretch -= data->stretch;
n--;
}
}
extraspace = space_left;
/*
Do a trial distribution and calculate how much it is off.
If there are more deficit pixels than surplus pixels, give
the minimum size items what they need, and repeat.
Otherwise give to the maximum size items, and repeat.
Paul Olav Tvete has a wonderful mathematical proof of the
correctness of this principle, but unfortunately this
comment is too small to contain it.
*/
int surplus, deficit;
do {
surplus = deficit = 0;
Fixed64 fp_space = toFixed(space_left);
Fixed64 fp_w = 0;
for (i = start; i < start + count; i++) {
LayoutStruct *data = &chain[i];
if (data->done)
continue;
extraspace = 0;
if (sumStretch <= 0)
fp_w += fp_space / n;
else
fp_w += (fp_space * data->stretch) / sumStretch;
int w = fRound(fp_w);
data->size = w;
fp_w -= toFixed(w); // give the difference to the next
if (w < data->smartSizeHint()) {
deficit += data->smartSizeHint() - w;
} else if (w > data->maximumSize) {
surplus += w - data->maximumSize;
}
}
if (deficit > 0 && surplus <= deficit) {
// give to the ones that have too little
for (i = start; i < start+count; i++) {
LayoutStruct *data = &chain[i];
if (!data->done && data->size < data->smartSizeHint()) {
data->size = data->smartSizeHint();
data->done = true;
space_left -= data->smartSizeHint();
sumStretch -= data->stretch;
n--;
}
}
}
if (surplus > 0 && surplus >= deficit) {
// take from the ones that have too much
for (i = start; i < start + count; i++) {
LayoutStruct *data = &chain[i];
if (!data->done && data->size > data->maximumSize) {
data->size = data->maximumSize;
data->done = true;
space_left -= data->maximumSize;
sumStretch -= data->stretch;
n--;
}
}
}
} while (n > 0 && surplus != deficit);
if (n == 0)
extraspace = space_left;
}
/*
As a last resort, we distribute the unwanted space equally
among the spacers (counting the start and end of the chain). We
could, but don't, attempt a sub-pixel allocation of the extra
space.
*/
int extra = extraspace / (spacerCount + 2);
int p = pos + extra;
for (i = start; i < start+count; i++) {
LayoutStruct *data = &chain[i];
data->position = p;
p += data->size;
if (!data->empty)
p += data->effectiveSpacer(spacer) + extra;
}
#ifdef QLAYOUT_EXTRA_DEBUG
qDebug() << "qGeomCalc" << "start" << start << "count" << count << "pos" << pos
<< "space" << space << "spacer" << spacer;
for (i = start; i < start + count; ++i) {
qDebug() << i << ':' << chain[i].minimumSize << chain[i].smartSizeHint()
<< chain[i].maximumSize << "stretch" << chain[i].stretch
<< "empty" << chain[i].empty << "expansive" << chain[i].expansive
<< "spacing" << chain[i].spacing;
qDebug() << "result pos" << chain[i].pos << "size" << chain[i].size;
}
#endif
}