Merge "tests: Remove Linux-specific includes for dlsym"
[trojita.git] / src / Gui / FlowLayout.cpp
blob4878f9be16d1eefbddb713d81c5dad41ce259b70
1 /* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
2 All rights reserved.
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$
11 Commercial Usage
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.
39 $QT_END_LICENSE$
42 #include <QWidget>
44 #include "FlowLayout.h"
46 namespace Gui
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()
63 QLayoutItem *item;
64 while ((item = takeAt(0)))
65 delete item;
68 void FlowLayout::addItem(QLayoutItem *item)
70 itemList.append(item);
73 int FlowLayout::horizontalSpacing() const
75 if (m_hSpace >= 0) {
76 return m_hSpace;
77 } else {
78 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
82 int FlowLayout::verticalSpacing() const
84 if (m_vSpace >= 0) {
85 return m_vSpace;
86 } else {
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);
105 else
106 return 0;
109 Qt::Orientations FlowLayout::expandingDirections() const
111 return 0;
114 bool FlowLayout::hasHeightForWidth() const
116 return true;
119 int FlowLayout::heightForWidth(int width) const
121 int height = doLayout(QRect(0, 0, width, 0), true);
122 return height;
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
138 QSize size;
139 QLayoutItem *item;
140 foreach(item, itemList)
141 size = size.expandedTo(item->minimumSize());
143 size += QSize(2*margin(), 2*margin());
144 return size;
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();
154 int lineHeight = 0;
156 QLayoutItem *item;
157 foreach(item, itemList) {
158 QWidget *wid = item->widget();
159 int spaceX = horizontalSpacing();
160 if (spaceX == -1)
161 spaceX = wid->style()->layoutSpacing(
162 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
163 int spaceY = verticalSpacing();
164 if (spaceY == -1)
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;
172 lineHeight = 0;
175 if (!testOnly)
176 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
178 x = nextX;
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();
186 if (!parent) {
187 return -1;
188 } else if (parent->isWidgetType()) {
189 QWidget *pw = static_cast<QWidget *>(parent);
190 return pw->style()->pixelMetric(pm, 0, pw);
191 } else {
192 return static_cast<QLayout *>(parent)->spacing();
196 } // namespace Gui