comparison examples/layouts/borderlayout/borderlayout.d @ 204:6aeaf24018d7

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