1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qglobal.h" |
5 | |
6 | #include "qgridlayoutengine_p.h" |
7 | #include "qvarlengtharray.h" |
8 | |
9 | #include <QtDebug> |
10 | #include <QtCore/qmath.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | using namespace Qt::StringLiterals; |
15 | |
16 | #define LAYOUTITEMSIZE_MAX (1 << 24) |
17 | |
18 | template<typename T> |
19 | static void insertOrRemoveItems(QList<T> &items, int index, int delta) |
20 | { |
21 | int count = items.size(); |
22 | if (index < count) { |
23 | if (delta > 0) { |
24 | items.insert(index, delta, T()); |
25 | } else if (delta < 0) { |
26 | items.remove(index, qMin(a: -delta, b: count - index)); |
27 | } |
28 | } |
29 | } |
30 | |
31 | static qreal growthFactorBelowPreferredSize(qreal desired, qreal sumAvailable, qreal sumDesired) |
32 | { |
33 | Q_ASSERT(sumDesired != 0.0); |
34 | return desired * qPow(x: sumAvailable / sumDesired, y: desired / sumDesired); |
35 | } |
36 | |
37 | static qreal fixedDescent(qreal descent, qreal ascent, qreal targetSize) |
38 | { |
39 | if (descent < 0.0) |
40 | return -1.0; |
41 | |
42 | Q_ASSERT(descent >= 0.0); |
43 | Q_ASSERT(ascent >= 0.0); |
44 | Q_ASSERT(targetSize >= ascent + descent); |
45 | |
46 | qreal extra = targetSize - (ascent + descent); |
47 | return descent + (extra / 2.0); |
48 | } |
49 | |
50 | static qreal compare(const QGridLayoutBox &box1, const QGridLayoutBox &box2, int which) |
51 | { |
52 | qreal size1 = box1.q_sizes(which); |
53 | qreal size2 = box2.q_sizes(which); |
54 | |
55 | if (which == MaximumSize) { |
56 | return size2 - size1; |
57 | } else { |
58 | return size1 - size2; |
59 | } |
60 | } |
61 | |
62 | void QGridLayoutBox::add(const QGridLayoutBox &other, int stretch, qreal spacing) |
63 | { |
64 | Q_ASSERT(q_minimumDescent < 0.0); |
65 | |
66 | q_minimumSize += other.q_minimumSize + spacing; |
67 | q_preferredSize += other.q_preferredSize + spacing; |
68 | q_maximumSize += ((stretch == 0) ? other.q_preferredSize : other.q_maximumSize) + spacing; |
69 | } |
70 | |
71 | void QGridLayoutBox::combine(const QGridLayoutBox &other) |
72 | { |
73 | q_minimumDescent = qMax(a: q_minimumDescent, b: other.q_minimumDescent); |
74 | q_minimumAscent = qMax(a: q_minimumAscent, b: other.q_minimumAscent); |
75 | |
76 | q_minimumSize = qMax(a: q_minimumAscent + q_minimumDescent, |
77 | b: qMax(a: q_minimumSize, b: other.q_minimumSize)); |
78 | qreal maxMax; |
79 | if (q_maximumSize == FLT_MAX && other.q_maximumSize != FLT_MAX) |
80 | maxMax = other.q_maximumSize; |
81 | else if (other.q_maximumSize == FLT_MAX && q_maximumSize != FLT_MAX) |
82 | maxMax = q_maximumSize; |
83 | else |
84 | maxMax = qMax(a: q_maximumSize, b: other.q_maximumSize); |
85 | |
86 | q_maximumSize = qMax(a: q_minimumSize, b: maxMax); |
87 | q_preferredSize = qBound(min: q_minimumSize, val: qMax(a: q_preferredSize, b: other.q_preferredSize), |
88 | max: q_maximumSize); |
89 | } |
90 | |
91 | void QGridLayoutBox::normalize() |
92 | { |
93 | q_maximumSize = qMax(a: qreal(0.0), b: q_maximumSize); |
94 | q_minimumSize = qBound(min: qreal(0.0), val: q_minimumSize, max: q_maximumSize); |
95 | q_preferredSize = qBound(min: q_minimumSize, val: q_preferredSize, max: q_maximumSize); |
96 | q_minimumDescent = qMin(a: q_minimumDescent, b: q_minimumSize); |
97 | |
98 | Q_ASSERT((q_minimumDescent < 0.0) == (q_minimumAscent < 0.0)); |
99 | } |
100 | |
101 | #ifdef QGRIDLAYOUTENGINE_DEBUG |
102 | void QGridLayoutBox::dump(int indent) const |
103 | { |
104 | qDebug("%*sBox (%g <= %g <= %g [%g/%g])", indent, "", q_minimumSize, q_preferredSize, |
105 | q_maximumSize, q_minimumAscent, q_minimumDescent); |
106 | } |
107 | #endif |
108 | |
109 | bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2) |
110 | { |
111 | for (int i = 0; i < NSizes; ++i) { |
112 | if (box1.q_sizes(which: i) != box2.q_sizes(which: i)) |
113 | return false; |
114 | } |
115 | return box1.q_minimumDescent == box2.q_minimumDescent |
116 | && box1.q_minimumAscent == box2.q_minimumAscent; |
117 | } |
118 | |
119 | void QGridLayoutRowData::reset(int count) |
120 | { |
121 | ignore.fill(aval: false, asize: count); |
122 | boxes.fill(t: QGridLayoutBox(), newSize: count); |
123 | multiCellMap.clear(); |
124 | stretches.fill(t: 0, newSize: count); |
125 | spacings.fill(t: 0.0, newSize: count); |
126 | hasIgnoreFlag = false; |
127 | } |
128 | |
129 | void QGridLayoutRowData::distributeMultiCells(const QGridLayoutRowInfo &rowInfo, bool snapToPixelGrid) |
130 | { |
131 | MultiCellMap::const_iterator i = multiCellMap.constBegin(); |
132 | for (; i != multiCellMap.constEnd(); ++i) { |
133 | int start = i.key().first; |
134 | int span = i.key().second; |
135 | int end = start + span; |
136 | const QGridLayoutBox &box = i.value().q_box; |
137 | int stretch = i.value().q_stretch; |
138 | |
139 | QGridLayoutBox totalBox = this->totalBox(start, end); |
140 | QVarLengthArray<QGridLayoutBox> extras(span); |
141 | QVarLengthArray<qreal> dummy(span); |
142 | QVarLengthArray<qreal> newSizes(span); |
143 | |
144 | for (int j = 0; j < NSizes; ++j) { |
145 | qreal extra = compare(box1: box, box2: totalBox, which: j); |
146 | if (extra > 0.0) { |
147 | calculateGeometries(start, end, targetSize: box.q_sizes(which: j), positions: dummy.data(), sizes: newSizes.data(), |
148 | descents: nullptr, totalBox, rowInfo, snapToPixelGrid); |
149 | |
150 | for (int k = 0; k < span; ++k) |
151 | extras[k].q_sizes(which: j) = newSizes[k]; |
152 | } |
153 | } |
154 | |
155 | for (int k = 0; k < span; ++k) { |
156 | boxes[start + k].combine(other: extras[k]); |
157 | if (stretch != 0) |
158 | stretches[start + k] = qMax(a: stretches[start + k], b: stretch); |
159 | } |
160 | } |
161 | multiCellMap.clear(); |
162 | } |
163 | namespace { |
164 | |
165 | // does not return int |
166 | static inline qreal qround(qreal f) |
167 | { |
168 | return std::floor(x: f + qreal(0.5)); |
169 | } |
170 | |
171 | } |
172 | void QGridLayoutRowData::calculateGeometries(int start, int end, qreal targetSize, qreal *positions, |
173 | qreal *sizes, qreal *descents, |
174 | const QGridLayoutBox &totalBox, |
175 | const QGridLayoutRowInfo &rowInfo, bool snapToPixelGrid) |
176 | { |
177 | Q_ASSERT(end > start); |
178 | |
179 | targetSize = qMax(a: totalBox.q_minimumSize, b: targetSize); |
180 | |
181 | int n = end - start; |
182 | QVarLengthArray<qreal> newSizes(n); |
183 | QVarLengthArray<qreal> factors(n); |
184 | qreal sumFactors = 0.0; |
185 | int sumStretches = 0; |
186 | qreal sumAvailable; |
187 | |
188 | for (int i = 0; i < n; ++i) { |
189 | const int stretch = stretches.at(i: start + i); |
190 | if (stretch > 0) |
191 | sumStretches += stretch; |
192 | } |
193 | |
194 | if (targetSize < totalBox.q_preferredSize) { |
195 | stealBox(start, end, which: MinimumSize, positions, sizes); |
196 | |
197 | sumAvailable = targetSize - totalBox.q_minimumSize; |
198 | if (sumAvailable > 0.0) { |
199 | const qreal totalBox_preferredSize = qMin(a: totalBox.q_preferredSize, b: qreal(LAYOUTITEMSIZE_MAX)); |
200 | qreal sumDesired = totalBox_preferredSize - totalBox.q_minimumSize; |
201 | |
202 | for (int i = 0; i < n; ++i) { |
203 | if (ignore.testBit(i: start + i)) { |
204 | factors[i] = 0.0; |
205 | continue; |
206 | } |
207 | |
208 | const QGridLayoutBox &box = boxes.at(i: start + i); |
209 | const qreal box_preferredSize = qMin(a: box.q_preferredSize, b: qreal(LAYOUTITEMSIZE_MAX)); |
210 | qreal desired = box_preferredSize - box.q_minimumSize; |
211 | factors[i] = growthFactorBelowPreferredSize(desired, sumAvailable, sumDesired); |
212 | sumFactors += factors[i]; |
213 | } |
214 | |
215 | for (int i = 0; i < n; ++i) { |
216 | Q_ASSERT(sumFactors > 0.0); |
217 | qreal delta = sumAvailable * factors[i] / sumFactors; |
218 | newSizes[i] = sizes[i] + delta; |
219 | } |
220 | } |
221 | } else { |
222 | bool isLargerThanMaximum = (targetSize > totalBox.q_maximumSize); |
223 | if (isLargerThanMaximum) { |
224 | stealBox(start, end, which: MaximumSize, positions, sizes); |
225 | sumAvailable = targetSize - totalBox.q_maximumSize; |
226 | } else { |
227 | stealBox(start, end, which: PreferredSize, positions, sizes); |
228 | sumAvailable = targetSize - totalBox.q_preferredSize; |
229 | } |
230 | |
231 | if (sumAvailable > 0.0) { |
232 | qreal sumCurrentAvailable = sumAvailable; |
233 | bool somethingHasAMaximumSize = false; |
234 | |
235 | qreal sumSizes = 0.0; |
236 | for (int i = 0; i < n; ++i) |
237 | sumSizes += sizes[i]; |
238 | |
239 | for (int i = 0; i < n; ++i) { |
240 | if (ignore.testBit(i: start + i)) { |
241 | newSizes[i] = 0.0; |
242 | factors[i] = 0.0; |
243 | continue; |
244 | } |
245 | |
246 | const QGridLayoutBox &box = boxes.at(i: start + i); |
247 | qreal boxSize; |
248 | |
249 | qreal desired; |
250 | if (isLargerThanMaximum) { |
251 | boxSize = box.q_maximumSize; |
252 | desired = rowInfo.boxes.value(i: start + i).q_maximumSize - boxSize; |
253 | } else { |
254 | boxSize = box.q_preferredSize; |
255 | desired = box.q_maximumSize - boxSize; |
256 | } |
257 | if (desired == 0.0) { |
258 | newSizes[i] = sizes[i]; |
259 | factors[i] = 0.0; |
260 | } else { |
261 | Q_ASSERT(desired > 0.0); |
262 | |
263 | int stretch = stretches[start + i]; |
264 | if (sumStretches == 0) { |
265 | if (hasIgnoreFlag || sizes[i] == 0.0) { |
266 | factors[i] = (stretch < 0) ? 1.0 : 0.0; |
267 | } else { |
268 | factors[i] = (stretch < 0) ? sizes[i] : 0.0; |
269 | } |
270 | } else if (stretch == sumStretches) { |
271 | factors[i] = 1.0; |
272 | } else if (stretch <= 0) { |
273 | factors[i] = 0.0; |
274 | } else { |
275 | qreal ultimateSize; |
276 | qreal ultimateSumSizes; |
277 | qreal x = ((stretch * sumSizes) |
278 | - (sumStretches * boxSize)) |
279 | / (sumStretches - stretch); |
280 | if (x >= 0.0) { |
281 | ultimateSize = boxSize + x; |
282 | ultimateSumSizes = sumSizes + x; |
283 | } else { |
284 | ultimateSize = boxSize; |
285 | ultimateSumSizes = (sumStretches * boxSize) |
286 | / stretch; |
287 | } |
288 | |
289 | /* |
290 | We multiply these by 1.5 to give some space for a smooth transition |
291 | (at the expense of the stretch factors, which are not fully respected |
292 | during the transition). |
293 | */ |
294 | ultimateSize = ultimateSize * 3 / 2; |
295 | ultimateSumSizes = ultimateSumSizes * 3 / 2; |
296 | |
297 | qreal beta = ultimateSumSizes - sumSizes; |
298 | if (!beta) { |
299 | factors[i] = 1; |
300 | } else { |
301 | qreal alpha = qMin(a: sumCurrentAvailable, b: beta); |
302 | qreal ultimateFactor = (stretch * ultimateSumSizes / sumStretches) |
303 | - (boxSize); |
304 | qreal transitionalFactor = sumCurrentAvailable * (ultimateSize - boxSize) / beta; |
305 | |
306 | factors[i] = ((alpha * ultimateFactor) |
307 | + ((beta - alpha) * transitionalFactor)) / beta; |
308 | } |
309 | |
310 | } |
311 | sumFactors += factors[i]; |
312 | if (desired < sumCurrentAvailable) |
313 | somethingHasAMaximumSize = true; |
314 | |
315 | newSizes[i] = -1.0; |
316 | } |
317 | } |
318 | |
319 | bool keepGoing = somethingHasAMaximumSize; |
320 | while (keepGoing) { |
321 | //sumCurrentAvailable is so large that something *might* reach its maximum size |
322 | keepGoing = false; |
323 | |
324 | for (int i = 0; i < n; ++i) { |
325 | if (newSizes[i] >= 0.0) |
326 | continue; |
327 | |
328 | const QList<QGridLayoutBox> &rBoxes = isLargerThanMaximum ? rowInfo.boxes : boxes; |
329 | const QGridLayoutBox &box = rBoxes.value(i: start + i); |
330 | qreal maxBoxSize = box.q_maximumSize; |
331 | |
332 | if (snapToPixelGrid) |
333 | maxBoxSize = qMax(a: box.q_minimumSize, b: std::floor(x: maxBoxSize)); |
334 | |
335 | qreal avail = sumCurrentAvailable * factors[i] / sumFactors; |
336 | if (sizes[i] + avail >= maxBoxSize) { |
337 | newSizes[i] = maxBoxSize; |
338 | sumCurrentAvailable -= maxBoxSize - sizes[i]; |
339 | sumFactors -= factors[i]; |
340 | keepGoing = (sumCurrentAvailable > 0.0); |
341 | if (!keepGoing) |
342 | break; |
343 | } |
344 | } |
345 | } |
346 | for (int i = 0; i < n; ++i) { |
347 | if (newSizes[i] < 0.0) { |
348 | qreal delta = (sumFactors == 0.0) ? 0.0 |
349 | : sumCurrentAvailable * factors[i] / sumFactors; |
350 | newSizes[i] = sizes[i] + delta; |
351 | } |
352 | } |
353 | } |
354 | } |
355 | |
356 | if (sumAvailable > 0) { |
357 | qreal offset = 0; |
358 | for (int i = 0; i < n; ++i) { |
359 | qreal delta = newSizes[i] - sizes[i]; |
360 | positions[i] += offset; |
361 | sizes[i] += delta; |
362 | offset += delta; |
363 | } |
364 | |
365 | #if 0 // some "pixel allocation" |
366 | int surplus = targetSize - (positions[n - 1] + sizes[n - 1]); |
367 | Q_ASSERT(surplus >= 0 && surplus <= n); |
368 | |
369 | int prevSurplus = -1; |
370 | while (surplus > 0 && surplus != prevSurplus) { |
371 | prevSurplus = surplus; |
372 | |
373 | int offset = 0; |
374 | for (int i = 0; i < n; ++i) { |
375 | const QGridLayoutBox &box = boxes.at(start + i); |
376 | int delta = (!ignore.testBit(start + i) && surplus > 0 |
377 | && factors[i] > 0 && sizes[i] < box.q_maximumSize) |
378 | ? 1 : 0; |
379 | |
380 | positions[i] += offset; |
381 | sizes[i] += delta; |
382 | offset += delta; |
383 | surplus -= delta; |
384 | } |
385 | } |
386 | Q_ASSERT(surplus == 0); |
387 | #endif |
388 | } |
389 | if (snapToPixelGrid) { |
390 | for (int i = 0; i < n; ++i) { |
391 | const qreal oldpos = positions[i]; |
392 | positions[i] = qround(f: oldpos); |
393 | const qreal delta = positions[i] - oldpos; |
394 | sizes[i] -= delta; |
395 | if (i > 0) |
396 | sizes[i - 1] += delta; |
397 | } |
398 | |
399 | sizes[n - 1] = targetSize - positions[n - 1]; |
400 | // This loop serves two purposes: |
401 | // 1. round off the small epsilons produced by the above loop. |
402 | // 2. avoid that the above loop didn't make the cell width smaller than its minimum constraint. |
403 | for (int i = 0; i < n; ++i) { |
404 | const QGridLayoutBox &box = boxes.at(i: start + i); |
405 | sizes[i] = qMax(a: box.q_minimumSize, b: qround(f: sizes[i])); |
406 | } |
407 | } |
408 | |
409 | if (descents) { |
410 | for (int i = 0; i < n; ++i) { |
411 | if (ignore.testBit(i: start + i)) |
412 | continue; |
413 | const QGridLayoutBox &box = boxes.at(i: start + i); |
414 | descents[i] = fixedDescent(descent: box.q_minimumDescent, ascent: box.q_minimumAscent, targetSize: sizes[i]); |
415 | } |
416 | } |
417 | } |
418 | |
419 | QGridLayoutBox QGridLayoutRowData::totalBox(int start, int end) const |
420 | { |
421 | QGridLayoutBox result; |
422 | if (start < end) { |
423 | result.q_maximumSize = 0.0; |
424 | qreal nextSpacing = 0.0; |
425 | for (int i = start; i < end; ++i) { |
426 | if (ignore.testBit(i)) |
427 | continue; |
428 | result.add(other: boxes.at(i), stretch: stretches.at(i), spacing: nextSpacing); |
429 | nextSpacing = spacings.at(i); |
430 | } |
431 | } |
432 | return result; |
433 | } |
434 | |
435 | void QGridLayoutRowData::stealBox(int start, int end, int which, qreal *positions, qreal *sizes) |
436 | { |
437 | qreal offset = 0.0; |
438 | qreal nextSpacing = 0.0; |
439 | |
440 | for (int i = start; i < end; ++i) { |
441 | qreal avail = 0.0; |
442 | |
443 | if (!ignore.testBit(i)) { |
444 | const QGridLayoutBox &box = boxes.at(i); |
445 | avail = box.q_sizes(which); |
446 | offset += nextSpacing; |
447 | nextSpacing = spacings.at(i); |
448 | } |
449 | |
450 | *positions++ = offset; |
451 | *sizes++ = avail; |
452 | offset += avail; |
453 | } |
454 | } |
455 | |
456 | #ifdef QGRIDLAYOUTENGINE_DEBUG |
457 | void QGridLayoutRowData::dump(int indent) const |
458 | { |
459 | qDebug("%*sData", indent, ""); |
460 | |
461 | for (int i = 0; i < ignore.count(); ++i) { |
462 | qDebug("%*s Row %d (stretch %d, spacing %g)", indent, "", i, stretches.at(i), |
463 | spacings.at(i)); |
464 | if (ignore.testBit(i)) |
465 | qDebug("%*s Ignored", indent, ""); |
466 | boxes.at(i).dump(indent + 2); |
467 | } |
468 | |
469 | MultiCellMap::const_iterator it = multiCellMap.constBegin(); |
470 | while (it != multiCellMap.constEnd()) { |
471 | qDebug("%*s Multi-cell entry <%d, %d> (stretch %d)", indent, "", it.key().first, |
472 | it.key().second, it.value().q_stretch); |
473 | it.value().q_box.dump(indent + 2); |
474 | } |
475 | } |
476 | #endif |
477 | |
478 | QGridLayoutItem::QGridLayoutItem(int row, int column, int rowSpan, int columnSpan, |
479 | Qt::Alignment alignment) |
480 | : q_firstRows{column, row}, |
481 | q_rowSpans{columnSpan, rowSpan}, |
482 | q_stretches{-1, -1}, |
483 | q_alignment(alignment) |
484 | { |
485 | } |
486 | |
487 | int QGridLayoutItem::firstRow(Qt::Orientation orientation) const |
488 | { |
489 | return q_firstRows[orientation]; |
490 | } |
491 | |
492 | int QGridLayoutItem::firstColumn(Qt::Orientation orientation) const |
493 | { |
494 | return q_firstRows.transposed()[orientation]; |
495 | } |
496 | |
497 | int QGridLayoutItem::lastRow(Qt::Orientation orientation) const |
498 | { |
499 | return firstRow(orientation) + rowSpan(orientation) - 1; |
500 | } |
501 | |
502 | int QGridLayoutItem::lastColumn(Qt::Orientation orientation) const |
503 | { |
504 | return firstColumn(orientation) + columnSpan(orientation) - 1; |
505 | } |
506 | |
507 | int QGridLayoutItem::rowSpan(Qt::Orientation orientation) const |
508 | { |
509 | return q_rowSpans[orientation]; |
510 | } |
511 | |
512 | int QGridLayoutItem::columnSpan(Qt::Orientation orientation) const |
513 | { |
514 | return q_rowSpans.transposed()[orientation]; |
515 | } |
516 | |
517 | void QGridLayoutItem::setFirstRow(int row, Qt::Orientation orientation) |
518 | { |
519 | q_firstRows[orientation] = row; |
520 | } |
521 | |
522 | void QGridLayoutItem::setRowSpan(int rowSpan, Qt::Orientation orientation) |
523 | { |
524 | q_rowSpans[orientation] = rowSpan; |
525 | } |
526 | |
527 | int QGridLayoutItem::stretchFactor(Qt::Orientation orientation) const |
528 | { |
529 | int stretch = q_stretches[orientation]; |
530 | if (stretch >= 0) |
531 | return stretch; |
532 | |
533 | QLayoutPolicy::Policy policy = sizePolicy(orientation); |
534 | |
535 | if (policy & QLayoutPolicy::ExpandFlag) { |
536 | return 1; |
537 | } else if (policy & QLayoutPolicy::GrowFlag) { |
538 | return -1; // because we max it up |
539 | } else { |
540 | return 0; |
541 | } |
542 | } |
543 | |
544 | void QGridLayoutItem::setStretchFactor(int stretch, Qt::Orientation orientation) |
545 | { |
546 | Q_ASSERT(stretch >= 0); // ### deal with too big stretches |
547 | q_stretches[orientation] = stretch; |
548 | } |
549 | |
550 | QLayoutPolicy::ControlTypes QGridLayoutItem::controlTypes(LayoutSide /*side*/) const |
551 | { |
552 | return QLayoutPolicy::DefaultType; |
553 | } |
554 | |
555 | QGridLayoutBox QGridLayoutItem::box(Qt::Orientation orientation, bool snapToPixelGrid, qreal constraint) const |
556 | { |
557 | QGridLayoutBox result; |
558 | QLayoutPolicy::Policy policy = sizePolicy(orientation); |
559 | |
560 | if (orientation == Qt::Horizontal) { |
561 | QSizeF constraintSize(-1.0, constraint); |
562 | |
563 | result.q_preferredSize = sizeHint(which: Qt::PreferredSize, constraint: constraintSize).width(); |
564 | |
565 | if (policy & QLayoutPolicy::ShrinkFlag) { |
566 | result.q_minimumSize = sizeHint(which: Qt::MinimumSize, constraint: constraintSize).width(); |
567 | } else { |
568 | result.q_minimumSize = result.q_preferredSize; |
569 | } |
570 | if (snapToPixelGrid) |
571 | result.q_minimumSize = qCeil(v: result.q_minimumSize); |
572 | |
573 | if (policy & (QLayoutPolicy::GrowFlag | QLayoutPolicy::ExpandFlag)) { |
574 | result.q_maximumSize = sizeHint(which: Qt::MaximumSize, constraint: constraintSize).width(); |
575 | } else { |
576 | result.q_maximumSize = result.q_preferredSize; |
577 | } |
578 | } else { |
579 | QSizeF constraintSize(constraint, -1.0); |
580 | |
581 | result.q_preferredSize = sizeHint(which: Qt::PreferredSize, constraint: constraintSize).height(); |
582 | |
583 | if (policy & QLayoutPolicy::ShrinkFlag) { |
584 | result.q_minimumSize = sizeHint(which: Qt::MinimumSize, constraint: constraintSize).height(); |
585 | } else { |
586 | result.q_minimumSize = result.q_preferredSize; |
587 | } |
588 | if (snapToPixelGrid) |
589 | result.q_minimumSize = qCeil(v: result.q_minimumSize); |
590 | |
591 | if (policy & (QLayoutPolicy::GrowFlag | QLayoutPolicy::ExpandFlag)) { |
592 | result.q_maximumSize = sizeHint(which: Qt::MaximumSize, constraint: constraintSize).height(); |
593 | } else { |
594 | result.q_maximumSize = result.q_preferredSize; |
595 | } |
596 | |
597 | if (alignment() & Qt::AlignBaseline) { |
598 | result.q_minimumDescent = sizeHint(which: Qt::MinimumDescent, constraint: constraintSize).height(); |
599 | if (result.q_minimumDescent != -1.0) { |
600 | const qreal minSizeHint = sizeHint(which: Qt::MinimumSize, constraint: constraintSize).height(); |
601 | result.q_minimumDescent -= (minSizeHint - result.q_minimumSize); |
602 | result.q_minimumAscent = result.q_minimumSize - result.q_minimumDescent; |
603 | } |
604 | } |
605 | } |
606 | if (policy & QLayoutPolicy::IgnoreFlag) |
607 | result.q_preferredSize = result.q_minimumSize; |
608 | |
609 | return result; |
610 | } |
611 | |
612 | QRectF QGridLayoutItem::geometryWithin(qreal x, qreal y, qreal width, qreal height, |
613 | qreal rowDescent, Qt::Alignment align, bool snapToPixelGrid) const |
614 | { |
615 | const qreal cellWidth = width; |
616 | const qreal cellHeight = height; |
617 | |
618 | QSizeF size = effectiveMaxSize(constraint: QSizeF(-1,-1)); |
619 | if (hasDynamicConstraint()) { |
620 | if (dynamicConstraintOrientation() == Qt::Vertical) { |
621 | if (size.width() > cellWidth) |
622 | size = effectiveMaxSize(constraint: QSizeF(cellWidth, -1)); |
623 | } else if (size.height() > cellHeight) { |
624 | size = effectiveMaxSize(constraint: QSizeF(-1, cellHeight)); |
625 | } |
626 | } |
627 | size = size.boundedTo(otherSize: QSizeF(cellWidth, cellHeight)); |
628 | width = size.width(); |
629 | height = size.height(); |
630 | |
631 | switch (align & Qt::AlignHorizontal_Mask) { |
632 | case Qt::AlignHCenter: |
633 | x += (cellWidth - width)/2; |
634 | break; |
635 | case Qt::AlignRight: |
636 | x += cellWidth - width; |
637 | break; |
638 | default: |
639 | break; |
640 | } |
641 | |
642 | switch (align & Qt::AlignVertical_Mask) { |
643 | case Qt::AlignVCenter: |
644 | y += (cellHeight - height)/2; |
645 | break; |
646 | case Qt::AlignBottom: |
647 | y += cellHeight - height; |
648 | break; |
649 | case Qt::AlignBaseline: { |
650 | width = qMin(a: effectiveMaxSize(constraint: QSizeF(-1,-1)).width(), b: width); |
651 | QGridLayoutBox vBox = box(orientation: Qt::Vertical, snapToPixelGrid); |
652 | const qreal descent = vBox.q_minimumDescent; |
653 | const qreal ascent = vBox.q_minimumSize - descent; |
654 | y += (cellHeight - rowDescent - ascent); |
655 | height = ascent + descent; |
656 | break; } |
657 | default: |
658 | break; |
659 | } |
660 | return QRectF(x, y, width, height); |
661 | } |
662 | |
663 | void QGridLayoutItem::transpose() |
664 | { |
665 | q_firstRows.transpose(); |
666 | q_rowSpans.transpose(); |
667 | q_stretches.transpose(); |
668 | } |
669 | |
670 | void QGridLayoutItem::insertOrRemoveRows(int row, int delta, Qt::Orientation orientation) |
671 | { |
672 | int oldFirstRow = firstRow(orientation); |
673 | if (oldFirstRow >= row) { |
674 | setFirstRow(row: oldFirstRow + delta, orientation); |
675 | } else if (lastRow(orientation) >= row) { |
676 | setRowSpan(rowSpan: rowSpan(orientation) + delta, orientation); |
677 | } |
678 | } |
679 | /*! |
680 | \internal |
681 | returns the effective maximumSize, will take the sizepolicy into |
682 | consideration. (i.e. if sizepolicy does not have QLayoutPolicy::Grow, then |
683 | maxSizeHint will be the preferredSize) |
684 | Note that effectiveSizeHint does not take sizePolicy into consideration, |
685 | (since it only evaluates the hints, as the name implies) |
686 | */ |
687 | QSizeF QGridLayoutItem::effectiveMaxSize(const QSizeF &constraint) const |
688 | { |
689 | QSizeF size = constraint; |
690 | bool vGrow = (sizePolicy(orientation: Qt::Vertical) & QLayoutPolicy::GrowFlag) == QLayoutPolicy::GrowFlag; |
691 | bool hGrow = (sizePolicy(orientation: Qt::Horizontal) & QLayoutPolicy::GrowFlag) == QLayoutPolicy::GrowFlag; |
692 | if (!vGrow || !hGrow) { |
693 | QSizeF pref = sizeHint(which: Qt::PreferredSize, constraint); |
694 | if (!vGrow) |
695 | size.setHeight(pref.height()); |
696 | if (!hGrow) |
697 | size.setWidth(pref.width()); |
698 | } |
699 | |
700 | if (!size.isValid()) { |
701 | QSizeF maxSize = sizeHint(which: Qt::MaximumSize, constraint: size); |
702 | if (size.width() == -1) |
703 | size.setWidth(maxSize.width()); |
704 | if (size.height() == -1) |
705 | size.setHeight(maxSize.height()); |
706 | } |
707 | return size; |
708 | } |
709 | |
710 | #ifdef QGRIDLAYOUTENGINE_DEBUG |
711 | void QGridLayoutItem::dump(int indent) const |
712 | { |
713 | qDebug("%*s (%d, %d) %d x %d", indent, "", firstRow(), firstColumn(), //### |
714 | rowSpan(), columnSpan()); |
715 | |
716 | if (q_stretches[Qt::Horizontal] >= 0) |
717 | qDebug("%*s Horizontal stretch: %d", indent, "", q_stretches[Qt::Horizontal]); |
718 | if (q_stretches[Qt::Vertical] >= 0) |
719 | qDebug("%*s Vertical stretch: %d", indent, "", q_stretches[Qt::Vertical]); |
720 | if (q_alignment != 0) |
721 | qDebug("%*s Alignment: %x", indent, "", uint(q_alignment)); |
722 | qDebug("%*s Horizontal size policy: %x Vertical size policy: %x", |
723 | indent, "", (unsigned int)sizePolicy(Qt::Horizontal), (unsigned int)sizePolicy(Qt::Vertical)); |
724 | } |
725 | #endif |
726 | |
727 | void QGridLayoutRowInfo::insertOrRemoveRows(int row, int delta) |
728 | { |
729 | count += delta; |
730 | |
731 | insertOrRemoveItems(items&: stretches, index: row, delta); |
732 | insertOrRemoveItems(items&: spacings, index: row, delta); |
733 | insertOrRemoveItems(items&: alignments, index: row, delta); |
734 | insertOrRemoveItems(items&: boxes, index: row, delta); |
735 | } |
736 | |
737 | #ifdef QGRIDLAYOUTENGINE_DEBUG |
738 | void QGridLayoutRowInfo::dump(int indent) const |
739 | { |
740 | qDebug("%*sInfo (count: %d)", indent, "", count); |
741 | for (int i = 0; i < count; ++i) { |
742 | QString message; |
743 | |
744 | if (stretches.value(i).value() >= 0) |
745 | message += QString::fromLatin1(" stretch %1").arg(stretches.value(i).value()); |
746 | if (spacings.value(i).value() >= 0.0) |
747 | message += QString::fromLatin1(" spacing %1").arg(spacings.value(i).value()); |
748 | if (alignments.value(i) != 0) |
749 | message += QString::fromLatin1(" alignment %1").arg(int(alignments.value(i)), 16); |
750 | |
751 | if (!message.isEmpty() || boxes.value(i) != QGridLayoutBox()) { |
752 | qDebug("%*s Row %d:%s", indent, "", i, qPrintable(message)); |
753 | if (boxes.value(i) != QGridLayoutBox()) |
754 | boxes.value(i).dump(indent + 1); |
755 | } |
756 | } |
757 | } |
758 | #endif |
759 | |
760 | QGridLayoutEngine::QGridLayoutEngine(Qt::Alignment defaultAlignment, bool snapToPixelGrid) |
761 | { |
762 | m_visualDirection = Qt::LeftToRight; |
763 | m_defaultAlignment = defaultAlignment; |
764 | m_snapToPixelGrid = snapToPixelGrid; |
765 | m_uniformCellWidths = false; |
766 | m_uniformCellHeights = false; |
767 | invalidate(); |
768 | } |
769 | |
770 | int QGridLayoutEngine::rowCount(Qt::Orientation orientation) const |
771 | { |
772 | return q_infos[orientation].count; |
773 | } |
774 | |
775 | int QGridLayoutEngine::columnCount(Qt::Orientation orientation) const |
776 | { |
777 | return q_infos.transposed()[orientation].count; |
778 | } |
779 | |
780 | int QGridLayoutEngine::itemCount() const |
781 | { |
782 | return q_items.size(); |
783 | } |
784 | |
785 | QGridLayoutItem *QGridLayoutEngine::itemAt(int index) const |
786 | { |
787 | Q_ASSERT(index >= 0 && index < itemCount()); |
788 | return q_items.at(i: index); |
789 | } |
790 | |
791 | int QGridLayoutEngine::effectiveFirstRow(Qt::Orientation orientation) const |
792 | { |
793 | ensureEffectiveFirstAndLastRows(); |
794 | return q_cachedEffectiveFirstRows[orientation]; |
795 | } |
796 | |
797 | int QGridLayoutEngine::effectiveLastRow(Qt::Orientation orientation) const |
798 | { |
799 | ensureEffectiveFirstAndLastRows(); |
800 | return q_cachedEffectiveLastRows[orientation]; |
801 | } |
802 | |
803 | void QGridLayoutEngine::setSpacing(qreal spacing, Qt::Orientations orientations) |
804 | { |
805 | if (orientations & Qt::Horizontal) |
806 | q_defaultSpacings[Qt::Horizontal].setUserValue(spacing); |
807 | if (orientations & Qt::Vertical) |
808 | q_defaultSpacings[Qt::Vertical].setUserValue(spacing); |
809 | |
810 | invalidate(); |
811 | } |
812 | |
813 | qreal QGridLayoutEngine::spacing(Qt::Orientation orientation, const QAbstractLayoutStyleInfo *styleInfo) const |
814 | { |
815 | if (!q_defaultSpacings[orientation].isUser()) { |
816 | qreal defaultSpacing = styleInfo->spacing(orientation); |
817 | q_defaultSpacings[orientation].setCachedValue(defaultSpacing); |
818 | } |
819 | return q_defaultSpacings[orientation].value(); |
820 | } |
821 | |
822 | void QGridLayoutEngine::setRowSpacing(int row, qreal spacing, Qt::Orientation orientation) |
823 | { |
824 | Q_ASSERT(row >= 0); |
825 | |
826 | QGridLayoutRowInfo &rowInfo = q_infos[orientation]; |
827 | if (row >= rowInfo.spacings.size()) |
828 | rowInfo.spacings.resize(size: row + 1); |
829 | if (spacing >= 0) |
830 | rowInfo.spacings[row].setUserValue(spacing); |
831 | else |
832 | rowInfo.spacings[row] = QLayoutParameter<qreal>(); |
833 | invalidate(); |
834 | } |
835 | |
836 | qreal QGridLayoutEngine::rowSpacing(int row, Qt::Orientation orientation) const |
837 | { |
838 | QLayoutParameter<qreal> spacing = q_infos[orientation].spacings.value(i: row); |
839 | if (!spacing.isDefault()) |
840 | return spacing.value(); |
841 | return q_defaultSpacings[orientation].value(); |
842 | } |
843 | |
844 | void QGridLayoutEngine::setRowStretchFactor(int row, int stretch, Qt::Orientation orientation) |
845 | { |
846 | Q_ASSERT(row >= 0); |
847 | Q_ASSERT(stretch >= 0); |
848 | |
849 | maybeExpandGrid(row, column: -1, orientation); |
850 | |
851 | QGridLayoutRowInfo &rowInfo = q_infos[orientation]; |
852 | if (row >= rowInfo.stretches.size()) |
853 | rowInfo.stretches.resize(size: row + 1); |
854 | rowInfo.stretches[row].setUserValue(stretch); |
855 | } |
856 | |
857 | int QGridLayoutEngine::rowStretchFactor(int row, Qt::Orientation orientation) const |
858 | { |
859 | QStretchParameter stretch = q_infos[orientation].stretches.value(i: row); |
860 | if (!stretch.isDefault()) |
861 | return stretch.value(); |
862 | return 0; |
863 | } |
864 | |
865 | void QGridLayoutEngine::setRowSizeHint(Qt::SizeHint which, int row, qreal size, |
866 | Qt::Orientation orientation) |
867 | { |
868 | Q_ASSERT(row >= 0); |
869 | Q_ASSERT(size >= 0.0); |
870 | |
871 | maybeExpandGrid(row, column: -1, orientation); |
872 | |
873 | QGridLayoutRowInfo &rowInfo = q_infos[orientation]; |
874 | if (row >= rowInfo.boxes.size()) |
875 | rowInfo.boxes.resize(size: row + 1); |
876 | rowInfo.boxes[row].q_sizes(which) = size; |
877 | } |
878 | |
879 | qreal QGridLayoutEngine::rowSizeHint(Qt::SizeHint which, int row, Qt::Orientation orientation) const |
880 | { |
881 | return q_infos[orientation].boxes.value(i: row).q_sizes(which); |
882 | } |
883 | |
884 | bool QGridLayoutEngine::uniformCellWidths() const |
885 | { |
886 | return m_uniformCellWidths; |
887 | } |
888 | |
889 | void QGridLayoutEngine::setUniformCellWidths(bool uniformCellWidths) |
890 | { |
891 | if (m_uniformCellWidths == uniformCellWidths) |
892 | return; |
893 | |
894 | m_uniformCellWidths = uniformCellWidths; |
895 | invalidate(); |
896 | } |
897 | |
898 | bool QGridLayoutEngine::uniformCellHeights() const |
899 | { |
900 | return m_uniformCellHeights; |
901 | } |
902 | |
903 | void QGridLayoutEngine::setUniformCellHeights(bool uniformCellHeights) |
904 | { |
905 | if (m_uniformCellHeights == uniformCellHeights) |
906 | return; |
907 | |
908 | m_uniformCellHeights = uniformCellHeights; |
909 | invalidate(); |
910 | } |
911 | |
912 | void QGridLayoutEngine::setRowAlignment(int row, Qt::Alignment alignment, |
913 | Qt::Orientation orientation) |
914 | { |
915 | Q_ASSERT(row >= 0); |
916 | |
917 | maybeExpandGrid(row, column: -1, orientation); |
918 | |
919 | QGridLayoutRowInfo &rowInfo = q_infos[orientation]; |
920 | if (row >= rowInfo.alignments.size()) |
921 | rowInfo.alignments.resize(size: row + 1); |
922 | rowInfo.alignments[row] = alignment; |
923 | } |
924 | |
925 | Qt::Alignment QGridLayoutEngine::rowAlignment(int row, Qt::Orientation orientation) const |
926 | { |
927 | Q_ASSERT(row >= 0); |
928 | return q_infos[orientation].alignments.value(i: row); |
929 | } |
930 | |
931 | Qt::Alignment QGridLayoutEngine::effectiveAlignment(const QGridLayoutItem *layoutItem) const |
932 | { |
933 | Qt::Alignment align = layoutItem->alignment(); |
934 | if (!(align & Qt::AlignVertical_Mask)) { |
935 | // no vertical alignment, respect the row alignment |
936 | int y = layoutItem->firstRow(); |
937 | align |= (rowAlignment(row: y, orientation: Qt::Vertical) & Qt::AlignVertical_Mask); |
938 | if (!(align & Qt::AlignVertical_Mask)) |
939 | align |= (m_defaultAlignment & Qt::AlignVertical_Mask); |
940 | } |
941 | if (!(align & Qt::AlignHorizontal_Mask)) { |
942 | // no horizontal alignment, respect the column alignment |
943 | int x = layoutItem->firstColumn(); |
944 | align |= (rowAlignment(row: x, orientation: Qt::Horizontal) & Qt::AlignHorizontal_Mask); |
945 | } |
946 | |
947 | return align; |
948 | } |
949 | |
950 | /*! |
951 | \internal |
952 | The \a index is only used by QGraphicsLinearLayout to ensure that itemAt() reflects the order |
953 | of visual arrangement. Strictly speaking it does not have to, but most people expect it to. |
954 | (And if it didn't we would have to add itemArrangedAt(int index) or something..) |
955 | */ |
956 | void QGridLayoutEngine::insertItem(QGridLayoutItem *item, int index) |
957 | { |
958 | maybeExpandGrid(row: item->lastRow(), column: item->lastColumn()); |
959 | |
960 | if (index < 0 || index >= q_items.size()) |
961 | q_items.append(t: item); |
962 | else |
963 | q_items.insert(i: index, t: item); |
964 | |
965 | for (int i = item->firstRow(); i <= item->lastRow(); ++i) { |
966 | for (int j = item->firstColumn(); j <= item->lastColumn(); ++j) { |
967 | const auto existingItem = itemAt(row: i, column: j); |
968 | if (existingItem) { |
969 | qWarning(msg: "QGridLayoutEngine::addItem: Can't add %s at cell (%d, %d) because it's already taken by %s", |
970 | qPrintable(item->toString()), i, j, qPrintable(existingItem->toString())); |
971 | } |
972 | setItemAt(row: i, column: j, item); |
973 | } |
974 | } |
975 | } |
976 | |
977 | void QGridLayoutEngine::addItem(QGridLayoutItem *item) |
978 | { |
979 | insertItem(item, index: -1); |
980 | } |
981 | |
982 | void QGridLayoutEngine::removeItem(QGridLayoutItem *item) |
983 | { |
984 | Q_ASSERT(q_items.contains(item)); |
985 | |
986 | invalidate(); |
987 | |
988 | for (int i = item->firstRow(); i <= item->lastRow(); ++i) { |
989 | for (int j = item->firstColumn(); j <= item->lastColumn(); ++j) { |
990 | if (itemAt(row: i, column: j) == item) |
991 | setItemAt(row: i, column: j, item: nullptr); |
992 | } |
993 | } |
994 | |
995 | q_items.removeAll(t: item); |
996 | } |
997 | |
998 | |
999 | QGridLayoutItem *QGridLayoutEngine::itemAt(int row, int column, Qt::Orientation orientation) const |
1000 | { |
1001 | if (orientation == Qt::Horizontal) |
1002 | qSwap(value1&: row, value2&: column); |
1003 | if (uint(row) >= uint(rowCount()) || uint(column) >= uint(columnCount())) |
1004 | return nullptr; |
1005 | return q_grid.at(i: (row * internalGridColumnCount()) + column); |
1006 | } |
1007 | |
1008 | void QGridLayoutEngine::invalidate() |
1009 | { |
1010 | q_cachedEffectiveFirstRows = {-1, -1}; |
1011 | q_cachedEffectiveLastRows = {-1, -1}; |
1012 | |
1013 | q_totalBoxCachedConstraints = {NotCached, NotCached}; |
1014 | |
1015 | q_cachedSize = QSizeF(); |
1016 | q_cachedConstraintOrientation = UnknownConstraint; |
1017 | } |
1018 | |
1019 | static void visualRect(QRectF *geom, Qt::LayoutDirection dir, const QRectF &contentsRect) |
1020 | { |
1021 | if (dir == Qt::RightToLeft) |
1022 | geom->moveRight(pos: contentsRect.right() - (geom->left() - contentsRect.left())); |
1023 | } |
1024 | |
1025 | void QGridLayoutEngine::setGeometries(const QRectF &contentsGeometry, const QAbstractLayoutStyleInfo *styleInfo) |
1026 | { |
1027 | if (rowCount() < 1 || columnCount() < 1) |
1028 | return; |
1029 | |
1030 | ensureGeometries(size: contentsGeometry.size(), styleInfo); |
1031 | |
1032 | for (int i = q_items.size() - 1; i >= 0; --i) { |
1033 | QGridLayoutItem *item = q_items.at(i); |
1034 | |
1035 | qreal x = q_xx.at(i: item->firstColumn()); |
1036 | qreal y = q_yy.at(i: item->firstRow()); |
1037 | qreal width = q_widths.at(i: item->lastColumn()); |
1038 | qreal height = q_heights.at(i: item->lastRow()); |
1039 | |
1040 | if (item->columnSpan() != 1) |
1041 | width += q_xx.at(i: item->lastColumn()) - x; |
1042 | if (item->rowSpan() != 1) |
1043 | height += q_yy.at(i: item->lastRow()) - y; |
1044 | |
1045 | const Qt::Alignment align = effectiveAlignment(layoutItem: item); |
1046 | QRectF geom = item->geometryWithin(x: contentsGeometry.x() + x, y: contentsGeometry.y() + y, |
1047 | width, height, rowDescent: q_descents.at(i: item->lastRow()), align, snapToPixelGrid: m_snapToPixelGrid); |
1048 | if (m_snapToPixelGrid) { |
1049 | // x and y should already be rounded, but the call to geometryWithin() above might |
1050 | // result in a geom with x,y at half-pixels (due to centering within the cell) |
1051 | // QRectF may change the width as it wants to maintain the right edge. In this |
1052 | // case the width need to be preserved. |
1053 | geom.moveLeft(pos: qround(f: geom.x())); |
1054 | // Do not snap baseline aligned items, since that might cause the baselines to not be aligned. |
1055 | if (align != Qt::AlignBaseline) |
1056 | geom.moveTop(pos: qround(f: geom.y())); |
1057 | } |
1058 | visualRect(geom: &geom, dir: visualDirection(), contentsRect: contentsGeometry); |
1059 | item->setGeometry(geom); |
1060 | } |
1061 | } |
1062 | |
1063 | // ### candidate for deletion |
1064 | QRectF QGridLayoutEngine::cellRect(const QRectF &contentsGeometry, int row, int column, int rowSpan, |
1065 | int columnSpan, const QAbstractLayoutStyleInfo *styleInfo) const |
1066 | { |
1067 | if (uint(row) >= uint(rowCount()) || uint(column) >= uint(columnCount()) |
1068 | || rowSpan < 1 || columnSpan < 1) |
1069 | return QRectF(); |
1070 | |
1071 | ensureGeometries(size: contentsGeometry.size(), styleInfo); |
1072 | |
1073 | int lastColumn = qMax(a: column + columnSpan, b: columnCount()) - 1; |
1074 | int lastRow = qMax(a: row + rowSpan, b: rowCount()) - 1; |
1075 | |
1076 | qreal x = q_xx[column]; |
1077 | qreal y = q_yy[row]; |
1078 | qreal width = q_widths[lastColumn]; |
1079 | qreal height = q_heights[lastRow]; |
1080 | |
1081 | if (columnSpan != 1) |
1082 | width += q_xx[lastColumn] - x; |
1083 | if (rowSpan != 1) |
1084 | height += q_yy[lastRow] - y; |
1085 | |
1086 | return QRectF(contentsGeometry.x() + x, contentsGeometry.y() + y, width, height); |
1087 | } |
1088 | |
1089 | QSizeF QGridLayoutEngine::sizeHint(Qt::SizeHint which, const QSizeF &constraint, |
1090 | const QAbstractLayoutStyleInfo *styleInfo) const |
1091 | { |
1092 | |
1093 | |
1094 | if (hasDynamicConstraint() && rowCount() > 0 && columnCount() > 0) { |
1095 | QHVContainer<QGridLayoutBox> sizehint_totalBoxes; |
1096 | bool sizeHintCalculated = false; |
1097 | if (constraintOrientation() == Qt::Vertical) { |
1098 | //We have items whose height depends on their width |
1099 | if (constraint.width() >= 0) { |
1100 | ensureColumnAndRowData(rowData: &q_columnData, totalBox: &sizehint_totalBoxes[Qt::Horizontal], colPositions: nullptr, colSizes: nullptr, orientation: Qt::Horizontal, styleInfo); |
1101 | QList<qreal> sizehint_xx; |
1102 | QList<qreal> sizehint_widths; |
1103 | |
1104 | sizehint_xx.resize(size: columnCount()); |
1105 | sizehint_widths.resize(size: columnCount()); |
1106 | qreal width = constraint.width(); |
1107 | //Calculate column widths and positions, and put results in q_xx.data() and q_widths.data() so that we can use this information as |
1108 | //constraints to find the row heights |
1109 | q_columnData.calculateGeometries(start: 0, end: columnCount(), targetSize: width, positions: sizehint_xx.data(), sizes: sizehint_widths.data(), |
1110 | descents: nullptr, totalBox: sizehint_totalBoxes[Qt::Horizontal], rowInfo: q_infos[Qt::Horizontal], snapToPixelGrid: m_snapToPixelGrid); |
1111 | ensureColumnAndRowData(rowData: &q_rowData, totalBox: &sizehint_totalBoxes[Qt::Vertical], colPositions: sizehint_xx.data(), colSizes: sizehint_widths.data(), orientation: Qt::Vertical, styleInfo); |
1112 | sizeHintCalculated = true; |
1113 | } |
1114 | } else { |
1115 | if (constraint.height() >= 0) { |
1116 | //We have items whose width depends on their height |
1117 | ensureColumnAndRowData(rowData: &q_rowData, totalBox: &sizehint_totalBoxes[Qt::Vertical], colPositions: nullptr, colSizes: nullptr, orientation: Qt::Vertical, styleInfo); |
1118 | QList<qreal> sizehint_yy; |
1119 | QList<qreal> sizehint_heights; |
1120 | |
1121 | sizehint_yy.resize(size: rowCount()); |
1122 | sizehint_heights.resize(size: rowCount()); |
1123 | qreal height = constraint.height(); |
1124 | //Calculate row heights and positions, and put results in q_yy.data() and q_heights.data() so that we can use this information as |
1125 | //constraints to find the column widths |
1126 | q_rowData.calculateGeometries(start: 0, end: rowCount(), targetSize: height, positions: sizehint_yy.data(), sizes: sizehint_heights.data(), |
1127 | descents: nullptr, totalBox: sizehint_totalBoxes[Qt::Vertical], rowInfo: q_infos[Qt::Vertical], snapToPixelGrid: m_snapToPixelGrid); |
1128 | ensureColumnAndRowData(rowData: &q_columnData, totalBox: &sizehint_totalBoxes[Qt::Horizontal], colPositions: sizehint_yy.data(), colSizes: sizehint_heights.data(), orientation: Qt::Horizontal, styleInfo); |
1129 | sizeHintCalculated = true; |
1130 | } |
1131 | } |
1132 | if (sizeHintCalculated) |
1133 | return QSizeF{sizehint_totalBoxes[Qt::Horizontal].q_sizes(which), |
1134 | sizehint_totalBoxes[Qt::Vertical].q_sizes(which)}; |
1135 | } |
1136 | |
1137 | //No items with height for width, so it doesn't matter which order we do these in |
1138 | ensureColumnAndRowData(rowData: &q_columnData, totalBox: &q_totalBoxes[Qt::Horizontal], colPositions: nullptr, colSizes: nullptr, orientation: Qt::Horizontal, styleInfo); |
1139 | ensureColumnAndRowData(rowData: &q_rowData, totalBox: &q_totalBoxes[Qt::Vertical], colPositions: nullptr, colSizes: nullptr, orientation: Qt::Vertical, styleInfo); |
1140 | return QSizeF(q_totalBoxes[Qt::Horizontal].q_sizes(which), q_totalBoxes[Qt::Vertical].q_sizes(which)); |
1141 | } |
1142 | |
1143 | QLayoutPolicy::ControlTypes QGridLayoutEngine::controlTypes(LayoutSide side) const |
1144 | { |
1145 | Qt::Orientation orientation = (side == Top || side == Bottom) ? Qt::Vertical : Qt::Horizontal; |
1146 | int row = (side == Top || side == Left) ? effectiveFirstRow(orientation) |
1147 | : effectiveLastRow(orientation); |
1148 | QLayoutPolicy::ControlTypes result; |
1149 | |
1150 | for (int column = columnCount(orientation) - 1; column >= 0; --column) { |
1151 | if (QGridLayoutItem *item = itemAt(row, column, orientation)) |
1152 | result |= item->controlTypes(side); |
1153 | } |
1154 | return result; |
1155 | } |
1156 | |
1157 | void QGridLayoutEngine::transpose() |
1158 | { |
1159 | invalidate(); |
1160 | |
1161 | for (int i = q_items.size() - 1; i >= 0; --i) |
1162 | q_items.at(i)->transpose(); |
1163 | |
1164 | q_defaultSpacings.transpose(); |
1165 | q_infos.transpose(); |
1166 | |
1167 | regenerateGrid(); |
1168 | } |
1169 | |
1170 | void QGridLayoutEngine::setVisualDirection(Qt::LayoutDirection direction) |
1171 | { |
1172 | m_visualDirection = direction; |
1173 | } |
1174 | |
1175 | Qt::LayoutDirection QGridLayoutEngine::visualDirection() const |
1176 | { |
1177 | return m_visualDirection; |
1178 | } |
1179 | |
1180 | #ifdef QGRIDLAYOUTENGINE_DEBUG |
1181 | void QGridLayoutEngine::dump(int indent) const |
1182 | { |
1183 | qDebug("%*sEngine", indent, ""); |
1184 | |
1185 | qDebug("%*s Items (%lld)", indent, "", q_items.count()); |
1186 | int i; |
1187 | for (i = 0; i < q_items.count(); ++i) |
1188 | q_items.at(i)->dump(indent + 2); |
1189 | |
1190 | qDebug("%*s Grid (%d x %d)", indent, "", internalGridRowCount(), |
1191 | internalGridColumnCount()); |
1192 | for (int row = 0; row < internalGridRowCount(); ++row) { |
1193 | QString message = "[ "_L1; |
1194 | for (int column = 0; column < internalGridColumnCount(); ++column) { |
1195 | message += QString::number(q_items.indexOf(itemAt(row, column))).rightJustified(3); |
1196 | message += u' '; |
1197 | } |
1198 | message += u']'; |
1199 | qDebug("%*s %s", indent, "", qPrintable(message)); |
1200 | } |
1201 | |
1202 | if (q_defaultSpacings[Qt::Horizontal].value() >= 0.0 || q_defaultSpacings[Qt::Vertical].value() >= 0.0) |
1203 | qDebug("%*s Default spacings: %g %g", indent, "", |
1204 | q_defaultSpacings[Qt::Horizontal].value(), |
1205 | q_defaultSpacings[Qt::Vertical].value()); |
1206 | |
1207 | qDebug("%*s Column and row info", indent, ""); |
1208 | q_infos[Qt::Horizontal].dump(indent + 2); |
1209 | q_infos[Qt::Vertical].dump(indent + 2); |
1210 | |
1211 | qDebug("%*s Column and row data", indent, ""); |
1212 | q_columnData.dump(indent + 2); |
1213 | q_rowData.dump(indent + 2); |
1214 | |
1215 | qDebug("%*s Geometries output", indent, ""); |
1216 | QList<qreal> *cellPos = &q_yy; |
1217 | for (int pass = 0; pass < 2; ++pass) { |
1218 | QString message; |
1219 | for (i = 0; i < cellPos->count(); ++i) { |
1220 | message += (message.isEmpty() ? "["_L1: ", "_L1); |
1221 | message += QString::number(cellPos->at(i)); |
1222 | } |
1223 | message += u']'; |
1224 | qDebug("%*s %s %s", indent, "", (pass == 0 ? "rows:": "columns:"), qPrintable(message)); |
1225 | cellPos = &q_xx; |
1226 | } |
1227 | } |
1228 | #endif |
1229 | |
1230 | void QGridLayoutEngine::maybeExpandGrid(int row, int column, Qt::Orientation orientation) |
1231 | { |
1232 | invalidate(); // ### move out of here? |
1233 | |
1234 | if (orientation == Qt::Horizontal) |
1235 | qSwap(value1&: row, value2&: column); |
1236 | |
1237 | if (row < rowCount() && column < columnCount()) |
1238 | return; |
1239 | |
1240 | int oldGridRowCount = internalGridRowCount(); |
1241 | int oldGridColumnCount = internalGridColumnCount(); |
1242 | |
1243 | q_infos[Qt::Vertical].count = qMax(a: row + 1, b: rowCount()); |
1244 | q_infos[Qt::Horizontal].count = qMax(a: column + 1, b: columnCount()); |
1245 | |
1246 | int newGridRowCount = internalGridRowCount(); |
1247 | int newGridColumnCount = internalGridColumnCount(); |
1248 | |
1249 | int newGridSize = newGridRowCount * newGridColumnCount; |
1250 | if (newGridSize != q_grid.size()) { |
1251 | q_grid.resize(size: newGridSize); |
1252 | |
1253 | if (newGridColumnCount != oldGridColumnCount) { |
1254 | for (int i = oldGridRowCount - 1; i >= 1; --i) { |
1255 | for (int j = oldGridColumnCount - 1; j >= 0; --j) { |
1256 | int oldIndex = (i * oldGridColumnCount) + j; |
1257 | int newIndex = (i * newGridColumnCount) + j; |
1258 | |
1259 | Q_ASSERT(newIndex > oldIndex); |
1260 | q_grid[newIndex] = q_grid[oldIndex]; |
1261 | q_grid[oldIndex] = nullptr; |
1262 | } |
1263 | } |
1264 | } |
1265 | } |
1266 | } |
1267 | |
1268 | void QGridLayoutEngine::regenerateGrid() |
1269 | { |
1270 | q_grid.fill(t: nullptr); |
1271 | |
1272 | for (int i = q_items.size() - 1; i >= 0; --i) { |
1273 | QGridLayoutItem *item = q_items.at(i); |
1274 | |
1275 | for (int j = item->firstRow(); j <= item->lastRow(); ++j) { |
1276 | for (int k = item->firstColumn(); k <= item->lastColumn(); ++k) { |
1277 | setItemAt(row: j, column: k, item); |
1278 | } |
1279 | } |
1280 | } |
1281 | } |
1282 | |
1283 | void QGridLayoutEngine::setItemAt(int row, int column, QGridLayoutItem *item) |
1284 | { |
1285 | Q_ASSERT(row >= 0 && row < rowCount()); |
1286 | Q_ASSERT(column >= 0 && column < columnCount()); |
1287 | q_grid[(row * internalGridColumnCount()) + column] = item; |
1288 | } |
1289 | |
1290 | void QGridLayoutEngine::insertOrRemoveRows(int row, int delta, Qt::Orientation orientation) |
1291 | { |
1292 | int oldRowCount = rowCount(orientation); |
1293 | Q_ASSERT(uint(row) <= uint(oldRowCount)); |
1294 | |
1295 | invalidate(); |
1296 | |
1297 | // appending rows (or columns) is easy |
1298 | if (row == oldRowCount && delta > 0) { |
1299 | maybeExpandGrid(row: oldRowCount + delta - 1, column: -1, orientation); |
1300 | return; |
1301 | } |
1302 | |
1303 | q_infos[orientation].insertOrRemoveRows(row, delta); |
1304 | |
1305 | for (int i = q_items.size() - 1; i >= 0; --i) |
1306 | q_items.at(i)->insertOrRemoveRows(row, delta, orientation); |
1307 | |
1308 | q_grid.resize(size: internalGridRowCount() * internalGridColumnCount()); |
1309 | regenerateGrid(); |
1310 | } |
1311 | |
1312 | void QGridLayoutEngine::fillRowData(QGridLayoutRowData *rowData, |
1313 | const qreal *colPositions, const qreal *colSizes, |
1314 | Qt::Orientation orientation, |
1315 | const QAbstractLayoutStyleInfo *styleInfo) const |
1316 | { |
1317 | const int ButtonMask = QLayoutPolicy::ButtonBox | QLayoutPolicy::PushButton; |
1318 | const QGridLayoutRowInfo &rowInfo = q_infos[orientation]; |
1319 | const QGridLayoutRowInfo &columnInfo = q_infos.other(o: orientation); |
1320 | LayoutSide top = (orientation == Qt::Vertical) ? Top : Left; |
1321 | LayoutSide bottom = (orientation == Qt::Vertical) ? Bottom : Right; |
1322 | |
1323 | const QLayoutParameter<qreal> &defaultSpacing = q_defaultSpacings[orientation]; |
1324 | qreal innerSpacing = styleInfo->spacing(orientation); |
1325 | if (innerSpacing >= 0.0) |
1326 | defaultSpacing.setCachedValue(innerSpacing); |
1327 | |
1328 | for (int row = 0; row < rowInfo.count; ++row) { |
1329 | bool rowIsEmpty = true; |
1330 | bool rowIsIdenticalToPrevious = (row > 0); |
1331 | |
1332 | for (int column = 0; column < columnInfo.count; ++column) { |
1333 | QGridLayoutItem *item = itemAt(row, column, orientation); |
1334 | |
1335 | if (rowIsIdenticalToPrevious && item != itemAt(row: row - 1, column, orientation)) |
1336 | rowIsIdenticalToPrevious = false; |
1337 | |
1338 | if (item && !item->isEmpty()) |
1339 | rowIsEmpty = false; |
1340 | } |
1341 | |
1342 | if ((rowIsEmpty || rowIsIdenticalToPrevious) |
1343 | && rowInfo.spacings.value(i: row).isDefault() |
1344 | && rowInfo.stretches.value(i: row).isDefault() |
1345 | && rowInfo.boxes.value(i: row) == QGridLayoutBox()) |
1346 | rowData->ignore.setBit(i: row, val: true); |
1347 | |
1348 | if (rowInfo.spacings.value(i: row).isUser()) { |
1349 | rowData->spacings[row] = rowInfo.spacings.at(i: row).value(); |
1350 | } else if (!defaultSpacing.isDefault()) { |
1351 | rowData->spacings[row] = defaultSpacing.value(); |
1352 | } |
1353 | |
1354 | rowData->stretches[row] = rowInfo.stretches.value(i: row).value(); |
1355 | } |
1356 | |
1357 | struct RowAdHocData { |
1358 | int q_row; |
1359 | unsigned int q_hasButtons : 8; |
1360 | unsigned int q_hasNonButtons : 8; |
1361 | |
1362 | inline RowAdHocData() : q_row(-1), q_hasButtons(false), q_hasNonButtons(false) {} |
1363 | inline void init(int row) { |
1364 | this->q_row = row; |
1365 | q_hasButtons = false; |
1366 | q_hasNonButtons = false; |
1367 | } |
1368 | inline bool hasOnlyButtons() const { return q_hasButtons && !q_hasNonButtons; } |
1369 | inline bool hasOnlyNonButtons() const { return q_hasNonButtons && !q_hasButtons; } |
1370 | }; |
1371 | RowAdHocData lastRowAdHocData; |
1372 | RowAdHocData nextToLastRowAdHocData; |
1373 | RowAdHocData nextToNextToLastRowAdHocData; |
1374 | |
1375 | rowData->hasIgnoreFlag = false; |
1376 | for (int row = 0; row < rowInfo.count; ++row) { |
1377 | if (rowData->ignore.testBit(i: row)) |
1378 | continue; |
1379 | |
1380 | QGridLayoutBox &rowBox = rowData->boxes[row]; |
1381 | if (styleInfo->isWindow()) { |
1382 | nextToNextToLastRowAdHocData = nextToLastRowAdHocData; |
1383 | nextToLastRowAdHocData = lastRowAdHocData; |
1384 | lastRowAdHocData.init(row); |
1385 | } |
1386 | |
1387 | bool userRowStretch = rowInfo.stretches.value(i: row).isUser(); |
1388 | int &rowStretch = rowData->stretches[row]; |
1389 | |
1390 | bool hasIgnoreFlag = true; |
1391 | for (int column = 0; column < columnInfo.count; ++column) { |
1392 | QGridLayoutItem *item = itemAt(row, column, orientation); |
1393 | if (item) { |
1394 | int itemRow = item->firstRow(orientation); |
1395 | int itemColumn = item->firstColumn(orientation); |
1396 | |
1397 | if (itemRow == row && itemColumn == column) { |
1398 | int itemStretch = item->stretchFactor(orientation); |
1399 | if (!(item->sizePolicy(orientation) & QLayoutPolicy::IgnoreFlag)) |
1400 | hasIgnoreFlag = false; |
1401 | int itemRowSpan = item->rowSpan(orientation); |
1402 | |
1403 | int effectiveRowSpan = 1; |
1404 | for (int i = 1; i < itemRowSpan; ++i) { |
1405 | if (!rowData->ignore.testBit(i: i + itemRow)) |
1406 | ++effectiveRowSpan; |
1407 | } |
1408 | |
1409 | QGridLayoutBox *box; |
1410 | if (effectiveRowSpan == 1) { |
1411 | box = &rowBox; |
1412 | if (!userRowStretch && itemStretch != 0) |
1413 | rowStretch = qMax(a: rowStretch, b: itemStretch); |
1414 | } else { |
1415 | QGridLayoutMultiCellData &multiCell = |
1416 | rowData->multiCellMap[qMakePair(value1&: row, value2&: itemRowSpan)]; |
1417 | box = &multiCell.q_box; |
1418 | multiCell.q_stretch = itemStretch; |
1419 | } |
1420 | // Items with constraints need to be passed the constraint |
1421 | if (colSizes && colPositions && item->hasDynamicConstraint() && orientation == item->dynamicConstraintOrientation()) { |
1422 | /* Get the width of the item by summing up the widths of the columns that it spans. |
1423 | * We need to have already calculated the widths of the columns by calling |
1424 | * q_columns->calculateGeometries() before hand and passing the value in the colSizes |
1425 | * and colPositions parameters. |
1426 | * The variable name is still colSizes even when it actually has the row sizes |
1427 | */ |
1428 | qreal length = colSizes[item->lastColumn(orientation)]; |
1429 | if (item->columnSpan(orientation) != 1) |
1430 | length += colPositions[item->lastColumn(orientation)] - colPositions[item->firstColumn(orientation)]; |
1431 | box->combine(other: item->box(orientation, snapToPixelGrid: m_snapToPixelGrid, constraint: length)); |
1432 | } else { |
1433 | box->combine(other: item->box(orientation, snapToPixelGrid: m_snapToPixelGrid)); |
1434 | } |
1435 | |
1436 | if (effectiveRowSpan == 1) { |
1437 | QLayoutPolicy::ControlTypes controls = item->controlTypes(top); |
1438 | if (controls & ButtonMask) |
1439 | lastRowAdHocData.q_hasButtons = true; |
1440 | if (controls & ~ButtonMask) |
1441 | lastRowAdHocData.q_hasNonButtons = true; |
1442 | } |
1443 | } |
1444 | } |
1445 | } |
1446 | if (row < rowInfo.boxes.size()) { |
1447 | QGridLayoutBox rowBoxInfo = rowInfo.boxes.at(i: row); |
1448 | rowBoxInfo.normalize(); |
1449 | rowBox.q_minimumSize = qMax(a: rowBox.q_minimumSize, b: rowBoxInfo.q_minimumSize); |
1450 | rowBox.q_maximumSize = qMax(a: rowBox.q_minimumSize, |
1451 | b: (rowBoxInfo.q_maximumSize != FLT_MAX ? |
1452 | rowBoxInfo.q_maximumSize : rowBox.q_maximumSize)); |
1453 | rowBox.q_preferredSize = qBound(min: rowBox.q_minimumSize, |
1454 | val: qMax(a: rowBox.q_preferredSize, b: rowBoxInfo.q_preferredSize), |
1455 | max: rowBox.q_maximumSize); |
1456 | } |
1457 | if (hasIgnoreFlag) |
1458 | rowData->hasIgnoreFlag = true; |
1459 | } |
1460 | |
1461 | /* |
1462 | Heuristic: Detect button boxes that don't use QLayoutPolicy::ButtonBox. |
1463 | This is somewhat ad hoc but it usually does the trick. |
1464 | */ |
1465 | bool lastRowIsButtonBox = (lastRowAdHocData.hasOnlyButtons() |
1466 | && nextToLastRowAdHocData.hasOnlyNonButtons()); |
1467 | bool lastTwoRowsIsButtonBox = (lastRowAdHocData.hasOnlyButtons() |
1468 | && nextToLastRowAdHocData.hasOnlyButtons() |
1469 | && nextToNextToLastRowAdHocData.hasOnlyNonButtons() |
1470 | && orientation == Qt::Vertical); |
1471 | |
1472 | if (defaultSpacing.isDefault()) { |
1473 | int prevRow = -1; |
1474 | for (int row = 0; row < rowInfo.count; ++row) { |
1475 | if (rowData->ignore.testBit(i: row)) |
1476 | continue; |
1477 | |
1478 | if (prevRow != -1 && !rowInfo.spacings.value(i: prevRow).isUser()) { |
1479 | qreal &rowSpacing = rowData->spacings[prevRow]; |
1480 | for (int column = 0; column < columnInfo.count; ++column) { |
1481 | QGridLayoutItem *item1 = itemAt(row: prevRow, column, orientation); |
1482 | QGridLayoutItem *item2 = itemAt(row, column, orientation); |
1483 | |
1484 | if (item1 && item2 && item1 != item2) { |
1485 | QLayoutPolicy::ControlTypes controls1 = item1->controlTypes(bottom); |
1486 | QLayoutPolicy::ControlTypes controls2 = item2->controlTypes(top); |
1487 | |
1488 | if (controls2 & QLayoutPolicy::PushButton) { |
1489 | if ((row == nextToLastRowAdHocData.q_row && lastTwoRowsIsButtonBox) |
1490 | || (row == lastRowAdHocData.q_row && lastRowIsButtonBox)) { |
1491 | controls2 &= ~QLayoutPolicy::PushButton; |
1492 | controls2 |= QLayoutPolicy::ButtonBox; |
1493 | } |
1494 | } |
1495 | |
1496 | qreal spacing = styleInfo->combinedLayoutSpacing(controls1, controls2, |
1497 | orientation); |
1498 | if (orientation == Qt::Horizontal) { |
1499 | qreal width1 = rowData->boxes.at(i: prevRow).q_minimumSize; |
1500 | qreal width2 = rowData->boxes.at(i: row).q_minimumSize; |
1501 | QRectF rect1 = item1->geometryWithin(x: 0.0, y: 0.0, width: width1, FLT_MAX, rowDescent: -1.0, align: effectiveAlignment(layoutItem: item1), snapToPixelGrid: m_snapToPixelGrid); |
1502 | QRectF rect2 = item2->geometryWithin(x: 0.0, y: 0.0, width: width2, FLT_MAX, rowDescent: -1.0, align: effectiveAlignment(layoutItem: item2), snapToPixelGrid: m_snapToPixelGrid); |
1503 | spacing -= (width1 - (rect1.x() + rect1.width())) + rect2.x(); |
1504 | } else { |
1505 | const QGridLayoutBox &box1 = rowData->boxes.at(i: prevRow); |
1506 | const QGridLayoutBox &box2 = rowData->boxes.at(i: row); |
1507 | qreal height1 = box1.q_minimumSize; |
1508 | qreal height2 = box2.q_minimumSize; |
1509 | qreal rowDescent1 = fixedDescent(descent: box1.q_minimumDescent, |
1510 | ascent: box1.q_minimumAscent, targetSize: height1); |
1511 | qreal rowDescent2 = fixedDescent(descent: box2.q_minimumDescent, |
1512 | ascent: box2.q_minimumAscent, targetSize: height2); |
1513 | QRectF rect1 = item1->geometryWithin(x: 0.0, y: 0.0, FLT_MAX, height: height1, |
1514 | rowDescent: rowDescent1, align: effectiveAlignment(layoutItem: item1), snapToPixelGrid: m_snapToPixelGrid); |
1515 | QRectF rect2 = item2->geometryWithin(x: 0.0, y: 0.0, FLT_MAX, height: height2, |
1516 | rowDescent: rowDescent2, align: effectiveAlignment(layoutItem: item2), snapToPixelGrid: m_snapToPixelGrid); |
1517 | spacing -= (height1 - (rect1.y() + rect1.height())) + rect2.y(); |
1518 | } |
1519 | rowSpacing = qMax(a: spacing, b: rowSpacing); |
1520 | } |
1521 | } |
1522 | } |
1523 | prevRow = row; |
1524 | } |
1525 | } else if (lastRowIsButtonBox || lastTwoRowsIsButtonBox) { |
1526 | /* |
1527 | Even for styles that define a uniform spacing, we cheat a |
1528 | bit and use the window margin as the spacing. This |
1529 | significantly improves the look of dialogs. |
1530 | */ |
1531 | int prevRow = lastRowIsButtonBox ? nextToLastRowAdHocData.q_row |
1532 | : nextToNextToLastRowAdHocData.q_row; |
1533 | if (!defaultSpacing.isUser() && !rowInfo.spacings.value(i: prevRow).isUser()) { |
1534 | qreal windowMargin = styleInfo->windowMargin(orientation); |
1535 | qreal &rowSpacing = rowData->spacings[prevRow]; |
1536 | rowSpacing = qMax(a: windowMargin, b: rowSpacing); |
1537 | } |
1538 | } |
1539 | |
1540 | if (rowData->boxes.size() > 1 && |
1541 | ((orientation == Qt::Horizontal && m_uniformCellWidths) || |
1542 | (orientation == Qt::Vertical && m_uniformCellHeights))) { |
1543 | qreal averagePreferredSize = 0.; |
1544 | qreal minimumMaximumSize = std::numeric_limits<qreal>::max(); |
1545 | qreal maximumMinimumSize = 0.; |
1546 | for (const auto &box : rowData->boxes) { |
1547 | averagePreferredSize += box.q_preferredSize; |
1548 | minimumMaximumSize = qMin(a: minimumMaximumSize, b: box.q_maximumSize); |
1549 | maximumMinimumSize = qMax(a: maximumMinimumSize, b: box.q_minimumSize); |
1550 | } |
1551 | averagePreferredSize /= rowData->boxes.size(); |
1552 | minimumMaximumSize = qMax(a: minimumMaximumSize, b: maximumMinimumSize); |
1553 | averagePreferredSize = qBound(min: maximumMinimumSize, val: averagePreferredSize, max: minimumMaximumSize); |
1554 | for (auto &box : rowData->boxes) { |
1555 | box.q_preferredSize = averagePreferredSize; |
1556 | box.q_minimumSize = maximumMinimumSize; |
1557 | box.q_maximumSize = minimumMaximumSize; |
1558 | } |
1559 | } |
1560 | } |
1561 | |
1562 | void QGridLayoutEngine::ensureEffectiveFirstAndLastRows() const |
1563 | { |
1564 | if (q_cachedEffectiveFirstRows[Qt::Horizontal] == -1 && !q_items.isEmpty()) { |
1565 | int rowCount = this->rowCount(); |
1566 | int columnCount = this->columnCount(); |
1567 | |
1568 | q_cachedEffectiveFirstRows = {columnCount, rowCount}; |
1569 | q_cachedEffectiveLastRows = {-1, -1}; |
1570 | |
1571 | for (int i = q_items.size() - 1; i >= 0; --i) { |
1572 | const QGridLayoutItem *item = q_items.at(i); |
1573 | |
1574 | for (Qt::Orientation o : {Qt::Horizontal, Qt::Vertical}) { |
1575 | if (item->firstRow(orientation: o) < q_cachedEffectiveFirstRows[o]) |
1576 | q_cachedEffectiveFirstRows[o] = item->firstRow(orientation: o); |
1577 | if (item->lastRow(orientation: o) > q_cachedEffectiveLastRows[o]) |
1578 | q_cachedEffectiveLastRows[o] = item->lastRow(orientation: o); |
1579 | } |
1580 | } |
1581 | } |
1582 | } |
1583 | |
1584 | void QGridLayoutEngine::ensureColumnAndRowData(QGridLayoutRowData *rowData, QGridLayoutBox *totalBox, |
1585 | const qreal *colPositions, const qreal *colSizes, |
1586 | Qt::Orientation orientation, |
1587 | const QAbstractLayoutStyleInfo *styleInfo) const |
1588 | { |
1589 | const int cc = columnCount(orientation); |
1590 | |
1591 | const qreal constraint = (colPositions && colSizes && hasDynamicConstraint()) ? (colPositions[cc - 1] + colSizes[cc - 1]) : qreal(CachedWithNoConstraint); |
1592 | qreal &cachedConstraint = q_totalBoxCachedConstraints[orientation]; |
1593 | if (cachedConstraint == constraint) { |
1594 | if (totalBox != &q_totalBoxes[orientation]) |
1595 | *totalBox = q_totalBoxes[orientation]; |
1596 | return; |
1597 | } |
1598 | rowData->reset(count: rowCount(orientation)); |
1599 | fillRowData(rowData, colPositions, colSizes, orientation, styleInfo); |
1600 | const QGridLayoutRowInfo &rowInfo = q_infos[orientation]; |
1601 | rowData->distributeMultiCells(rowInfo, snapToPixelGrid: m_snapToPixelGrid); |
1602 | *totalBox = rowData->totalBox(start: 0, end: rowCount(orientation)); |
1603 | |
1604 | if (totalBox != &q_totalBoxes[orientation]) |
1605 | q_totalBoxes[orientation] = *totalBox; |
1606 | |
1607 | cachedConstraint = constraint; |
1608 | } |
1609 | |
1610 | /** |
1611 | returns false if the layout has contradicting constraints (i.e. some items with a horizontal |
1612 | constraint and other items with a vertical constraint) |
1613 | */ |
1614 | bool QGridLayoutEngine::ensureDynamicConstraint() const |
1615 | { |
1616 | if (q_cachedConstraintOrientation == UnknownConstraint) { |
1617 | for (int i = q_items.size() - 1; i >= 0; --i) { |
1618 | QGridLayoutItem *item = q_items.at(i); |
1619 | if (item->hasDynamicConstraint()) { |
1620 | Qt::Orientation itemConstraintOrientation = item->dynamicConstraintOrientation(); |
1621 | if (q_cachedConstraintOrientation == UnknownConstraint) { |
1622 | q_cachedConstraintOrientation = itemConstraintOrientation; |
1623 | } else if (q_cachedConstraintOrientation != itemConstraintOrientation) { |
1624 | q_cachedConstraintOrientation = UnfeasibleConstraint; |
1625 | qWarning(msg: "QGridLayoutEngine: Unfeasible, cannot mix horizontal and" |
1626 | " vertical constraint in the same layout"); |
1627 | return false; |
1628 | } |
1629 | } |
1630 | } |
1631 | if (q_cachedConstraintOrientation == UnknownConstraint) |
1632 | q_cachedConstraintOrientation = NoConstraint; |
1633 | } |
1634 | return true; |
1635 | } |
1636 | |
1637 | bool QGridLayoutEngine::hasDynamicConstraint() const |
1638 | { |
1639 | if (!ensureDynamicConstraint()) |
1640 | return false; |
1641 | return q_cachedConstraintOrientation != NoConstraint; |
1642 | } |
1643 | |
1644 | /* |
1645 | * return value is only valid if hasConstraint() returns \c true |
1646 | */ |
1647 | Qt::Orientation QGridLayoutEngine::constraintOrientation() const |
1648 | { |
1649 | (void)ensureDynamicConstraint(); |
1650 | return (Qt::Orientation)q_cachedConstraintOrientation; |
1651 | } |
1652 | |
1653 | void QGridLayoutEngine::ensureGeometries(const QSizeF &size, |
1654 | const QAbstractLayoutStyleInfo *styleInfo) const |
1655 | { |
1656 | if (q_cachedSize == size) |
1657 | return; |
1658 | |
1659 | q_cachedSize = size; |
1660 | |
1661 | q_xx.resize(size: columnCount()); |
1662 | q_widths.resize(size: columnCount()); |
1663 | q_yy.resize(size: rowCount()); |
1664 | q_heights.resize(size: rowCount()); |
1665 | q_descents.resize(size: rowCount()); |
1666 | |
1667 | if (constraintOrientation() != Qt::Horizontal) { |
1668 | //We might have items whose height depends on their width (HFW) |
1669 | ensureColumnAndRowData(rowData: &q_columnData, totalBox: &q_totalBoxes[Qt::Horizontal], colPositions: nullptr, colSizes: nullptr, orientation: Qt::Horizontal, styleInfo); |
1670 | //Calculate column widths and positions, and put results in q_xx.data() and q_widths.data() so that we can use this information as |
1671 | //constraints to find the row heights |
1672 | q_columnData.calculateGeometries(start: 0, end: columnCount(), targetSize: size.width(), positions: q_xx.data(), sizes: q_widths.data(), |
1673 | descents: nullptr, totalBox: q_totalBoxes[Qt::Horizontal], rowInfo: q_infos[Qt::Horizontal], snapToPixelGrid: m_snapToPixelGrid); |
1674 | ensureColumnAndRowData(rowData: &q_rowData, totalBox: &q_totalBoxes[Qt::Vertical], colPositions: q_xx.data(), colSizes: q_widths.data(), orientation: Qt::Vertical, styleInfo); |
1675 | //Calculate row heights and positions, and put results in q_yy.data() and q_heights.data() |
1676 | q_rowData.calculateGeometries(start: 0, end: rowCount(), targetSize: size.height(), positions: q_yy.data(), sizes: q_heights.data(), |
1677 | descents: q_descents.data(), totalBox: q_totalBoxes[Qt::Vertical], rowInfo: q_infos[Qt::Vertical], snapToPixelGrid: m_snapToPixelGrid); |
1678 | } else { |
1679 | //We have items whose width depends on their height (WFH) |
1680 | ensureColumnAndRowData(rowData: &q_rowData, totalBox: &q_totalBoxes[Qt::Vertical], colPositions: nullptr, colSizes: nullptr, orientation: Qt::Vertical, styleInfo); |
1681 | //Calculate row heights and positions, and put results in q_yy.data() and q_heights.data() so that we can use this information as |
1682 | //constraints to find the column widths |
1683 | q_rowData.calculateGeometries(start: 0, end: rowCount(), targetSize: size.height(), positions: q_yy.data(), sizes: q_heights.data(), |
1684 | descents: q_descents.data(), totalBox: q_totalBoxes[Qt::Vertical], rowInfo: q_infos[Qt::Vertical], snapToPixelGrid: m_snapToPixelGrid); |
1685 | ensureColumnAndRowData(rowData: &q_columnData, totalBox: &q_totalBoxes[Qt::Horizontal], colPositions: q_yy.data(), colSizes: q_heights.data(), orientation: Qt::Horizontal, styleInfo); |
1686 | //Calculate row heights and positions, and put results in q_yy.data() and q_heights.data() |
1687 | q_columnData.calculateGeometries(start: 0, end: columnCount(), targetSize: size.width(), positions: q_xx.data(), sizes: q_widths.data(), |
1688 | descents: nullptr, totalBox: q_totalBoxes[Qt::Horizontal], rowInfo: q_infos[Qt::Horizontal], snapToPixelGrid: m_snapToPixelGrid); |
1689 | } |
1690 | } |
1691 | |
1692 | QT_END_NAMESPACE |
1693 |
Definitions
- insertOrRemoveItems
- growthFactorBelowPreferredSize
- fixedDescent
- compare
- add
- combine
- normalize
- operator==
- reset
- distributeMultiCells
- qround
- calculateGeometries
- totalBox
- stealBox
- QGridLayoutItem
- firstRow
- firstColumn
- lastRow
- lastColumn
- rowSpan
- columnSpan
- setFirstRow
- setRowSpan
- stretchFactor
- setStretchFactor
- controlTypes
- box
- geometryWithin
- transpose
- insertOrRemoveRows
- effectiveMaxSize
- insertOrRemoveRows
- QGridLayoutEngine
- rowCount
- columnCount
- itemCount
- itemAt
- effectiveFirstRow
- effectiveLastRow
- setSpacing
- spacing
- setRowSpacing
- rowSpacing
- setRowStretchFactor
- rowStretchFactor
- setRowSizeHint
- rowSizeHint
- uniformCellWidths
- setUniformCellWidths
- uniformCellHeights
- setUniformCellHeights
- setRowAlignment
- rowAlignment
- effectiveAlignment
- insertItem
- addItem
- removeItem
- itemAt
- invalidate
- visualRect
- setGeometries
- cellRect
- sizeHint
- controlTypes
- transpose
- setVisualDirection
- visualDirection
- maybeExpandGrid
- regenerateGrid
- setItemAt
- insertOrRemoveRows
- fillRowData
- ensureEffectiveFirstAndLastRows
- ensureColumnAndRowData
- ensureDynamicConstraint
- hasDynamicConstraint
- constraintOrientation
Start learning QML with our Intro Training
Find out more