1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
44 #include "borderlayout.h"
46 BorderLayout::BorderLayout(QWidget
*parent
, int margin
, int spacing
)
53 BorderLayout::BorderLayout(int spacing
)
59 BorderLayout::~BorderLayout()
62 while ((l
= takeAt(0)))
66 void BorderLayout::addItem(QLayoutItem
*item
)
71 void BorderLayout::addWidget(QWidget
*widget
, Position position
)
73 add(new QWidgetItem(widget
), position
);
76 Qt::Orientations
BorderLayout::expandingDirections() const
78 return Qt::Horizontal
| Qt::Vertical
;
81 bool BorderLayout::hasHeightForWidth() const
86 int BorderLayout::count() const
91 QLayoutItem
*BorderLayout::itemAt(int index
) const
93 ItemWrapper
*wrapper
= list
.value(index
);
100 QSize
BorderLayout::minimumSize() const
102 return calculateSize(MinimumSize
);
105 void BorderLayout::setGeometry(const QRect
&rect
)
107 ItemWrapper
*center
= 0;
112 int centerHeight
= 0;
115 QLayout::setGeometry(rect
);
117 for (i
= 0; i
< list
.size(); ++i
) {
118 ItemWrapper
*wrapper
= list
.at(i
);
119 QLayoutItem
*item
= wrapper
->item
;
120 Position position
= wrapper
->position
;
122 if (position
== North
) {
123 item
->setGeometry(QRect(rect
.x(), northHeight
, rect
.width(),
124 item
->sizeHint().height()));
126 northHeight
+= item
->geometry().height() + spacing();
127 } else if (position
== South
) {
128 item
->setGeometry(QRect(item
->geometry().x(),
129 item
->geometry().y(), rect
.width(),
130 item
->sizeHint().height()));
132 southHeight
+= item
->geometry().height() + spacing();
134 item
->setGeometry(QRect(rect
.x(),
135 rect
.y() + rect
.height() - southHeight
+ spacing(),
136 item
->geometry().width(),
137 item
->geometry().height()));
138 } else if (position
== Center
) {
143 centerHeight
= rect
.height() - northHeight
- southHeight
;
145 for (i
= 0; i
< list
.size(); ++i
) {
146 ItemWrapper
*wrapper
= list
.at(i
);
147 QLayoutItem
*item
= wrapper
->item
;
148 Position position
= wrapper
->position
;
150 if (position
== West
) {
151 item
->setGeometry(QRect(rect
.x() + westWidth
, northHeight
,
152 item
->sizeHint().width(), centerHeight
));
154 westWidth
+= item
->geometry().width() + spacing();
155 } else if (position
== East
) {
156 item
->setGeometry(QRect(item
->geometry().x(), item
->geometry().y(),
157 item
->sizeHint().width(), centerHeight
));
159 eastWidth
+= item
->geometry().width() + spacing();
161 item
->setGeometry(QRect(
162 rect
.x() + rect
.width() - eastWidth
+ spacing(),
163 northHeight
, item
->geometry().width(),
164 item
->geometry().height()));
169 center
->item
->setGeometry(QRect(westWidth
, northHeight
,
170 rect
.width() - eastWidth
- westWidth
,
174 QSize
BorderLayout::sizeHint() const
176 return calculateSize(SizeHint
);
179 QLayoutItem
*BorderLayout::takeAt(int index
)
181 if (index
>= 0 && index
< list
.size()) {
182 ItemWrapper
*layoutStruct
= list
.takeAt(index
);
183 return layoutStruct
->item
;
188 void BorderLayout::add(QLayoutItem
*item
, Position position
)
190 list
.append(new ItemWrapper(item
, position
));
193 QSize
BorderLayout::calculateSize(SizeType sizeType
) const
197 for (int i
= 0; i
< list
.size(); ++i
) {
198 ItemWrapper
*wrapper
= list
.at(i
);
199 Position position
= wrapper
->position
;
202 if (sizeType
== MinimumSize
)
203 itemSize
= wrapper
->item
->minimumSize();
204 else // (sizeType == SizeHint)
205 itemSize
= wrapper
->item
->sizeHint();
207 if (position
== North
|| position
== South
|| position
== Center
)
208 totalSize
.rheight() += itemSize
.height();
210 if (position
== West
|| position
== East
|| position
== Center
)
211 totalSize
.rwidth() += itemSize
.width();