aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicklayouts/qquickflexboxlayoutengine.cpp
blob: e27c9070843cc253b0275bc9c90a43a5293564fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtQuickLayouts/private/qquickflexboxlayoutengine_p.h>

QT_BEGIN_NAMESPACE

QQuickFlexboxLayoutEngine::QQuickFlexboxLayoutEngine()
{
}

QQuickFlexboxLayoutEngine::~QQuickFlexboxLayoutEngine()
{
    clearItems();
}

void QQuickFlexboxLayoutEngine::setFlexboxParentItem(QQuickFlexboxLayoutItem *item)
{
    Q_ASSERT(item != nullptr);
    if (qobject_cast<QQuickFlexboxLayout *>(item->quickItem())) {
        m_flexboxParentItem = item;
        // Yoga parent item shouldn't have measure function
        if (m_flexboxParentItem->hasMeasureFunc())
            m_flexboxParentItem->resetMeasureFunc();
    }
}

void QQuickFlexboxLayoutEngine::clearItems()
{
    for (auto &flexItem: m_flexLayoutItems)
        delete flexItem;
    m_flexLayoutItems.clear();
    // Clear the size hints as we removed all the items from the flex layout
    for (int hintIndex = 0; hintIndex < Qt::NSizeHints; hintIndex++)
        m_cachedSizeHints[hintIndex] = QSizeF();
}

void QQuickFlexboxLayoutEngine::insertItem(QQuickFlexboxLayoutItem *item)
{
    m_flexboxParentItem->insertChild(item, m_flexLayoutItems.count());
    m_flexLayoutItems.append(item);
}

int QQuickFlexboxLayoutEngine::itemCount() const
{
    return m_flexLayoutItems.count();
}

QQuickItem *QQuickFlexboxLayoutEngine::itemAt(int index) const
{
    if (index < 0 || index >= m_flexLayoutItems.count())
        return nullptr;
    return m_flexLayoutItems.at(index)->quickItem();
}

QQuickFlexboxLayoutItem *QQuickFlexboxLayoutEngine::findFlexboxLayoutItem(QQuickItem *item) const
{
    if (!item || (m_flexLayoutItems.count() <= 0))
        return nullptr;
    auto iterator = std::find_if(m_flexLayoutItems.cbegin(), m_flexLayoutItems.cend(),
                                    [item] (QQuickFlexboxLayoutItem *flexLayoutItem){
                                        return (flexLayoutItem->quickItem() == item);
                                    });
    return (iterator == m_flexLayoutItems.cend()) ? nullptr : *iterator;
}

void QQuickFlexboxLayoutEngine::collectItemSizeHints(QQuickFlexboxLayoutItem *flexItem, QSizeF *sizeHints) const
{
    QQuickLayoutAttached *info = nullptr;
    QQuickLayout::effectiveSizeHints_helper(flexItem->quickItem(), sizeHints, &info, true);

    if (!info)
        return;

    // Set layout margins to the flex item (Layout.margins)
    if (info->isMarginsSet())
        flexItem->setFlexMargin(QQuickFlexboxLayout::EdgeAll, info->margins());
    if (info->isLeftMarginSet())
        flexItem->setFlexMargin(QQuickFlexboxLayout::EdgeLeft, info->leftMargin());
    if (info->isRightMarginSet())
        flexItem->setFlexMargin(QQuickFlexboxLayout::EdgeRight, info->rightMargin());
    if (info->isTopMarginSet())
        flexItem->setFlexMargin(QQuickFlexboxLayout::EdgeTop, info->topMargin());
    if (info->isBottomMarginSet())
        flexItem->setFlexMargin(QQuickFlexboxLayout::EdgeBottom, info->bottomMargin());

    // Set child item to grow, shrink and stretch depending on the layout
    // properties.
    // If Layout.fillWidth or Layout.fillHeight is set as true, then the child
    // item within the layout can grow or shrink (considering the minimum and
    // maximum sizes) along the main axis which depends upon the flex
    // direction.
    // If both Layout.fillWidth and Layout.fillHeight are set as true, then the
    // child item within the layout need to grow or shrink in cross section and
    // it require stretch need to be set for the yoga flex child item.
    if (info->isFillWidthSet() || info->isFillHeightSet()) {
        // Set stretch to child item both width and height
        if (auto *parentLayoutItem = qobject_cast<QQuickFlexboxLayout *>(m_flexboxParentItem->quickItem())) {
            if (parentLayoutItem->direction() == QQuickFlexboxLayout::Row ||
                parentLayoutItem->direction() == QQuickFlexboxLayout::RowReverse) {
                // If the Layout.fillHeight not been set, the preferred height
                // will be set as height
                if (!info->fillHeight())
                    flexItem->setHeight(sizeHints[Qt::PreferredSize].height());
                flexItem->setFlexBasis(sizeHints[Qt::PreferredSize].width(), !info->fillWidth());
                // Set child item to grow on main-axis (i.e. the flex
                // direction)
                flexItem->setItemGrowAlongMainAxis(info->fillWidth() ? 1.0f : 0.0f);
                // Set child item to shrink on main-axis (i.e. the flex
                // direction)
                flexItem->setItemShrinkAlongMainAxis(info->fillWidth() ? 1.0f : 0.0f);
            }
            else {
                // If the Layout.fillWidth not been set, the preferred width
                // will be set as width
                if (!info->fillWidth())
                    flexItem->setWidth(sizeHints[Qt::PreferredSize].width());
                flexItem->setFlexBasis(sizeHints[Qt::PreferredSize].height(), !info->fillHeight());
                // Set child item to grow on main-axis (i.e. the flex
                // direction)
                flexItem->setItemGrowAlongMainAxis(info->fillHeight() ? 1.0f : 0.0f);
                // Set child item to shrink on main-axis (i.e. the flex
                // direction)
                flexItem->setItemShrinkAlongMainAxis(info->fillHeight() ? 1.0f : 0.0f);
            }
        }
        // If the Layout.fillHeight not been set, the preferred height will be
        // set as height in the previous condition. Otherwise (for
        // Layout.fillHeight been set as true), make flex item to AlignStretch.
        // Thus it can also grow vertically.
        // Note: The same applies for Layout.fillWidth to grow horizontally.
        if ((qt_is_nan(flexItem->size().width()) && info->fillWidth()) ||
            (qt_is_nan(flexItem->size().height()) && info->fillHeight())) {
            flexItem->setItemStretchAlongCrossSection();
        } else {
            flexItem->inheritItemStretchAlongCrossSection();
        }
    }
}

SizeHints &QQuickFlexboxLayoutEngine::cachedItemSizeHints(int index) const
{
    QQuickFlexboxLayoutItem *flexBoxLayoutItem = m_flexLayoutItems.at(index);
    Q_ASSERT(flexBoxLayoutItem);
    SizeHints &hints = flexBoxLayoutItem->cachedItemSizeHints();
    if (!hints.min().isValid())
        collectItemSizeHints(flexBoxLayoutItem, hints.array);
    return hints;
}

QSizeF QQuickFlexboxLayoutEngine::sizeHint(Qt::SizeHint whichSizeHint) const
{
    QSizeF &askingFor = m_cachedSizeHints[whichSizeHint];
    if (!askingFor.isValid()) {
        QSizeF &minS = m_cachedSizeHints[Qt::MinimumSize];
        QSizeF &prefS = m_cachedSizeHints[Qt::PreferredSize];
        QSizeF &maxS = m_cachedSizeHints[Qt::MaximumSize];

        minS = QSizeF(0,0);
        prefS = QSizeF(0,0);
        maxS = QSizeF(std::numeric_limits<qreal>::infinity(), std::numeric_limits<qreal>::infinity());

        const int count = itemCount();
        for (int i = 0; i < count; ++i) {
            SizeHints &hints = cachedItemSizeHints(i);
            auto &flexLayoutItem = m_flexLayoutItems.at(i);
            flexLayoutItem->setMinSize(hints.min());
            if (flexLayoutItem->isFlexBasisUndefined()) {
                // If flex basis is undefined and item is still stretched, it
                // meant the flex child item has a const width or height but
                // want to stretch vertically or horizontally
                if (flexLayoutItem->isItemStreched()) {
                    if (auto *parentLayoutItem = qobject_cast<QQuickFlexboxLayout *>(m_flexboxParentItem->quickItem())) {
                        // Reset the size of the child item if the parent sets
                        // its property 'align-item' to strecth
                        // Note: The child item can also override the parent
                        // align-item property through align-self
                        // (this is FlexboxLayout.alignItem for quick items)
                        flexLayoutItem->resetSize();
                        if (parentLayoutItem->direction() == QQuickFlexboxLayout::Row ||
                            parentLayoutItem->direction() == QQuickFlexboxLayout::RowReverse) {
                            flexLayoutItem->setWidth(hints.pref().width());
                        } else {
                            flexLayoutItem->setHeight(hints.pref().height());
                        }
                    }
                } else {
                    flexLayoutItem->setSize(hints.pref());
                }
            }
            flexLayoutItem->setMaxSize(hints.max());
            // The preferred size, minimum and maximum size of the parent item
            // will be calculated as follows
            // If no wrap enabled in the flex layout:
            //      For flex direction Row or RowReversed:
            //              Parent pref, min and max width:
            //                  Sum of the pref, min and max width of the child
            //                  items
            //              Parent pref, min and max height:
            //                  Max of pref, min and max height of the child
            //                  items
            //      For flex direction Column or ColumnReversed:
            //              Parent pref, min and max width:
            //                  Max of pref, min and max width of the child
            //                  items
            //              Parent pref, min and max height:
            //                  Sum of the pref, min and max height of the
            //                  child items
            // Else if wrap enabled in the flex layout: (either Wrap or
            // WrapReversed)
            //      For flex direction Row or RowReversed or Column or
            //      ColumnReversed:
            //              Parent pref, min, max width/height:
            //                  Sum of the pref, min and max width/height of
            //                  the child items
            if (auto *qFlexLayout = qobject_cast<QQuickFlexboxLayout *>(m_flexboxParentItem->quickItem())) {
                if (qFlexLayout->wrap() == QQuickFlexboxLayout::NoWrap) {
                    if (qFlexLayout->direction() == QQuickFlexboxLayout::Row ||
                        qFlexLayout->direction() == QQuickFlexboxLayout::RowReverse) {
                        // Minimum size
                        minS.setWidth(minS.width() + hints.min().width());
                        minS.setHeight(qMax(minS.height(), hints.min().height()));
                        // Preferred size
                        prefS.setWidth(prefS.width() + hints.pref().width());
                        prefS.setHeight(qMax(prefS.height(), hints.pref().height()));
                        // Maximum size
                        maxS.setWidth(maxS.width() + hints.max().width());
                        maxS.setHeight(qMax(maxS.height(), hints.max().height()));
                    } else if (qFlexLayout->direction() == QQuickFlexboxLayout::Column ||
                               qFlexLayout->direction() == QQuickFlexboxLayout::ColumnReverse) {
                        // Minimum size
                        minS.setWidth(qMax(minS.width(), hints.min().width()));
                        minS.setHeight(minS.height() + hints.min().height());
                        // Preferred size
                        prefS.setWidth(qMax(prefS.width(), hints.pref().width()));
                        prefS.setHeight(prefS.height() + hints.pref().height());
                        // Maximum size
                        maxS.setWidth(qMax(maxS.width(), hints.max().width()));
                        maxS.setHeight(maxS.height() + hints.max().height());
                    }
                } else if (qFlexLayout->wrap() == QQuickFlexboxLayout::Wrap ||
                           qFlexLayout->wrap() == QQuickFlexboxLayout::WrapReverse) {
                    minS += hints.min();
                    prefS += hints.pref();
                    maxS += hints.max();
                }
            }
        }
    }
    return askingFor;
}

void QQuickFlexboxLayoutEngine::invalidateItemSizeHint(QQuickItem *item)
{
    if (auto *flexLayoutItem = findFlexboxLayoutItem(item)) {
        SizeHints &hints = flexLayoutItem->cachedItemSizeHints();
        hints.min() = QSizeF();
        hints.pref() = QSizeF();
        hints.max() = QSizeF();
    }
}

void QQuickFlexboxLayoutEngine::setGeometries(const QSizeF &contentSize)
{
    m_flexboxParentItem->setSize(contentSize);
    m_flexboxParentItem->computeLayout(contentSize);
    for (auto *item : m_flexLayoutItems) {
        item->quickItem()->setPosition(item->position());
        QSizeF oldSize = item->quickItem()->size();
        QSizeF newSize = item->size();
        if (oldSize == newSize) {
            // Enforce rearrange as the size remains the same.
            // This can happen in a case where we add a child item to the layout
            // (which is already a child to a layout)
            if (auto *layout = qobject_cast<QQuickLayout *>(item->quickItem())) {
                if (layout->invalidatedArrangement())
                    layout->rearrange(newSize);
            }
        } else {
            item->quickItem()->setSize(newSize);
        }
    }
}

// TODO: Need to check whether its needed to get the size of the flex item
// through the callback measure function
// QSizeF QQuickFlexboxLayoutItem::getSizeHint(float width,
// YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
// {
//     QSizeF newSize(width, height);
//     switch (widthMode) {
//     case YGMeasureModeAtMost:
//         newSize.setWidth(m_cachedSizeHint.max().width());
//         break;
//     case YGMeasureModeExactly:
//     case YGMeasureModeUndefined:
//         newSize.setWidth(m_cachedSizeHint.pref().width());
//         break;
//     default: break;
//     }
//     switch (heightMode) {
//     case YGMeasureModeAtMost:
//         newSize.setHeight(m_cachedSizeHint.max().height());
//         break;
//     case YGMeasureModeExactly:
//     case YGMeasureModeUndefined:
//         newSize.setHeight(m_cachedSizeHint.pref().height());
//         break;
//     default: break;
//     }
//     return newSize;
// }

// YGSize QQuickFlexboxLayoutItem::measureFunc(YGNodeRef nodeRef, float width,
// YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
// {
//     YGSize defaultSize;
//     auto *layoutItem = static_cast<QQuickFlexboxLayoutItem *>(YGNodeGetContext(nodeRef));
//     if (layoutItem) {
//         QSizeF size = layoutItem->getSizeHint(width, widthMode, height, heightMode);
//         defaultSize.width = (qt_is_nan(size.width()) || qt_is_inf(size.width())) ? YGUndefined : size.width();
//         defaultSize.height = (qt_is_nan(size.height()) || qt_is_inf(size.height())) ? YGUndefined : size.height();
//     }
//     return defaultSize;
// }

QT_END_NAMESPACE