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 QLAYOUTITEM_H
5#define QLAYOUTITEM_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtWidgets/qsizepolicy.h>
9#include <QtCore/qrect.h>
10
11#include <limits.h>
12
13QT_BEGIN_NAMESPACE
14
15
16inline constexpr int QLAYOUTSIZE_MAX = INT_MAX/256/16;
17
18class QLayout;
19class QLayoutItem;
20class QSpacerItem;
21class QWidget;
22class QSize;
23
24class Q_WIDGETS_EXPORT QLayoutItem
25{
26public:
27 inline explicit QLayoutItem(Qt::Alignment alignment = Qt::Alignment());
28 virtual ~QLayoutItem();
29 virtual QSize sizeHint() const = 0;
30 virtual QSize minimumSize() const = 0;
31 virtual QSize maximumSize() const = 0;
32 virtual Qt::Orientations expandingDirections() const = 0;
33 virtual void setGeometry(const QRect&) = 0;
34 virtual QRect geometry() const = 0;
35 virtual bool isEmpty() const = 0;
36 virtual bool hasHeightForWidth() const;
37 virtual int heightForWidth(int) const;
38 virtual int minimumHeightForWidth(int) const;
39 virtual void invalidate();
40
41 virtual QWidget *widget() const;
42 virtual QLayout *layout();
43 virtual QSpacerItem *spacerItem();
44
45 Qt::Alignment alignment() const { return align; }
46 void setAlignment(Qt::Alignment a);
47 virtual QSizePolicy::ControlTypes controlTypes() const;
48
49protected:
50 Qt::Alignment align;
51};
52
53inline QLayoutItem::QLayoutItem(Qt::Alignment aalignment)
54 : align(aalignment) { }
55
56class Q_WIDGETS_EXPORT QSpacerItem : public QLayoutItem
57{
58public:
59 QSpacerItem(int w, int h,
60 QSizePolicy::Policy hData = QSizePolicy::Minimum,
61 QSizePolicy::Policy vData = QSizePolicy::Minimum)
62 : width(w), height(h), sizeP(hData, vData) { }
63 ~QSpacerItem();
64
65 void changeSize(int w, int h,
66 QSizePolicy::Policy hData = QSizePolicy::Minimum,
67 QSizePolicy::Policy vData = QSizePolicy::Minimum);
68 QSize sizeHint() const override;
69 QSize minimumSize() const override;
70 QSize maximumSize() const override;
71 Qt::Orientations expandingDirections() const override;
72 bool isEmpty() const override;
73 void setGeometry(const QRect&) override;
74 QRect geometry() const override;
75 QSpacerItem *spacerItem() override;
76 QSizePolicy sizePolicy() const { return sizeP; }
77
78private:
79 int width;
80 int height;
81 QSizePolicy sizeP;
82 QRect rect;
83};
84
85class Q_WIDGETS_EXPORT QWidgetItem : public QLayoutItem
86{
87 Q_DISABLE_COPY(QWidgetItem)
88
89public:
90 explicit QWidgetItem(QWidget *w) : wid(w) { }
91 ~QWidgetItem();
92
93 QSize sizeHint() const override;
94 QSize minimumSize() const override;
95 QSize maximumSize() const override;
96 Qt::Orientations expandingDirections() const override;
97 bool isEmpty() const override;
98 void setGeometry(const QRect&) override;
99 QRect geometry() const override;
100 QWidget *widget() const override;
101
102 bool hasHeightForWidth() const override;
103 int heightForWidth(int) const override;
104 int minimumHeightForWidth(int) const override;
105 QSizePolicy::ControlTypes controlTypes() const override;
106protected:
107 QWidget *wid;
108};
109
110class Q_WIDGETS_EXPORT QWidgetItemV2 : public QWidgetItem
111{
112public:
113 explicit QWidgetItemV2(QWidget *widget);
114 ~QWidgetItemV2();
115
116 QSize sizeHint() const override;
117 QSize minimumSize() const override;
118 QSize maximumSize() const override;
119 int heightForWidth(int width) const override;
120
121private:
122 enum { Dirty = -123, HfwCacheMaxSize = 3 };
123
124 inline bool useSizeCache() const;
125 void updateCacheIfNecessary() const;
126 inline void invalidateSizeCache() {
127 q_cachedMinimumSize.setWidth(Dirty);
128 q_hfwCacheSize = 0;
129 }
130
131 mutable QSize q_cachedMinimumSize;
132 mutable QSize q_cachedSizeHint;
133 mutable QSize q_cachedMaximumSize;
134 mutable QSize q_cachedHfws[HfwCacheMaxSize];
135 mutable short q_firstCachedHfw;
136 mutable short q_hfwCacheSize;
137 void *d;
138
139 friend class QWidgetPrivate;
140
141 Q_DISABLE_COPY(QWidgetItemV2)
142};
143
144QT_END_NAMESPACE
145
146#endif // QLAYOUTITEM_H
147

source code of qtbase/src/widgets/kernel/qlayoutitem.h