Mercurial > projects > qtd
annotate examples/layouts/borderlayout/borderlayout.d @ 322:7c2cf27391c4 signals
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
author | eldar_ins@eldar-laptop |
---|---|
date | Sun, 27 Dec 2009 21:13:38 +0500 |
parents | 6aeaf24018d7 |
children | 778ef7374fb5 |
rev | line source |
---|---|
118 | 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 | |
204 | 55 enum Position { West, North, South, East, Center }; |
118 | 56 |
204 | 57 this(QWidget parent, int margin = 0, int spacing = -1) |
58 { | |
59 super(parent); | |
60 setMargin(margin); | |
61 setWidgetSpacing(spacing); | |
62 } | |
118 | 63 |
204 | 64 this(int spacing = -1) |
65 { | |
66 setWidgetSpacing(spacing); | |
67 } | |
118 | 68 |
204 | 69 ~this() |
70 { | |
71 QLayoutItem l = takeAt(0); | |
72 while (l) { | |
73 delete l; | |
74 l = takeAt(0); | |
75 } | |
76 } | |
118 | 77 |
204 | 78 void addWidget(QWidget widget, Position position) |
79 { | |
80 add(cast(IQLayoutItem) new QWidgetItem(widget), position); | |
81 } | |
118 | 82 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
83 void add(IQLayoutItem item, Position position) |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
84 { |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
85 list ~= new ItemWrapper(item, position); |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
86 } |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
87 |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
88 override |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
89 { |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
90 void addItem(IQLayoutItem item) |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
91 { |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
92 add(item, Position.West); |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
93 } |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
94 |
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
95 int expandingDirections() const |
204 | 96 { |
97 return Qt.Horizontal | Qt.Vertical; | |
98 } | |
118 | 99 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
100 bool hasHeightForWidth() const |
204 | 101 { |
102 return false; | |
103 } | |
118 | 104 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
105 int count() const |
204 | 106 { |
107 return list.length; | |
108 } | |
118 | 109 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
110 IQLayoutItem itemAt(int index) const |
204 | 111 { |
112 if(index >= 0 && index < list.length) | |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
113 return cast(IQLayoutItem) list[index].item; |
204 | 114 else |
115 return null; | |
116 } | |
118 | 117 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
118 QSize minimumSize() const |
204 | 119 { |
120 return calculateSize(SizeType.MinimumSize); | |
121 } | |
118 | 122 |
204 | 123 void setGeometry(const QRect rect) |
124 { | |
125 ItemWrapper center = null; | |
126 int eastWidth = 0; | |
127 int westWidth = 0; | |
128 int northHeight = 0; | |
129 int southHeight = 0; | |
130 int centerHeight = 0; | |
131 int i; | |
118 | 132 |
204 | 133 super.setGeometry(rect); |
118 | 134 |
204 | 135 for (i = 0; i < list.length; ++i) { |
136 ItemWrapper wrapper = list[i]; | |
137 IQLayoutItem item = wrapper.item; | |
138 Position position = wrapper.position; | |
118 | 139 |
204 | 140 if (position == Position.North) { |
141 item.setGeometry(QRect(rect.x(), northHeight, rect.width(), | |
142 item.sizeHint().height())); | |
118 | 143 |
204 | 144 northHeight += item.geometry().height() + widgetSpacing(); |
145 } else if (position == Position.South) { | |
146 item.setGeometry(QRect(item.geometry().x(), | |
147 item.geometry().y(), rect.width(), | |
148 item.sizeHint().height())); | |
118 | 149 |
204 | 150 southHeight += item.geometry().height() + widgetSpacing(); |
118 | 151 |
204 | 152 item.setGeometry(QRect(rect.x(), |
153 rect.y() + rect.height() - southHeight + widgetSpacing(), | |
154 item.geometry().width(), | |
155 item.geometry().height())); | |
156 } else if (position == Position.Center) { | |
157 center = wrapper; | |
158 } | |
159 } | |
118 | 160 |
204 | 161 centerHeight = rect.height() - northHeight - southHeight; |
118 | 162 |
204 | 163 for (i = 0; i < list.length; ++i) { |
164 ItemWrapper wrapper = list[i]; | |
165 IQLayoutItem item = wrapper.item; | |
166 Position position = wrapper.position; | |
118 | 167 |
204 | 168 if (position == Position.West) { |
169 item.setGeometry(QRect(rect.x() + westWidth, northHeight, | |
170 item.sizeHint().width(), centerHeight)); | |
118 | 171 |
204 | 172 westWidth += item.geometry().width() + widgetSpacing(); |
173 } else if (position == Position.East) { | |
174 item.setGeometry(QRect(item.geometry().x(), item.geometry().y(), | |
175 item.sizeHint().width(), centerHeight)); | |
118 | 176 |
204 | 177 eastWidth += item.geometry().width() + widgetSpacing(); |
118 | 178 |
204 | 179 item.setGeometry(QRect( |
180 rect.x() + rect.width() - eastWidth + widgetSpacing(), | |
181 northHeight, item.geometry().width(), | |
182 item.geometry().height())); | |
183 } | |
184 } | |
118 | 185 |
204 | 186 if (center) |
187 center.item.setGeometry(QRect(westWidth, northHeight, | |
188 rect.width() - eastWidth - westWidth, | |
189 centerHeight)); | |
190 } | |
118 | 191 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
192 QSize sizeHint() const |
204 | 193 { |
194 return calculateSize(SizeType.SizeHint); | |
195 } | |
118 | 196 |
204 | 197 QLayoutItem takeAt(int index) |
198 { | |
199 if (index >= 0 && index < list.length) { | |
200 ItemWrapper layoutStruct = list[index]; | |
201 return layoutStruct.item; | |
202 } | |
203 return null; | |
204 } | |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
205 } |
118 | 206 |
207 private: | |
208 | |
204 | 209 class ItemWrapper |
210 { | |
211 this(IQLayoutItem i, Position p) | |
212 { | |
213 item = i; | |
214 position = p; | |
215 } | |
118 | 216 |
204 | 217 IQLayoutItem item; |
218 Position position; | |
219 }; | |
118 | 220 |
204 | 221 enum SizeType { MinimumSize, SizeHint }; |
118 | 222 |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
223 QSize calculateSize(SizeType sizeType) const |
204 | 224 { |
225 QSize totalSize; | |
118 | 226 |
204 | 227 for (int i = 0; i < list.length; ++i) { |
322
7c2cf27391c4
A slight change of the connect syntax. More sofisticated signals/slots lookup. Some fixes in examples.
eldar_ins@eldar-laptop
parents:
204
diff
changeset
|
228 const ItemWrapper wrapper = list[i]; |
204 | 229 Position position = wrapper.position; |
230 QSize itemSize; | |
118 | 231 |
204 | 232 if (sizeType == SizeType.MinimumSize) |
233 itemSize = wrapper.item.minimumSize(); | |
234 else // (sizeType == SizeHint) | |
235 itemSize = wrapper.item.sizeHint(); | |
118 | 236 |
204 | 237 if (position == Position.North || position == Position.South || position == Position.Center) |
238 totalSize.setHeight(totalSize.height + itemSize.height); | |
239 | |
240 if (position == Position.West || position == Position.East || position == Position.Center) | |
241 totalSize.setWidth(totalSize.width + itemSize.width); | |
242 } | |
243 return totalSize; | |
244 } | |
118 | 245 |
204 | 246 ItemWrapper[] list; |
118 | 247 } |