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 "borderlayout.h"
52
53BorderLayout::BorderLayout(QWidget *parent, const QMargins &margins, int spacing)
54 : QLayout(parent)
55{
56 setContentsMargins(margins);
57 setSpacing(spacing);
58}
59
60BorderLayout::BorderLayout(int spacing)
61{
62 setSpacing(spacing);
63}
64
65
66BorderLayout::~BorderLayout()
67{
68 QLayoutItem *l;
69 while ((l = takeAt(index: 0)))
70 delete l;
71}
72
73void BorderLayout::addItem(QLayoutItem *item)
74{
75 add(item, position: West);
76}
77
78void BorderLayout::addWidget(QWidget *widget, Position position)
79{
80 add(item: new QWidgetItem(widget), position);
81}
82
83Qt::Orientations BorderLayout::expandingDirections() const
84{
85 return Qt::Horizontal | Qt::Vertical;
86}
87
88bool BorderLayout::hasHeightForWidth() const
89{
90 return false;
91}
92
93int BorderLayout::count() const
94{
95 return list.size();
96}
97
98QLayoutItem *BorderLayout::itemAt(int index) const
99{
100 ItemWrapper *wrapper = list.value(i: index);
101 return wrapper ? wrapper->item : nullptr;
102}
103
104QSize BorderLayout::minimumSize() const
105{
106 return calculateSize(sizeType: MinimumSize);
107}
108
109void BorderLayout::setGeometry(const QRect &rect)
110{
111 ItemWrapper *center = nullptr;
112 int eastWidth = 0;
113 int westWidth = 0;
114 int northHeight = 0;
115 int southHeight = 0;
116 int centerHeight = 0;
117 int i;
118
119 QLayout::setGeometry(rect);
120
121 for (i = 0; i < list.size(); ++i) {
122 ItemWrapper *wrapper = list.at(i);
123 QLayoutItem *item = wrapper->item;
124 Position position = wrapper->position;
125
126 if (position == North) {
127 item->setGeometry(QRect(rect.x(), northHeight, rect.width(),
128 item->sizeHint().height()));
129
130 northHeight += item->geometry().height() + spacing();
131 } else if (position == South) {
132 item->setGeometry(QRect(item->geometry().x(),
133 item->geometry().y(), rect.width(),
134 item->sizeHint().height()));
135
136 southHeight += item->geometry().height() + spacing();
137
138 item->setGeometry(QRect(rect.x(),
139 rect.y() + rect.height() - southHeight + spacing(),
140 item->geometry().width(),
141 item->geometry().height()));
142 } else if (position == Center) {
143 center = wrapper;
144 }
145 }
146
147 centerHeight = rect.height() - northHeight - southHeight;
148
149 for (i = 0; i < list.size(); ++i) {
150 ItemWrapper *wrapper = list.at(i);
151 QLayoutItem *item = wrapper->item;
152 Position position = wrapper->position;
153
154 if (position == West) {
155 item->setGeometry(QRect(rect.x() + westWidth, northHeight,
156 item->sizeHint().width(), centerHeight));
157
158 westWidth += item->geometry().width() + spacing();
159 } else if (position == East) {
160 item->setGeometry(QRect(item->geometry().x(), item->geometry().y(),
161 item->sizeHint().width(), centerHeight));
162
163 eastWidth += item->geometry().width() + spacing();
164
165 item->setGeometry(QRect(
166 rect.x() + rect.width() - eastWidth + spacing(),
167 northHeight, item->geometry().width(),
168 item->geometry().height()));
169 }
170 }
171
172 if (center)
173 center->item->setGeometry(QRect(westWidth, northHeight,
174 rect.width() - eastWidth - westWidth,
175 centerHeight));
176}
177
178QSize BorderLayout::sizeHint() const
179{
180 return calculateSize(sizeType: SizeHint);
181}
182
183QLayoutItem *BorderLayout::takeAt(int index)
184{
185 if (index >= 0 && index < list.size()) {
186 ItemWrapper *layoutStruct = list.takeAt(i: index);
187 return layoutStruct->item;
188 }
189 return nullptr;
190}
191
192void BorderLayout::add(QLayoutItem *item, Position position)
193{
194 list.append(t: new ItemWrapper(item, position));
195}
196
197QSize BorderLayout::calculateSize(SizeType sizeType) const
198{
199 QSize totalSize;
200
201 for (int i = 0; i < list.size(); ++i) {
202 ItemWrapper *wrapper = list.at(i);
203 Position position = wrapper->position;
204 QSize itemSize;
205
206 if (sizeType == MinimumSize)
207 itemSize = wrapper->item->minimumSize();
208 else // (sizeType == SizeHint)
209 itemSize = wrapper->item->sizeHint();
210
211 if (position == North || position == South || position == Center)
212 totalSize.rheight() += itemSize.height();
213
214 if (position == West || position == East || position == Center)
215 totalSize.rwidth() += itemSize.width();
216 }
217 return totalSize;
218}
219

source code of qtbase/examples/widgets/layouts/borderlayout/borderlayout.cpp