1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
20 ** distribution.
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 ** the names of its contributors may be used to endorse or promote
23 ** products derived from this software without specific prior written
24 ** permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QWidget>
42
43 #include "flowlayout.h"
44 //! [1]
FlowLayout(QWidget * parent,int margin,int hSpacing,int vSpacing)45 FlowLayout::FlowLayout(QWidget* parent, int margin, int hSpacing, int vSpacing)
46 : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing) {
47 setContentsMargins(margin, margin, margin, margin);
48 }
49
FlowLayout(int margin,int hSpacing,int vSpacing)50 FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
51 : m_hSpace(hSpacing), m_vSpace(vSpacing) {
52 setContentsMargins(margin, margin, margin, margin);
53 }
54 //! [1]
55
56 //! [2]
~FlowLayout()57 FlowLayout::~FlowLayout() {
58 QLayoutItem* item;
59 while ((item = takeAt(0))) delete item;
60 }
61 //! [2]
62
63 //! [3]
addItem(QLayoutItem * item)64 void FlowLayout::addItem(QLayoutItem* item) { itemList.append(item); }
65 //! [3]
66
67 //! [4]
horizontalSpacing() const68 int FlowLayout::horizontalSpacing() const {
69 if (m_hSpace >= 0) {
70 return m_hSpace;
71 } else {
72 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
73 }
74 }
75
verticalSpacing() const76 int FlowLayout::verticalSpacing() const {
77 if (m_vSpace >= 0) {
78 return m_vSpace;
79 } else {
80 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
81 }
82 }
83 //! [4]
84
85 //! [5]
count() const86 int FlowLayout::count() const { return itemList.size(); }
87
itemAt(int index) const88 QLayoutItem* FlowLayout::itemAt(int index) const {
89 return itemList.value(index);
90 }
91
takeAt(int index)92 QLayoutItem* FlowLayout::takeAt(int index) {
93 if (index >= 0 && index < itemList.size())
94 return itemList.takeAt(index);
95 else
96 return 0;
97 }
98 //! [5]
99
100 //! [6]
expandingDirections() const101 Qt::Orientations FlowLayout::expandingDirections() const { return 0; }
102 //! [6]
103
104 //! [7]
hasHeightForWidth() const105 bool FlowLayout::hasHeightForWidth() const { return true; }
106
heightForWidth(int width) const107 int FlowLayout::heightForWidth(int width) const {
108 int height = doLayout(QRect(0, 0, width, 0), true);
109 return height;
110 }
111 //! [7]
112
113 //! [8]
setGeometry(const QRect & rect)114 void FlowLayout::setGeometry(const QRect& rect) {
115 QLayout::setGeometry(rect);
116 doLayout(rect, false);
117 }
118
sizeHint() const119 QSize FlowLayout::sizeHint() const { return minimumSize(); }
120
minimumSize() const121 QSize FlowLayout::minimumSize() const {
122 QSize size;
123 for (QLayoutItem* item : itemList)
124 size = size.expandedTo(item->minimumSize());
125
126 size += QSize(2 * margin(), 2 * margin());
127 return size;
128 }
129 //! [8]
130
131 //! [9]
doLayout(const QRect & rect,bool testOnly) const132 int FlowLayout::doLayout(const QRect& rect, bool testOnly) const {
133 int left, top, right, bottom;
134 getContentsMargins(&left, &top, &right, &bottom);
135 QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
136 int x = effectiveRect.x();
137 int y = effectiveRect.y();
138 int lineHeight = 0;
139 //! [9]
140
141 //! [10]
142 for (QLayoutItem* item : itemList) {
143 QWidget* wid = item->widget();
144 int spaceX = horizontalSpacing();
145 if (spaceX == -1)
146 spaceX = wid->style()->layoutSpacing(
147 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
148 int spaceY = verticalSpacing();
149 if (spaceY == -1)
150 spaceY = wid->style()->layoutSpacing(
151 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
152 //! [10]
153 //! [11]
154 int nextX = x + item->sizeHint().width() + spaceX;
155 if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
156 x = effectiveRect.x();
157 y = y + lineHeight + spaceY;
158 nextX = x + item->sizeHint().width() + spaceX;
159 lineHeight = 0;
160 }
161
162 if (!testOnly) item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
163
164 x = nextX;
165 lineHeight = qMax(lineHeight, item->sizeHint().height());
166 }
167 return y + lineHeight - rect.y() + bottom;
168 }
169 //! [11]
170 //! [12]
smartSpacing(QStyle::PixelMetric pm) const171 int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const {
172 QObject* parent = this->parent();
173 if (!parent) {
174 return -1;
175 } else if (parent->isWidgetType()) {
176 QWidget* pw = static_cast<QWidget*>(parent);
177 return pw->style()->pixelMetric(pm, 0, pw);
178 } else {
179 return static_cast<QLayout*>(parent)->spacing();
180 }
181 }
182 //! [12]
183