1 /* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3 Contact: Nokia Corporation (qt-info@nokia.com)
5 This file is part of the Trojita Qt IMAP e-mail client,
6 http://trojita.flaska.net/
8 It was taken from the examples of the Qt Toolkit.
10 $QT_BEGIN_LICENSE:LGPL$
12 Licensees holding valid Qt Commercial licenses may use this file in
13 accordance with the Qt Commercial License Agreement provided with the
14 Software or, alternatively, in accordance with the terms contained in
15 a written agreement between you and Nokia.
17 GNU Lesser General Public License Usage
18 Alternatively, this file may be used under the terms of the GNU Lesser
19 General Public License version 2.1 as published by the Free Software
20 Foundation and appearing in the file LICENSE.LGPL included in the
21 packaging of this file. Please review the following information to
22 ensure the GNU Lesser General Public License version 2.1 requirements
23 will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 In addition, as a special exception, Nokia gives you certain additional
26 rights. These rights are described in the Nokia Qt LGPL Exception
27 version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 GNU General Public License Usage
30 Alternatively, this file may be used under the terms of the GNU
31 General Public License version 3.0 as published by the Free Software
32 Foundation and appearing in the file LICENSE.GPL included in the
33 packaging of this file. Please review the following information to
34 ensure the GNU General Public License version 3.0 requirements will be
35 met: http://www.gnu.org/copyleft/gpl.html.
37 If you have questions regarding the use of this file, please contact
38 Nokia at qt-info@nokia.com.
44 #include "FlowLayout.h"
49 FlowLayout::FlowLayout(QWidget
*parent
, int margin
, int hSpacing
, int vSpacing
)
50 : QLayout(parent
), m_hSpace(hSpacing
), m_vSpace(vSpacing
)
52 setContentsMargins(margin
, margin
, margin
, margin
);
55 FlowLayout::FlowLayout(int margin
, int hSpacing
, int vSpacing
)
56 : m_hSpace(hSpacing
), m_vSpace(vSpacing
)
58 setContentsMargins(margin
, margin
, margin
, margin
);
61 FlowLayout::~FlowLayout()
64 while ((item
= takeAt(0)))
68 void FlowLayout::addItem(QLayoutItem
*item
)
70 itemList
.append(item
);
73 int FlowLayout::horizontalSpacing() const
78 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing
);
82 int FlowLayout::verticalSpacing() const
87 return smartSpacing(QStyle::PM_LayoutVerticalSpacing
);
91 int FlowLayout::count() const
93 return itemList
.size();
96 QLayoutItem
*FlowLayout::itemAt(int index
) const
98 return itemList
.value(index
);
101 QLayoutItem
*FlowLayout::takeAt(int index
)
103 if (index
>= 0 && index
< itemList
.size())
104 return itemList
.takeAt(index
);
109 Qt::Orientations
FlowLayout::expandingDirections() const
114 bool FlowLayout::hasHeightForWidth() const
119 int FlowLayout::heightForWidth(int width
) const
121 int height
= doLayout(QRect(0, 0, width
, 0), true);
125 void FlowLayout::setGeometry(const QRect
&rect
)
127 QLayout::setGeometry(rect
);
128 doLayout(rect
, false);
131 QSize
FlowLayout::sizeHint() const
133 return minimumSize();
136 QSize
FlowLayout::minimumSize() const
140 foreach(item
, itemList
)
141 size
= size
.expandedTo(item
->minimumSize());
143 size
+= QSize(2*margin(), 2*margin());
147 int FlowLayout::doLayout(const QRect
&rect
, bool testOnly
) const
149 int left
, top
, right
, bottom
;
150 getContentsMargins(&left
, &top
, &right
, &bottom
);
151 QRect effectiveRect
= rect
.adjusted(+left
, +top
, -right
, -bottom
);
152 int x
= effectiveRect
.x();
153 int y
= effectiveRect
.y();
157 foreach(item
, itemList
) {
158 QWidget
*wid
= item
->widget();
159 int spaceX
= horizontalSpacing();
161 spaceX
= wid
->style()->layoutSpacing(
162 QSizePolicy::PushButton
, QSizePolicy::PushButton
, Qt::Horizontal
);
163 int spaceY
= verticalSpacing();
165 spaceY
= wid
->style()->layoutSpacing(
166 QSizePolicy::PushButton
, QSizePolicy::PushButton
, Qt::Vertical
);
167 int nextX
= x
+ item
->sizeHint().width() + spaceX
;
168 if (nextX
- spaceX
> effectiveRect
.right() && lineHeight
> 0) {
169 x
= effectiveRect
.x();
170 y
= y
+ lineHeight
+ spaceY
;
171 nextX
= x
+ item
->sizeHint().width() + spaceX
;
176 item
->setGeometry(QRect(QPoint(x
, y
), item
->sizeHint()));
179 lineHeight
= qMax(lineHeight
, item
->sizeHint().height());
181 return y
+ lineHeight
- rect
.y() + bottom
;
183 int FlowLayout::smartSpacing(QStyle::PixelMetric pm
) const
185 QObject
*parent
= this->parent();
188 } else if (parent
->isWidgetType()) {
189 QWidget
*pw
= static_cast<QWidget
*>(parent
);
190 return pw
->style()->pixelMetric(pm
, 0, pw
);
192 return static_cast<QLayout
*>(parent
)->spacing();