Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquicklayout.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6#include <QEvent>
7#include <QtCore/qcoreapplication.h>
8#include <QtCore/private/qnumeric_p.h>
9#include <QtCore/qstack.h>
10#include <QtCore/qmath.h>
11#include <QtQml/qqmlinfo.h>
12#include <limits>
13
14/*!
15 \qmltype Layout
16 //! \nativetype QQuickLayoutAttached
17 \inqmlmodule QtQuick.Layouts
18 \ingroup layouts
19 \brief Provides attached properties for items pushed onto a \l GridLayout,
20 \l RowLayout or \l ColumnLayout.
21
22 An object of type Layout is attached to children of the layout to provide layout specific
23 information about the item.
24 The properties of the attached object influence how the layout will arrange the items.
25
26 For instance, you can specify \l minimumWidth, \l preferredWidth, and
27 \l maximumWidth if the default values are not satisfactory.
28
29 When a layout is resized, items may grow or shrink. Due to this, items have a
30 \l{Layout::minimumWidth}{minimum size}, \l{Layout::preferredWidth}{preferred size} and a
31 \l{Layout::maximumWidth}{maximum size}.
32
33 If minimum size has not been explicitly specified on an item, the size is set to \c 0.
34 If maximum size has not been explicitly specified on an item, the size is set to
35 \c Number.POSITIVE_INFINITY.
36
37 For layouts, the implicit minimum and maximum sizes depend on the content of the layouts.
38
39 The \l fillWidth and \l fillHeight properties can either be \c true or \c false. If they are \c
40 false, the item's size will be fixed to its preferred size. Otherwise, it will grow or shrink
41 between its minimum and maximum size as the layout is resized. If there are multiple items
42 with \l fillWidth (or \l fillHeight) set to \c true, the layout will grow or shrink the items
43 relative to the ratio of their preferred size.
44
45 For more details on the layout algorithm, see also the \l {Qt Quick Layouts Overview}.
46
47 \note Do not bind to the x, y, width, or height properties of items in a layout,
48 as this would conflict with the goals of Layout, and can also cause binding loops.
49 The width and height properties are used by the layout engine to store the current
50 size of items as calculated from the minimum/preferred/maximum attached properties,
51 and can be ovewritten each time the items are laid out. Use
52 \l {Layout.preferredWidth} and \l {Layout.preferredHeight}, or \l {Item::}{implicitWidth}
53 and \l {Item::}{implicitHeight} to specify the preferred size of items.
54
55 \sa GridLayout
56 \sa RowLayout
57 \sa ColumnLayout
58*/
59
61
62Q_LOGGING_CATEGORY(lcQuickLayouts, "qt.quick.layouts")
63
64QQuickLayoutAttached::QQuickLayoutAttached(QObject *parent)
65 : QObject(parent),
66 m_minimumWidth(0),
67 m_minimumHeight(0),
68 m_preferredWidth(-1),
69 m_preferredHeight(-1),
70 m_maximumWidth(std::numeric_limits<qreal>::infinity()),
71 m_maximumHeight(std::numeric_limits<qreal>::infinity()),
72 m_defaultMargins(0),
73 m_fallbackWidth(-1),
74 m_fallbackHeight(-1),
75 m_row(-1),
76 m_column(-1),
77 m_rowSpan(1),
78 m_columnSpan(1),
79 m_fillWidth(false),
80 m_fillHeight(false),
81 m_ignored(false),
82 m_isFillWidthSet(false),
83 m_isFillHeightSet(false),
84 m_isUseDefaultSizePolicySet(false),
85 m_useDefaultSizePolicy(QQuickLayout::SizePolicyExplicit),
86 m_isMinimumWidthSet(false),
87 m_isMinimumHeightSet(false),
88 m_isMaximumWidthSet(false),
89 m_isMaximumHeightSet(false),
90 m_changesNotificationEnabled(true),
91 m_isMarginsSet(false),
92 m_isLeftMarginSet(false),
93 m_isTopMarginSet(false),
94 m_isRightMarginSet(false),
95 m_isBottomMarginSet(false),
96 m_isAlignmentSet(false),
97 m_horizontalStretch(-1),
98 m_verticalStretch(-1)
99{
100
101}
102
103/*!
104 \qmlattachedproperty real Layout::minimumWidth
105
106 This property holds the minimum width of an item in a layout.
107 The default value is the item's implicit minimum width.
108
109 If the item is a layout, the implicit minimum width will be the minimum width the layout can
110 have without any of its items shrinking below their minimum width.
111 The implicit minimum width for any other item is \c 0.
112
113 Setting this value to -1 will reset the width back to its implicit minimum width.
114
115
116 \sa preferredWidth
117 \sa maximumWidth
118*/
119void QQuickLayoutAttached::setMinimumWidth(qreal width)
120{
121 if (qt_is_nan(width))
122 return;
123 m_isMinimumWidthSet = width >= 0;
124 if (m_minimumWidth == width)
125 return;
126
127 m_minimumWidth = width;
128 invalidateItem();
129 emit minimumWidthChanged();
130}
131
132/*!
133 \qmlattachedproperty real Layout::minimumHeight
134
135 This property holds the minimum height of an item in a layout.
136 The default value is the item's implicit minimum height.
137
138 If the item is a layout, the implicit minimum height will be the minimum height the layout can
139 have without any of its items shrinking below their minimum height.
140 The implicit minimum height for any other item is \c 0.
141
142 Setting this value to -1 will reset the height back to its implicit minimum height.
143
144 \sa preferredHeight
145 \sa maximumHeight
146*/
147void QQuickLayoutAttached::setMinimumHeight(qreal height)
148{
149 if (qt_is_nan(height))
150 return;
151 m_isMinimumHeightSet = height >= 0;
152 if (m_minimumHeight == height)
153 return;
154
155 m_minimumHeight = height;
156 invalidateItem();
157 emit minimumHeightChanged();
158}
159
160/*!
161 \qmlattachedproperty real Layout::preferredWidth
162
163 This property holds the preferred width of an item in a layout.
164 If the preferred width is \c -1 it will be ignored, and the layout
165 will use \l{Item::implicitWidth}{implicitWidth} instead.
166 The default is \c -1.
167
168 \sa minimumWidth
169 \sa maximumWidth
170*/
171void QQuickLayoutAttached::setPreferredWidth(qreal width)
172{
173 if (qt_is_nan(width) || m_preferredWidth == width)
174 return;
175
176 m_preferredWidth = width;
177 invalidateItem();
178 emit preferredWidthChanged();
179}
180
181/*!
182 \qmlattachedproperty real Layout::preferredHeight
183
184 This property holds the preferred height of an item in a layout.
185 If the preferred height is \c -1 it will be ignored, and the layout
186 will use \l{Item::implicitHeight}{implicitHeight} instead.
187 The default is \c -1.
188
189 \sa minimumHeight
190 \sa maximumHeight
191*/
192void QQuickLayoutAttached::setPreferredHeight(qreal height)
193{
194 if (qt_is_nan(height) || m_preferredHeight == height)
195 return;
196
197 m_preferredHeight = height;
198 invalidateItem();
199 emit preferredHeightChanged();
200}
201
202/*!
203 \qmlattachedproperty real Layout::maximumWidth
204
205 This property holds the maximum width of an item in a layout.
206 The default value is the item's implicit maximum width.
207
208 If the item is a layout, the implicit maximum width will be the maximum width the layout can
209 have without any of its items growing beyond their maximum width.
210 The implicit maximum width for any other item is \c Number.POSITIVE_INFINITY.
211
212 Setting this value to \c -1 will reset the width back to its implicit maximum width.
213
214 \sa minimumWidth
215 \sa preferredWidth
216*/
217void QQuickLayoutAttached::setMaximumWidth(qreal width)
218{
219 if (qt_is_nan(width))
220 return;
221 m_isMaximumWidthSet = width >= 0;
222 if (m_maximumWidth == width)
223 return;
224
225 m_maximumWidth = width;
226 invalidateItem();
227 emit maximumWidthChanged();
228}
229
230/*!
231 \qmlattachedproperty real Layout::maximumHeight
232
233 The default value is the item's implicit maximum height.
234
235 If the item is a layout, the implicit maximum height will be the maximum height the layout can
236 have without any of its items growing beyond their maximum height.
237 The implicit maximum height for any other item is \c Number.POSITIVE_INFINITY.
238
239 Setting this value to \c -1 will reset the height back to its implicit maximum height.
240
241 \sa minimumHeight
242 \sa preferredHeight
243*/
244void QQuickLayoutAttached::setMaximumHeight(qreal height)
245{
246 if (qt_is_nan(height))
247 return;
248 m_isMaximumHeightSet = height >= 0;
249 if (m_maximumHeight == height)
250 return;
251
252 m_maximumHeight = height;
253 invalidateItem();
254 emit maximumHeightChanged();
255}
256
257void QQuickLayoutAttached::setMinimumImplicitSize(const QSizeF &sz)
258{
259 bool emitWidthChanged = false;
260 bool emitHeightChanged = false;
261 if (!m_isMinimumWidthSet && m_minimumWidth != sz.width()) {
262 m_minimumWidth = sz.width();
263 emitWidthChanged = true;
264 }
265 if (!m_isMinimumHeightSet && m_minimumHeight != sz.height()) {
266 m_minimumHeight = sz.height();
267 emitHeightChanged = true;
268 }
269 // Only invalidate the item once, and make sure we emit signal changed after the call to
270 // invalidateItem()
271 if (emitWidthChanged || emitHeightChanged) {
272 invalidateItem();
273 if (emitWidthChanged)
274 emit minimumWidthChanged();
275 if (emitHeightChanged)
276 emit minimumHeightChanged();
277 }
278}
279
280void QQuickLayoutAttached::setMaximumImplicitSize(const QSizeF &sz)
281{
282 bool emitWidthChanged = false;
283 bool emitHeightChanged = false;
284 if (!m_isMaximumWidthSet && m_maximumWidth != sz.width()) {
285 m_maximumWidth = sz.width();
286 emitWidthChanged = true;
287 }
288 if (!m_isMaximumHeightSet && m_maximumHeight != sz.height()) {
289 m_maximumHeight = sz.height();
290 emitHeightChanged = true;
291 }
292 // Only invalidate the item once, and make sure we emit changed signal after the call to
293 // invalidateItem()
294 if (emitWidthChanged || emitHeightChanged) {
295 invalidateItem();
296 if (emitWidthChanged)
297 emit maximumWidthChanged();
298 if (emitHeightChanged)
299 emit maximumHeightChanged();
300 }
301}
302
303/*!
304 \qmlattachedproperty bool Layout::fillWidth
305
306 If this property is \c true, the item will be as wide as possible while respecting
307 the given constraints. If the property is \c false, the item will have a fixed width
308 set to the preferred width.
309 The default depends on implicit (built-in) size policy of item.
310
311 \note By default, this property is \c true for layouts.
312
313 \sa fillHeight
314*/
315void QQuickLayoutAttached::setFillWidth(bool fill)
316{
317 bool oldFillWidth = fillWidth();
318 m_isFillWidthSet = true;
319 m_fillWidth = fill;
320 if (oldFillWidth != fill) {
321 invalidateItem();
322 emit fillWidthChanged();
323 }
324}
325
326/*!
327 \qmlattachedproperty bool Layout::fillHeight
328
329 If this property is \c true, the item will be as tall as possible while respecting
330 the given constraints. If the property is \c false, the item will have a fixed height
331 set to the preferred height.
332 The default depends on implicit (built-in) size policy of the item.
333
334 \note By default, this property is \c true for layouts.
335
336 \sa fillWidth
337*/
338void QQuickLayoutAttached::setFillHeight(bool fill)
339{
340 bool oldFillHeight = fillHeight();
341 m_isFillHeightSet = true;
342 m_fillHeight = fill;
343 if (oldFillHeight != fill) {
344 invalidateItem();
345 emit fillHeightChanged();
346 }
347}
348
349/*!
350 \qmlattachedproperty bool Layout::ignored
351 \since 6.13
352
353 If this property is \c true, the item is not managed by its parent layout.
354 The layout will not set the item's \c x, \c y, \c width or \c height, the
355 item does not contribute to the size hints of the layout, it does not occupy
356 a cell in a \l GridLayout, and it is not included in \l{StackLayout::count}
357 {StackLayout.count}.
358
359 This is useful for items that are declared as children of a layout, but
360 positioned by other means, such as decorations or overlays:
361
362 \qml
363 RowLayout {
364 Rectangle { Layout.fillWidth: true }
365 Rectangle {
366 Layout.ignored: true
367 anchors.right: parent.right
368 width: 16; height: 16
369 }
370 }
371 \endqml
372
373 Unlike items that are managed by a layout, an ignored item may use
374 \l{Item::anchors}{anchors} without producing a warning.
375
376 \note When an item becomes ignored, it keeps whatever geometry the layout
377 last assigned to it. The layout only stops updating that geometry; it does
378 not restore any binding that was overwritten while the item was managed. It
379 is up to you to position and size the item afterwards.
380
381 By default this property is \c false.
382*/
383void QQuickLayoutAttached::setIgnored(bool ignored)
384{
385 if (m_ignored == ignored)
386 return;
387 m_ignored = ignored;
388 if (QQuickLayout *layout = parentLayout())
389 layout->itemIgnoredChanged(item());
390 emit ignoredChanged();
391}
392
393/*!
394 \qmlattachedproperty enumeration Layout::useDefaultSizePolicy
395 \since 6.8
396
397 This property allows the user to configure the layout size policy at the component
398 level.
399
400 The default value will be inherited by querying the application attribute
401 \l Qt::AA_QtQuickUseDefaultSizePolicy. You can use this property to override that value.
402
403 \value Layout.SizePolicyImplicit
404 The item in the layout uses implicit or built-in size policy
405 \value Layout.SizePolicyExplicit
406 The item in the layout doesn't use implicit size policies.
407*/
408void QQuickLayoutAttached::setUseDefaultSizePolicy(QQuickLayout::SizePolicy sizePolicy)
409{
410 m_isUseDefaultSizePolicySet = true;
411 if (m_useDefaultSizePolicy != sizePolicy) {
412 m_useDefaultSizePolicy = sizePolicy;
413 emit useDefaultSizePolicyChanged();
414 }
415}
416
417/*!
418 \qmlattachedproperty int Layout::row
419
420 This property allows you to specify the row position of an item in a \l GridLayout.
421
422 If both \l column and this property are not set, it is up to the layout to assign a cell to the item.
423
424 The default value is \c 0.
425
426 \sa column
427 \sa rowSpan
428*/
429void QQuickLayoutAttached::setRow(int row)
430{
431 if (row >= 0 && row != m_row) {
432 m_row = row;
433 invalidateItem();
434 emit rowChanged();
435 }
436}
437
438/*!
439 \qmlattachedproperty int Layout::column
440
441 This property allows you to specify the column position of an item in a \l GridLayout.
442
443 If both \l row and this property are not set, it is up to the layout to assign a cell to the item.
444
445 The default value is \c 0.
446
447 \sa row
448 \sa columnSpan
449*/
450void QQuickLayoutAttached::setColumn(int column)
451{
452 if (column >= 0 && column != m_column) {
453 m_column = column;
454 invalidateItem();
455 emit columnChanged();
456 }
457}
458
459
460/*!
461 \qmlattachedproperty Qt.Alignment Layout::alignment
462
463 This property allows you to specify the alignment of an item within the cell(s) it occupies.
464
465 The default value is \c 0, which means it will be \c{Qt.AlignVCenter | Qt.AlignLeft}.
466 These defaults also apply if only a horizontal or vertical flag is specified:
467 if only a horizontal flag is specified, the default vertical flag will be
468 \c Qt.AlignVCenter, and if only a vertical flag is specified, the default
469 horizontal flag will be \c Qt.AlignLeft.
470
471 A valid alignment is a combination of the following flags:
472 \list
473 \li Qt::AlignLeft
474 \li Qt::AlignHCenter
475 \li Qt::AlignRight
476 \li Qt::AlignTop
477 \li Qt::AlignVCenter
478 \li Qt::AlignBottom
479 \li Qt::AlignBaseline
480 \endlist
481
482*/
483void QQuickLayoutAttached::setAlignment(Qt::Alignment align)
484{
485 m_isAlignmentSet = true;
486 if (align != m_alignment) {
487 m_alignment = align;
488 if (QQuickLayout *layout = parentLayout()) {
489 layout->setAlignment(item(), align);
490 invalidateItem();
491 }
492 emit alignmentChanged();
493 }
494}
495
496/*!
497 \qmlattachedproperty int Layout::horizontalStretchFactor
498
499 This property allows you to specify the horizontal stretch factor. By default, two identical
500 items arranged in a linear layout will have the same size, but if the first item has a
501 stretch factor of 1 and the second item has a stretch factor of 2, the first item will \e
502 aim to get 1/3 of the available space, and the second will \e aim to get 2/3 of the available
503 space. Note that, whether they become exactly 1/3 and 2/3 of the available space depends on
504 their size hints. This is because when e.g a horizontal layout is shown in its minimum width
505 all its child items will consequently also have their minimum width.
506
507 Likewise, when a horizontal layout has its preferred width, all child items will have their
508 preferred widths, and when a horizontal layout has its maximum width, all child items will have
509 their maximum widths. This strategy is applied regardless of what the individual stretch
510 factors are. As a consequence of this, stretch factors will only determine the growth rate of
511 child items \e between the preferredWidth and maximumWidth range.
512
513 The default value is \c -1, which means that no stretch factor is applied.
514
515 \note This requires that Layout::fillWidth is set to true
516
517 \since Qt 6.5
518
519 \sa verticalStretchFactor
520*/
521void QQuickLayoutAttached::setHorizontalStretchFactor(int factor)
522{
523 if (factor != m_horizontalStretch) {
524 m_horizontalStretch = factor;
525 if (QQuickLayout *layout = parentLayout()) {
526 layout->setStretchFactor(item(), factor, Qt::Horizontal);
527 invalidateItem();
528 }
529 emit horizontalStretchFactorChanged();
530 }
531}
532
533/*!
534 \qmlattachedproperty int Layout::verticalStretchFactor
535
536 This property allows you to specify the vertical stretch factor. By default, two identical
537 items arranged in a linear layout will have the same size, but if the first item has a
538 stretch factor of 1 and the second item has a stretch factor of 2, the first item will \e
539 aim to get 1/3 of the available space, and the second will \e aim to get 2/3 of the available
540 space. Note that, whether they become exactly 1/3 and 2/3 of the available space depends on
541 their size hints. This is because when e.g a vertical layout is shown in its minimum height
542 all its child items will consequently also have their minimum height.
543
544 Likewise, when a vertical layout has its preferred height, all child items will have their
545 preferred heights, and when a vertical layout has its maximum height, all child items will have
546 their maximum heights. This strategy is applied regardless of what the individual stretch
547 factors are. As a consequence of this, stretch factors will only determine the growth rate of
548 child items \e between the preferredHeight and maximumHeight range.
549
550 The default value is \c -1, which means that no stretch factor is applied.
551
552 \note This requires that Layout::fillHeight is set to true
553
554 \since Qt 6.5
555
556 \sa horizontalStretchFactor
557*/
558void QQuickLayoutAttached::setVerticalStretchFactor(int factor)
559{
560 if (factor != m_verticalStretch) {
561 m_verticalStretch = factor;
562 if (QQuickLayout *layout = parentLayout()) {
563 layout->setStretchFactor(item(), factor, Qt::Vertical);
564 invalidateItem();
565 }
566 emit verticalStretchFactorChanged();
567 }
568}
569
570/*!
571 \qmlattachedproperty real Layout::margins
572
573 Sets the margins outside of an item to all have the same value. The item
574 itself does not evaluate its own margins. It is the parent's responsibility
575 to decide if it wants to evaluate the margins.
576
577 Specifically, margins are only evaluated by ColumnLayout, RowLayout,
578 GridLayout, and other layout-like containers, such as SplitView, where the
579 effective cell size of an item will be increased as the margins are
580 increased.
581
582 Therefore, if an item with margins is a child of another \c Item, its
583 position, size and implicit size will remain unchanged.
584
585 Combining margins with alignment will align the item \e including its
586 margins. For instance, a vertically-centered Item with a top margin of \c 1
587 and a bottom margin of \c 9 will cause the Items effective alignment within
588 the cell to be 4 pixels above the center.
589
590 The default value is \c 0.
591
592 \sa leftMargin
593 \sa topMargin
594 \sa rightMargin
595 \sa bottomMargin
596
597 \since QtQuick.Layouts 1.2
598*/
599void QQuickLayoutAttached::setMargins(qreal m)
600{
601 m_isMarginsSet = true;
602 if (m == m_defaultMargins)
603 return;
604
605 m_defaultMargins = m;
606 invalidateItem();
607 if (!m_isLeftMarginSet && m_margins.left() != m)
608 emit leftMarginChanged();
609 if (!m_isTopMarginSet && m_margins.top() != m)
610 emit topMarginChanged();
611 if (!m_isRightMarginSet && m_margins.right() != m)
612 emit rightMarginChanged();
613 if (!m_isBottomMarginSet && m_margins.bottom() != m)
614 emit bottomMarginChanged();
615 emit marginsChanged();
616}
617
618/*!
619 \qmlattachedproperty real Layout::leftMargin
620
621 Specifies the left margin outside of an item.
622 If the value is not set, it will use the value from \l margins.
623
624 \sa margins
625
626 \since QtQuick.Layouts 1.2
627*/
628void QQuickLayoutAttached::setLeftMargin(qreal m)
629{
630 const bool changed = leftMargin() != m;
631 m_margins.setLeft(m);
632 m_isLeftMarginSet = true;
633 if (changed) {
634 invalidateItem();
635 emit leftMarginChanged();
636 }
637}
638
639void QQuickLayoutAttached::resetLeftMargin()
640{
641 const bool changed = m_isLeftMarginSet && (m_defaultMargins != m_margins.left());
642 m_isLeftMarginSet = false;
643 if (changed) {
644 invalidateItem();
645 emit leftMarginChanged();
646 }
647}
648
649/*!
650 \qmlattachedproperty real Layout::topMargin
651
652 Specifies the top margin outside of an item.
653 If the value is not set, it will use the value from \l margins.
654
655 \sa margins
656
657 \since QtQuick.Layouts 1.2
658*/
659void QQuickLayoutAttached::setTopMargin(qreal m)
660{
661 const bool changed = topMargin() != m;
662 m_margins.setTop(m);
663 m_isTopMarginSet = true;
664 if (changed) {
665 invalidateItem();
666 emit topMarginChanged();
667 }
668}
669
670void QQuickLayoutAttached::resetTopMargin()
671{
672 const bool changed = m_isTopMarginSet && (m_defaultMargins != m_margins.top());
673 m_isTopMarginSet = false;
674 if (changed) {
675 invalidateItem();
676 emit topMarginChanged();
677 }
678}
679
680/*!
681 \qmlattachedproperty real Layout::rightMargin
682
683 Specifies the right margin outside of an item.
684 If the value is not set, it will use the value from \l margins.
685
686 \sa margins
687
688 \since QtQuick.Layouts 1.2
689*/
690void QQuickLayoutAttached::setRightMargin(qreal m)
691{
692 const bool changed = rightMargin() != m;
693 m_margins.setRight(m);
694 m_isRightMarginSet = true;
695 if (changed) {
696 invalidateItem();
697 emit rightMarginChanged();
698 }
699}
700
701void QQuickLayoutAttached::resetRightMargin()
702{
703 const bool changed = m_isRightMarginSet && (m_defaultMargins != m_margins.right());
704 m_isRightMarginSet = false;
705 if (changed) {
706 invalidateItem();
707 emit rightMarginChanged();
708 }
709}
710
711/*!
712 \qmlattachedproperty real Layout::bottomMargin
713
714 Specifies the bottom margin outside of an item.
715 If the value is not set, it will use the value from \l margins.
716
717 \sa margins
718
719 \since QtQuick.Layouts 1.2
720*/
721void QQuickLayoutAttached::setBottomMargin(qreal m)
722{
723 const bool changed = bottomMargin() != m;
724 m_margins.setBottom(m);
725 m_isBottomMarginSet = true;
726 if (changed) {
727 invalidateItem();
728 emit bottomMarginChanged();
729 }
730}
731
732void QQuickLayoutAttached::resetBottomMargin()
733{
734 const bool changed = m_isBottomMarginSet && (m_defaultMargins != m_margins.bottom());
735 m_isBottomMarginSet = false;
736 if (changed) {
737 invalidateItem();
738 emit bottomMarginChanged();
739 }
740}
741
742
743/*!
744 \qmlattachedproperty int Layout::rowSpan
745
746 This property allows you to specify the row span of an item in a \l GridLayout.
747
748 The default value is \c 1.
749
750 \sa columnSpan
751 \sa row
752*/
753void QQuickLayoutAttached::setRowSpan(int span)
754{
755 if (span != m_rowSpan) {
756 m_rowSpan = span;
757 invalidateItem();
758 emit rowSpanChanged();
759 }
760}
761
762
763/*!
764 \qmlattachedproperty int Layout::columnSpan
765
766 This property allows you to specify the column span of an item in a \l GridLayout.
767
768 The default value is \c 1.
769
770 \sa rowSpan
771 \sa column
772*/
773void QQuickLayoutAttached::setColumnSpan(int span)
774{
775 if (span != m_columnSpan) {
776 m_columnSpan = span;
777 invalidateItem();
778 emit columnSpanChanged();
779 }
780}
781
782
783qreal QQuickLayoutAttached::sizeHint(Qt::SizeHint which, Qt::Orientation orientation) const
784{
785 qreal result = 0;
786 if (QQuickLayout *layout = qobject_cast<QQuickLayout *>(item())) {
787 const QSizeF sz = layout->sizeHint(which);
788 result = (orientation == Qt::Horizontal ? sz.width() : sz.height());
789 } else {
790 if (which == Qt::MaximumSize)
791 result = std::numeric_limits<qreal>::infinity();
792 }
793 return result;
794}
795
796void QQuickLayoutAttached::invalidateItem()
797{
798 qCDebug(lcQuickLayouts) << "QQuickLayoutAttached::invalidateItem";
799 if (QQuickLayout *layout = parentLayout()) {
800 layout->invalidate(item());
801 }
802}
803
804QQuickLayout *QQuickLayoutAttached::parentLayout() const
805{
806 QQuickItem *parentItem = item();
807 if (parentItem) {
808 parentItem = parentItem->parentItem();
809 return qobject_cast<QQuickLayout *>(parentItem);
810 } else {
811 qmlWarning(parent()) << "Layout attached property must be attached to an object deriving from Item";
812 }
813 return nullptr;
814}
815
816QQuickItem *QQuickLayoutAttached::item() const
817{
818 return qobject_cast<QQuickItem *>(parent());
819}
820
821void QQuickLayoutPrivate::applySizeHints() const
822{
823 Q_Q(const QQuickLayout);
824
825 QQuickLayout *that = const_cast<QQuickLayout*>(q);
826 QQuickLayoutAttached *info = attachedLayoutObject(that, true);
827
828 const QSizeF min = q->sizeHint(Qt::MinimumSize);
829 const QSizeF max = q->sizeHint(Qt::MaximumSize);
830 const QSizeF pref = q->sizeHint(Qt::PreferredSize);
831 info->setMinimumImplicitSize(min);
832 info->setMaximumImplicitSize(max);
833 that->setImplicitSize(pref.width(), pref.height());
834}
835
836QQuickLayout::QQuickLayout(QQuickLayoutPrivate &dd, QQuickItem *parent)
837 : QQuickItem(dd, parent)
838 , m_inUpdatePolish(false)
839 , m_polishInsideUpdatePolish(0)
840{
841}
842
844 QQuickItemPrivate::SiblingOrder
845 | QQuickItemPrivate::ImplicitWidth
846 | QQuickItemPrivate::ImplicitHeight
847 | QQuickItemPrivate::Destroyed
848 | QQuickItemPrivate::Visibility;
849
850QQuickLayout::~QQuickLayout()
851{
852 d_func()->m_isReady = false;
853
854 const auto childItems = d_func()->childItems;
855 for (QQuickItem *child : childItems)
856 QQuickItemPrivate::get(child)->removeItemChangeListener(this, changeTypes);
857}
858
859QQuickLayoutAttached *QQuickLayout::qmlAttachedProperties(QObject *object)
860{
861 return new QQuickLayoutAttached(object);
862}
863
864void QQuickLayout::updatePolish()
865{
866 qCDebug(lcQuickLayouts) << "updatePolish() ENTERING" << this;
867 m_inUpdatePolish = true;
868
869 // Might have become "undirty" before we reach this updatePolish()
870 // (e.g. if somebody queried for implicitWidth it will immediately
871 // calculate size hints)
872 // Note that we need to call ensureLayoutItemsUpdated() *before* we query width() and height(),
873 // because width()/height() might return their implicitWidth/implicitHeight (e.g. for a layout
874 // with no explicitly specified size, (nor anchors.fill: parent))
875 ensureLayoutItemsUpdated(QQuickLayout::ApplySizeHints | QQuickLayout::Recursive);
876 rearrange(QSizeF(width(), height()));
877 m_inUpdatePolish = false;
878 qCDebug(lcQuickLayouts) << "updatePolish() LEAVING" << this;
879}
880
881void QQuickLayout::componentComplete()
882{
883 Q_D(QQuickLayout);
884 d->m_disableRearrange = true;
885 QQuickItem::componentComplete(); // will call our geometryChange(), (where isComponentComplete() == true)
886 d->m_disableRearrange = false;
887 d->m_isReady = true;
888}
889
890void QQuickLayout::maybeSubscribeToBaseLineOffsetChanges(QQuickItem *item)
891{
892 QQuickLayoutAttached *info = attachedLayoutObject(item, false);
893 if (info) {
894 if (info->alignment() == Qt::AlignBaseline && static_cast<QQuickLayout*>(item->parentItem()) == this) {
895 qmlobject_connect(item, QQuickItem, SIGNAL(baselineOffsetChanged(qreal)), this, QQuickLayout, SLOT(invalidateSenderItem()));
896 } else {
897 qmlobject_disconnect(item, QQuickItem, SIGNAL(baselineOffsetChanged(qreal)), this, QQuickLayout, SLOT(invalidateSenderItem()));
898 }
899 }
900}
901
902void QQuickLayout::invalidate(QQuickItem * /*childItem*/)
903{
904 Q_D(QQuickLayout);
905 if (invalidated())
906 return;
907
908 qCDebug(lcQuickLayouts) << "QQuickLayout::invalidate()" << this;
909 d->m_dirty = true;
910 d->m_dirtyArrangement = true;
911
912 if (!qobject_cast<QQuickLayout *>(parentItem())) {
913 polish();
914
915 if (m_inUpdatePolish) {
916 if (++m_polishInsideUpdatePolish > 2)
917 // allow at most two consecutive loops in order to respond to height-for-width
918 // (e.g QQuickText changes implicitHeight when its width gets changed)
919 qCDebug(lcQuickLayouts) << "Layout polish loop detected for " << this
920 << ". The polish request will still be scheduled.";
921 } else {
922 m_polishInsideUpdatePolish = 0;
923 }
924 }
925}
926
927/*!
928 \brief QQuickLayout::itemIgnoredChanged
929 \internal
930
931 Called when the Layout.ignored property of \a childItem changed. The default
932 implementation just invalidates the layout, which is sufficient for layouts
933 that rebuild their items from scratch in updateLayoutItems().
934*/
935void QQuickLayout::itemIgnoredChanged(QQuickItem *childItem)
936{
937 if (!isReady())
938 return;
939 invalidate(childItem);
940}
941
942bool QQuickLayout::shouldIgnoreItem(QQuickItem *child) const
943{
944 if (isItemIgnored(child))
945 return true;
946 QQuickItemPrivate *childPrivate = QQuickItemPrivate::get(child);
947 bool ignoreItem = !childPrivate->explicitVisible;
948 if (!ignoreItem && childPrivate->isTransparentForPositioner())
949 ignoreItem = true;
950 return ignoreItem;
951}
952
953void QQuickLayout::checkAnchors(QQuickItem *item) const
954{
955 QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors;
956 if (anchors && anchors->activeDirections())
957 qmlWarning(item) << "Detected anchors on an item that is managed by a layout. This is undefined behavior; use Layout.alignment instead.";
958}
959
960void QQuickLayout::ensureLayoutItemsUpdated(EnsureLayoutItemsUpdatedOptions options) const
961{
962 Q_D(const QQuickLayout);
963 if (!invalidated())
964 return;
965 qCDebug(lcQuickLayouts) << "ENTER QQuickLayout::ensureLayoutItemsUpdated()" << this << options;
966 QQuickLayoutPrivate *priv = const_cast<QQuickLayoutPrivate*>(d);
967
968 // breadth-first
969 // must update the root first, and continue towards the leaf nodes.
970 // Otherwise, we wouldn't know which children to traverse to
971 const_cast<QQuickLayout*>(this)->updateLayoutItems();
972
973 // make invalidate() return true
974 d->m_dirty = false;
975
976 if (options & Recursive) {
977 for (int i = 0; i < itemCount(); ++i) {
978 QQuickItem *itm = itemAt(i);
979 if (QQuickLayout *lay = qobject_cast<QQuickLayout*>(itm)) {
980 lay->ensureLayoutItemsUpdated(options);
981 }
982 }
983 }
984
985 // size hints are updated depth-first (parent size hints depends on their childrens size hints)
986 if (options & ApplySizeHints)
987 priv->applySizeHints();
988 qCDebug(lcQuickLayouts) << "LEAVE QQuickLayout::ensureLayoutItemsUpdated()" << this;
989}
990
991
992void QQuickLayout::itemChange(ItemChange change, const ItemChangeData &value)
993{
994 if (change == ItemChildAddedChange) {
995 Q_D(QQuickLayout);
996 QQuickItem *item = value.item;
997 maybeSubscribeToBaseLineOffsetChanges(item);
998 QQuickItemPrivate::get(item)->addItemChangeListener(this, changeTypes);
999 d->m_hasItemChangeListeners = true;
1000 qCDebug(lcQuickLayouts) << "ChildAdded" << item;
1001 if (isReady())
1002 invalidate();
1003 } else if (change == ItemChildRemovedChange) {
1004 QQuickItem *item = value.item;
1005 maybeSubscribeToBaseLineOffsetChanges(item);
1006 QQuickItemPrivate::get(item)->removeItemChangeListener(this, changeTypes);
1007 qCDebug(lcQuickLayouts) << "ChildRemoved" << item;
1008 if (isReady())
1009 invalidate();
1010 }
1011 QQuickItem::itemChange(change, value);
1012}
1013
1014void QQuickLayout::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
1015{
1016 Q_D(QQuickLayout);
1017 qCDebug(lcQuickLayouts) << "QQuickLayout::geometryChange"
1018 << oldGeometry << "-->" << newGeometry;
1019
1020 QQuickItem::geometryChange(newGeometry, oldGeometry);
1021
1022 if ((invalidated() && !qobject_cast<QQuickLayout *>(parentItem())) ||
1023 d->m_disableRearrange || !isReady())
1024 return;
1025
1026 // The geometryChange call above might recursively update the
1027 // geometry of this layout, via item change listeners, in which
1028 // case the recursive call has already rearranged the layout for
1029 // the new size. We don't want to rearrange here based on the old
1030 // 'new geometry', as that would revert the most up to date layout.
1031 const qreal w = d->width.valueBypassingBindings();
1032 const qreal h = d->height.valueBypassingBindings();
1033 const QSizeF currentSize(w, h);
1034 if (currentSize != newGeometry.size()) {
1035 qCDebug(lcQuickLayouts) << "QQuickItem::geometryChange resulted"
1036 << "in size change from" << newGeometry.size() << "to"
1037 << currentSize << "; layout should already be up to date.";
1038 return;
1039 }
1040
1041 rearrange(newGeometry.size());
1042}
1043
1044void QQuickLayout::invalidateSenderItem()
1045{
1046 if (!isReady())
1047 return;
1048 QQuickItem *item = static_cast<QQuickItem *>(sender());
1049 Q_ASSERT(item);
1050 invalidate(item);
1051}
1052
1053bool QQuickLayout::isReady() const
1054{
1055 return d_func()->m_isReady;
1056}
1057
1058/*!
1059 * \brief QQuickLayout::deactivateRecur
1060 * \internal
1061 *
1062 * Call this from the dtor of the top-level layout.
1063 * Otherwise, it will trigger lots of unneeded item change listeners (itemVisibleChanged()) for all its descendants
1064 * that will have its impact thrown away.
1065 */
1066void QQuickLayout::deactivateRecur()
1067{
1068 if (d_func()->m_hasItemChangeListeners) {
1069 ensureLayoutItemsUpdated();
1070 for (int i = 0; i < itemCount(); ++i) {
1071 QQuickItem *item = itemAt(i);
1072 // When deleting a layout with children, there is no reason for the children to inform the layout that their
1073 // e.g. visibility got changed. The layout already knows that all its children will eventually become invisible, so
1074 // we therefore remove its change listener.
1075 QQuickItemPrivate::get(item)->removeItemChangeListener(this, changeTypes);
1076 if (QQuickLayout *layout = qobject_cast<QQuickLayout*>(item))
1077 layout->deactivateRecur();
1078 }
1079 d_func()->m_hasItemChangeListeners = false;
1080 }
1081}
1082
1083bool QQuickLayout::invalidated() const
1084{
1085 return d_func()->m_dirty;
1086}
1087
1088bool QQuickLayout::invalidatedArrangement() const
1089{
1090 return d_func()->m_dirtyArrangement;
1091}
1092
1093bool QQuickLayout::isMirrored() const
1094{
1095 return d_func()->isMirrored();
1096}
1097
1098void QQuickLayout::itemSiblingOrderChanged(QQuickItem *item)
1099{
1100 Q_UNUSED(item);
1101 invalidate();
1102}
1103
1104void QQuickLayout::itemImplicitWidthChanged(QQuickItem *item)
1105{
1106 if (!isReady())
1107 return;
1108 invalidate(item);
1109}
1110
1111void QQuickLayout::itemImplicitHeightChanged(QQuickItem *item)
1112{
1113 if (!isReady())
1114 return;
1115 invalidate(item);
1116}
1117
1118void QQuickLayout::itemDestroyed(QQuickItem *item)
1119{
1120 Q_UNUSED(item);
1121}
1122
1123void QQuickLayout::itemVisibilityChanged(QQuickItem *item)
1124{
1125 Q_UNUSED(item);
1126}
1127
1128void QQuickLayout::rearrange(const QSizeF &/*size*/)
1129{
1130 d_func()->m_dirtyArrangement = false;
1131}
1132
1133
1134/*
1135 The layout engine assumes:
1136 1. minimum <= preferred <= maximum
1137 2. descent is within minimum and maximum bounds (### verify)
1138
1139 This function helps to ensure that by the following rules (in the following order):
1140 1. If minimum > maximum, set minimum = maximum
1141 2. Clamp preferred to be between the [minimum,maximum] range.
1142 3. If descent > minimum, set descent = minimum (### verify if this is correct, it might
1143 need some refinements to multiline texts)
1144
1145 If any values are "not set" (i.e. negative), they will be left untouched, so that we
1146 know which values needs to be fetched from the implicit hints (not user hints).
1147 */
1148static void normalizeHints(qreal &minimum, qreal &preferred, qreal &maximum, qreal &descent)
1149{
1150 if (minimum >= 0 && maximum >= 0 && minimum > maximum)
1151 minimum = maximum;
1152
1153 if (preferred >= 0) {
1154 if (minimum >= 0 && preferred < minimum) {
1155 preferred = minimum;
1156 } else if (maximum >= 0 && preferred > maximum) {
1157 preferred = maximum;
1158 }
1159 }
1160
1161 if (minimum >= 0 && descent > minimum)
1162 descent = minimum;
1163}
1164
1165static void boundSize(QSizeF &result, const QSizeF &size)
1166{
1167 if (size.width() >= 0 && size.width() < result.width())
1168 result.setWidth(size.width());
1169 if (size.height() >= 0 && size.height() < result.height())
1170 result.setHeight(size.height());
1171}
1172
1173static void expandSize(QSizeF &result, const QSizeF &size)
1174{
1175 if (size.width() >= 0 && size.width() > result.width())
1176 result.setWidth(size.width());
1177 if (size.height() >= 0 && size.height() > result.height())
1178 result.setHeight(size.height());
1179}
1180
1181static inline void combineHints(qreal &current, qreal fallbackHint)
1182{
1183 if (current < 0)
1184 current = fallbackHint;
1185}
1186
1187static inline void combineSize(QSizeF &result, const QSizeF &fallbackSize)
1188{
1189 combineHints(result.rwidth(), fallbackSize.width());
1190 combineHints(result.rheight(), fallbackSize.height());
1191}
1192
1193static inline void combineImplicitHints(QQuickLayoutAttached *info, Qt::SizeHint which, QSizeF *size)
1194{
1195 if (!info) return;
1196
1197 Q_ASSERT(which == Qt::MinimumSize || which == Qt::MaximumSize);
1198
1199 const QSizeF constraint(which == Qt::MinimumSize
1200 ? QSizeF(info->minimumWidth(), info->minimumHeight())
1201 : QSizeF(info->maximumWidth(), info->maximumHeight()));
1202
1203 if (!info->isExtentExplicitlySet(Qt::Horizontal, which))
1204 combineHints(size->rwidth(), constraint.width());
1205 if (!info->isExtentExplicitlySet(Qt::Vertical, which))
1206 combineHints(size->rheight(), constraint.height());
1207}
1208
1210
1211/*!
1212 \internal
1213 Note: Can potentially return the attached QQuickLayoutAttached object through \a attachedInfo.
1214
1215 It is like this is because it enables it to be reused.
1216
1217 The goal of this function is to return the effective minimum, preferred and maximum size hints
1218 that the layout will use for this item.
1219 This function takes care of gathering all explicitly set size hints, normalizes them so
1220 that min < pref < max.
1221 Further, the hints _not_explicitly_ set will then be initialized with the implicit size hints,
1222 which is usually derived from the content of the layouts (or items).
1223
1224 The following table illustrates the preference of the properties used for measuring layout
1225 items. If present, the USER properties will be preferred. If USER properties are not present,
1226 the HINT properties will be preferred. Finally, the FALLBACK properties will be used as an
1227 ultimate fallback.
1228
1229 Note that one can query if the value of Layout.minimumWidth or Layout.maximumWidth has been
1230 explicitly or implicitly set with QQuickLayoutAttached::isExtentExplicitlySet(). This
1231 determines if it should be used as a USER or as a HINT value.
1232
1233 Fractional size hints will be ceiled to the closest integer. This is in order to give some
1234 slack when the items are snapped to the pixel grid.
1235
1236 | *Minimum* | *Preferred* | *Maximum* |
1237+----------------+----------------------+-----------------------+--------------------------+
1238|USER (explicit) | Layout.minimumWidth | Layout.preferredWidth | Layout.maximumWidth |
1239|HINT (implicit) | Layout.minimumWidth | implicitWidth | Layout.maximumWidth |
1240|FALLBACK | 0 | width | Number.POSITIVE_INFINITY |
1241+----------------+----------------------+-----------------------+--------------------------+
1242 */
1243void QQuickLayout::effectiveSizeHints_helper(QQuickItem *item, QSizeF *cachedSizeHints, QQuickLayoutAttached **attachedInfo, bool useFallbackToWidthOrHeight)
1244{
1245 for (int i = 0; i < Qt::NSizeHints; ++i)
1246 cachedSizeHints[i] = QSizeF();
1247 QQuickLayoutAttached *info = attachedLayoutObject(item, false);
1248 // First, retrieve the user-specified hints from the attached "Layout." properties
1249 if (info) {
1250 struct Getters {
1251 SizeGetter call[NSizes];
1252 };
1253
1254 static Getters horGetters = {
1255 {&QQuickLayoutAttached::minimumWidth, &QQuickLayoutAttached::preferredWidth, &QQuickLayoutAttached::maximumWidth},
1256 };
1257
1258 static Getters verGetters = {
1259 {&QQuickLayoutAttached::minimumHeight, &QQuickLayoutAttached::preferredHeight, &QQuickLayoutAttached::maximumHeight}
1260 };
1261 for (int i = 0; i < NSizes; ++i) {
1262 SizeGetter getter = horGetters.call[i];
1263 Q_ASSERT(getter);
1264
1265 if (info->isExtentExplicitlySet(Qt::Horizontal, (Qt::SizeHint)i))
1266 cachedSizeHints[i].setWidth((info->*getter)());
1267
1268 getter = verGetters.call[i];
1269 Q_ASSERT(getter);
1270 if (info->isExtentExplicitlySet(Qt::Vertical, (Qt::SizeHint)i))
1271 cachedSizeHints[i].setHeight((info->*getter)());
1272 }
1273 }
1274
1275 QSizeF &minS = cachedSizeHints[Qt::MinimumSize];
1276 QSizeF &prefS = cachedSizeHints[Qt::PreferredSize];
1277 QSizeF &maxS = cachedSizeHints[Qt::MaximumSize];
1278 QSizeF &descentS = cachedSizeHints[Qt::MinimumDescent];
1279
1280 // For instance, will normalize the following user-set hints
1281 // from: [10, 5, 60]
1282 // to: [10, 10, 60]
1283 normalizeHints(minS.rwidth(), prefS.rwidth(), maxS.rwidth(), descentS.rwidth());
1284 normalizeHints(minS.rheight(), prefS.rheight(), maxS.rheight(), descentS.rheight());
1285
1286 // All explicit values gathered, now continue to gather the implicit sizes
1287
1288 //--- GATHER MAXIMUM SIZE HINTS ---
1289 combineImplicitHints(info, Qt::MaximumSize, &maxS);
1290 combineSize(maxS, QSizeF(std::numeric_limits<qreal>::infinity(), std::numeric_limits<qreal>::infinity()));
1291 // implicit max or min sizes should not limit an explicitly set preferred size
1292 expandSize(maxS, prefS);
1293 expandSize(maxS, minS);
1294
1295 //--- GATHER MINIMUM SIZE HINTS ---
1296 combineImplicitHints(info, Qt::MinimumSize, &minS);
1297 expandSize(minS, QSizeF(0,0));
1298 boundSize(minS, prefS);
1299 boundSize(minS, maxS);
1300
1301 //--- GATHER PREFERRED SIZE HINTS ---
1302 // First, from implicitWidth/Height
1303 qreal &prefWidth = prefS.rwidth();
1304 qreal &prefHeight = prefS.rheight();
1305 if (prefWidth < 0 && item->implicitWidth() > 0)
1306 prefWidth = qCeil(item->implicitWidth());
1307 if (prefHeight < 0 && item->implicitHeight() > 0)
1308 prefHeight = qCeil(item->implicitHeight());
1309
1310 // If that fails, make an ultimate fallback to width/height
1311 if (useFallbackToWidthOrHeight && !prefS.isValid()) {
1312 /* If we want to support using width/height as preferred size hints in
1313 layouts, (which we think most people expect), we only want to use the
1314 initial width.
1315 This is because the width will change due to layout rearrangement,
1316 and the preferred width should return the same value, regardless of
1317 the current width.
1318 We therefore store this initial width in the attached layout object
1319 and reuse it if needed rather than querying the width another time.
1320 That means we need to ensure that an Layout attached object is available
1321 by creating one if necessary.
1322 */
1323 if (!info)
1324 info = attachedLayoutObject(item);
1325
1326 auto updatePreferredSizes = [](qreal &cachedSize, qreal &attachedSize, qreal size) {
1327 if (cachedSize < 0) {
1328 if (attachedSize < 0)
1329 attachedSize = size;
1330
1331 cachedSize = attachedSize;
1332 }
1333 };
1334 updatePreferredSizes(prefWidth, info->m_fallbackWidth, item->width());
1335 updatePreferredSizes(prefHeight, info->m_fallbackHeight, item->height());
1336 }
1337
1338 // Normalize again after the implicit hints have been gathered
1339 expandSize(prefS, minS);
1340 boundSize(prefS, maxS);
1341
1342 //--- GATHER DESCENT
1343 // Minimum descent is only applicable for the effective minimum height,
1344 // so we gather the descent last.
1345 const qreal minimumDescent = minS.height() - item->baselineOffset();
1346 descentS.setHeight(minimumDescent);
1347
1348 if (info) {
1349 QMarginsF margins = info->qMargins();
1350 QSizeF extraMargins(margins.left() + margins.right(), margins.top() + margins.bottom());
1351 minS += extraMargins;
1352 prefS += extraMargins;
1353 maxS += extraMargins;
1354 descentS += extraMargins;
1355 }
1356 if (attachedInfo)
1357 *attachedInfo = info;
1358}
1359
1360/*!
1361 \internal
1362
1363 Assumes \a info is set (if the object has an attached property)
1364 */
1365QLayoutPolicy::Policy QQuickLayout::effectiveSizePolicy_helper(QQuickItem *item, Qt::Orientation orientation, QQuickLayoutAttached *info)
1366{
1367 QLayoutPolicy::Policy pol{QLayoutPolicy::Fixed};
1368 bool isSet = false;
1369 if (info) {
1370 if (orientation == Qt::Horizontal) {
1371 isSet = info->isFillWidthSet();
1372 if (isSet && info->fillWidth())
1373 pol = QLayoutPolicy::Preferred;
1374 } else {
1375 isSet = info->isFillHeightSet();
1376 if (isSet && info->fillHeight())
1377 pol = QLayoutPolicy::Preferred;
1378 }
1379 }
1380 if (!isSet && item) {
1381 auto effectiveUseDefaultSizePolicy = [info]() {
1382 return info ? info->useDefaultSizePolicy() == QQuickLayout::SizePolicyImplicit
1383 : QGuiApplication::testAttribute(Qt::AA_QtQuickUseDefaultSizePolicy);
1384 };
1385 if (qobject_cast<QQuickLayout*>(item)) {
1386 pol = QLayoutPolicy::Preferred;
1387 } else if (effectiveUseDefaultSizePolicy()) {
1388 QLayoutPolicy sizePolicy = QQuickItemPrivate::get(item)->sizePolicy();
1389 pol = (orientation == Qt::Horizontal) ? sizePolicy.horizontalPolicy() : sizePolicy.verticalPolicy();
1390 }
1391 }
1392
1393 return pol;
1394}
1395
1396void QQuickLayout::_q_dumpLayoutTree() const
1397{
1398 QString buf;
1399 dumpLayoutTreeRecursive(0, buf);
1400 qDebug("\n%s", qPrintable(buf));
1401}
1402
1403void QQuickLayout::dumpLayoutTreeRecursive(int level, QString &buf) const
1404{
1405 auto formatLine = [&level](const char *fmt) -> QString {
1406 QString ss(level *4, QLatin1Char(' '));
1407 return ss + QLatin1String(fmt) + QLatin1Char('\n');
1408 };
1409
1410 auto f2s = [](qreal f) {
1411 return QString::number(f);
1412 };
1413 auto b2s = [](bool b) {
1414 static const char *strBool[] = {"false", "true"};
1415 return QLatin1String(strBool[int(b)]);
1416 };
1417
1418 buf += formatLine("%1 {").arg(QQmlMetaType::prettyTypeName(this));
1419 ++level;
1420 buf += formatLine("// Effective calculated values:");
1421 buf += formatLine("sizeHintDirty: %2").arg(invalidated());
1422 QSizeF min = sizeHint(Qt::MinimumSize);
1423 buf += formatLine("sizeHint.min : [%1, %2]").arg(f2s(min.width()), 5).arg(min.height(), 5);
1424 QSizeF pref = sizeHint(Qt::PreferredSize);
1425 buf += formatLine("sizeHint.pref: [%1, %2]").arg(pref.width(), 5).arg(pref.height(), 5);
1426 QSizeF max = sizeHint(Qt::MaximumSize);
1427 buf += formatLine("sizeHint.max : [%1, %2]").arg(f2s(max.width()), 5).arg(f2s(max.height()), 5);
1428
1429 for (QQuickItem *item : childItems()) {
1430 buf += QLatin1Char('\n');
1431 if (QQuickLayout *childLayout = qobject_cast<QQuickLayout*>(item)) {
1432 childLayout->dumpLayoutTreeRecursive(level, buf);
1433 } else {
1434 buf += formatLine("%1 {").arg(QQmlMetaType::prettyTypeName(item));
1435 ++level;
1436 if (item->implicitWidth() > 0)
1437 buf += formatLine("implicitWidth: %1").arg(f2s(item->implicitWidth()));
1438 if (item->implicitHeight() > 0)
1439 buf += formatLine("implicitHeight: %1").arg(f2s(item->implicitHeight()));
1440 QSizeF min;
1441 QSizeF pref;
1442 QSizeF max;
1443 QQuickLayoutAttached *info = attachedLayoutObject(item, false);
1444 if (info) {
1445 min = QSizeF(info->minimumWidth(), info->minimumHeight());
1446 pref = QSizeF(info->preferredWidth(), info->preferredHeight());
1447 max = QSizeF(info->maximumWidth(), info->maximumHeight());
1448 if (info->isExtentExplicitlySet(Qt::Horizontal, Qt::MinimumSize))
1449 buf += formatLine("Layout.minimumWidth: %1").arg(f2s(min.width()));
1450 if (info->isExtentExplicitlySet(Qt::Vertical, Qt::MinimumSize))
1451 buf += formatLine("Layout.minimumHeight: %1").arg(f2s(min.height()));
1452 if (pref.width() >= 0)
1453 buf += formatLine("Layout.preferredWidth: %1").arg(f2s(pref.width()));
1454 if (pref.height() >= 0)
1455 buf += formatLine("Layout.preferredHeight: %1").arg(f2s(pref.height()));
1456 if (info->isExtentExplicitlySet(Qt::Horizontal, Qt::MaximumSize))
1457 buf += formatLine("Layout.maximumWidth: %1").arg(f2s(max.width()));
1458 if (info->isExtentExplicitlySet(Qt::Vertical, Qt::MaximumSize))
1459 buf += formatLine("Layout.maximumHeight: %1").arg(f2s(max.height()));
1460
1461 if (info->isFillWidthSet())
1462 buf += formatLine("Layout.fillWidth: %1").arg(b2s(info->fillWidth()));
1463 if (info->isFillHeightSet())
1464 buf += formatLine("Layout.fillHeight: %1").arg(b2s(info->fillHeight()));
1465 }
1466 --level;
1467 buf += formatLine("}");
1468 }
1469 }
1470 --level;
1471 buf += formatLine("}");
1472}
1473
1474QT_END_NAMESPACE
1475
1476#include "moc_qquicklayout_p.cpp"
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
static QQuickItemPrivate::ChangeTypes changeTypes
static void combineHints(qreal &current, qreal fallbackHint)
static void combineImplicitHints(QQuickLayoutAttached *info, Qt::SizeHint which, QSizeF *size)
static void normalizeHints(qreal &minimum, qreal &preferred, qreal &maximum, qreal &descent)
static void boundSize(QSizeF &result, const QSizeF &size)
qreal(QQuickLayoutAttached::*) SizeGetter() const
static void combineSize(QSizeF &result, const QSizeF &fallbackSize)
static void expandSize(QSizeF &result, const QSizeF &size)