Mercurial > projects > qtd
comparison examples/layouts/borderlayout/borderlayout.d @ 118:c28c0340fdf3
add borderlayout example
author | mandel |
---|---|
date | Thu, 04 Jun 2009 21:29:08 +0000 |
parents | |
children | 6aeaf24018d7 |
comparison
equal
deleted
inserted
replaced
117:670414f31f5f | 118:c28c0340fdf3 |
---|---|
1 /**************************************************************************** | |
2 ** | |
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Qt Software Information (qt-info@nokia.com) | |
5 ** | |
6 ** This file is part of the examples of the Qt Toolkit. | |
7 ** | |
8 ** $QT_BEGIN_LICENSE:LGPL$ | |
9 ** Commercial Usage | |
10 ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 ** accordance with the Qt Commercial License Agreement provided with the | |
12 ** Software or, alternatively, in accordance with the terms contained in | |
13 ** a written agreement between you and Nokia. | |
14 ** | |
15 ** GNU Lesser General Public License Usage | |
16 ** Alternatively, this file may be used under the terms of the GNU Lesser | |
17 ** General Public License version 2.1 as published by the Free Software | |
18 ** Foundation and appearing in the file LICENSE.LGPL included in the | |
19 ** packaging of this file. Please review the following information to | |
20 ** ensure the GNU Lesser General Public License version 2.1 requirements | |
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
22 ** | |
23 ** In addition, as a special exception, Nokia gives you certain | |
24 ** additional rights. These rights are described in the Nokia Qt LGPL | |
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | |
26 ** package. | |
27 ** | |
28 ** GNU General Public License Usage | |
29 ** Alternatively, this file may be used under the terms of the GNU | |
30 ** General Public License version 3.0 as published by the Free Software | |
31 ** Foundation and appearing in the file LICENSE.GPL included in the | |
32 ** packaging of this file. Please review the following information to | |
33 ** ensure the GNU General Public License version 3.0 requirements will be | |
34 ** met: http://www.gnu.org/copyleft/gpl.html. | |
35 ** | |
36 ** If you are unsure which license is appropriate for your use, please | |
37 ** contact the sales department at qt-sales@nokia.com. | |
38 ** $QT_END_LICENSE$ | |
39 ** | |
40 ****************************************************************************/ | |
41 module borderlayout; | |
42 | |
43 | |
44 import qt.gui.QLayout; | |
45 import qt.gui.QWidgetItem; | |
46 import qt.gui.QTextBrowser; | |
47 import qt.core.QSize; | |
48 import qt.core.QRect; | |
49 | |
50 | |
51 class BorderLayout : public QLayout | |
52 { | |
53 public: | |
54 | |
55 enum Position { West, North, South, East, Center }; | |
56 | |
57 this(QWidget parent, int margin = 0, int spacing = -1) | |
58 { | |
59 super(parent); | |
60 setMargin(margin); | |
61 setWidgetSpacing(spacing); | |
62 } | |
63 | |
64 this(int spacing = -1) | |
65 { | |
66 setWidgetSpacing(spacing); | |
67 } | |
68 | |
69 ~this() | |
70 { | |
71 QLayoutItem l = takeAt(0); | |
72 while (l) { | |
73 delete l; | |
74 l = takeAt(0); | |
75 } | |
76 } | |
77 | |
78 void addItem(IQLayoutItem item) | |
79 { | |
80 add(item, Position.West); | |
81 } | |
82 | |
83 void addWidget(QWidget widget, Position position) | |
84 { | |
85 add(cast(IQLayoutItem) new QWidgetItem(widget), position); | |
86 } | |
87 | |
88 int expandingDirections() | |
89 { | |
90 return Qt.Horizontal | Qt.Vertical; | |
91 } | |
92 | |
93 bool hasHeightForWidth() | |
94 { | |
95 return false; | |
96 } | |
97 | |
98 int count() | |
99 { | |
100 return list.length; | |
101 } | |
102 | |
103 QLayoutItem itemAt(int index) | |
104 { | |
105 if(index >= 0 && index < list.length) | |
106 return list[index].item; | |
107 else | |
108 return null; | |
109 } | |
110 | |
111 QSize minimumSize() | |
112 { | |
113 return calculateSize(SizeType.MinimumSize); | |
114 } | |
115 | |
116 void setGeometry(QRect rect) | |
117 { | |
118 ItemWrapper center = null; | |
119 int eastWidth = 0; | |
120 int westWidth = 0; | |
121 int northHeight = 0; | |
122 int southHeight = 0; | |
123 int centerHeight = 0; | |
124 int i; | |
125 | |
126 super.setGeometry(rect); | |
127 | |
128 for (i = 0; i < list.length; ++i) { | |
129 ItemWrapper wrapper = list[i]; | |
130 IQLayoutItem item = wrapper.item; | |
131 Position position = wrapper.position; | |
132 | |
133 if (position == Position.North) { | |
134 item.setGeometry(QRect(rect.x(), northHeight, rect.width(), | |
135 item.sizeHint().height())); | |
136 | |
137 northHeight += item.geometry().height() + widgetSpacing(); | |
138 } else if (position == Position.South) { | |
139 item.setGeometry(QRect(item.geometry().x(), | |
140 item.geometry().y(), rect.width(), | |
141 item.sizeHint().height())); | |
142 | |
143 southHeight += item.geometry().height() + widgetSpacing(); | |
144 | |
145 item.setGeometry(QRect(rect.x(), | |
146 rect.y() + rect.height() - southHeight + widgetSpacing(), | |
147 item.geometry().width(), | |
148 item.geometry().height())); | |
149 } else if (position == Position.Center) { | |
150 center = wrapper; | |
151 } | |
152 } | |
153 | |
154 centerHeight = rect.height() - northHeight - southHeight; | |
155 | |
156 for (i = 0; i < list.length; ++i) { | |
157 ItemWrapper wrapper = list[i]; | |
158 IQLayoutItem item = wrapper.item; | |
159 Position position = wrapper.position; | |
160 | |
161 if (position == Position.West) { | |
162 item.setGeometry(QRect(rect.x() + westWidth, northHeight, | |
163 item.sizeHint().width(), centerHeight)); | |
164 | |
165 westWidth += item.geometry().width() + widgetSpacing(); | |
166 } else if (position == Position.East) { | |
167 item.setGeometry(QRect(item.geometry().x(), item.geometry().y(), | |
168 item.sizeHint().width(), centerHeight)); | |
169 | |
170 eastWidth += item.geometry().width() + widgetSpacing(); | |
171 | |
172 item.setGeometry(QRect( | |
173 rect.x() + rect.width() - eastWidth + widgetSpacing(), | |
174 northHeight, item.geometry().width(), | |
175 item.geometry().height())); | |
176 } | |
177 } | |
178 | |
179 if (center) | |
180 center.item.setGeometry(QRect(westWidth, northHeight, | |
181 rect.width() - eastWidth - westWidth, | |
182 centerHeight)); | |
183 } | |
184 | |
185 QSize sizeHint() | |
186 { | |
187 return calculateSize(SizeType.SizeHint); | |
188 } | |
189 | |
190 QLayoutItem takeAt(int index) | |
191 { | |
192 if (index >= 0 && index < list.length) { | |
193 ItemWrapper layoutStruct = list[index]; | |
194 return layoutStruct.item; | |
195 } | |
196 return null; | |
197 } | |
198 | |
199 void add(IQLayoutItem item, Position position) | |
200 { | |
201 list ~= new ItemWrapper(item, position); | |
202 } | |
203 | |
204 private: | |
205 | |
206 class ItemWrapper | |
207 { | |
208 this(IQLayoutItem i, Position p) | |
209 { | |
210 item = i; | |
211 position = p; | |
212 } | |
213 | |
214 IQLayoutItem item; | |
215 Position position; | |
216 }; | |
217 | |
218 enum SizeType { MinimumSize, SizeHint }; | |
219 | |
220 QSize calculateSize(SizeType sizeType) | |
221 { | |
222 QSize totalSize; | |
223 | |
224 for (int i = 0; i < list.length; ++i) { | |
225 ItemWrapper wrapper = list[i]; | |
226 Position position = wrapper.position; | |
227 QSize itemSize; | |
228 | |
229 if (sizeType == SizeType.MinimumSize) | |
230 itemSize = wrapper.item.minimumSize(); | |
231 else // (sizeType == SizeHint) | |
232 itemSize = wrapper.item.sizeHint(); | |
233 | |
234 if (position == Position.North || position == Position.South || position == Position.Center) | |
235 totalSize.setHeight(totalSize.height + itemSize.height); | |
236 | |
237 if (position == Position.West || position == Position.East || position == Position.Center) | |
238 totalSize.setWidth(totalSize.width + itemSize.width); | |
239 } | |
240 return totalSize; | |
241 } | |
242 | |
243 ItemWrapper[] list; | |
244 } |