1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Quick Layouts module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QQUICKGRIDLAYOUTENGINE_P_H
41 #define QQUICKGRIDLAYOUTENGINE_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of the graphics view layout classes.  This header
49 // file may change from version to version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtGui/private/qgridlayoutengine_p.h>
55 #include <QtGui/private/qlayoutpolicy_p.h>
56 #include <QtCore/qmath.h>
57 
58 #include "qquickitem.h"
59 #include "qquicklayout_p.h"
60 #include "qdebug.h"
61 QT_BEGIN_NAMESPACE
62 
63 class QQuickGridLayoutItem : public QGridLayoutItem {
64 public:
65     QQuickGridLayoutItem(QQuickItem *item, int row, int column,
66                          int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = { })
QGridLayoutItem(row,column,rowSpan,columnSpan,alignment)67         : QGridLayoutItem(row, column, rowSpan, columnSpan, alignment), m_item(item), sizeHintCacheDirty(true), useFallbackToWidthOrHeight(true) {}
68 
69 
sizeHint(Qt::SizeHint which,const QSizeF & constraint)70     QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const override
71     {
72         Q_UNUSED(constraint);   // Quick Layouts does not support constraint atm
73         return effectiveSizeHints()[which];
74     }
75 
effectiveSizeHints()76     QSizeF *effectiveSizeHints() const
77     {
78         if (!sizeHintCacheDirty)
79             return cachedSizeHints;
80 
81         QQuickLayout::effectiveSizeHints_helper(m_item, cachedSizeHints, 0, useFallbackToWidthOrHeight);
82         useFallbackToWidthOrHeight = false;
83 
84         sizeHintCacheDirty = false;
85         return cachedSizeHints;
86     }
87 
setCachedSizeHints(QSizeF * sizeHints)88     void setCachedSizeHints(QSizeF *sizeHints)
89     {
90         for (int i = 0; i < Qt::NSizeHints; ++i) {
91             cachedSizeHints[i] = sizeHints[i];
92         }
93         sizeHintCacheDirty = false;
94     }
95 
invalidate()96     void invalidate()
97     {
98         qCDebug(lcQuickLayouts) << "QQuickGridLayoutItem::invalidate()";
99         sizeHintCacheDirty = true;
100     }
101 
sizePolicy(Qt::Orientation orientation)102     QLayoutPolicy::Policy sizePolicy(Qt::Orientation orientation) const override
103     {
104         return QQuickLayout::effectiveSizePolicy_helper(m_item, orientation, attachedLayoutObject(m_item, false));
105     }
106 
setGeometry(const QRectF & rect)107     void setGeometry(const QRectF &rect) override
108     {
109         QQuickLayoutAttached *info = attachedLayoutObject(m_item, false);
110         const QRectF r = info ? rect.marginsRemoved(info->effectiveQMargins()) : rect;
111         const QSizeF oldSize(m_item->width(), m_item->height());
112         const QSizeF newSize = r.size();
113         m_item->setPosition(r.topLeft());
114         if (newSize == oldSize) {
115             // We need to enforce a rearrange when the geometry is the same
116             if (QQuickLayout *lay = qobject_cast<QQuickLayout *>(m_item)) {
117                 if (lay->invalidatedArrangement())
118                     lay->rearrange(newSize);
119             }
120         } else {
121             m_item->setSize(newSize);
122         }
123     }
124 
layoutItem()125     QQuickItem *layoutItem() const { return m_item; }
126 
127     QQuickItem *m_item;
128 private:
129     mutable QSizeF cachedSizeHints[Qt::NSizeHints];
130     mutable unsigned sizeHintCacheDirty : 1;
131     mutable unsigned useFallbackToWidthOrHeight : 1;
132 };
133 
134 class QQuickGridLayoutEngine : public QGridLayoutEngine {
135 public:
QQuickGridLayoutEngine()136     QQuickGridLayoutEngine() : QGridLayoutEngine(Qt::AlignVCenter, true /*snapToPixelGrid*/) { }
137 
indexOf(QQuickItem * item)138     int indexOf(QQuickItem *item) const {
139         for (int i = 0; i < q_items.size(); ++i) {
140             if (item == static_cast<QQuickGridLayoutItem*>(q_items.at(i))->layoutItem())
141                 return i;
142         }
143         return -1;
144     }
145 
findLayoutItem(QQuickItem * layoutItem)146     QQuickGridLayoutItem *findLayoutItem(QQuickItem *layoutItem) const
147     {
148         for (int i = q_items.count() - 1; i >= 0; --i) {
149             QQuickGridLayoutItem *item = static_cast<QQuickGridLayoutItem*>(q_items.at(i));
150             if (item->layoutItem() == layoutItem)
151                 return item;
152         }
153         return 0;
154     }
155 
156     void setAlignment(QQuickItem *quickItem, Qt::Alignment alignment);
157     Qt::Alignment alignment(QQuickItem *quickItem) const;
158 
159 };
160 
161 
162 
163 QT_END_NAMESPACE
164 
165 #endif // QQUICKGRIDLAYOUTENGINE_P_H
166