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 QQUICKGRIDLAYOUTENGINE_P_H |
5 | #define QQUICKGRIDLAYOUTENGINE_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 for the convenience |
12 | // of the graphics view layout classes. This header |
13 | // file may change from version to version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtGui/private/qgridlayoutengine_p.h> |
19 | #include <QtGui/private/qlayoutpolicy_p.h> |
20 | #include <QtCore/qmath.h> |
21 | |
22 | #include "qquickitem.h" |
23 | #include "qquicklayout_p.h" |
24 | #include "qdebug.h" |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QQuickGridLayoutItem : public QGridLayoutItem { |
28 | public: |
29 | QQuickGridLayoutItem(QQuickItem *item, int row, int column, |
30 | int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = { }) |
31 | : QGridLayoutItem(row, column, rowSpan, columnSpan, alignment), m_item(item), sizeHintCacheDirty(true), useFallbackToWidthOrHeight(true) {} |
32 | |
33 | |
34 | QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const override |
35 | { |
36 | Q_UNUSED(constraint); // Quick Layouts does not support constraint atm |
37 | return effectiveSizeHints()[which]; |
38 | } |
39 | |
40 | QSizeF *effectiveSizeHints() const |
41 | { |
42 | if (!sizeHintCacheDirty) |
43 | return cachedSizeHints; |
44 | |
45 | QQuickLayout::effectiveSizeHints_helper(item: m_item, cachedSizeHints, info: nullptr, useFallbackToWidthOrHeight); |
46 | useFallbackToWidthOrHeight = false; |
47 | |
48 | sizeHintCacheDirty = false; |
49 | return cachedSizeHints; |
50 | } |
51 | |
52 | void setCachedSizeHints(QSizeF *sizeHints) |
53 | { |
54 | for (int i = 0; i < Qt::NSizeHints; ++i) { |
55 | cachedSizeHints[i] = sizeHints[i]; |
56 | } |
57 | sizeHintCacheDirty = false; |
58 | } |
59 | |
60 | void invalidate() |
61 | { |
62 | qCDebug(lcQuickLayouts) << "QQuickGridLayoutItem::invalidate()" ; |
63 | sizeHintCacheDirty = true; |
64 | } |
65 | |
66 | QLayoutPolicy::Policy sizePolicy(Qt::Orientation orientation) const override |
67 | { |
68 | return QQuickLayout::effectiveSizePolicy_helper(item: m_item, orientation, info: attachedLayoutObject(item: m_item, create: false)); |
69 | } |
70 | |
71 | void setGeometry(const QRectF &rect) override |
72 | { |
73 | QQuickLayoutAttached *info = attachedLayoutObject(item: m_item, create: false); |
74 | const QRectF r = info ? rect.marginsRemoved(margins: info->effectiveQMargins()) : rect; |
75 | const QSizeF oldSize(m_item->width(), m_item->height()); |
76 | const QSizeF newSize = r.size(); |
77 | m_item->setPosition(r.topLeft()); |
78 | if (newSize == oldSize) { |
79 | // We need to enforce a rearrange when the geometry is the same |
80 | if (QQuickLayout *lay = qobject_cast<QQuickLayout *>(object: m_item)) { |
81 | if (lay->invalidatedArrangement()) |
82 | lay->rearrange(newSize); |
83 | } |
84 | } else { |
85 | m_item->setSize(newSize); |
86 | } |
87 | } |
88 | |
89 | QQuickItem *layoutItem() const { return m_item; } |
90 | |
91 | QQuickItem *m_item; |
92 | private: |
93 | mutable QSizeF cachedSizeHints[Qt::NSizeHints]; |
94 | mutable unsigned sizeHintCacheDirty : 1; |
95 | mutable unsigned useFallbackToWidthOrHeight : 1; |
96 | }; |
97 | |
98 | class QQuickGridLayoutEngine : public QGridLayoutEngine { |
99 | public: |
100 | QQuickGridLayoutEngine() : QGridLayoutEngine(Qt::AlignVCenter, true /*snapToPixelGrid*/) { } |
101 | |
102 | int indexOf(QQuickItem *item) const { |
103 | for (int i = 0; i < q_items.size(); ++i) { |
104 | if (item == static_cast<QQuickGridLayoutItem*>(q_items.at(i))->layoutItem()) |
105 | return i; |
106 | } |
107 | return -1; |
108 | } |
109 | |
110 | QQuickGridLayoutItem *findLayoutItem(QQuickItem *layoutItem) const |
111 | { |
112 | for (int i = q_items.size() - 1; i >= 0; --i) { |
113 | QQuickGridLayoutItem *item = static_cast<QQuickGridLayoutItem*>(q_items.at(i)); |
114 | if (item->layoutItem() == layoutItem) |
115 | return item; |
116 | } |
117 | return 0; |
118 | } |
119 | |
120 | void setAlignment(QQuickItem *quickItem, Qt::Alignment alignment); |
121 | |
122 | void setStretchFactor(QQuickItem *quickItem, int stretch, Qt::Orientation orientation); |
123 | |
124 | }; |
125 | |
126 | |
127 | |
128 | QT_END_NAMESPACE |
129 | |
130 | #endif // QQUICKGRIDLAYOUTENGINE_P_H |
131 | |