1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
43 #include "flowlayout.hpp"
45 FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) :
51 setContentsMargins(margin, margin, margin, margin);
54 FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) :
59 setContentsMargins(margin, margin, margin, margin);
62 FlowLayout::~FlowLayout()
65 while ((item = takeAt(0)))
69 void FlowLayout::addItem(QLayoutItem *item)
71 itemList.append(item);
74 int FlowLayout::horizontalSpacing() const
79 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
82 int FlowLayout::verticalSpacing() const
87 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
90 int FlowLayout::count() const
92 return itemList.size();
95 QLayoutItem *FlowLayout::itemAt(int index) const
97 return itemList.value(index);
100 QLayoutItem *FlowLayout::takeAt(int index)
102 if ((index >= 0) && (index < itemList.size()))
103 return itemList.takeAt(index);
108 Qt::Orientations FlowLayout::expandingDirections() const
110 return Qt::Horizontal | Qt::Vertical;
113 bool FlowLayout::hasHeightForWidth() const
118 int FlowLayout::heightForWidth(int width) const
120 int height = doLayout(QRect(0, 0, width, 0), true);
124 void FlowLayout::setGeometry(const QRect &rect)
126 QLayout::setGeometry(rect);
127 doLayout(rect, false);
130 QSize FlowLayout::sizeHint() const
132 return minimumSize();
135 QSize FlowLayout::minimumSize() const
139 for (QLayoutItem* item : itemList) {
140 int w = item->geometry().x() + item->geometry().width();
141 if (w > size.width())
144 int h = item->geometry().y() + item->geometry().height();
145 if (h > size.height())
149 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
150 int left, top, right, bottom;
151 getContentsMargins(&left, &top, &right, &bottom);
152 size += QSize(left + right, top + bottom);
154 size += QSize(2 * margin(), 2 * margin());
160 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
162 int left, top, right, bottom;
163 getContentsMargins(&left, &top, &right, &bottom);
165 QRect effectiveRect = rect.adjusted(left, top, -right, -bottom);
166 int x = effectiveRect.x();
167 int y = effectiveRect.y();
170 for (QLayoutItem* item : itemList) {
171 QWidget* w = item->widget();
173 int spaceX = horizontalSpacing();
175 spaceX = w->style()->layoutSpacing(
176 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
178 int spaceY = verticalSpacing();
180 spaceY = w->style()->layoutSpacing(
181 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
183 int nextX = x + item->sizeHint().width() + spaceX;
184 if (((nextX - spaceX) > effectiveRect.right()) && (lineHeight > 0)) {
185 x = effectiveRect.x();
186 y = y + lineHeight + spaceY;
187 nextX = x + item->sizeHint().width() + spaceX;
192 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
195 lineHeight = qMax(lineHeight, item->sizeHint().height());
198 int height = y + lineHeight - rect.y() + bottom;
201 m_parent->setMinimumHeight(height);
206 int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
208 QObject *parent = this->parent();
213 if (parent->isWidgetType()) {
214 QWidget *pw = qobject_cast<QWidget*>(parent);
215 return pw->style()->pixelMetric(pm, nullptr, pw);
217 return static_cast<QLayout*>(parent)->spacing();