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