flowlayout.cpp
Go to the documentation of this file.
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 examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "flowlayout.h"
52
53#include <QtWidgets>
54
55//! [1]
56FlowLayout::FlowLayout(QWidget* parent, int margin, int hSpacing, int vSpacing) :
57 QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
58{
59 setContentsMargins(margin, margin, margin, margin);
60}
61
62FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) :
63 m_hSpace(hSpacing), m_vSpace(vSpacing)
64{
65 setContentsMargins(margin, margin, margin, margin);
66}
67
68//! [1]
69
70//! [2]
72{
73 QLayoutItem* item;
74 while ((item = takeAt(0)))
75 delete item;
76}
77
78//! [2]
79
80//! [3]
81void
82FlowLayout::addItem(QLayoutItem* item)
83{
84 itemList.append(item);
85}
86
87//! [3]
88
89//! [4]
90int
92{
93 if (m_hSpace >= 0)
94 {
95 return m_hSpace;
96 }
97 else
98 {
99 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
100 }
101}
102
103int
105{
106 if (m_vSpace >= 0)
107 {
108 return m_vSpace;
109 }
110 else
111 {
112 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
113 }
114}
115
116//! [4]
117
118//! [5]
119int
121{
122 return itemList.size();
123}
124
125QLayoutItem*
127{
128 return itemList.value(index);
129}
130
131QLayoutItem*
133{
134 if (index >= 0 && index < itemList.size())
135 return itemList.takeAt(index);
136 return nullptr;
137}
138
139//! [5]
140
141//! [6]
142Qt::Orientations
144{
145 return {};
146}
147
148//! [6]
149
150//! [7]
151bool
153{
154 return true;
155}
156
157int
159{
160 int height = doLayout(QRect(0, 0, width, 0), true);
161 return height;
162}
163
164//! [7]
165
166//! [8]
167void
168FlowLayout::setGeometry(const QRect& rect)
169{
170 QLayout::setGeometry(rect);
171 doLayout(rect, false);
172}
173
174QSize
176{
177 return minimumSize();
178}
179
180QSize
182{
183 QSize size;
184 for (const QLayoutItem* item : qAsConst(itemList))
185 size = size.expandedTo(item->minimumSize());
186
187 const QMargins margins = contentsMargins();
188 size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
189 return size;
190}
191
192//! [8]
193
194//! [9]
195int
196FlowLayout::doLayout(const QRect& rect, bool testOnly) const
197{
198 int left, top, right, bottom;
199 getContentsMargins(&left, &top, &right, &bottom);
200 QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
201 int x = effectiveRect.x();
202 int y = effectiveRect.y();
203 int lineHeight = 0;
204 //! [9]
205
206 //! [10]
207 for (QLayoutItem* item : qAsConst(itemList))
208 {
209 const QWidget* wid = item->widget();
210 int spaceX = horizontalSpacing();
211 if (spaceX == -1)
212 spaceX = wid->style()->layoutSpacing(
213 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
214 int spaceY = verticalSpacing();
215 if (spaceY == -1)
216 spaceY = wid->style()->layoutSpacing(
217 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
218 //! [10]
219 //! [11]
220 int nextX = x + item->sizeHint().width() + spaceX;
221 if (nextX - spaceX > effectiveRect.right() && lineHeight > 0)
222 {
223 x = effectiveRect.x();
224 y = y + lineHeight + spaceY;
225 nextX = x + item->sizeHint().width() + spaceX;
226 lineHeight = 0;
227 }
228
229 if (!testOnly)
230 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
231
232 x = nextX;
233 lineHeight = qMax(lineHeight, item->sizeHint().height());
234 }
235 return y + lineHeight - rect.y() + bottom;
236}
237
238//! [11]
239//! [12]
240int
241FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
242{
243 QObject* parent = this->parent();
244 if (!parent)
245 {
246 return -1;
247 }
248 else if (parent->isWidgetType())
249 {
250 QWidget* pw = static_cast<QWidget*>(parent);
251 return pw->style()->pixelMetric(pm, nullptr, pw);
252 }
253 else
254 {
255 return static_cast<QLayout*>(parent)->spacing();
256 }
257}
258
259//! [12]
uint8_t index
~FlowLayout()
[1]
void addItem(QLayoutItem *item) override
[2]
Qt::Orientations expandingDirections() const override
[5]
QSize sizeHint() const override
int count() const override
[4]
int heightForWidth(int) const override
bool hasHeightForWidth() const override
[6]
FlowLayout(QWidget *parent, int margin=-1, int hSpacing=-1, int vSpacing=-1)
[1]
QLayoutItem * itemAt(int index) const override
int horizontalSpacing() const
[3]
int verticalSpacing() const
QLayoutItem * takeAt(int index) override
void setGeometry(const QRect &rect) override
[7]
QSize minimumSize() const override
This file offers overloads of toIce() and fromIce() functions for STL container types.