1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module 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 [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qboxlayout.h"
|
---|
43 | #include "qapplication.h"
|
---|
44 | #include "qwidget.h"
|
---|
45 | #include "qlist.h"
|
---|
46 | #include "qsizepolicy.h"
|
---|
47 | #include "qvector.h"
|
---|
48 |
|
---|
49 | #include "qlayoutengine_p.h"
|
---|
50 | #include "qlayout_p.h"
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | /*
|
---|
55 | Returns true if the \a widget can be added to the \a layout;
|
---|
56 | otherwise returns false.
|
---|
57 | */
|
---|
58 | static bool checkWidget(QLayout *layout, QWidget *widget)
|
---|
59 | {
|
---|
60 | if (!widget) {
|
---|
61 | qWarning("QLayout: Cannot add null widget to %s/%s", layout->metaObject()->className(),
|
---|
62 | layout->objectName().toLocal8Bit().data());
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | struct QBoxLayoutItem
|
---|
69 | {
|
---|
70 | QBoxLayoutItem(QLayoutItem *it, int stretch_ = 0)
|
---|
71 | : item(it), stretch(stretch_), magic(false) { }
|
---|
72 | ~QBoxLayoutItem() { delete item; }
|
---|
73 |
|
---|
74 | int hfw(int w) {
|
---|
75 | if (item->hasHeightForWidth()) {
|
---|
76 | return item->heightForWidth(w);
|
---|
77 | } else {
|
---|
78 | return item->sizeHint().height();
|
---|
79 | }
|
---|
80 | }
|
---|
81 | int mhfw(int w) {
|
---|
82 | if (item->hasHeightForWidth()) {
|
---|
83 | return item->heightForWidth(w);
|
---|
84 | } else {
|
---|
85 | return item->minimumSize().height();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | int hStretch() {
|
---|
89 | if (stretch == 0 && item->widget()) {
|
---|
90 | return item->widget()->sizePolicy().horizontalStretch();
|
---|
91 | } else {
|
---|
92 | return stretch;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | int vStretch() {
|
---|
96 | if (stretch == 0 && item->widget()) {
|
---|
97 | return item->widget()->sizePolicy().verticalStretch();
|
---|
98 | } else {
|
---|
99 | return stretch;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | QLayoutItem *item;
|
---|
104 | int stretch;
|
---|
105 | bool magic;
|
---|
106 | };
|
---|
107 |
|
---|
108 | class QBoxLayoutPrivate : public QLayoutPrivate
|
---|
109 | {
|
---|
110 | Q_DECLARE_PUBLIC(QBoxLayout)
|
---|
111 | public:
|
---|
112 | QBoxLayoutPrivate() : hfwWidth(-1), dirty(true), spacing(-1) { }
|
---|
113 | ~QBoxLayoutPrivate();
|
---|
114 |
|
---|
115 | void setDirty() {
|
---|
116 | geomArray.clear();
|
---|
117 | hfwWidth = -1;
|
---|
118 | hfwHeight = -1;
|
---|
119 | dirty = true;
|
---|
120 | }
|
---|
121 |
|
---|
122 | QList<QBoxLayoutItem *> list;
|
---|
123 | QVector<QLayoutStruct> geomArray;
|
---|
124 | int hfwWidth;
|
---|
125 | int hfwHeight;
|
---|
126 | int hfwMinHeight;
|
---|
127 | QSize sizeHint;
|
---|
128 | QSize minSize;
|
---|
129 | QSize maxSize;
|
---|
130 | int leftMargin, topMargin, rightMargin, bottomMargin;
|
---|
131 | Qt::Orientations expanding;
|
---|
132 | uint hasHfw : 1;
|
---|
133 | uint dirty : 1;
|
---|
134 | QBoxLayout::Direction dir;
|
---|
135 | int spacing;
|
---|
136 |
|
---|
137 | inline void deleteAll() { while (!list.isEmpty()) delete list.takeFirst(); }
|
---|
138 |
|
---|
139 | void setupGeom();
|
---|
140 | void calcHfw(int);
|
---|
141 |
|
---|
142 | void effectiveMargins(int *left, int *top, int *right, int *bottom) const;
|
---|
143 | };
|
---|
144 |
|
---|
145 | QBoxLayoutPrivate::~QBoxLayoutPrivate()
|
---|
146 | {
|
---|
147 | }
|
---|
148 |
|
---|
149 | static inline bool horz(QBoxLayout::Direction dir)
|
---|
150 | {
|
---|
151 | return dir == QBoxLayout::RightToLeft || dir == QBoxLayout::LeftToRight;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * The purpose of this function is to make sure that widgets are not laid out outside its layout.
|
---|
156 | * E.g. the layoutItemRect margins are only meant to take of the surrounding margins/spacings.
|
---|
157 | * However, if the margin is 0, it can easily cover the area of a widget above it.
|
---|
158 | */
|
---|
159 | void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *bottom) const
|
---|
160 | {
|
---|
161 | int l = leftMargin;
|
---|
162 | int t = topMargin;
|
---|
163 | int r = rightMargin;
|
---|
164 | int b = bottomMargin;
|
---|
165 | #ifdef Q_WS_MAC
|
---|
166 | Q_Q(const QBoxLayout);
|
---|
167 | if (horz(dir)) {
|
---|
168 | QBoxLayoutItem *leftBox = 0;
|
---|
169 | QBoxLayoutItem *rightBox = 0;
|
---|
170 |
|
---|
171 | if (left || right) {
|
---|
172 | leftBox = list.value(0);
|
---|
173 | rightBox = list.value(list.count() - 1);
|
---|
174 | if (dir == QBoxLayout::RightToLeft)
|
---|
175 | qSwap(leftBox, rightBox);
|
---|
176 |
|
---|
177 | int leftDelta = 0;
|
---|
178 | int rightDelta = 0;
|
---|
179 | if (leftBox) {
|
---|
180 | QLayoutItem *itm = leftBox->item;
|
---|
181 | if (QWidget *w = itm->widget())
|
---|
182 | leftDelta = itm->geometry().left() - w->geometry().left();
|
---|
183 | }
|
---|
184 | if (rightBox) {
|
---|
185 | QLayoutItem *itm = rightBox->item;
|
---|
186 | if (QWidget *w = itm->widget())
|
---|
187 | rightDelta = w->geometry().right() - itm->geometry().right();
|
---|
188 | }
|
---|
189 | QWidget *w = q->parentWidget();
|
---|
190 | Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QApplication::layoutDirection();
|
---|
191 | if (layoutDirection == Qt::RightToLeft)
|
---|
192 | qSwap(leftDelta, rightDelta);
|
---|
193 |
|
---|
194 | l = qMax(l, leftDelta);
|
---|
195 | r = qMax(r, rightDelta);
|
---|
196 | }
|
---|
197 |
|
---|
198 | int count = top || bottom ? list.count() : 0;
|
---|
199 | for (int i = 0; i < count; ++i) {
|
---|
200 | QBoxLayoutItem *box = list.at(i);
|
---|
201 | QLayoutItem *itm = box->item;
|
---|
202 | QWidget *w = itm->widget();
|
---|
203 | if (w) {
|
---|
204 | QRect lir = itm->geometry();
|
---|
205 | QRect wr = w->geometry();
|
---|
206 | if (top)
|
---|
207 | t = qMax(t, lir.top() - wr.top());
|
---|
208 | if (bottom)
|
---|
209 | b = qMax(b, wr.bottom() - lir.bottom());
|
---|
210 | }
|
---|
211 | }
|
---|
212 | } else { // vertical layout
|
---|
213 | QBoxLayoutItem *topBox = 0;
|
---|
214 | QBoxLayoutItem *bottomBox = 0;
|
---|
215 |
|
---|
216 | if (top || bottom) {
|
---|
217 | topBox = list.value(0);
|
---|
218 | bottomBox = list.value(list.count() - 1);
|
---|
219 | if (dir == QBoxLayout::BottomToTop) {
|
---|
220 | qSwap(topBox, bottomBox);
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (top && topBox) {
|
---|
224 | QLayoutItem *itm = topBox->item;
|
---|
225 | QWidget *w = itm->widget();
|
---|
226 | if (w)
|
---|
227 | t = qMax(t, itm->geometry().top() - w->geometry().top());
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (bottom && bottomBox) {
|
---|
231 | QLayoutItem *itm = bottomBox->item;
|
---|
232 | QWidget *w = itm->widget();
|
---|
233 | if (w)
|
---|
234 | b = qMax(b, w->geometry().bottom() - itm->geometry().bottom());
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | int count = left || right ? list.count() : 0;
|
---|
239 | for (int i = 0; i < count; ++i) {
|
---|
240 | QBoxLayoutItem *box = list.at(i);
|
---|
241 | QLayoutItem *itm = box->item;
|
---|
242 | QWidget *w = itm->widget();
|
---|
243 | if (w) {
|
---|
244 | QRect lir = itm->geometry();
|
---|
245 | QRect wr = w->geometry();
|
---|
246 | if (left)
|
---|
247 | l = qMax(l, lir.left() - wr.left());
|
---|
248 | if (right)
|
---|
249 | r = qMax(r, wr.right() - lir.right());
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | #endif
|
---|
254 | if (left)
|
---|
255 | *left = l;
|
---|
256 | if (top)
|
---|
257 | *top = t;
|
---|
258 | if (right)
|
---|
259 | *right = r;
|
---|
260 | if (bottom)
|
---|
261 | *bottom = b;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /*
|
---|
266 | Initializes the data structure needed by qGeomCalc and
|
---|
267 | recalculates max/min and size hint.
|
---|
268 | */
|
---|
269 | void QBoxLayoutPrivate::setupGeom()
|
---|
270 | {
|
---|
271 | if (!dirty)
|
---|
272 | return;
|
---|
273 |
|
---|
274 | Q_Q(QBoxLayout);
|
---|
275 | int maxw = horz(dir) ? 0 : QLAYOUTSIZE_MAX;
|
---|
276 | int maxh = horz(dir) ? QLAYOUTSIZE_MAX : 0;
|
---|
277 | int minw = 0;
|
---|
278 | int minh = 0;
|
---|
279 | int hintw = 0;
|
---|
280 | int hinth = 0;
|
---|
281 |
|
---|
282 | bool horexp = false;
|
---|
283 | bool verexp = false;
|
---|
284 |
|
---|
285 | hasHfw = false;
|
---|
286 |
|
---|
287 | int n = list.count();
|
---|
288 | geomArray.clear();
|
---|
289 | QVector<QLayoutStruct> a(n);
|
---|
290 |
|
---|
291 | QSizePolicy::ControlTypes controlTypes1;
|
---|
292 | QSizePolicy::ControlTypes controlTypes2;
|
---|
293 | int fixedSpacing = q->spacing();
|
---|
294 | int previousNonEmptyIndex = -1;
|
---|
295 |
|
---|
296 | QStyle *style = 0;
|
---|
297 | if (fixedSpacing < 0) {
|
---|
298 | if (QWidget *parentWidget = q->parentWidget())
|
---|
299 | style = parentWidget->style();
|
---|
300 | }
|
---|
301 |
|
---|
302 | for (int i = 0; i < n; i++) {
|
---|
303 | QBoxLayoutItem *box = list.at(i);
|
---|
304 | QSize max = box->item->maximumSize();
|
---|
305 | QSize min = box->item->minimumSize();
|
---|
306 | QSize hint = box->item->sizeHint();
|
---|
307 | Qt::Orientations exp = box->item->expandingDirections();
|
---|
308 | bool empty = box->item->isEmpty();
|
---|
309 | int spacing = 0;
|
---|
310 |
|
---|
311 | if (!empty) {
|
---|
312 | if (fixedSpacing >= 0) {
|
---|
313 | spacing = (previousNonEmptyIndex >= 0) ? fixedSpacing : 0;
|
---|
314 | #ifdef Q_WS_MAC
|
---|
315 | if (!horz(dir) && previousNonEmptyIndex >= 0) {
|
---|
316 | QBoxLayoutItem *sibling = (dir == QBoxLayout::TopToBottom ? box : list.at(previousNonEmptyIndex));
|
---|
317 | if (sibling) {
|
---|
318 | QWidget *wid = sibling->item->widget();
|
---|
319 | if (wid)
|
---|
320 | spacing = qMax(spacing, sibling->item->geometry().top() - wid->geometry().top());
|
---|
321 | }
|
---|
322 | }
|
---|
323 | #endif
|
---|
324 | } else {
|
---|
325 | controlTypes1 = controlTypes2;
|
---|
326 | controlTypes2 = box->item->controlTypes();
|
---|
327 | if (previousNonEmptyIndex >= 0) {
|
---|
328 | QSizePolicy::ControlTypes actual1 = controlTypes1;
|
---|
329 | QSizePolicy::ControlTypes actual2 = controlTypes2;
|
---|
330 | if (dir == QBoxLayout::RightToLeft || dir == QBoxLayout::BottomToTop)
|
---|
331 | qSwap(actual1, actual2);
|
---|
332 |
|
---|
333 | if (style) {
|
---|
334 | spacing = style->combinedLayoutSpacing(actual1, actual2,
|
---|
335 | horz(dir) ? Qt::Horizontal : Qt::Vertical,
|
---|
336 | 0, q->parentWidget());
|
---|
337 | if (spacing < 0)
|
---|
338 | spacing = 0;
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (previousNonEmptyIndex >= 0)
|
---|
344 | a[previousNonEmptyIndex].spacing = spacing;
|
---|
345 | previousNonEmptyIndex = i;
|
---|
346 | }
|
---|
347 |
|
---|
348 | bool ignore = empty && box->item->widget(); // ignore hidden widgets
|
---|
349 | bool dummy = true;
|
---|
350 | if (horz(dir)) {
|
---|
351 | bool expand = (exp & Qt::Horizontal || box->stretch > 0);
|
---|
352 | horexp = horexp || expand;
|
---|
353 | maxw += spacing + max.width();
|
---|
354 | minw += spacing + min.width();
|
---|
355 | hintw += spacing + hint.width();
|
---|
356 | if (!ignore)
|
---|
357 | qMaxExpCalc(maxh, verexp, dummy,
|
---|
358 | max.height(), exp & Qt::Vertical, box->item->isEmpty());
|
---|
359 | minh = qMax(minh, min.height());
|
---|
360 | hinth = qMax(hinth, hint.height());
|
---|
361 |
|
---|
362 | a[i].sizeHint = hint.width();
|
---|
363 | a[i].maximumSize = max.width();
|
---|
364 | a[i].minimumSize = min.width();
|
---|
365 | a[i].expansive = expand;
|
---|
366 | a[i].stretch = box->stretch ? box->stretch : box->hStretch();
|
---|
367 | } else {
|
---|
368 | bool expand = (exp & Qt::Vertical || box->stretch > 0);
|
---|
369 | verexp = verexp || expand;
|
---|
370 | maxh += spacing + max.height();
|
---|
371 | minh += spacing + min.height();
|
---|
372 | hinth += spacing + hint.height();
|
---|
373 | if (!ignore)
|
---|
374 | qMaxExpCalc(maxw, horexp, dummy,
|
---|
375 | max.width(), exp & Qt::Horizontal, box->item->isEmpty());
|
---|
376 | minw = qMax(minw, min.width());
|
---|
377 | hintw = qMax(hintw, hint.width());
|
---|
378 |
|
---|
379 | a[i].sizeHint = hint.height();
|
---|
380 | a[i].maximumSize = max.height();
|
---|
381 | a[i].minimumSize = min.height();
|
---|
382 | a[i].expansive = expand;
|
---|
383 | a[i].stretch = box->stretch ? box->stretch : box->vStretch();
|
---|
384 | }
|
---|
385 |
|
---|
386 | a[i].empty = empty;
|
---|
387 | a[i].spacing = 0; // might be be initialized with a non-zero value in a later iteration
|
---|
388 | hasHfw = hasHfw || box->item->hasHeightForWidth();
|
---|
389 | }
|
---|
390 |
|
---|
391 | geomArray = a;
|
---|
392 |
|
---|
393 | expanding = (Qt::Orientations)
|
---|
394 | ((horexp ? Qt::Horizontal : 0)
|
---|
395 | | (verexp ? Qt::Vertical : 0));
|
---|
396 |
|
---|
397 | minSize = QSize(minw, minh);
|
---|
398 | maxSize = QSize(maxw, maxh).expandedTo(minSize);
|
---|
399 | sizeHint = QSize(hintw, hinth).expandedTo(minSize).boundedTo(maxSize);
|
---|
400 |
|
---|
401 | q->getContentsMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin);
|
---|
402 | int left, top, right, bottom;
|
---|
403 | effectiveMargins(&left, &top, &right, &bottom);
|
---|
404 | QSize extra(left + right, top + bottom);
|
---|
405 |
|
---|
406 | minSize += extra;
|
---|
407 | maxSize += extra;
|
---|
408 | sizeHint += extra;
|
---|
409 |
|
---|
410 | dirty = false;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /*
|
---|
414 | Calculates and stores the preferred height given the width \a w.
|
---|
415 | */
|
---|
416 | void QBoxLayoutPrivate::calcHfw(int w)
|
---|
417 | {
|
---|
418 | QVector<QLayoutStruct> &a = geomArray;
|
---|
419 | int n = a.count();
|
---|
420 | int h = 0;
|
---|
421 | int mh = 0;
|
---|
422 |
|
---|
423 | Q_ASSERT(n == list.size());
|
---|
424 |
|
---|
425 | if (horz(dir)) {
|
---|
426 | qGeomCalc(a, 0, n, 0, w);
|
---|
427 | for (int i = 0; i < n; i++) {
|
---|
428 | QBoxLayoutItem *box = list.at(i);
|
---|
429 | h = qMax(h, box->hfw(a.at(i).size));
|
---|
430 | mh = qMax(mh, box->mhfw(a.at(i).size));
|
---|
431 | }
|
---|
432 | } else {
|
---|
433 | for (int i = 0; i < n; ++i) {
|
---|
434 | QBoxLayoutItem *box = list.at(i);
|
---|
435 | int spacing = a.at(i).spacing;
|
---|
436 | h += box->hfw(w);
|
---|
437 | mh += box->mhfw(w);
|
---|
438 | h += spacing;
|
---|
439 | mh += spacing;
|
---|
440 | }
|
---|
441 | }
|
---|
442 | hfwWidth = w;
|
---|
443 | hfwHeight = h;
|
---|
444 | hfwMinHeight = mh;
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | /*!
|
---|
449 | \class QBoxLayout
|
---|
450 |
|
---|
451 | \brief The QBoxLayout class lines up child widgets horizontally or
|
---|
452 | vertically.
|
---|
453 |
|
---|
454 | \ingroup geomanagement
|
---|
455 | \ingroup appearance
|
---|
456 |
|
---|
457 | QBoxLayout takes the space it gets (from its parent layout or from
|
---|
458 | the parentWidget()), divides it up into a row of boxes, and makes
|
---|
459 | each managed widget fill one box.
|
---|
460 |
|
---|
461 | \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets
|
---|
462 |
|
---|
463 | If the QBoxLayout's orientation is Qt::Horizontal the boxes are
|
---|
464 | placed in a row, with suitable sizes. Each widget (or other box)
|
---|
465 | will get at least its minimum size and at most its maximum size.
|
---|
466 | Any excess space is shared according to the stretch factors (more
|
---|
467 | about that below).
|
---|
468 |
|
---|
469 | \image qvboxlayout-with-5-children.png Vertical box layout with five child widgets
|
---|
470 |
|
---|
471 | If the QBoxLayout's orientation is Qt::Vertical, the boxes are
|
---|
472 | placed in a column, again with suitable sizes.
|
---|
473 |
|
---|
474 | The easiest way to create a QBoxLayout is to use one of the
|
---|
475 | convenience classes, e.g. QHBoxLayout (for Qt::Horizontal boxes)
|
---|
476 | or QVBoxLayout (for Qt::Vertical boxes). You can also use the
|
---|
477 | QBoxLayout constructor directly, specifying its direction as
|
---|
478 | LeftToRight, RightToLeft, TopToBottom, or BottomToTop.
|
---|
479 |
|
---|
480 | If the QBoxLayout is not the top-level layout (i.e. it is not
|
---|
481 | managing all of the widget's area and children), you must add it
|
---|
482 | to its parent layout before you can do anything with it. The
|
---|
483 | normal way to add a layout is by calling
|
---|
484 | parentLayout-\>addLayout().
|
---|
485 |
|
---|
486 | Once you have done this, you can add boxes to the QBoxLayout using
|
---|
487 | one of four functions:
|
---|
488 |
|
---|
489 | \list
|
---|
490 | \o addWidget() to add a widget to the QBoxLayout and set the
|
---|
491 | widget's stretch factor. (The stretch factor is along the row of
|
---|
492 | boxes.)
|
---|
493 |
|
---|
494 | \o addSpacing() to create an empty box; this is one of the
|
---|
495 | functions you use to create nice and spacious dialogs. See below
|
---|
496 | for ways to set margins.
|
---|
497 |
|
---|
498 | \o addStretch() to create an empty, stretchable box.
|
---|
499 |
|
---|
500 | \o addLayout() to add a box containing another QLayout to the row
|
---|
501 | and set that layout's stretch factor.
|
---|
502 | \endlist
|
---|
503 |
|
---|
504 | Use insertWidget(), insertSpacing(), insertStretch() or
|
---|
505 | insertLayout() to insert a box at a specified position in the
|
---|
506 | layout.
|
---|
507 |
|
---|
508 | QBoxLayout also includes two margin widths:
|
---|
509 |
|
---|
510 | \list
|
---|
511 | \o setContentsMargins() sets the width of the outer border on
|
---|
512 | each side of the widget. This is the width of the reserved space
|
---|
513 | along each of the QBoxLayout's four sides.
|
---|
514 | \o setSpacing() sets the width between neighboring boxes. (You
|
---|
515 | can use addSpacing() to get more space at a particular spot.)
|
---|
516 | \endlist
|
---|
517 |
|
---|
518 | The margin default is provided by the style. The default margin
|
---|
519 | most Qt styles specify is 9 for child widgets and 11 for windows.
|
---|
520 | The spacing defaults to the same as the margin width for a
|
---|
521 | top-level layout, or to the same as the parent layout.
|
---|
522 |
|
---|
523 | To remove a widget from a layout, call removeWidget(). Calling
|
---|
524 | QWidget::hide() on a widget also effectively removes the widget
|
---|
525 | from the layout until QWidget::show() is called.
|
---|
526 |
|
---|
527 | You will almost always want to use QVBoxLayout and QHBoxLayout
|
---|
528 | rather than QBoxLayout because of their convenient constructors.
|
---|
529 |
|
---|
530 | \sa QGridLayout, QStackedLayout, {Layout Classes}
|
---|
531 | */
|
---|
532 |
|
---|
533 | /*!
|
---|
534 | \enum QBoxLayout::Direction
|
---|
535 |
|
---|
536 | This type is used to determine the direction of a box layout.
|
---|
537 |
|
---|
538 | \value LeftToRight Horizontal from left to right.
|
---|
539 | \value RightToLeft Horizontal from right to left.
|
---|
540 | \value TopToBottom Vertical from top to bottom.
|
---|
541 | \value BottomToTop Vertical from bottom to top.
|
---|
542 |
|
---|
543 | \omitvalue Down
|
---|
544 | \omitvalue Up
|
---|
545 | */
|
---|
546 |
|
---|
547 | /*!
|
---|
548 | Constructs a new QBoxLayout with direction \a dir and parent widget \a
|
---|
549 | parent.
|
---|
550 |
|
---|
551 | \sa direction()
|
---|
552 | */
|
---|
553 | QBoxLayout::QBoxLayout(Direction dir, QWidget *parent)
|
---|
554 | : QLayout(*new QBoxLayoutPrivate, 0, parent)
|
---|
555 | {
|
---|
556 | Q_D(QBoxLayout);
|
---|
557 | d->dir = dir;
|
---|
558 | }
|
---|
559 |
|
---|
560 | #ifdef QT3_SUPPORT
|
---|
561 | /*!
|
---|
562 | Constructs a new QBoxLayout with direction \a dir and main widget \a
|
---|
563 | parent. \a parent may not be 0.
|
---|
564 |
|
---|
565 | The \a margin is the number of pixels between the edge of the
|
---|
566 | widget and its managed children. The \a spacing is the default
|
---|
567 | number of pixels between neighboring children. If \a spacing is -1
|
---|
568 | the value of \a margin is used for \a spacing.
|
---|
569 |
|
---|
570 | \a name is the internal object name.
|
---|
571 |
|
---|
572 | \sa direction()
|
---|
573 | */
|
---|
574 | QBoxLayout::QBoxLayout(QWidget *parent, Direction dir,
|
---|
575 | int margin, int spacing, const char *name)
|
---|
576 | : QLayout(*new QBoxLayoutPrivate, 0, parent)
|
---|
577 | {
|
---|
578 | Q_D(QBoxLayout);
|
---|
579 | d->dir = dir;
|
---|
580 | setMargin(margin);
|
---|
581 | setObjectName(QString::fromAscii(name));
|
---|
582 | setSpacing(spacing<0 ? margin : spacing);
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*!
|
---|
586 | Constructs a new QBoxLayout called \a name, with direction \a dir,
|
---|
587 | and inserts it into \a parentLayout.
|
---|
588 |
|
---|
589 | The \a spacing is the default number of pixels between neighboring
|
---|
590 | children. If \a spacing is -1, the layout will inherit its
|
---|
591 | parent's spacing().
|
---|
592 | */
|
---|
593 | QBoxLayout::QBoxLayout(QLayout *parentLayout, Direction dir, int spacing,
|
---|
594 | const char *name)
|
---|
595 | : QLayout(*new QBoxLayoutPrivate, parentLayout, 0)
|
---|
596 | {
|
---|
597 | Q_D(QBoxLayout);
|
---|
598 | d->dir = dir;
|
---|
599 | setObjectName(QString::fromAscii(name));
|
---|
600 | setSpacing(spacing);
|
---|
601 | }
|
---|
602 |
|
---|
603 | /*!
|
---|
604 | Constructs a new QBoxLayout called \a name, with direction \a dir.
|
---|
605 |
|
---|
606 | If \a spacing is -1, the layout will inherit its parent's
|
---|
607 | spacing(); otherwise \a spacing is used.
|
---|
608 |
|
---|
609 | You must insert this box into another layout.
|
---|
610 | */
|
---|
611 | QBoxLayout::QBoxLayout(Direction dir, int spacing, const char *name)
|
---|
612 | : QLayout(*new QBoxLayoutPrivate,0, 0)
|
---|
613 | {
|
---|
614 | Q_D(QBoxLayout);
|
---|
615 | d->dir = dir;
|
---|
616 | setObjectName(QString::fromAscii(name));
|
---|
617 | setSpacing(spacing);
|
---|
618 | }
|
---|
619 | #endif // QT3_SUPPORT
|
---|
620 |
|
---|
621 |
|
---|
622 | /*!
|
---|
623 | Destroys this box layout.
|
---|
624 |
|
---|
625 | The layout's widgets aren't destroyed.
|
---|
626 | */
|
---|
627 | QBoxLayout::~QBoxLayout()
|
---|
628 | {
|
---|
629 | Q_D(QBoxLayout);
|
---|
630 | d->deleteAll(); // must do it before QObject deletes children, so can't be in ~QBoxLayoutPrivate
|
---|
631 | }
|
---|
632 |
|
---|
633 | /*!
|
---|
634 | Reimplements QLayout::spacing(). If the spacing property is
|
---|
635 | valid, that value is returned. Otherwise, a value for the spacing
|
---|
636 | property is computed and returned. Since layout spacing in a widget
|
---|
637 | is style dependent, if the parent is a widget, it queries the style
|
---|
638 | for the (horizontal or vertical) spacing of the layout. Otherwise,
|
---|
639 | the parent is a layout, and it queries the parent layout for the
|
---|
640 | spacing().
|
---|
641 |
|
---|
642 | \sa QLayout::spacing(), setSpacing()
|
---|
643 | */
|
---|
644 | int QBoxLayout::spacing() const
|
---|
645 | {
|
---|
646 | Q_D(const QBoxLayout);
|
---|
647 | if (d->spacing >=0) {
|
---|
648 | return d->spacing;
|
---|
649 | } else {
|
---|
650 | return qSmartSpacing(this, d->dir == LeftToRight || d->dir == RightToLeft
|
---|
651 | ? QStyle::PM_LayoutHorizontalSpacing
|
---|
652 | : QStyle::PM_LayoutVerticalSpacing);
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | /*!
|
---|
657 | Reimplements QLayout::setSpacing(). Sets the spacing
|
---|
658 | property to \a spacing.
|
---|
659 |
|
---|
660 | \sa QLayout::setSpacing(), spacing()
|
---|
661 | */
|
---|
662 | void QBoxLayout::setSpacing(int spacing)
|
---|
663 | {
|
---|
664 | Q_D(QBoxLayout);
|
---|
665 | d->spacing = spacing;
|
---|
666 | invalidate();
|
---|
667 | }
|
---|
668 |
|
---|
669 | /*!
|
---|
670 | \reimp
|
---|
671 | */
|
---|
672 | QSize QBoxLayout::sizeHint() const
|
---|
673 | {
|
---|
674 | Q_D(const QBoxLayout);
|
---|
675 | if (d->dirty)
|
---|
676 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
|
---|
677 | return d->sizeHint;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /*!
|
---|
681 | \reimp
|
---|
682 | */
|
---|
683 | QSize QBoxLayout::minimumSize() const
|
---|
684 | {
|
---|
685 | Q_D(const QBoxLayout);
|
---|
686 | if (d->dirty)
|
---|
687 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
|
---|
688 | return d->minSize;
|
---|
689 | }
|
---|
690 |
|
---|
691 | /*!
|
---|
692 | \reimp
|
---|
693 | */
|
---|
694 | QSize QBoxLayout::maximumSize() const
|
---|
695 | {
|
---|
696 | Q_D(const QBoxLayout);
|
---|
697 | if (d->dirty)
|
---|
698 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
|
---|
699 |
|
---|
700 | QSize s = d->maxSize.boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX));
|
---|
701 |
|
---|
702 | if (alignment() & Qt::AlignHorizontal_Mask)
|
---|
703 | s.setWidth(QLAYOUTSIZE_MAX);
|
---|
704 | if (alignment() & Qt::AlignVertical_Mask)
|
---|
705 | s.setHeight(QLAYOUTSIZE_MAX);
|
---|
706 | return s;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /*!
|
---|
710 | \reimp
|
---|
711 | */
|
---|
712 | bool QBoxLayout::hasHeightForWidth() const
|
---|
713 | {
|
---|
714 | Q_D(const QBoxLayout);
|
---|
715 | if (d->dirty)
|
---|
716 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
|
---|
717 | return d->hasHfw;
|
---|
718 | }
|
---|
719 |
|
---|
720 | /*!
|
---|
721 | \reimp
|
---|
722 | */
|
---|
723 | int QBoxLayout::heightForWidth(int w) const
|
---|
724 | {
|
---|
725 | Q_D(const QBoxLayout);
|
---|
726 | if (!hasHeightForWidth())
|
---|
727 | return -1;
|
---|
728 |
|
---|
729 | int left, top, right, bottom;
|
---|
730 | d->effectiveMargins(&left, &top, &right, &bottom);
|
---|
731 |
|
---|
732 | w -= left + right;
|
---|
733 | if (w != d->hfwWidth)
|
---|
734 | const_cast<QBoxLayout*>(this)->d_func()->calcHfw(w);
|
---|
735 |
|
---|
736 | return d->hfwHeight + top + bottom;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /*!
|
---|
740 | \reimp
|
---|
741 | */
|
---|
742 | int QBoxLayout::minimumHeightForWidth(int w) const
|
---|
743 | {
|
---|
744 | Q_D(const QBoxLayout);
|
---|
745 | (void) heightForWidth(w);
|
---|
746 | int top, bottom;
|
---|
747 | d->effectiveMargins(0, &top, 0, &bottom);
|
---|
748 | return d->hasHfw ? (d->hfwMinHeight + top + bottom) : -1;
|
---|
749 | }
|
---|
750 |
|
---|
751 | /*!
|
---|
752 | Resets cached information.
|
---|
753 | */
|
---|
754 | void QBoxLayout::invalidate()
|
---|
755 | {
|
---|
756 | Q_D(QBoxLayout);
|
---|
757 | d->setDirty();
|
---|
758 | QLayout::invalidate();
|
---|
759 | }
|
---|
760 |
|
---|
761 | /*!
|
---|
762 | \reimp
|
---|
763 | */
|
---|
764 | int QBoxLayout::count() const
|
---|
765 | {
|
---|
766 | Q_D(const QBoxLayout);
|
---|
767 | return d->list.count();
|
---|
768 | }
|
---|
769 |
|
---|
770 | /*!
|
---|
771 | \reimp
|
---|
772 | */
|
---|
773 | QLayoutItem *QBoxLayout::itemAt(int index) const
|
---|
774 | {
|
---|
775 | Q_D(const QBoxLayout);
|
---|
776 | return index >= 0 && index < d->list.count() ? d->list.at(index)->item : 0;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /*!
|
---|
780 | \reimp
|
---|
781 | */
|
---|
782 | QLayoutItem *QBoxLayout::takeAt(int index)
|
---|
783 | {
|
---|
784 | Q_D(QBoxLayout);
|
---|
785 | if (index < 0 || index >= d->list.count())
|
---|
786 | return 0;
|
---|
787 | QBoxLayoutItem *b = d->list.takeAt(index);
|
---|
788 | QLayoutItem *item = b->item;
|
---|
789 | b->item = 0;
|
---|
790 | delete b;
|
---|
791 |
|
---|
792 | invalidate();
|
---|
793 | return item;
|
---|
794 | }
|
---|
795 |
|
---|
796 |
|
---|
797 | /*!
|
---|
798 | \reimp
|
---|
799 | */
|
---|
800 | Qt::Orientations QBoxLayout::expandingDirections() const
|
---|
801 | {
|
---|
802 | Q_D(const QBoxLayout);
|
---|
803 | if (d->dirty)
|
---|
804 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
|
---|
805 | return d->expanding;
|
---|
806 | }
|
---|
807 |
|
---|
808 | /*!
|
---|
809 | \reimp
|
---|
810 | */
|
---|
811 | void QBoxLayout::setGeometry(const QRect &r)
|
---|
812 | {
|
---|
813 | Q_D(QBoxLayout);
|
---|
814 | if (d->dirty || r != geometry()) {
|
---|
815 | QRect oldRect = geometry();
|
---|
816 | QLayout::setGeometry(r);
|
---|
817 | if (d->dirty)
|
---|
818 | d->setupGeom();
|
---|
819 | QRect cr = alignment() ? alignmentRect(r) : r;
|
---|
820 |
|
---|
821 | int left, top, right, bottom;
|
---|
822 | d->effectiveMargins(&left, &top, &right, &bottom);
|
---|
823 | QRect s(cr.x() + left, cr.y() + top,
|
---|
824 | cr.width() - (left + right),
|
---|
825 | cr.height() - (top + bottom));
|
---|
826 |
|
---|
827 | QVector<QLayoutStruct> a = d->geomArray;
|
---|
828 | int pos = horz(d->dir) ? s.x() : s.y();
|
---|
829 | int space = horz(d->dir) ? s.width() : s.height();
|
---|
830 | int n = a.count();
|
---|
831 | if (d->hasHfw && !horz(d->dir)) {
|
---|
832 | for (int i = 0; i < n; i++) {
|
---|
833 | QBoxLayoutItem *box = d->list.at(i);
|
---|
834 | if (box->item->hasHeightForWidth())
|
---|
835 | a[i].sizeHint = a[i].minimumSize =
|
---|
836 | box->item->heightForWidth(s.width());
|
---|
837 | }
|
---|
838 | }
|
---|
839 |
|
---|
840 | Direction visualDir = d->dir;
|
---|
841 | QWidget *parent = parentWidget();
|
---|
842 | if (parent && parent->isRightToLeft()) {
|
---|
843 | if (d->dir == LeftToRight)
|
---|
844 | visualDir = RightToLeft;
|
---|
845 | else if (d->dir == RightToLeft)
|
---|
846 | visualDir = LeftToRight;
|
---|
847 | }
|
---|
848 |
|
---|
849 | qGeomCalc(a, 0, n, pos, space);
|
---|
850 |
|
---|
851 | bool reverse = (horz(visualDir)
|
---|
852 | ? ((r.right() > oldRect.right()) != (visualDir == RightToLeft))
|
---|
853 | : r.bottom() > oldRect.bottom());
|
---|
854 | for (int j = 0; j < n; j++) {
|
---|
855 | int i = reverse ? n-j-1 : j;
|
---|
856 | QBoxLayoutItem *box = d->list.at(i);
|
---|
857 |
|
---|
858 | switch (visualDir) {
|
---|
859 | case LeftToRight:
|
---|
860 | box->item->setGeometry(QRect(a.at(i).pos, s.y(), a.at(i).size, s.height()));
|
---|
861 | break;
|
---|
862 | case RightToLeft:
|
---|
863 | box->item->setGeometry(QRect(s.left() + s.right() - a.at(i).pos - a.at(i).size + 1,
|
---|
864 | s.y(), a.at(i).size, s.height()));
|
---|
865 | break;
|
---|
866 | case TopToBottom:
|
---|
867 | box->item->setGeometry(QRect(s.x(), a.at(i).pos, s.width(), a.at(i).size));
|
---|
868 | break;
|
---|
869 | case BottomToTop:
|
---|
870 | box->item->setGeometry(QRect(s.x(),
|
---|
871 | s.top() + s.bottom() - a.at(i).pos - a.at(i).size + 1,
|
---|
872 | s.width(), a.at(i).size));
|
---|
873 | }
|
---|
874 | }
|
---|
875 | }
|
---|
876 | }
|
---|
877 |
|
---|
878 | /*!
|
---|
879 | \reimp
|
---|
880 | */
|
---|
881 | void QBoxLayout::addItem(QLayoutItem *item)
|
---|
882 | {
|
---|
883 | Q_D(QBoxLayout);
|
---|
884 | QBoxLayoutItem *it = new QBoxLayoutItem(item);
|
---|
885 | d->list.append(it);
|
---|
886 | invalidate();
|
---|
887 | }
|
---|
888 |
|
---|
889 | /*!
|
---|
890 | Inserts \a item into this box layout at position \a index. If \a
|
---|
891 | index is negative, the item is added at the end.
|
---|
892 |
|
---|
893 | \sa addItem(), insertWidget(), insertLayout(), insertStretch(),
|
---|
894 | insertSpacing()
|
---|
895 | */
|
---|
896 | void QBoxLayout::insertItem(int index, QLayoutItem *item)
|
---|
897 | {
|
---|
898 | Q_D(QBoxLayout);
|
---|
899 | if (index < 0) // append
|
---|
900 | index = d->list.count();
|
---|
901 |
|
---|
902 | QBoxLayoutItem *it = new QBoxLayoutItem(item);
|
---|
903 | d->list.insert(index, it);
|
---|
904 | invalidate();
|
---|
905 | }
|
---|
906 |
|
---|
907 | /*!
|
---|
908 | Inserts a non-stretchable space (a QSpacerItem) at position \a index, with
|
---|
909 | size \a size. If \a index is negative the space is added at the end.
|
---|
910 |
|
---|
911 | The box layout has default margin and spacing. This function adds
|
---|
912 | additional space.
|
---|
913 |
|
---|
914 | \sa addSpacing(), insertItem(), QSpacerItem
|
---|
915 | */
|
---|
916 | void QBoxLayout::insertSpacing(int index, int size)
|
---|
917 | {
|
---|
918 | Q_D(QBoxLayout);
|
---|
919 | if (index < 0) // append
|
---|
920 | index = d->list.count();
|
---|
921 |
|
---|
922 | QLayoutItem *b;
|
---|
923 | if (horz(d->dir))
|
---|
924 | b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum);
|
---|
925 | else
|
---|
926 | b = QLayoutPrivate::createSpacerItem(this, 0, size, QSizePolicy::Minimum, QSizePolicy::Fixed);
|
---|
927 |
|
---|
928 | QBoxLayoutItem *it = new QBoxLayoutItem(b);
|
---|
929 | it->magic = true;
|
---|
930 | d->list.insert(index, it);
|
---|
931 | invalidate();
|
---|
932 | }
|
---|
933 |
|
---|
934 | /*!
|
---|
935 | Inserts a stretchable space (a QSpacerItem) at position \a
|
---|
936 | index, with zero minimum size and stretch factor \a stretch. If \a
|
---|
937 | index is negative the space is added at the end.
|
---|
938 |
|
---|
939 | \sa addStretch(), insertItem(), QSpacerItem
|
---|
940 | */
|
---|
941 | void QBoxLayout::insertStretch(int index, int stretch)
|
---|
942 | {
|
---|
943 | Q_D(QBoxLayout);
|
---|
944 | if (index < 0) // append
|
---|
945 | index = d->list.count();
|
---|
946 |
|
---|
947 | QLayoutItem *b;
|
---|
948 | if (horz(d->dir))
|
---|
949 | b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
---|
950 | else
|
---|
951 | b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
|
---|
952 |
|
---|
953 | QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch);
|
---|
954 | it->magic = true;
|
---|
955 | d->list.insert(index, it);
|
---|
956 | invalidate();
|
---|
957 | }
|
---|
958 |
|
---|
959 | /*!
|
---|
960 | \since 4.4
|
---|
961 |
|
---|
962 | Inserts \a spacerItem at position \a index, with zero minimum
|
---|
963 | size and stretch factor. If \a index is negative the
|
---|
964 | space is added at the end.
|
---|
965 |
|
---|
966 | \sa addSpacerItem(), insertStretch(), insertSpacing()
|
---|
967 | */
|
---|
968 | void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem)
|
---|
969 | {
|
---|
970 | Q_D(QBoxLayout);
|
---|
971 | if (index < 0) // append
|
---|
972 | index = d->list.count();
|
---|
973 |
|
---|
974 | QBoxLayoutItem *it = new QBoxLayoutItem(spacerItem);
|
---|
975 | it->magic = true;
|
---|
976 | d->list.insert(index, it);
|
---|
977 | invalidate();
|
---|
978 | }
|
---|
979 |
|
---|
980 | /*!
|
---|
981 | Inserts \a layout at position \a index, with stretch factor \a
|
---|
982 | stretch. If \a index is negative, the layout is added at the end.
|
---|
983 |
|
---|
984 | \a layout becomes a child of the box layout.
|
---|
985 |
|
---|
986 | \sa addLayout(), insertItem()
|
---|
987 | */
|
---|
988 | void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch)
|
---|
989 | {
|
---|
990 | Q_D(QBoxLayout);
|
---|
991 | addChildLayout(layout);
|
---|
992 | if (index < 0) // append
|
---|
993 | index = d->list.count();
|
---|
994 | QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch);
|
---|
995 | d->list.insert(index, it);
|
---|
996 | invalidate();
|
---|
997 | }
|
---|
998 |
|
---|
999 | /*!
|
---|
1000 | Inserts \a widget at position \a index, with stretch factor \a
|
---|
1001 | stretch and alignment \a alignment. If \a index is negative, the
|
---|
1002 | widget is added at the end.
|
---|
1003 |
|
---|
1004 | The stretch factor applies only in the \l{direction()}{direction}
|
---|
1005 | of the QBoxLayout, and is relative to the other boxes and widgets
|
---|
1006 | in this QBoxLayout. Widgets and boxes with higher stretch factors
|
---|
1007 | grow more.
|
---|
1008 |
|
---|
1009 | If the stretch factor is 0 and nothing else in the QBoxLayout has
|
---|
1010 | a stretch factor greater than zero, the space is distributed
|
---|
1011 | according to the QWidget:sizePolicy() of each widget that's
|
---|
1012 | involved.
|
---|
1013 |
|
---|
1014 | The alignment is specified by \a alignment. The default alignment
|
---|
1015 | is 0, which means that the widget fills the entire cell.
|
---|
1016 |
|
---|
1017 | \sa addWidget(), insertItem()
|
---|
1018 | */
|
---|
1019 | void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch,
|
---|
1020 | Qt::Alignment alignment)
|
---|
1021 | {
|
---|
1022 | Q_D(QBoxLayout);
|
---|
1023 | if (!checkWidget(this, widget))
|
---|
1024 | return;
|
---|
1025 | addChildWidget(widget);
|
---|
1026 | if (index < 0) // append
|
---|
1027 | index = d->list.count();
|
---|
1028 | QWidgetItem *b = QLayoutPrivate::createWidgetItem(this, widget);
|
---|
1029 | b->setAlignment(alignment);
|
---|
1030 | QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch);
|
---|
1031 | d->list.insert(index, it);
|
---|
1032 | invalidate();
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /*!
|
---|
1036 | Adds a non-stretchable space (a QSpacerItem) with size \a size
|
---|
1037 | to the end of this box layout. QBoxLayout provides default margin
|
---|
1038 | and spacing. This function adds additional space.
|
---|
1039 |
|
---|
1040 | \sa insertSpacing(), addItem(), QSpacerItem
|
---|
1041 | */
|
---|
1042 | void QBoxLayout::addSpacing(int size)
|
---|
1043 | {
|
---|
1044 | insertSpacing(-1, size);
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | /*!
|
---|
1048 | Adds a stretchable space (a QSpacerItem) with zero minimum
|
---|
1049 | size and stretch factor \a stretch to the end of this box layout.
|
---|
1050 |
|
---|
1051 | \sa insertStretch(), addItem(), QSpacerItem
|
---|
1052 | */
|
---|
1053 | void QBoxLayout::addStretch(int stretch)
|
---|
1054 | {
|
---|
1055 | insertStretch(-1, stretch);
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | /*!
|
---|
1059 | \since 4.4
|
---|
1060 |
|
---|
1061 | Adds \a spacerItem to the end of this box layout.
|
---|
1062 |
|
---|
1063 | \sa addSpacing(), addStretch()
|
---|
1064 | */
|
---|
1065 | void QBoxLayout::addSpacerItem(QSpacerItem *spacerItem)
|
---|
1066 | {
|
---|
1067 | insertSpacerItem(-1, spacerItem);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | /*!
|
---|
1071 | Adds \a widget to the end of this box layout, with a stretch
|
---|
1072 | factor of \a stretch and alignment \a alignment.
|
---|
1073 |
|
---|
1074 | The stretch factor applies only in the \l{direction()}{direction}
|
---|
1075 | of the QBoxLayout, and is relative to the other boxes and widgets
|
---|
1076 | in this QBoxLayout. Widgets and boxes with higher stretch factors
|
---|
1077 | grow more.
|
---|
1078 |
|
---|
1079 | If the stretch factor is 0 and nothing else in the QBoxLayout has
|
---|
1080 | a stretch factor greater than zero, the space is distributed
|
---|
1081 | according to the QWidget:sizePolicy() of each widget that's
|
---|
1082 | involved.
|
---|
1083 |
|
---|
1084 | The alignment is specified by \a alignment. The default
|
---|
1085 | alignment is 0, which means that the widget fills the entire cell.
|
---|
1086 |
|
---|
1087 | \sa insertWidget(), addItem(), addLayout(), addStretch(),
|
---|
1088 | addSpacing(), addStrut()
|
---|
1089 | */
|
---|
1090 | void QBoxLayout::addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
|
---|
1091 | {
|
---|
1092 | insertWidget(-1, widget, stretch, alignment);
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | /*!
|
---|
1096 | Adds \a layout to the end of the box, with serial stretch factor
|
---|
1097 | \a stretch.
|
---|
1098 |
|
---|
1099 | \sa insertLayout(), addItem(), addWidget()
|
---|
1100 | */
|
---|
1101 | void QBoxLayout::addLayout(QLayout *layout, int stretch)
|
---|
1102 | {
|
---|
1103 | insertLayout(-1, layout, stretch);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | /*!
|
---|
1107 | Limits the perpendicular dimension of the box (e.g. height if the
|
---|
1108 | box is \l LeftToRight) to a minimum of \a size. Other constraints
|
---|
1109 | may increase the limit.
|
---|
1110 |
|
---|
1111 | \sa addItem()
|
---|
1112 | */
|
---|
1113 | void QBoxLayout::addStrut(int size)
|
---|
1114 | {
|
---|
1115 | Q_D(QBoxLayout);
|
---|
1116 | QLayoutItem *b;
|
---|
1117 | if (horz(d->dir))
|
---|
1118 | b = QLayoutPrivate::createSpacerItem(this, 0, size, QSizePolicy::Fixed, QSizePolicy::Minimum);
|
---|
1119 | else
|
---|
1120 | b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Minimum, QSizePolicy::Fixed);
|
---|
1121 |
|
---|
1122 | QBoxLayoutItem *it = new QBoxLayoutItem(b);
|
---|
1123 | it->magic = true;
|
---|
1124 | d->list.append(it);
|
---|
1125 | invalidate();
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | /*!
|
---|
1129 | \fn int QBoxLayout::findWidget(QWidget *widget)
|
---|
1130 |
|
---|
1131 | Use indexOf(\a widget) instead.
|
---|
1132 | */
|
---|
1133 |
|
---|
1134 | /*!
|
---|
1135 | Sets the stretch factor for \a widget to \a stretch and returns
|
---|
1136 | true if \a widget is found in this layout (not including child
|
---|
1137 | layouts); otherwise returns false.
|
---|
1138 |
|
---|
1139 | \sa setAlignment()
|
---|
1140 | */
|
---|
1141 | bool QBoxLayout::setStretchFactor(QWidget *widget, int stretch)
|
---|
1142 | {
|
---|
1143 | Q_D(QBoxLayout);
|
---|
1144 | if (!widget)
|
---|
1145 | return false;
|
---|
1146 | for (int i = 0; i < d->list.size(); ++i) {
|
---|
1147 | QBoxLayoutItem *box = d->list.at(i);
|
---|
1148 | if (box->item->widget() == widget) {
|
---|
1149 | box->stretch = stretch;
|
---|
1150 | invalidate();
|
---|
1151 | return true;
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 | return false;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | /*!
|
---|
1158 | \overload
|
---|
1159 |
|
---|
1160 | Sets the stretch factor for the layout \a layout to \a stretch and
|
---|
1161 | returns true if \a layout is found in this layout (not including
|
---|
1162 | child layouts); otherwise returns false.
|
---|
1163 | */
|
---|
1164 | bool QBoxLayout::setStretchFactor(QLayout *layout, int stretch)
|
---|
1165 | {
|
---|
1166 | Q_D(QBoxLayout);
|
---|
1167 | for (int i = 0; i < d->list.size(); ++i) {
|
---|
1168 | QBoxLayoutItem *box = d->list.at(i);
|
---|
1169 | if (box->item->layout() == layout) {
|
---|
1170 | if (box->stretch != stretch) {
|
---|
1171 | box->stretch = stretch;
|
---|
1172 | invalidate();
|
---|
1173 | }
|
---|
1174 | return true;
|
---|
1175 | }
|
---|
1176 | }
|
---|
1177 | return false;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | /*!
|
---|
1181 | Sets the stretch factor at position \a index. to \a stretch.
|
---|
1182 |
|
---|
1183 | \since 4.5
|
---|
1184 | */
|
---|
1185 |
|
---|
1186 | void QBoxLayout::setStretch(int index, int stretch)
|
---|
1187 | {
|
---|
1188 | Q_D(QBoxLayout);
|
---|
1189 | if (index >= 0 && index < d->list.size()) {
|
---|
1190 | QBoxLayoutItem *box = d->list.at(index);
|
---|
1191 | if (box->stretch != stretch) {
|
---|
1192 | box->stretch = stretch;
|
---|
1193 | invalidate();
|
---|
1194 | }
|
---|
1195 | }
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | /*!
|
---|
1199 | Returns the stretch factor at position \a index.
|
---|
1200 |
|
---|
1201 | \since 4.5
|
---|
1202 | */
|
---|
1203 |
|
---|
1204 | int QBoxLayout::stretch(int index) const
|
---|
1205 | {
|
---|
1206 | Q_D(const QBoxLayout);
|
---|
1207 | if (index >= 0 && index < d->list.size())
|
---|
1208 | return d->list.at(index)->stretch;
|
---|
1209 | return -1;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | /*!
|
---|
1213 | Sets the direction of this layout to \a direction.
|
---|
1214 | */
|
---|
1215 | void QBoxLayout::setDirection(Direction direction)
|
---|
1216 | {
|
---|
1217 | Q_D(QBoxLayout);
|
---|
1218 | if (d->dir == direction)
|
---|
1219 | return;
|
---|
1220 | if (horz(d->dir) != horz(direction)) {
|
---|
1221 | //swap around the spacers (the "magic" bits)
|
---|
1222 | //#### a bit yucky, knows too much.
|
---|
1223 | //#### probably best to add access functions to spacerItem
|
---|
1224 | //#### or even a QSpacerItem::flip()
|
---|
1225 | for (int i = 0; i < d->list.size(); ++i) {
|
---|
1226 | QBoxLayoutItem *box = d->list.at(i);
|
---|
1227 | if (box->magic) {
|
---|
1228 | QSpacerItem *sp = box->item->spacerItem();
|
---|
1229 | if (sp) {
|
---|
1230 | if (sp->expandingDirections() == Qt::Orientations(0) /*No Direction*/) {
|
---|
1231 | //spacing or strut
|
---|
1232 | QSize s = sp->sizeHint();
|
---|
1233 | sp->changeSize(s.height(), s.width(),
|
---|
1234 | horz(direction) ? QSizePolicy::Fixed:QSizePolicy::Minimum,
|
---|
1235 | horz(direction) ? QSizePolicy::Minimum:QSizePolicy::Fixed);
|
---|
1236 |
|
---|
1237 | } else {
|
---|
1238 | //stretch
|
---|
1239 | if (horz(direction))
|
---|
1240 | sp->changeSize(0, 0, QSizePolicy::Expanding,
|
---|
1241 | QSizePolicy::Minimum);
|
---|
1242 | else
|
---|
1243 | sp->changeSize(0, 0, QSizePolicy::Minimum,
|
---|
1244 | QSizePolicy::Expanding);
|
---|
1245 | }
|
---|
1246 | }
|
---|
1247 | }
|
---|
1248 | }
|
---|
1249 | }
|
---|
1250 | d->dir = direction;
|
---|
1251 | invalidate();
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | /*!
|
---|
1255 | \fn QBoxLayout::Direction QBoxLayout::direction() const
|
---|
1256 |
|
---|
1257 | Returns the direction of the box. addWidget() and addSpacing()
|
---|
1258 | work in this direction; the stretch stretches in this direction.
|
---|
1259 |
|
---|
1260 | \sa QBoxLayout::Direction addWidget() addSpacing()
|
---|
1261 | */
|
---|
1262 |
|
---|
1263 | QBoxLayout::Direction QBoxLayout::direction() const
|
---|
1264 | {
|
---|
1265 | Q_D(const QBoxLayout);
|
---|
1266 | return d->dir;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | /*!
|
---|
1270 | \class QHBoxLayout
|
---|
1271 | \brief The QHBoxLayout class lines up widgets horizontally.
|
---|
1272 |
|
---|
1273 | \ingroup geomanagement
|
---|
1274 | \ingroup appearance
|
---|
1275 | \mainclass
|
---|
1276 |
|
---|
1277 | This class is used to construct horizontal box layout objects. See
|
---|
1278 | QBoxLayout for details.
|
---|
1279 |
|
---|
1280 | The simplest use of the class is like this:
|
---|
1281 |
|
---|
1282 | \snippet doc/src/snippets/layouts/layouts.cpp 0
|
---|
1283 | \snippet doc/src/snippets/layouts/layouts.cpp 1
|
---|
1284 | \snippet doc/src/snippets/layouts/layouts.cpp 2
|
---|
1285 | \codeline
|
---|
1286 | \snippet doc/src/snippets/layouts/layouts.cpp 3
|
---|
1287 | \snippet doc/src/snippets/layouts/layouts.cpp 4
|
---|
1288 | \snippet doc/src/snippets/layouts/layouts.cpp 5
|
---|
1289 |
|
---|
1290 | First, we create the widgets we want in the layout. Then, we
|
---|
1291 | create the QHBoxLayout object and add the widgets into the
|
---|
1292 | layout. Finally, we call QWidget::setLayout() to install the
|
---|
1293 | QHBoxLayout object onto the widget. At that point, the widgets in
|
---|
1294 | the layout are reparented to have \c window as their parent.
|
---|
1295 |
|
---|
1296 | \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets
|
---|
1297 |
|
---|
1298 | \sa QVBoxLayout, QGridLayout, QStackedLayout, {Layout Classes}, {Basic Layouts Example}
|
---|
1299 | */
|
---|
1300 |
|
---|
1301 |
|
---|
1302 | /*!
|
---|
1303 | Constructs a new top-level horizontal box with
|
---|
1304 | parent \a parent.
|
---|
1305 | */
|
---|
1306 | QHBoxLayout::QHBoxLayout(QWidget *parent)
|
---|
1307 | : QBoxLayout(LeftToRight, parent)
|
---|
1308 | {
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | /*!
|
---|
1312 | Constructs a new horizontal box. You must add
|
---|
1313 | it to another layout.
|
---|
1314 | */
|
---|
1315 | QHBoxLayout::QHBoxLayout()
|
---|
1316 | : QBoxLayout(LeftToRight)
|
---|
1317 | {
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 |
|
---|
1321 |
|
---|
1322 | #ifdef QT3_SUPPORT
|
---|
1323 | /*!
|
---|
1324 | Constructs a new top-level horizontal box called \a name, with
|
---|
1325 | parent \a parent.
|
---|
1326 |
|
---|
1327 | The \a margin is the number of pixels between the edge of the
|
---|
1328 | widget and its managed children. The \a spacing is the default
|
---|
1329 | number of pixels between neighboring children. If \a spacing is -1
|
---|
1330 | the value of \a margin is used for \a spacing.
|
---|
1331 | */
|
---|
1332 | QHBoxLayout::QHBoxLayout(QWidget *parent, int margin,
|
---|
1333 | int spacing, const char *name)
|
---|
1334 | : QBoxLayout(LeftToRight, parent)
|
---|
1335 | {
|
---|
1336 | setMargin(margin);
|
---|
1337 | setSpacing(spacing<0 ? margin : spacing);
|
---|
1338 | setObjectName(QString::fromAscii(name));
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | /*!
|
---|
1342 | Constructs a new horizontal box called name \a name and adds it to
|
---|
1343 | \a parentLayout.
|
---|
1344 |
|
---|
1345 | The \a spacing is the default number of pixels between neighboring
|
---|
1346 | children. If \a spacing is -1, this QHBoxLayout will inherit its
|
---|
1347 | parent's spacing().
|
---|
1348 | */
|
---|
1349 | QHBoxLayout::QHBoxLayout(QLayout *parentLayout, int spacing,
|
---|
1350 | const char *name)
|
---|
1351 | : QBoxLayout(LeftToRight)
|
---|
1352 | {
|
---|
1353 | setSpacing(spacing);
|
---|
1354 | setObjectName(QString::fromAscii(name));
|
---|
1355 | if (parentLayout) {
|
---|
1356 | setParent(parentLayout);
|
---|
1357 | parentLayout->addItem(this);
|
---|
1358 | }
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | /*!
|
---|
1362 | Constructs a new horizontal box called name \a name. You must add
|
---|
1363 | it to another layout.
|
---|
1364 |
|
---|
1365 | The \a spacing is the default number of pixels between neighboring
|
---|
1366 | children. If \a spacing is -1, this QHBoxLayout will inherit its
|
---|
1367 | parent's spacing().
|
---|
1368 | */
|
---|
1369 | QHBoxLayout::QHBoxLayout(int spacing, const char *name)
|
---|
1370 | : QBoxLayout(LeftToRight)
|
---|
1371 | {
|
---|
1372 | setSpacing(spacing);
|
---|
1373 | setObjectName(QString::fromAscii(name));
|
---|
1374 | }
|
---|
1375 | #endif
|
---|
1376 |
|
---|
1377 |
|
---|
1378 | /*!
|
---|
1379 | Destroys this box layout.
|
---|
1380 |
|
---|
1381 | The layout's widgets aren't destroyed.
|
---|
1382 | */
|
---|
1383 | QHBoxLayout::~QHBoxLayout()
|
---|
1384 | {
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | /*!
|
---|
1388 | \class QVBoxLayout
|
---|
1389 | \brief The QVBoxLayout class lines up widgets vertically.
|
---|
1390 |
|
---|
1391 | \ingroup geomanagement
|
---|
1392 | \ingroup appearance
|
---|
1393 | \mainclass
|
---|
1394 |
|
---|
1395 | This class is used to construct vertical box layout objects. See
|
---|
1396 | QBoxLayout for details.
|
---|
1397 |
|
---|
1398 | The simplest use of the class is like this:
|
---|
1399 |
|
---|
1400 | \snippet doc/src/snippets/layouts/layouts.cpp 6
|
---|
1401 | \snippet doc/src/snippets/layouts/layouts.cpp 7
|
---|
1402 | \snippet doc/src/snippets/layouts/layouts.cpp 8
|
---|
1403 | \codeline
|
---|
1404 | \snippet doc/src/snippets/layouts/layouts.cpp 9
|
---|
1405 | \snippet doc/src/snippets/layouts/layouts.cpp 10
|
---|
1406 | \snippet doc/src/snippets/layouts/layouts.cpp 11
|
---|
1407 |
|
---|
1408 | First, we create the widgets we want in the layout. Then, we
|
---|
1409 | create the QVBoxLayout object and add the widgets into the
|
---|
1410 | layout. Finally, we call QWidget::setLayout() to install the
|
---|
1411 | QVBoxLayout object onto the widget. At that point, the widgets in
|
---|
1412 | the layout are reparented to have \c window as their parent.
|
---|
1413 |
|
---|
1414 | \image qvboxlayout-with-5-children.png Horizontal box layout with five child widgets
|
---|
1415 |
|
---|
1416 | \sa QHBoxLayout, QGridLayout, QStackedLayout, {Layout Classes}, {Basic Layouts Example}
|
---|
1417 | */
|
---|
1418 |
|
---|
1419 | /*!
|
---|
1420 | Constructs a new top-level vertical box with
|
---|
1421 | parent \a parent.
|
---|
1422 | */
|
---|
1423 | QVBoxLayout::QVBoxLayout(QWidget *parent)
|
---|
1424 | : QBoxLayout(TopToBottom, parent)
|
---|
1425 | {
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | /*!
|
---|
1429 | Constructs a new vertical box. You must add
|
---|
1430 | it to another layout.
|
---|
1431 |
|
---|
1432 | */
|
---|
1433 | QVBoxLayout::QVBoxLayout()
|
---|
1434 | : QBoxLayout(TopToBottom)
|
---|
1435 | {
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | #ifdef QT3_SUPPORT
|
---|
1439 | /*!
|
---|
1440 | Constructs a new top-level vertical box called \a name, with
|
---|
1441 | parent \a parent.
|
---|
1442 |
|
---|
1443 | The \a margin is the number of pixels between the edge of the
|
---|
1444 | widget and its managed children. The \a spacing is the default
|
---|
1445 | number of pixels between neighboring children. If \a spacing is -1
|
---|
1446 | the value of \a margin is used for \a spacing.
|
---|
1447 | */
|
---|
1448 | QVBoxLayout::QVBoxLayout(QWidget *parent, int margin, int spacing,
|
---|
1449 | const char *name)
|
---|
1450 | : QBoxLayout(TopToBottom, parent)
|
---|
1451 | {
|
---|
1452 | setMargin(margin);
|
---|
1453 | setSpacing(spacing<0 ? margin : spacing);
|
---|
1454 | setObjectName(QString::fromAscii(name));
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | /*!
|
---|
1458 | Constructs a new vertical box called name \a name and adds it to
|
---|
1459 | \a parentLayout.
|
---|
1460 |
|
---|
1461 | The \a spacing is the default number of pixels between neighboring
|
---|
1462 | children. If \a spacing is -1, this QVBoxLayout will inherit its
|
---|
1463 | parent's spacing().
|
---|
1464 | */
|
---|
1465 | QVBoxLayout::QVBoxLayout(QLayout *parentLayout, int spacing,
|
---|
1466 | const char *name)
|
---|
1467 | : QBoxLayout(TopToBottom)
|
---|
1468 | {
|
---|
1469 | setSpacing(spacing);
|
---|
1470 | setObjectName(QString::fromAscii(name));
|
---|
1471 | if (parentLayout) {
|
---|
1472 | setParent(parentLayout);
|
---|
1473 | parentLayout->addItem(this);
|
---|
1474 | }
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | /*!
|
---|
1478 | Constructs a new vertical box called name \a name. You must add
|
---|
1479 | it to another layout.
|
---|
1480 |
|
---|
1481 | The \a spacing is the default number of pixels between neighboring
|
---|
1482 | children. If \a spacing is -1, this QVBoxLayout will inherit its
|
---|
1483 | parent's spacing().
|
---|
1484 | */
|
---|
1485 | QVBoxLayout::QVBoxLayout(int spacing, const char *name)
|
---|
1486 | : QBoxLayout(TopToBottom)
|
---|
1487 | {
|
---|
1488 | setSpacing(spacing);
|
---|
1489 | setObjectName(QString::fromAscii(name));
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 |
|
---|
1493 | #endif
|
---|
1494 |
|
---|
1495 | /*!
|
---|
1496 | Destroys this box layout.
|
---|
1497 |
|
---|
1498 | The layout's widgets aren't destroyed.
|
---|
1499 | */
|
---|
1500 | QVBoxLayout::~QVBoxLayout()
|
---|
1501 | {
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | /*!
|
---|
1505 | \fn QWidget *QLayout::mainWidget() const
|
---|
1506 |
|
---|
1507 | Use parentWidget() instead.
|
---|
1508 | */
|
---|
1509 |
|
---|
1510 | /*!
|
---|
1511 | \fn void QLayout::remove(QWidget *widget)
|
---|
1512 |
|
---|
1513 | Use removeWidget(\a widget) instead.
|
---|
1514 | */
|
---|
1515 |
|
---|
1516 | /*!
|
---|
1517 | \fn void QLayout::add(QWidget *widget)
|
---|
1518 |
|
---|
1519 | Use addWidget(\a widget) instead.
|
---|
1520 | */
|
---|
1521 |
|
---|
1522 | /*!
|
---|
1523 | \fn QLayoutIterator QLayout::iterator()
|
---|
1524 |
|
---|
1525 | Use a QLayoutIterator() constructor instead.
|
---|
1526 | */
|
---|
1527 |
|
---|
1528 | /*!
|
---|
1529 | \fn int QLayout::defaultBorder() const
|
---|
1530 |
|
---|
1531 | Use spacing() instead.
|
---|
1532 | */
|
---|
1533 |
|
---|
1534 | QT_END_NAMESPACE
|
---|