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 <QtMath>
54
55FlowLayout::FlowLayout(QGraphicsLayoutItem *parent) : QGraphicsLayout(parent)
56{
57 QSizePolicy sp = sizePolicy();
58 sp.setHeightForWidth(true);
59 setSizePolicy(sp);
60}
61
62void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item)
63{
64 item->setParentLayoutItem(this);
65 if (index > m_items.count() || index < 0)
66 index = m_items.count();
67 m_items.insert(i: index, t: item);
68 invalidate();
69}
70
71int FlowLayout::count() const
72{
73 return m_items.count();
74}
75
76QGraphicsLayoutItem *FlowLayout::itemAt(int index) const
77{
78 return m_items.value(i: index);
79}
80
81void FlowLayout::removeAt(int index)
82{
83 m_items.removeAt(i: index);
84 invalidate();
85}
86
87qreal FlowLayout::spacing(Qt::Orientation o) const
88{
89 return m_spacing[int(o) - 1];
90}
91
92void FlowLayout::setSpacing(Qt::Orientations o, qreal spacing)
93{
94 if (o & Qt::Horizontal)
95 m_spacing[0] = spacing;
96 if (o & Qt::Vertical)
97 m_spacing[1] = spacing;
98}
99
100void FlowLayout::setGeometry(const QRectF &geom)
101{
102 QGraphicsLayout::setGeometry(geom);
103 doLayout(geom, applyNewGeometry: true);
104}
105
106qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const
107{
108 qreal left, top, right, bottom;
109 getContentsMargins(left: &left, top: &top, right: &right, bottom: &bottom);
110 const qreal maxw = geom.width() - left - right;
111
112 qreal x = 0;
113 qreal y = 0;
114 qreal maxRowHeight = 0;
115 QSizeF pref;
116 for (QGraphicsLayoutItem *item : m_items) {
117 pref = item->effectiveSizeHint(which: Qt::PreferredSize);
118 maxRowHeight = qMax(a: maxRowHeight, b: pref.height());
119
120 qreal next_x;
121 next_x = x + pref.width();
122 if (next_x > maxw) {
123 if (qFuzzyIsNull(d: x)) {
124 pref.setWidth(maxw);
125 } else {
126 x = 0;
127 next_x = pref.width();
128 }
129 y += maxRowHeight + spacing(o: Qt::Vertical);
130 maxRowHeight = 0;
131 }
132
133 if (applyNewGeometry)
134 item->setGeometry(QRectF(QPointF(left + x, top + y), pref));
135 x = next_x + spacing(o: Qt::Horizontal);
136 }
137 maxRowHeight = qMax(a: maxRowHeight, b: pref.height());
138 return top + y + maxRowHeight + bottom;
139}
140
141QSizeF FlowLayout::minSize(const QSizeF &constraint) const
142{
143 QSizeF size(0, 0);
144 qreal left, top, right, bottom;
145 getContentsMargins(left: &left, top: &top, right: &right, bottom: &bottom);
146 if (constraint.width() >= 0) { // height for width
147 const qreal height = doLayout(geom: QRectF(QPointF(0,0), constraint), applyNewGeometry: false);
148 size = QSizeF(constraint.width(), height);
149 } else if (constraint.height() >= 0) { // width for height?
150 // not supported
151 } else {
152 for (const QGraphicsLayoutItem *item : qAsConst(t: m_items))
153 size = size.expandedTo(otherSize: item->effectiveSizeHint(which: Qt::MinimumSize));
154 size += QSizeF(left + right, top + bottom);
155 }
156 return size;
157}
158
159QSizeF FlowLayout::prefSize() const
160{
161 qreal left, right;
162 getContentsMargins(left: &left, top: nullptr, right: &right, bottom: nullptr);
163
164 qreal maxh = 0;
165 qreal totalWidth = 0;
166 for (const QGraphicsLayoutItem *item : qAsConst(t: m_items)) {
167 if (totalWidth > 0)
168 totalWidth += spacing(o: Qt::Horizontal);
169 QSizeF pref = item->effectiveSizeHint(which: Qt::PreferredSize);
170 totalWidth += pref.width();
171 maxh = qMax(a: maxh, b: pref.height());
172 }
173 maxh += spacing(o: Qt::Vertical);
174
175 const qreal goldenAspectRatio = 1.61803399;
176 qreal w = qSqrt(v: totalWidth * maxh * goldenAspectRatio) + left + right;
177
178 return minSize(constraint: QSizeF(w, -1));
179}
180
181QSizeF FlowLayout::maxSize() const
182{
183 qreal totalWidth = 0;
184 qreal totalHeight = 0;
185 for (const QGraphicsLayoutItem *item : qAsConst(t: m_items)) {
186 if (totalWidth > 0)
187 totalWidth += spacing(o: Qt::Horizontal);
188 if (totalHeight > 0)
189 totalHeight += spacing(o: Qt::Vertical);
190 QSizeF pref = item->effectiveSizeHint(which: Qt::PreferredSize);
191 totalWidth += pref.width();
192 totalHeight += pref.height();
193 }
194
195 qreal left, top, right, bottom;
196 getContentsMargins(left: &left, top: &top, right: &right, bottom: &bottom);
197 return QSizeF(left + totalWidth + right, top + totalHeight + bottom);
198}
199
200QSizeF FlowLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
201{
202 QSizeF sh = constraint;
203 switch (which) {
204 case Qt::PreferredSize:
205 sh = prefSize();
206 break;
207 case Qt::MinimumSize:
208 sh = minSize(constraint);
209 break;
210 case Qt::MaximumSize:
211 sh = maxSize();
212 break;
213 default:
214 break;
215 }
216 return sh;
217}
218

source code of qtbase/examples/widgets/graphicsview/flowlayout/flowlayout.cpp