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 | |
55 | FlowLayout::FlowLayout(QGraphicsLayoutItem *parent) : QGraphicsLayout(parent) |
56 | { |
57 | QSizePolicy sp = sizePolicy(); |
58 | sp.setHeightForWidth(true); |
59 | setSizePolicy(sp); |
60 | } |
61 | |
62 | void 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(index, item); |
68 | invalidate(); |
69 | } |
70 | |
71 | int FlowLayout::count() const |
72 | { |
73 | return m_items.count(); |
74 | } |
75 | |
76 | QGraphicsLayoutItem *FlowLayout::itemAt(int index) const |
77 | { |
78 | return m_items.value(index); |
79 | } |
80 | |
81 | void FlowLayout::removeAt(int index) |
82 | { |
83 | m_items.removeAt(index); |
84 | invalidate(); |
85 | } |
86 | |
87 | qreal FlowLayout::spacing(Qt::Orientation o) const |
88 | { |
89 | return m_spacing[int(o) - 1]; |
90 | } |
91 | |
92 | void 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 | |
100 | void FlowLayout::setGeometry(const QRectF &geom) |
101 | { |
102 | QGraphicsLayout::setGeometry(geom); |
103 | doLayout(geom, true); |
104 | } |
105 | |
106 | qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const |
107 | { |
108 | qreal left, top, right, bottom; |
109 | getContentsMargins(&left, &top, &right, &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(Qt::PreferredSize); |
118 | maxRowHeight = qMax(maxRowHeight, pref.height()); |
119 | |
120 | qreal next_x; |
121 | next_x = x + pref.width(); |
122 | if (next_x > maxw) { |
123 | if (qFuzzyIsNull(x)) { |
124 | pref.setWidth(maxw); |
125 | } else { |
126 | x = 0; |
127 | next_x = pref.width(); |
128 | } |
129 | y += maxRowHeight + spacing(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(Qt::Horizontal); |
136 | } |
137 | maxRowHeight = qMax(maxRowHeight, pref.height()); |
138 | return top + y + maxRowHeight + bottom; |
139 | } |
140 | |
141 | QSizeF FlowLayout::minSize(const QSizeF &constraint) const |
142 | { |
143 | QSizeF size(0, 0); |
144 | qreal left, top, right, bottom; |
145 | getContentsMargins(&left, &top, &right, &bottom); |
146 | if (constraint.width() >= 0) { // height for width |
147 | const qreal height = doLayout(QRectF(QPointF(0,0), constraint), 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(m_items)) |
153 | size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); |
154 | size += QSizeF(left + right, top + bottom); |
155 | } |
156 | return size; |
157 | } |
158 | |
159 | QSizeF FlowLayout::prefSize() const |
160 | { |
161 | qreal left, right; |
162 | getContentsMargins(&left, nullptr, &right, nullptr); |
163 | |
164 | qreal maxh = 0; |
165 | qreal totalWidth = 0; |
166 | for (const QGraphicsLayoutItem *item : qAsConst(m_items)) { |
167 | if (totalWidth > 0) |
168 | totalWidth += spacing(Qt::Horizontal); |
169 | QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); |
170 | totalWidth += pref.width(); |
171 | maxh = qMax(maxh, pref.height()); |
172 | } |
173 | maxh += spacing(Qt::Vertical); |
174 | |
175 | const qreal goldenAspectRatio = 1.61803399; |
176 | qreal w = qSqrt(totalWidth * maxh * goldenAspectRatio) + left + right; |
177 | |
178 | return minSize(QSizeF(w, -1)); |
179 | } |
180 | |
181 | QSizeF FlowLayout::maxSize() const |
182 | { |
183 | qreal totalWidth = 0; |
184 | qreal totalHeight = 0; |
185 | for (const QGraphicsLayoutItem *item : qAsConst(m_items)) { |
186 | if (totalWidth > 0) |
187 | totalWidth += spacing(Qt::Horizontal); |
188 | if (totalHeight > 0) |
189 | totalHeight += spacing(Qt::Vertical); |
190 | QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); |
191 | totalWidth += pref.width(); |
192 | totalHeight += pref.height(); |
193 | } |
194 | |
195 | qreal left, top, right, bottom; |
196 | getContentsMargins(&left, &top, &right, &bottom); |
197 | return QSizeF(left + totalWidth + right, top + totalHeight + bottom); |
198 | } |
199 | |
200 | QSizeF 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 | |