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#ifndef QQUICKPOSITIONERS_P_H
5#define QQUICKPOSITIONERS_P_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 <QtQuick/private/qtquickglobal_p.h>
19
20QT_REQUIRE_CONFIG(quick_positioners);
21
22#include "qquickimplicitsizeitem_p.h"
23#if QT_CONFIG(quick_viewtransitions)
24#include "qquickitemviewtransition_p.h"
25#endif
26
27#include <QtCore/qobject.h>
28#include <QtCore/qstring.h>
29
30#include <memory>
31#include <vector>
32
33QT_BEGIN_NAMESPACE
34
35class QQuickBasePositionerPrivate;
36
37class QQuickPositionerAttached : public QObject
38{
39 Q_OBJECT
40
41public:
42 QQuickPositionerAttached(QObject *parent);
43
44 Q_PROPERTY(int index READ index NOTIFY indexChanged FINAL)
45 Q_PROPERTY(bool isFirstItem READ isFirstItem NOTIFY isFirstItemChanged FINAL)
46 Q_PROPERTY(bool isLastItem READ isLastItem NOTIFY isLastItemChanged FINAL)
47
48 int index() const { return m_index; }
49 void setIndex(int index);
50
51 bool isFirstItem() const { return m_isFirstItem; }
52 void setIsFirstItem(bool isFirstItem);
53
54 bool isLastItem() const { return m_isLastItem; }
55 void setIsLastItem(bool isLastItem);
56
57Q_SIGNALS:
58 void indexChanged();
59 void isFirstItemChanged();
60 void isLastItemChanged();
61
62private:
63 int m_index;
64 bool m_isFirstItem;
65 bool m_isLastItem;
66};
67
68class Q_QUICK_EXPORT QQuickBasePositioner : public QQuickImplicitSizeItem
69{
70 Q_OBJECT
71
72 Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged)
73#if QT_CONFIG(quick_viewtransitions)
74 Q_PROPERTY(QQuickTransition *populate READ populate WRITE setPopulate NOTIFY populateChanged)
75 Q_PROPERTY(QQuickTransition *move READ move WRITE setMove NOTIFY moveChanged)
76 Q_PROPERTY(QQuickTransition *add READ add WRITE setAdd NOTIFY addChanged)
77#endif
78
79 Q_PROPERTY(qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged REVISION(2, 6))
80 Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding NOTIFY topPaddingChanged REVISION(2, 6))
81 Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding NOTIFY leftPaddingChanged REVISION(2, 6))
82 Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding NOTIFY rightPaddingChanged REVISION(2, 6))
83 Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET resetBottomPadding NOTIFY bottomPaddingChanged REVISION(2, 6))
84
85 QML_NAMED_ELEMENT(Positioner)
86 QML_ADDED_IN_VERSION(2, 0)
87 QML_UNCREATABLE("Positioner is an abstract type that is only available as an attached property.")
88 QML_ATTACHED(QQuickPositionerAttached)
89
90public:
91 enum PositionerType { None = 0x0, Horizontal = 0x1, Vertical = 0x2, Both = 0x3 };
92
93 QQuickBasePositioner(PositionerType, QQuickItem *parent);
94 ~QQuickBasePositioner();
95
96 qreal spacing() const;
97 void setSpacing(qreal);
98
99#if QT_CONFIG(quick_viewtransitions)
100 QQuickTransition *populate() const;
101 void setPopulate(QQuickTransition *);
102
103 QQuickTransition *move() const;
104 void setMove(QQuickTransition *);
105
106 QQuickTransition *add() const;
107 void setAdd(QQuickTransition *);
108#endif
109
110 static QQuickPositionerAttached *qmlAttachedProperties(QObject *obj);
111
112 void updateAttachedProperties(QQuickPositionerAttached *specificProperty = nullptr, QQuickItem *specificPropertyOwner = nullptr) const;
113
114 qreal padding() const;
115 void setPadding(qreal padding);
116 void resetPadding();
117
118 qreal topPadding() const;
119 void setTopPadding(qreal padding);
120 void resetTopPadding();
121
122 qreal leftPadding() const;
123 void setLeftPadding(qreal padding);
124 void resetLeftPadding();
125
126 qreal rightPadding() const;
127 void setRightPadding(qreal padding);
128 void resetRightPadding();
129
130 qreal bottomPadding() const;
131 void setBottomPadding(qreal padding);
132 void resetBottomPadding();
133
134 Q_REVISION(2, 9) Q_INVOKABLE void forceLayout();
135
136protected:
137 QQuickBasePositioner(QQuickBasePositionerPrivate &dd, PositionerType at, QQuickItem *parent);
138 void componentComplete() override;
139 void itemChange(ItemChange, const ItemChangeData &) override;
140
141 void updatePolish() override;
142
143Q_SIGNALS:
144 void spacingChanged();
145 void populateChanged();
146 void moveChanged();
147 void addChanged();
148 Q_REVISION(2, 6) void paddingChanged();
149 Q_REVISION(2, 6) void topPaddingChanged();
150 Q_REVISION(2, 6) void leftPaddingChanged();
151 Q_REVISION(2, 6) void rightPaddingChanged();
152 Q_REVISION(2, 6) void bottomPaddingChanged();
153 Q_REVISION(2, 9) void positioningComplete();
154
155protected Q_SLOTS:
156 void prePositioning();
157
158protected:
159 virtual void doPositioning(QSizeF *contentSize)=0;
160 virtual void reportConflictingAnchors()=0;
161
162 class PositionedItem
163 {
164 public :
165 PositionedItem(QQuickItem *i);
166 bool operator==(const PositionedItem &other) const { return other.item == item; }
167 bool operator==(const QQuickItem *other) const { return other == this->item; }
168
169 qreal itemX() const;
170 qreal itemY() const;
171
172 void moveTo(const QPointF &pos);
173
174#if QT_CONFIG(quick_viewtransitions)
175 void transitionNextReposition(QQuickItemViewTransitioner *transitioner, QQuickItemViewTransitioner::TransitionType type, bool asTarget);
176 bool prepareTransition(QQuickItemViewTransitioner *transitioner, const QRectF &viewBounds);
177 void startTransition(QQuickItemViewTransitioner *transitioner);
178#endif
179
180 void updatePadding(qreal lp, qreal tp, qreal rp, qreal bp);
181
182 QQuickItem *item;
183#if QT_CONFIG(quick_viewtransitions)
184 std::unique_ptr<QQuickItemViewTransitionableItem> transitionableItem;
185#endif
186 int index;
187 bool isNew;
188 bool isVisible;
189
190 qreal topPadding;
191 qreal leftPadding;
192 qreal rightPadding;
193 qreal bottomPadding;
194 };
195
196 std::vector<PositionedItem> positionedItems;
197 std::vector<PositionedItem> unpositionedItems; //Still 'in' the positioner, just not positioned
198
199 void positionItem(qreal x, qreal y, PositionedItem *target);
200 void positionItemX(qreal, PositionedItem *target);
201 void positionItemY(qreal, PositionedItem *target);
202
203private:
204 Q_DISABLE_COPY(QQuickBasePositioner)
205 Q_DECLARE_PRIVATE(QQuickBasePositioner)
206};
207
208class Q_QUICK_EXPORT QQuickColumn : public QQuickBasePositioner
209{
210 Q_OBJECT
211 QML_NAMED_ELEMENT(Column)
212 QML_ADDED_IN_VERSION(2, 0)
213public:
214 QQuickColumn(QQuickItem *parent=nullptr);
215
216protected:
217 void doPositioning(QSizeF *contentSize) override;
218 void reportConflictingAnchors() override;
219};
220
221class QQuickRowPrivate;
222class Q_QUICK_EXPORT QQuickRow: public QQuickBasePositioner
223{
224 Q_OBJECT
225 Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
226 Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
227 QML_NAMED_ELEMENT(Row)
228 QML_ADDED_IN_VERSION(2, 0)
229
230public:
231 QQuickRow(QQuickItem *parent=nullptr);
232
233 Qt::LayoutDirection layoutDirection() const;
234 void setLayoutDirection (Qt::LayoutDirection);
235 Qt::LayoutDirection effectiveLayoutDirection() const;
236
237Q_SIGNALS:
238 void layoutDirectionChanged();
239 void effectiveLayoutDirectionChanged();
240
241protected:
242 void doPositioning(QSizeF *contentSize) override;
243 void reportConflictingAnchors() override;
244private:
245 Q_DECLARE_PRIVATE(QQuickRow)
246};
247
248class QQuickGridPrivate;
249class Q_QUICK_EXPORT QQuickGrid : public QQuickBasePositioner
250{
251 Q_OBJECT
252 Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged)
253 Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged)
254 Q_PROPERTY(qreal rowSpacing READ rowSpacing WRITE setRowSpacing NOTIFY rowSpacingChanged RESET resetRowSpacing)
255 Q_PROPERTY(qreal columnSpacing READ columnSpacing WRITE setColumnSpacing NOTIFY columnSpacingChanged RESET resetColumnSpacing)
256 Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged)
257 Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
258 Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
259 Q_PROPERTY(HAlignment horizontalItemAlignment READ hItemAlign WRITE setHItemAlign NOTIFY horizontalAlignmentChanged REVISION(2, 1))
260 Q_PROPERTY(HAlignment effectiveHorizontalItemAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged REVISION(2, 1))
261 Q_PROPERTY(VAlignment verticalItemAlignment READ vItemAlign WRITE setVItemAlign NOTIFY verticalAlignmentChanged REVISION(2, 1))
262 QML_NAMED_ELEMENT(Grid)
263 QML_ADDED_IN_VERSION(2, 0)
264
265public:
266 QQuickGrid(QQuickItem *parent=nullptr);
267
268 int rows() const { return m_rows; }
269 void setRows(const int rows);
270
271 int columns() const { return m_columns; }
272 void setColumns(const int columns);
273
274 qreal rowSpacing() const { return m_rowSpacing; }
275 void setRowSpacing(qreal);
276 void resetRowSpacing() { m_useRowSpacing = false; }
277
278 qreal columnSpacing() const { return m_columnSpacing; }
279 void setColumnSpacing(qreal);
280 void resetColumnSpacing() { m_useColumnSpacing = false; }
281
282 enum Flow { LeftToRight, TopToBottom };
283 Q_ENUM(Flow)
284 Flow flow() const;
285 void setFlow(Flow);
286
287 Qt::LayoutDirection layoutDirection() const;
288 void setLayoutDirection (Qt::LayoutDirection);
289 Qt::LayoutDirection effectiveLayoutDirection() const;
290
291 enum HAlignment { AlignLeft = Qt::AlignLeft,
292 AlignRight = Qt::AlignRight,
293 AlignHCenter = Qt::AlignHCenter};
294 Q_ENUM(HAlignment)
295 enum VAlignment { AlignTop = Qt::AlignTop,
296 AlignBottom = Qt::AlignBottom,
297 AlignVCenter = Qt::AlignVCenter };
298 Q_ENUM(VAlignment)
299
300 HAlignment hItemAlign() const;
301 void setHItemAlign(HAlignment align);
302 HAlignment effectiveHAlign() const;
303
304 VAlignment vItemAlign() const;
305 void setVItemAlign(VAlignment align);
306
307Q_SIGNALS:
308 void rowsChanged();
309 void columnsChanged();
310 void flowChanged();
311 void layoutDirectionChanged();
312 void effectiveLayoutDirectionChanged();
313 void rowSpacingChanged();
314 void columnSpacingChanged();
315 Q_REVISION(2, 1) void horizontalAlignmentChanged(QQuickGrid::HAlignment alignment);
316 Q_REVISION(2, 1) void effectiveHorizontalAlignmentChanged(QQuickGrid::HAlignment alignment);
317 Q_REVISION(2, 1) void verticalAlignmentChanged(QQuickGrid::VAlignment alignment);
318
319protected:
320 void doPositioning(QSizeF *contentSize) override;
321 void reportConflictingAnchors() override;
322
323private:
324 int m_rows;
325 int m_columns;
326 qreal m_rowSpacing;
327 qreal m_columnSpacing;
328 bool m_useRowSpacing;
329 bool m_useColumnSpacing;
330 Flow m_flow;
331 HAlignment m_hItemAlign;
332 VAlignment m_vItemAlign;
333 Q_DECLARE_PRIVATE(QQuickGrid)
334};
335
336class QQuickFlowPrivate;
337class Q_QUICK_EXPORT QQuickFlow: public QQuickBasePositioner
338{
339 Q_OBJECT
340 Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged)
341 Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
342 Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
343 QML_NAMED_ELEMENT(Flow)
344 QML_ADDED_IN_VERSION(2, 0)
345public:
346 QQuickFlow(QQuickItem *parent=nullptr);
347
348 enum Flow { LeftToRight, TopToBottom };
349 Q_ENUM(Flow)
350 Flow flow() const;
351 void setFlow(Flow);
352
353 Qt::LayoutDirection layoutDirection() const;
354 void setLayoutDirection (Qt::LayoutDirection);
355 Qt::LayoutDirection effectiveLayoutDirection() const;
356
357Q_SIGNALS:
358 void flowChanged();
359 void layoutDirectionChanged();
360 void effectiveLayoutDirectionChanged();
361
362protected:
363 void doPositioning(QSizeF *contentSize) override;
364 void reportConflictingAnchors() override;
365protected:
366 QQuickFlow(QQuickFlowPrivate &dd, QQuickItem *parent);
367private:
368 Q_DECLARE_PRIVATE(QQuickFlow)
369};
370
371QT_END_NAMESPACE
372
373#endif // QQUICKPOSITIONERS_P_H
374

source code of qtdeclarative/src/quick/items/qquickpositioners_p.h