aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicklayouts/qquickflexboxlayoutitem.cpp
blob: 300eec3b5b7a402ccfa2db9d3640147f883803dd (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
// 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 <QtCore/qloggingcategory.h>
#include <QtQuickLayouts/private/qquickflexboxlayoutitem_p.h>
#include <yoga/YGNode.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcQuickFlexLayoutItem, "qt.quick.flexlayouts.item")

static constexpr YGWrap QtToYGFlexboxWrap(QQuickFlexboxLayout::FlexboxWrap qtWrap) {
    switch (qtWrap) {
        case QQuickFlexboxLayout::NoWrap: return YGWrap::YGWrapNoWrap;
        case QQuickFlexboxLayout::Wrap: return YGWrap::YGWrapWrap;
        case QQuickFlexboxLayout::WrapReverse: return YGWrap::YGWrapWrapReverse;
        default: {
            qWarning("Not a valid wrap");
            return YGWrap{};
        }
    }
};

static constexpr YGFlexDirection QtToYGFlexboxDirection(QQuickFlexboxLayout::FlexboxDirection qtDirection) {
    switch (qtDirection) {
        case QQuickFlexboxLayout::Column: return YGFlexDirection::YGFlexDirectionColumn;
        case QQuickFlexboxLayout::ColumnReverse: return YGFlexDirection::YGFlexDirectionColumnReverse;
        case QQuickFlexboxLayout::Row: return YGFlexDirection::YGFlexDirectionRow;
        case QQuickFlexboxLayout::RowReverse: return YGFlexDirection::YGFlexDirectionRowReverse;
        default: {
            qWarning("Not a valid direction");
            return YGFlexDirection{};
        }
    }
};

static constexpr YGAlign QtToYGFlexboxAlignment(QQuickFlexboxLayout::FlexboxAlignment qtAlignment) {
    switch (qtAlignment) {
        case QQuickFlexboxLayout::AlignAuto: return YGAlign::YGAlignAuto;
        case QQuickFlexboxLayout::AlignStart: return YGAlign::YGAlignFlexStart;
        case QQuickFlexboxLayout::AlignCenter: return YGAlign::YGAlignCenter;
        case QQuickFlexboxLayout::AlignEnd: return YGAlign::YGAlignFlexEnd;
        case QQuickFlexboxLayout::AlignStretch: return YGAlign::YGAlignStretch;
        case QQuickFlexboxLayout::AlignBaseline: return YGAlign::YGAlignBaseline;
        case QQuickFlexboxLayout::AlignSpaceBetween: return YGAlign::YGAlignSpaceBetween;
        case QQuickFlexboxLayout::AlignSpaceAround: return YGAlign::YGAlignSpaceAround;
        case QQuickFlexboxLayout::AlignSpaceEvenly: {
            return YGAlign{};
        }
        default: {
            qWarning("Not a valid alignment");
            return YGAlign{};
        }
    }
};

static constexpr YGJustify QtToYGFlexboxJustify(QQuickFlexboxLayout::FlexboxJustify qtJustify) {
    switch (qtJustify) {
    case QQuickFlexboxLayout::JustifyStart: return YGJustify::YGJustifyFlexStart;
    case QQuickFlexboxLayout::JustifyCenter: return YGJustify::YGJustifyCenter;
        case QQuickFlexboxLayout::JustifyEnd: return YGJustify::YGJustifyFlexEnd;
        case QQuickFlexboxLayout::JustifySpaceBetween: return YGJustify::YGJustifySpaceBetween;
        case QQuickFlexboxLayout::JustifySpaceAround: return YGJustify::YGJustifySpaceAround;
        case QQuickFlexboxLayout::JustifySpaceEvenly: return YGJustify::YGJustifySpaceEvenly;
        default: {
            qWarning("Not a valid justify");
            return YGJustify{};
        }
    }
};

static constexpr YGEdge QtToYGFlexboxEdge(QQuickFlexboxLayout::FlexboxEdge qtEdge) {
    switch (qtEdge) {
        case QQuickFlexboxLayout::EdgeLeft: return YGEdge::YGEdgeLeft;
        case QQuickFlexboxLayout::EdgeRight: return YGEdge::YGEdgeRight;
        case QQuickFlexboxLayout::EdgeTop: return YGEdge::YGEdgeTop;
        case QQuickFlexboxLayout::EdgeBottom: return YGEdge::YGEdgeBottom;
        case QQuickFlexboxLayout::EdgeAll: return YGEdge::YGEdgeAll;
        default: {
            qWarning("Not a valid edge");
            return YGEdge{};
        }
    }
};

static constexpr YGGutter QtToYGFlexboxGap(QQuickFlexboxLayout::FlexboxGap qtGap) {
    switch (qtGap) {
        case QQuickFlexboxLayout::GapRow: return YGGutter::YGGutterRow;
        case QQuickFlexboxLayout::GapColumn: return YGGutter::YGGutterColumn;
        case QQuickFlexboxLayout::GapAll: return YGGutter::YGGutterAll;
        default: {
            qWarning("Not a valid gap");
            return YGGutter{};
        }
    }
};

QQuickFlexboxLayoutItem::QQuickFlexboxLayoutItem(QQuickItem *item)
    : m_item(item)
{
    Q_ASSERT(m_item != nullptr);
    m_yogaNode = YGNodeNew();
    resetDefault();
}

QQuickFlexboxLayoutItem::~QQuickFlexboxLayoutItem()
{
    YGNodeFree(m_yogaNode);
}

void QQuickFlexboxLayoutItem::setMinSize(const QSizeF &size)
{
    YGNodeStyleSetMinWidth(m_yogaNode, static_cast<float>(size.width()));
    YGNodeStyleSetMinHeight(m_yogaNode, static_cast<float>(size.height()));
}

void QQuickFlexboxLayoutItem::setSize(const QSizeF &size)
{
    YGNodeStyleSetWidth(m_yogaNode, static_cast<float>(size.width()));
    YGNodeStyleSetHeight(m_yogaNode, static_cast<float>(size.height()));
}

void QQuickFlexboxLayoutItem::setWidth(const qreal &width)
{
    YGNodeStyleSetWidth(m_yogaNode, static_cast<float>(width));
}

void QQuickFlexboxLayoutItem::setHeight(const qreal &height)
{
    YGNodeStyleSetHeight(m_yogaNode, static_cast<float>(height));
}

void QQuickFlexboxLayoutItem::setMaxSize(const QSizeF &size)
{
    YGNodeStyleSetMaxWidth(m_yogaNode, static_cast<float>(size.width()));
    YGNodeStyleSetMaxHeight(m_yogaNode, static_cast<float>(size.height()));
}

void QQuickFlexboxLayoutItem::setFlexBasis(const qreal value, bool reset)
{
    YGNodeStyleSetFlexBasis(m_yogaNode, reset ? qQNaN() : value);
}

bool QQuickFlexboxLayoutItem::isFlexBasisUndefined() const
{
    float value = YGNodeStyleGetFlexBasis(m_yogaNode).value;
    return (value == YGUndefined || qt_is_nan(value));
}

void QQuickFlexboxLayoutItem::setItemGrowAlongMainAxis(const qreal value)
{
    YGNodeStyleSetFlexGrow(m_yogaNode, value);
}

void QQuickFlexboxLayoutItem::setItemShrinkAlongMainAxis(const qreal value)
{
    YGNodeStyleSetFlexShrink(m_yogaNode, value);
}

void QQuickFlexboxLayoutItem::setItemStretchAlongCrossSection()
{
    YGNodeStyleSetAlignSelf(m_yogaNode, YGAlignStretch);
}

void QQuickFlexboxLayoutItem::setFlexDirection(QQuickFlexboxLayout::FlexboxDirection direction)
{
    YGNodeStyleSetFlexDirection(m_yogaNode, QtToYGFlexboxDirection(direction));
}

void QQuickFlexboxLayoutItem::setFlexWrap(QQuickFlexboxLayout::FlexboxWrap wrap)
{
    YGNodeStyleSetFlexWrap(m_yogaNode, QtToYGFlexboxWrap(wrap));
}

void QQuickFlexboxLayoutItem::setFlexAlignItemsProperty(QQuickFlexboxLayout::FlexboxAlignment align)
{
    YGNodeStyleSetAlignItems(m_yogaNode, QtToYGFlexboxAlignment(align));
}

void QQuickFlexboxLayoutItem::setFlexAlignContentProperty(QQuickFlexboxLayout::FlexboxAlignment align)
{
    YGNodeStyleSetAlignContent(m_yogaNode, QtToYGFlexboxAlignment(align));
}

void QQuickFlexboxLayoutItem::setFlexJustifyContentProperty(QQuickFlexboxLayout::FlexboxJustify justify)
{
    YGNodeStyleSetJustifyContent(m_yogaNode, QtToYGFlexboxJustify(justify));
}

void QQuickFlexboxLayoutItem::setFlexAlignSelfProperty(QQuickFlexboxLayout::FlexboxAlignment align)
{
    YGNodeStyleSetAlignSelf(m_yogaNode, QtToYGFlexboxAlignment(align));
}

void QQuickFlexboxLayoutItem::setFlexMargin(QQuickFlexboxLayout::FlexboxEdge edge, const qreal value)
{
    YGNodeStyleSetMargin(m_yogaNode, QtToYGFlexboxEdge(edge), value);
}

void QQuickFlexboxLayoutItem::setFlexPadding(QQuickFlexboxLayout::FlexboxEdge edge, const qreal value)
{
    YGNodeStyleSetPadding(m_yogaNode, QtToYGFlexboxEdge(edge), value);
}

void QQuickFlexboxLayoutItem::setFlexGap(QQuickFlexboxLayout::FlexboxGap gap, const qreal value)
{
    YGNodeStyleSetGap(m_yogaNode, QtToYGFlexboxGap(gap), value);
}

bool QQuickFlexboxLayoutItem::isItemStreched() const
{
    return ((YGNodeStyleGetAlignSelf(m_yogaNode) == YGAlignStretch) ||
            ((YGNodeStyleGetAlignSelf(m_yogaNode) == YGAlignAuto) &&
             (YGNodeStyleGetAlignItems(m_yogaNode->getParent()) == YGAlignStretch)));
}

void QQuickFlexboxLayoutItem::inheritItemStretchAlongCrossSection()
{
    YGNodeStyleSetAlignSelf(m_yogaNode, YGAlignAuto);
}

void QQuickFlexboxLayoutItem::resetMargins()
{
    YGNodeStyleSetMargin(m_yogaNode, YGEdgeLeft, 0);
    YGNodeStyleSetMargin(m_yogaNode, YGEdgeTop, 0);
    YGNodeStyleSetMargin(m_yogaNode, YGEdgeRight, 0);
    YGNodeStyleSetMargin(m_yogaNode, YGEdgeBottom, 0);
}

void QQuickFlexboxLayoutItem::resetPaddings()
{
    YGNodeStyleSetPadding(m_yogaNode, YGEdgeAll, 0);
}

void QQuickFlexboxLayoutItem::resetSize()
{
    YGNodeStyleSetWidth(m_yogaNode, YGUndefined);
    YGNodeStyleSetHeight(m_yogaNode, YGUndefined);
}

QPoint QQuickFlexboxLayoutItem::position() const
{
    return QPoint(YGNodeLayoutGetLeft(m_yogaNode), YGNodeLayoutGetTop(m_yogaNode));
}

QSizeF QQuickFlexboxLayoutItem::size() const
{
    return QSizeF(YGNodeLayoutGetWidth(m_yogaNode), YGNodeLayoutGetHeight(m_yogaNode));
}

void QQuickFlexboxLayoutItem::insertChild(QQuickFlexboxLayoutItem *child, int index)
{
    YGNodeInsertChild(m_yogaNode, child->yogaItem(), index);
    // TODO: We may need this only for the text node?
    // YGNodeSetMeasureFunc(m_yogaNode, &QQuickFlexboxLayoutItem::measureFunc);
}

void QQuickFlexboxLayoutItem::resetDefault()
{
    // Context object is required here for callback functionality
    // For instance, the measurement function would be called to determine the
    // layout size
    YGNodeSetContext(m_yogaNode, this);
    resetMargins();
    resetPaddings();
    YGNodeStyleSetFlexBasis(m_yogaNode, YGUndefined);
}

bool QQuickFlexboxLayoutItem::hasMeasureFunc() const
{
    return YGNodeHasMeasureFunc(m_yogaNode);
}

void QQuickFlexboxLayoutItem::resetMeasureFunc()
{
    YGNodeSetMeasureFunc(m_yogaNode, nullptr);
}

SizeHints &QQuickFlexboxLayoutItem::cachedItemSizeHints() const
{
    return m_cachedSizeHint;
}

void QQuickFlexboxLayoutItem::computeLayout(const QSizeF &size)
{
    // Consider either NaN or Inf as YGUndefined
    const float width = (qt_is_nan(size.width()) || qt_is_inf(size.width()) ||
                         !size.width()) ? YGUndefined : size.width();
    const float height = (qt_is_nan(size.height()) || qt_is_inf(size.height()) ||
                          !size.height()) ? YGUndefined : size.height();
    YGNodeCalculateLayout(m_yogaNode, width, height, YGDirectionLTR);
}

QT_END_NAMESPACE