| 1 | // Copyright (C) 2025 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 | #ifndef QQUICKFLEXBOXLAYOUT_H |
| 5 | #define QQUICKFLEXBOXLAYOUT_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <bitset> |
| 19 | #include <QtQuickLayouts/private/qquicklayoutglobal_p.h> |
| 20 | #include <QtQuickLayouts/private/qquicklayout_p.h> |
| 21 | |
| 22 | QT_BEGIN_NAMESPACE |
| 23 | |
| 24 | class QQuickFlexboxLayoutPrivate; |
| 25 | class QQuickFlexboxLayoutAttached; |
| 26 | |
| 27 | class Q_QUICKLAYOUTS_EXPORT QQuickFlexboxLayout : public QQuickLayout |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | |
| 31 | Q_PROPERTY(FlexboxDirection direction READ direction WRITE setDirection NOTIFY directionChanged FINAL) |
| 32 | Q_PROPERTY(FlexboxWrap wrap READ wrap WRITE setWrap NOTIFY wrapChanged FINAL) |
| 33 | Q_PROPERTY(FlexboxAlignment alignItems READ alignItems WRITE setAlignItems NOTIFY alignItemsChanged FINAL) |
| 34 | Q_PROPERTY(FlexboxAlignment alignContent READ alignContent WRITE setAlignContent NOTIFY alignContentChanged FINAL) |
| 35 | Q_PROPERTY(FlexboxJustify justifyContent READ justifyContent WRITE setJustifyContent NOTIFY justifyContentChanged FINAL) |
| 36 | Q_PROPERTY(qreal gap READ gap WRITE setGap NOTIFY gapChanged RESET resetGap FINAL) |
| 37 | Q_PROPERTY(qreal rowGap READ rowGap WRITE setRowGap NOTIFY rowGapChanged RESET resetRowGap FINAL) |
| 38 | Q_PROPERTY(qreal columnGap READ columnGap WRITE setColumnGap NOTIFY columnGapChanged RESET resetColumnGap FINAL) |
| 39 | |
| 40 | QML_NAMED_ELEMENT(FlexboxLayout) |
| 41 | QML_ADDED_IN_VERSION(6, 10) |
| 42 | QML_ATTACHED(QQuickFlexboxLayoutAttached) |
| 43 | |
| 44 | public: |
| 45 | explicit QQuickFlexboxLayout(QQuickItem *parent = nullptr); |
| 46 | ~QQuickFlexboxLayout(); |
| 47 | |
| 48 | enum FlexboxDirection { // Used as similar to CSS standard |
| 49 | Column, |
| 50 | ColumnReverse, |
| 51 | Row, |
| 52 | RowReverse |
| 53 | }; |
| 54 | Q_ENUM(FlexboxDirection); |
| 55 | |
| 56 | enum FlexboxWrap { |
| 57 | NoWrap, |
| 58 | Wrap, |
| 59 | WrapReverse |
| 60 | }; |
| 61 | Q_ENUM(FlexboxWrap); |
| 62 | |
| 63 | // The alignments here can be mapped to the flexbox CSS assignments: align-items, align-content |
| 64 | // |
| 65 | // alignItems: AlignStart | AlignCenter | AlignEnd | AlignStretch |
| 66 | // Note: AlignSpace* not supported by the flexAlignItems |
| 67 | // |
| 68 | // alignContent: AlignStart | AlignEnd | AlignCenter | AlignStretch | AlignSpaceBetween | |
| 69 | // AlignSpaceAround |
| 70 | // |
| 71 | // For instance, consider placing the items are placed within the flex with flexDirection |
| 72 | // set to Row |
| 73 | // |
| 74 | // alignItems - This property causes flex items to be positioned as below with respective |
| 75 | // value set |
| 76 | // |
| 77 | // AlignStart - Flex items are positioned from the start of the cross axis |
| 78 | // [[Item1][Item2][Item3][Item4][Item5]...] |
| 79 | // AlignEnd - Flex items are positioned from the end of the cross axis |
| 80 | // [...[Item1][Item2][Item3][Item4][Item5]] |
| 81 | // AlignStretch - Flex items are stretched along the cross axis |
| 82 | // || | | | | || |
| 83 | // ||Item1|Item2|Item3|Item4|Item5|| |
| 84 | // || | | | | || |
| 85 | // AlignCenter - Flex items are centered along the cross axis |
| 86 | // | | || |
| 87 | // |[Item1][Item2][Item3][Item4]|Item5|| |
| 88 | // | | || |
| 89 | // |
| 90 | // alignContent - This property causes flex items to be positioned considering space around |
| 91 | // edge lines and in-between with respective value set |
| 92 | // |
| 93 | // AlignStart - Lines are packed towards the start of the container |
| 94 | // [[Item1][Item2][Item3][Item4][Item5]...] |
| 95 | // AlignEnd - Lines are packed towards the end of the container |
| 96 | // [...[Item1][Item2][Item3][Item4][Item5]] |
| 97 | // AlignCenter - Lines are packed towards the center |
| 98 | // || | | | | || |
| 99 | // ||Item1|Item2|Item3|Item4|Item5|| |
| 100 | // || | | | | || |
| 101 | // AlignSpaceBetween - Lines are packed at the edges of the container and spaces |
| 102 | // are placed in-between rows of the flex items |
| 103 | // |[Item1][Item2][Item3]| |
| 104 | // | | |
| 105 | // |[Item4][Item5][Item6]| |
| 106 | // AlignSpaceAround - Spaces are placed in-between the rows of the flex items and |
| 107 | // would be shared around the edges (i.e. the space between the |
| 108 | // items and at the edge of the container will vary) |
| 109 | // | | |
| 110 | // |[Item1][Item2][Item3]| |
| 111 | // | | |
| 112 | // | | |
| 113 | // |[Item4][Item5][Item6]| |
| 114 | // | | |
| 115 | // AlignStretch - Lines are stretched and there will be no space in-between |
| 116 | // |[Item1][Item2][Item3]| |
| 117 | // |[Item4][Item5][Item6]| |
| 118 | enum FlexboxAlignment { |
| 119 | AlignAuto = 0, |
| 120 | AlignStart, |
| 121 | AlignCenter, |
| 122 | AlignEnd, |
| 123 | AlignStretch, // Same as Layout.fillHeight or Layout.fillWidth |
| 124 | AlignBaseline, |
| 125 | AlignSpaceBetween, |
| 126 | AlignSpaceAround, |
| 127 | AlignSpaceEvenly |
| 128 | }; |
| 129 | Q_ENUM(FlexboxAlignment) |
| 130 | |
| 131 | // The alignments can be used for justify-content |
| 132 | enum FlexboxJustify { |
| 133 | JustifyStart, |
| 134 | JustifyCenter, |
| 135 | JustifyEnd, |
| 136 | JustifySpaceBetween, |
| 137 | JustifySpaceAround, |
| 138 | JustifySpaceEvenly |
| 139 | }; |
| 140 | Q_ENUM(FlexboxJustify) |
| 141 | |
| 142 | // The alignments can be used for justify-content |
| 143 | enum FlexboxEdge { |
| 144 | EdgeLeft, |
| 145 | EdgeRight, |
| 146 | EdgeTop, |
| 147 | EdgeBottom, |
| 148 | EdgeAll, |
| 149 | EdgeMax |
| 150 | }; |
| 151 | Q_ENUM(FlexboxEdge) |
| 152 | |
| 153 | // The alignments can be used for justify-content |
| 154 | enum FlexboxGap { |
| 155 | GapRow, |
| 156 | GapColumn, |
| 157 | GapAll, |
| 158 | GapMax |
| 159 | }; |
| 160 | Q_ENUM(FlexboxGap) |
| 161 | |
| 162 | FlexboxDirection direction() const; |
| 163 | void setDirection(FlexboxDirection); |
| 164 | |
| 165 | FlexboxWrap wrap() const; |
| 166 | void setWrap(FlexboxWrap); |
| 167 | |
| 168 | FlexboxAlignment alignItems() const; |
| 169 | void setAlignItems(FlexboxAlignment); |
| 170 | |
| 171 | FlexboxJustify justifyContent() const; |
| 172 | void setJustifyContent(FlexboxJustify); |
| 173 | |
| 174 | FlexboxAlignment alignContent() const; |
| 175 | void setAlignContent(FlexboxAlignment); |
| 176 | |
| 177 | qreal gap() const; |
| 178 | void setGap(qreal); |
| 179 | void resetGap(); |
| 180 | |
| 181 | qreal rowGap() const; |
| 182 | void setRowGap(qreal); |
| 183 | void resetRowGap(); |
| 184 | |
| 185 | qreal columnGap() const; |
| 186 | void setColumnGap(qreal); |
| 187 | void resetColumnGap(); |
| 188 | |
| 189 | void componentComplete() override; |
| 190 | QSizeF sizeHint(Qt::SizeHint whichSizeHint) const override; |
| 191 | void setAlignment(QQuickItem *, Qt::Alignment) override {} |
| 192 | void setStretchFactor(QQuickItem *, int, Qt::Orientation) override {} |
| 193 | |
| 194 | void invalidate(QQuickItem *childItem = nullptr) override; |
| 195 | void updateLayoutItems() override; |
| 196 | void rearrange(const QSizeF &) override; |
| 197 | |
| 198 | // iterator |
| 199 | QQuickItem *itemAt(int index) const override; |
| 200 | int itemCount() const override; |
| 201 | |
| 202 | /* QQuickItemChangeListener */ |
| 203 | void itemSiblingOrderChanged(QQuickItem *item) override; |
| 204 | void itemVisibilityChanged(QQuickItem *item) override; |
| 205 | |
| 206 | /* internal */ |
| 207 | static QQuickFlexboxLayoutAttached *qmlAttachedProperties(QObject *object); |
| 208 | bool isGapBitSet(QQuickFlexboxLayout::FlexboxGap gap) const; |
| 209 | void checkAnchors(QQuickItem *item) const; |
| 210 | |
| 211 | Q_SIGNALS: |
| 212 | void countChanged(); |
| 213 | void directionChanged(); |
| 214 | void wrapChanged(); |
| 215 | void alignItemsChanged(); |
| 216 | void alignContentChanged(); |
| 217 | void justifyContentChanged(); |
| 218 | void gapChanged(); |
| 219 | void rowGapChanged(); |
| 220 | void columnGapChanged(); |
| 221 | |
| 222 | private: |
| 223 | void childItemsChanged(); |
| 224 | |
| 225 | friend class QQuickFlexboxLayoutAttached; |
| 226 | Q_DECLARE_PRIVATE(QQuickFlexboxLayout) |
| 227 | }; |
| 228 | |
| 229 | class Q_QUICKLAYOUTS_EXPORT QQuickFlexboxLayoutAttached : public QObject |
| 230 | { |
| 231 | Q_OBJECT |
| 232 | |
| 233 | Q_PROPERTY(QQuickFlexboxLayout::FlexboxAlignment alignSelf READ alignSelf WRITE setAlignSelf NOTIFY alignSelfChanged FINAL) |
| 234 | |
| 235 | public: |
| 236 | QQuickFlexboxLayoutAttached(QObject *object); |
| 237 | |
| 238 | QQuickFlexboxLayout::FlexboxAlignment alignSelf() const; |
| 239 | void setAlignSelf(const QQuickFlexboxLayout::FlexboxAlignment); |
| 240 | |
| 241 | Q_SIGNALS: |
| 242 | void alignSelfChanged(); |
| 243 | |
| 244 | private: |
| 245 | // The child item in the flex layout allowed to override the parent align-item property |
| 246 | QQuickFlexboxLayout::FlexboxAlignment m_alignSelf = QQuickFlexboxLayout::AlignAuto; |
| 247 | }; |
| 248 | |
| 249 | QT_END_NAMESPACE |
| 250 | |
| 251 | #endif // QQUICKFLEXBOXLAYOUT_H |
| 252 | |