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 size
+= QSize(2 * margin(), 2 * margin());
154 int FlowLayout::doLayout(const QRect
&rect
, bool testOnly
) const
156 int left
, top
, right
, bottom
;
157 getContentsMargins(&left
, &top
, &right
, &bottom
);
159 QRect effectiveRect
= rect
.adjusted(left
, top
, -right
, -bottom
);
160 int x
= effectiveRect
.x();
161 int y
= effectiveRect
.y();
164 for (QLayoutItem
* item
: itemList
) {
165 QWidget
* w
= item
->widget();
167 int spaceX
= horizontalSpacing();
169 spaceX
= w
->style()->layoutSpacing(
170 QSizePolicy::PushButton
, QSizePolicy::PushButton
, Qt::Horizontal
);
172 int spaceY
= verticalSpacing();
174 spaceY
= w
->style()->layoutSpacing(
175 QSizePolicy::PushButton
, QSizePolicy::PushButton
, Qt::Vertical
);
177 int nextX
= x
+ item
->sizeHint().width() + spaceX
;
178 if (((nextX
- spaceX
) > effectiveRect
.right()) && (lineHeight
> 0)) {
179 x
= effectiveRect
.x();
180 y
= y
+ lineHeight
+ spaceY
;
181 nextX
= x
+ item
->sizeHint().width() + spaceX
;
186 item
->setGeometry(QRect(QPoint(x
, y
), item
->sizeHint()));
189 lineHeight
= qMax(lineHeight
, item
->sizeHint().height());
192 int height
= y
+ lineHeight
- rect
.y() + bottom
;
195 m_parent
->setMinimumHeight(height
);
200 int FlowLayout::smartSpacing(QStyle::PixelMetric pm
) const
202 QObject
*parent
= this->parent();
207 if (parent
->isWidgetType()) {
208 QWidget
*pw
= qobject_cast
<QWidget
*>(parent
);
209 return pw
->style()->pixelMetric(pm
, nullptr, pw
);
211 return static_cast<QLayout
*>(parent
)->spacing();