YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
flowlayout.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
10/****************************************************************************
11**
12** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
13** Contact: http://www.qt-project.org/legal
14**
15** This file is part of the examples of the Qt Toolkit.
16**
17** $QT_BEGIN_LICENSE:BSD$
18** You may use this file under the terms of the BSD license as follows:
19**
20** "Redistribution and use in source and binary forms, with or without
21** modification, are permitted provided that the following conditions are
22** met:
23** * Redistributions of source code must retain the above copyright
24** notice, this list of conditions and the following disclaimer.
25** * Redistributions in binary form must reproduce the above copyright
26** notice, this list of conditions and the following disclaimer in
27** the documentation and/or other materials provided with the
28** distribution.
29** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
30** of its contributors may be used to endorse or promote products derived
31** from this software without specific prior written permission.
32**
33**
34** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
45**
46** $QT_END_LICENSE$
47**
48****************************************************************************/
49
50#include <QtWidgets>
51
52#include "flowlayout.h"
53
54FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
55 : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
56{
57 setContentsMargins(margin, margin, margin, margin);
58}
59
60FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
61 : m_hSpace(hSpacing), m_vSpace(vSpacing)
62{
63 setContentsMargins(margin, margin, margin, margin);
64}
65
66
67
69{
70 QLayoutItem *item;
71 while ((item = takeAt(0))) {
72 delete item;
73 }
74}
75
76
77
78void FlowLayout::addItem(QLayoutItem *item)
79{
80 itemList.append(item);
81}
82
83
84
86{
87 if (m_hSpace >= 0) {
88 return m_hSpace;
89 } else {
90 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
91 }
92}
93
95{
96 if (m_vSpace >= 0) {
97 return m_vSpace;
98 } else {
99 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
100 }
101}
102
103
104
106{
107 return itemList.size();
108}
109
110QLayoutItem *FlowLayout::itemAt(int index) const
111{
112 return itemList.value(index);
113}
114
115QLayoutItem *FlowLayout::takeAt(int index)
116{
117 if (index >= 0 && index < itemList.size()) {
118 return itemList.takeAt(index);
119 } else {
120 return nullptr;
121 }
122}
123
124
125
126Qt::Orientations FlowLayout::expandingDirections() const
127{
128#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
129 return {};
130#else
131 return nullptr;
132#endif
133}
134
135
137{
138 return true;
139}
140
141int FlowLayout::heightForWidth(int width) const
142{
143 int height = doLayout(QRect(0, 0, width, 0), true);
144 return height;
145}
146
147void FlowLayout::setGeometry(const QRect &rect)
148{
149 QLayout::setGeometry(rect);
150 doLayout(rect, false);
151}
152
154{
155 return minimumSize();
156}
157
159{
160 QSize size;
161 QLayoutItem *item;
162 foreach (item, itemList)
163 size = size.expandedTo(item->minimumSize());
164
165 size += QSize(2*margin(), 2*margin());
166 return size;
167}
168
169int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
170{
171 int left, top, right, bottom;
172 getContentsMargins(&left, &top, &right, &bottom);
173 QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
174 int x = effectiveRect.x();
175 int y = effectiveRect.y();
176 int lineHeight = 0;
177
178 QLayoutItem *item;
179 foreach (item, itemList) {
180 QWidget *wid = item->widget();
181 int spaceX = horizontalSpacing();
182 if (spaceX == -1) {
183 spaceX = wid->style()->layoutSpacing(
184 QSizePolicy::PushButton,
185 QSizePolicy::PushButton,
186 Qt::Horizontal);
187 }
188 int spaceY = verticalSpacing();
189 if (spaceY == -1) {
190 spaceY = wid->style()->layoutSpacing(
191 QSizePolicy::PushButton,
192 QSizePolicy::PushButton,
193 Qt::Vertical);
194 }
195
196 int nextX = x + item->sizeHint().width() + spaceX;
197 if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
198 x = effectiveRect.x();
199 y = y + lineHeight + spaceY;
200 nextX = x + item->sizeHint().width() + spaceX;
201 lineHeight = 0;
202 }
203
204 if (!testOnly) {
205 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
206 }
207
208 x = nextX;
209 lineHeight = qMax(lineHeight, item->sizeHint().height());
210 }
211 return y + lineHeight - rect.y() + bottom;
212}
213
214int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
215{
216 QObject *parent = this->parent();
217 if (!parent) {
218 return -1;
219 } else if (parent->isWidgetType()) {
220 auto* pw = static_cast<QWidget *>(parent);
221 return pw->style()->pixelMetric(pm, nullptr, pw);
222 } else {
223 return static_cast<QLayout *>(parent)->spacing();
224 }
225}
int horizontalSpacing() const
int verticalSpacing() const
int heightForWidth(int) const override
QLayoutItem * itemAt(int index) const override
QSize minimumSize() const override
bool hasHeightForWidth() const override
int count() const override
QSize sizeHint() const override
void setGeometry(const QRect &rect) override
QLayoutItem * takeAt(int index) override
Qt::Orientations expandingDirections() const override
void addItem(QLayoutItem *item) override
FlowLayout(QWidget *parent, int margin=-1, int hSpacing=-1, int vSpacing=-1)
Original license follows: