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 QQUICKSTACKLAYOUT_H |
5 | #define QQUICKSTACKLAYOUT_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 <QtQuickLayouts/private/qquicklayoutglobal_p.h> |
19 | #include <QtQuickLayouts/private/qquicklayout_p.h> |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | class QQuickStackLayoutPrivate; |
24 | class QQuickStackLayoutAttached; |
25 | |
26 | class Q_QUICKLAYOUTS_PRIVATE_EXPORT QQuickStackLayout : public QQuickLayout |
27 | { |
28 | Q_OBJECT |
29 | Q_PROPERTY(int count READ count NOTIFY countChanged FINAL) |
30 | Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged FINAL) |
31 | QML_NAMED_ELEMENT(StackLayout) |
32 | QML_ADDED_IN_VERSION(1, 3) |
33 | QML_ATTACHED(QQuickStackLayoutAttached) |
34 | |
35 | public: |
36 | explicit QQuickStackLayout(QQuickItem *parent = nullptr); |
37 | int count() const; |
38 | int currentIndex() const; |
39 | void setCurrentIndex(int index); |
40 | |
41 | void componentComplete() override; |
42 | void itemChange(ItemChange change, const ItemChangeData &value) override; |
43 | QSizeF sizeHint(Qt::SizeHint whichSizeHint) const override; |
44 | void setAlignment(QQuickItem *item, Qt::Alignment align) override; |
45 | void invalidate(QQuickItem *childItem = nullptr) override; |
46 | void updateLayoutItems() override { } |
47 | void rearrange(const QSizeF &) override; |
48 | void setStretchFactor(QQuickItem *item, int stretchFactor, Qt::Orientation orient) override; |
49 | |
50 | // iterator |
51 | Q_INVOKABLE QQuickItem *itemAt(int index) const override; |
52 | int itemCount() const override; |
53 | int indexOf(QQuickItem *item) const; |
54 | |
55 | /* QQuickItemChangeListener */ |
56 | void itemSiblingOrderChanged(QQuickItem *item) override; |
57 | |
58 | static QQuickStackLayoutAttached *qmlAttachedProperties(QObject *object); |
59 | |
60 | Q_SIGNALS: |
61 | void currentIndexChanged(); |
62 | void countChanged(); |
63 | |
64 | private: |
65 | enum AdjustCurrentIndexPolicy { |
66 | DontAdjustCurrentIndex, |
67 | AdjustCurrentIndex |
68 | }; |
69 | |
70 | static void collectItemSizeHints(QQuickItem *item, QSizeF *sizeHints); |
71 | bool shouldIgnoreItem(QQuickItem *item) const; |
72 | void childItemsChanged(AdjustCurrentIndexPolicy adjustCurrentIndexPolicy = DontAdjustCurrentIndex); |
73 | Q_DECLARE_PRIVATE(QQuickStackLayout) |
74 | |
75 | friend class QQuickStackLayoutAttached; |
76 | |
77 | QList<QQuickItem*> m_items; |
78 | |
79 | struct SizeHints { |
80 | inline QSizeF &min() { return array[Qt::MinimumSize]; } |
81 | inline QSizeF &pref() { return array[Qt::PreferredSize]; } |
82 | inline QSizeF &max() { return array[Qt::MaximumSize]; } |
83 | QSizeF array[Qt::NSizeHints]; |
84 | }; |
85 | |
86 | mutable QHash<QQuickItem*, SizeHints> m_cachedItemSizeHints; |
87 | mutable QSizeF m_cachedSizeHints[Qt::NSizeHints]; |
88 | SizeHints &cachedItemSizeHints(int index) const; |
89 | }; |
90 | |
91 | class QQuickStackLayoutPrivate : public QQuickLayoutPrivate |
92 | { |
93 | Q_DECLARE_PUBLIC(QQuickStackLayout) |
94 | public: |
95 | QQuickStackLayoutPrivate() : count(0), currentIndex(-1), explicitCurrentIndex(false) {} |
96 | private: |
97 | int count; |
98 | int currentIndex; |
99 | bool explicitCurrentIndex; |
100 | }; |
101 | |
102 | class Q_QUICKLAYOUTS_PRIVATE_EXPORT QQuickStackLayoutAttached : public QObject |
103 | { |
104 | Q_OBJECT |
105 | Q_PROPERTY(int index READ index NOTIFY indexChanged FINAL) |
106 | Q_PROPERTY(bool isCurrentItem READ isCurrentItem NOTIFY isCurrentItemChanged FINAL) |
107 | Q_PROPERTY(QQuickStackLayout *layout READ layout NOTIFY layoutChanged FINAL) |
108 | |
109 | public: |
110 | QQuickStackLayoutAttached(QObject *object); |
111 | |
112 | int index() const; |
113 | void setIndex(int index); |
114 | |
115 | bool isCurrentItem() const; |
116 | void setIsCurrentItem(bool isCurrentItem); |
117 | |
118 | QQuickStackLayout *layout() const; |
119 | void setLayout(QQuickStackLayout *layout); |
120 | |
121 | Q_SIGNALS: |
122 | void indexChanged(); |
123 | void isCurrentItemChanged(); |
124 | void layoutChanged(); |
125 | |
126 | private: |
127 | int m_index = -1; |
128 | bool m_isCurrentItem = false; |
129 | QQuickStackLayout *m_layout = nullptr; |
130 | }; |
131 | |
132 | QT_END_NAMESPACE |
133 | |
134 | #endif // QQUICKSTACKLAYOUT_H |
135 | |