Qt 4.8
qboxlayout.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
53 
54 /*
55  Returns true if the \a widget can be added to the \a layout;
56  otherwise returns false.
57 */
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 
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 
104  int stretch;
105  bool magic;
106 };
107 
109 {
111 public:
112  QBoxLayoutPrivate() : hfwWidth(-1), dirty(true), spacing(-1) { }
114 
115  void setDirty() {
116  geomArray.clear();
117  hfwWidth = -1;
118  hfwHeight = -1;
119  dirty = true;
120  }
121 
124  int hfwWidth;
130  int leftMargin, topMargin, rightMargin, bottomMargin;
131  Qt::Orientations expanding;
133  uint dirty : 1;
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 
146 {
147 }
148 
149 static inline bool horz(QBoxLayout::Direction dir)
150 {
151  return dir == QBoxLayout::RightToLeft || dir == QBoxLayout::LeftToRight;
152 }
153 
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();
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 */
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();
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;
331  qSwap(actual1, actual2);
332 
333  if (style) {
334  spacing = style->combinedLayoutSpacing(actual1, actual2,
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 */
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 
559  : QLayout(*new QBoxLayoutPrivate, 0, parent)
560 {
561  Q_D(QBoxLayout);
562  d->dir = dir;
563 }
564 
565 #ifdef QT3_SUPPORT
566 
580  int margin, int spacing, const char *name)
581  : QLayout(*new QBoxLayoutPrivate, 0, parent)
582 {
583  Q_D(QBoxLayout);
584  d->dir = dir;
585  setMargin(margin);
588 }
589 
598 QBoxLayout::QBoxLayout(QLayout *parentLayout, Direction dir, int spacing,
599  const char *name)
600  : QLayout(*new QBoxLayoutPrivate, parentLayout, 0)
601 {
602  Q_D(QBoxLayout);
603  d->dir = dir;
606 }
607 
616 QBoxLayout::QBoxLayout(Direction dir, int spacing, const char *name)
617  : QLayout(*new QBoxLayoutPrivate,0, 0)
618 {
619  Q_D(QBoxLayout);
620  d->dir = dir;
623 }
624 #endif // QT3_SUPPORT
625 
626 
633 {
634  Q_D(QBoxLayout);
635  d->deleteAll(); // must do it before QObject deletes children, so can't be in ~QBoxLayoutPrivate
636 }
637 
650 {
651  Q_D(const QBoxLayout);
652  if (d->spacing >=0) {
653  return d->spacing;
654  } else {
655  return qSmartSpacing(this, d->dir == LeftToRight || d->dir == RightToLeft
658  }
659 }
660 
668 {
669  Q_D(QBoxLayout);
670  d->spacing = spacing;
671  invalidate();
672 }
673 
678 {
679  Q_D(const QBoxLayout);
680  if (d->dirty)
681  const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
682  return d->sizeHint;
683 }
684 
689 {
690  Q_D(const QBoxLayout);
691  if (d->dirty)
692  const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
693  return d->minSize;
694 }
695 
700 {
701  Q_D(const QBoxLayout);
702  if (d->dirty)
703  const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
704 
705  QSize s = d->maxSize.boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX));
706 
711  return s;
712 }
713 
718 {
719  Q_D(const QBoxLayout);
720  if (d->dirty)
721  const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
722  return d->hasHfw;
723 }
724 
729 {
730  Q_D(const QBoxLayout);
731  if (!hasHeightForWidth())
732  return -1;
733 
734  int left, top, right, bottom;
735  d->effectiveMargins(&left, &top, &right, &bottom);
736 
737  w -= left + right;
738  if (w != d->hfwWidth)
739  const_cast<QBoxLayout*>(this)->d_func()->calcHfw(w);
740 
741  return d->hfwHeight + top + bottom;
742 }
743 
748 {
749  Q_D(const QBoxLayout);
750  (void) heightForWidth(w);
751  int top, bottom;
752  d->effectiveMargins(0, &top, 0, &bottom);
753  return d->hasHfw ? (d->hfwMinHeight + top + bottom) : -1;
754 }
755 
760 {
761  Q_D(QBoxLayout);
762  d->setDirty();
764 }
765 
769 int QBoxLayout::count() const
770 {
771  Q_D(const QBoxLayout);
772  return d->list.count();
773 }
774 
779 {
780  Q_D(const QBoxLayout);
781  return index >= 0 && index < d->list.count() ? d->list.at(index)->item : 0;
782 }
783 
788 {
789  Q_D(QBoxLayout);
790  if (index < 0 || index >= d->list.count())
791  return 0;
792  QBoxLayoutItem *b = d->list.takeAt(index);
793  QLayoutItem *item = b->item;
794  b->item = 0;
795  delete b;
796 
797  if (QLayout *l = item->layout()) {
798  // sanity check in case the user passed something weird to QObject::setParent()
799  if (l->parent() == this)
800  l->setParent(0);
801  }
802 
803  invalidate();
804  return item;
805 }
806 
807 
811 Qt::Orientations QBoxLayout::expandingDirections() const
812 {
813  Q_D(const QBoxLayout);
814  if (d->dirty)
815  const_cast<QBoxLayout*>(this)->d_func()->setupGeom();
816  return d->expanding;
817 }
818 
823 {
824  Q_D(QBoxLayout);
825  if (d->dirty || r != geometry()) {
826  QRect oldRect = geometry();
828  if (d->dirty)
829  d->setupGeom();
830  QRect cr = alignment() ? alignmentRect(r) : r;
831 
832  int left, top, right, bottom;
833  d->effectiveMargins(&left, &top, &right, &bottom);
834  QRect s(cr.x() + left, cr.y() + top,
835  cr.width() - (left + right),
836  cr.height() - (top + bottom));
837 
838  QVector<QLayoutStruct> a = d->geomArray;
839  int pos = horz(d->dir) ? s.x() : s.y();
840  int space = horz(d->dir) ? s.width() : s.height();
841  int n = a.count();
842  if (d->hasHfw && !horz(d->dir)) {
843  for (int i = 0; i < n; i++) {
844  QBoxLayoutItem *box = d->list.at(i);
845  if (box->item->hasHeightForWidth()) {
846  int width = qBound(box->item->minimumSize().width(), s.width(), box->item->maximumSize().width());
847  a[i].sizeHint = a[i].minimumSize =
848  box->item->heightForWidth(width);
849  }
850  }
851  }
852 
853  Direction visualDir = d->dir;
854  QWidget *parent = parentWidget();
855  if (parent && parent->isRightToLeft()) {
856  if (d->dir == LeftToRight)
857  visualDir = RightToLeft;
858  else if (d->dir == RightToLeft)
859  visualDir = LeftToRight;
860  }
861 
862  qGeomCalc(a, 0, n, pos, space);
863 
864  bool reverse = (horz(visualDir)
865  ? ((r.right() > oldRect.right()) != (visualDir == RightToLeft))
866  : r.bottom() > oldRect.bottom());
867  for (int j = 0; j < n; j++) {
868  int i = reverse ? n-j-1 : j;
869  QBoxLayoutItem *box = d->list.at(i);
870 
871  switch (visualDir) {
872  case LeftToRight:
873  box->item->setGeometry(QRect(a.at(i).pos, s.y(), a.at(i).size, s.height()));
874  break;
875  case RightToLeft:
876  box->item->setGeometry(QRect(s.left() + s.right() - a.at(i).pos - a.at(i).size + 1,
877  s.y(), a.at(i).size, s.height()));
878  break;
879  case TopToBottom:
880  box->item->setGeometry(QRect(s.x(), a.at(i).pos, s.width(), a.at(i).size));
881  break;
882  case BottomToTop:
883  box->item->setGeometry(QRect(s.x(),
884  s.top() + s.bottom() - a.at(i).pos - a.at(i).size + 1,
885  s.width(), a.at(i).size));
886  }
887  }
888  }
889 }
890 
895 {
896  Q_D(QBoxLayout);
897  QBoxLayoutItem *it = new QBoxLayoutItem(item);
898  d->list.append(it);
899  invalidate();
900 }
901 
910 {
911  Q_D(QBoxLayout);
912  if (index < 0) // append
913  index = d->list.count();
914 
915  QBoxLayoutItem *it = new QBoxLayoutItem(item);
916  d->list.insert(index, it);
917  invalidate();
918 }
919 
929 void QBoxLayout::insertSpacing(int index, int size)
930 {
931  Q_D(QBoxLayout);
932  if (index < 0) // append
933  index = d->list.count();
934 
935  QLayoutItem *b;
936  if (horz(d->dir))
938  else
940 
941  QT_TRY {
942  QBoxLayoutItem *it = new QBoxLayoutItem(b);
943  it->magic = true;
944  d->list.insert(index, it);
945 
946  } QT_CATCH(...) {
947  delete b;
948  QT_RETHROW;
949  }
950  invalidate();
951 }
952 
961 {
962  Q_D(QBoxLayout);
963  if (index < 0) // append
964  index = d->list.count();
965 
966  QLayoutItem *b;
967  if (horz(d->dir))
969  else
971 
972  QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch);
973  it->magic = true;
974  d->list.insert(index, it);
975  invalidate();
976 }
977 
991 {
992  Q_D(QBoxLayout);
993  if (index < 0) // append
994  index = d->list.count();
995 
996  QBoxLayoutItem *it = new QBoxLayoutItem(spacerItem);
997  it->magic = true;
998  d->list.insert(index, it);
999  invalidate();
1000 }
1001 
1011 {
1012  Q_D(QBoxLayout);
1013  if (!adoptLayout(layout))
1014  return;
1015  if (index < 0) // append
1016  index = d->list.count();
1017  QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch);
1018  d->list.insert(index, it);
1019  invalidate();
1020 }
1021 
1043  Qt::Alignment alignment)
1044 {
1045  Q_D(QBoxLayout);
1046  if (!checkWidget(this, widget))
1047  return;
1048  addChildWidget(widget);
1049  if (index < 0) // append
1050  index = d->list.count();
1051  QWidgetItem *b = QLayoutPrivate::createWidgetItem(this, widget);
1052  b->setAlignment(alignment);
1053 
1054  QBoxLayoutItem *it;
1055  QT_TRY{
1056  it = new QBoxLayoutItem(b, stretch);
1057  } QT_CATCH(...) {
1058  delete b;
1059  QT_RETHROW;
1060  }
1061 
1062  QT_TRY{
1063  d->list.insert(index, it);
1064  } QT_CATCH(...) {
1065  delete it;
1066  QT_RETHROW;
1067  }
1068  invalidate();
1069 }
1070 
1079 {
1080  insertSpacing(-1, size);
1081 }
1082 
1090 {
1091  insertStretch(-1, stretch);
1092 }
1093 
1105 {
1106  insertSpacerItem(-1, spacerItem);
1107 }
1108 
1130 {
1131  insertWidget(-1, widget, stretch, alignment);
1132 }
1133 
1141 {
1142  insertLayout(-1, layout, stretch);
1143 }
1144 
1152 void QBoxLayout::addStrut(int size)
1153 {
1154  Q_D(QBoxLayout);
1155  QLayoutItem *b;
1156  if (horz(d->dir))
1158  else
1160 
1161  QBoxLayoutItem *it = new QBoxLayoutItem(b);
1162  it->magic = true;
1163  d->list.append(it);
1164  invalidate();
1165 }
1166 
1181 {
1182  Q_D(QBoxLayout);
1183  if (!widget)
1184  return false;
1185  for (int i = 0; i < d->list.size(); ++i) {
1186  QBoxLayoutItem *box = d->list.at(i);
1187  if (box->item->widget() == widget) {
1188  box->stretch = stretch;
1189  invalidate();
1190  return true;
1191  }
1192  }
1193  return false;
1194 }
1195 
1207 {
1208  Q_D(QBoxLayout);
1209  for (int i = 0; i < d->list.size(); ++i) {
1210  QBoxLayoutItem *box = d->list.at(i);
1211  if (box->item->layout() == layout) {
1212  if (box->stretch != stretch) {
1213  box->stretch = stretch;
1214  invalidate();
1215  }
1216  return true;
1217  }
1218  }
1219  return false;
1220 }
1221 
1229 {
1230  Q_D(QBoxLayout);
1231  if (index >= 0 && index < d->list.size()) {
1232  QBoxLayoutItem *box = d->list.at(index);
1233  if (box->stretch != stretch) {
1234  box->stretch = stretch;
1235  invalidate();
1236  }
1237  }
1238 }
1239 
1247 {
1248  Q_D(const QBoxLayout);
1249  if (index >= 0 && index < d->list.size())
1250  return d->list.at(index)->stretch;
1251  return -1;
1252 }
1253 
1258 {
1259  Q_D(QBoxLayout);
1260  if (d->dir == direction)
1261  return;
1262  if (horz(d->dir) != horz(direction)) {
1263  //swap around the spacers (the "magic" bits)
1264  //#### a bit yucky, knows too much.
1265  //#### probably best to add access functions to spacerItem
1266  //#### or even a QSpacerItem::flip()
1267  for (int i = 0; i < d->list.size(); ++i) {
1268  QBoxLayoutItem *box = d->list.at(i);
1269  if (box->magic) {
1270  QSpacerItem *sp = box->item->spacerItem();
1271  if (sp) {
1272  if (sp->expandingDirections() == Qt::Orientations(0) /*No Direction*/) {
1273  //spacing or strut
1274  QSize s = sp->sizeHint();
1275  sp->changeSize(s.height(), s.width(),
1278 
1279  } else {
1280  //stretch
1281  if (horz(direction))
1284  else
1285  sp->changeSize(0, 0, QSizePolicy::Minimum,
1287  }
1288  }
1289  }
1290  }
1291  }
1292  d->dir = direction;
1293  invalidate();
1294 }
1295 
1309 {
1310  Q_D(const QBoxLayout);
1311  return d->dir;
1312 }
1313 
1353  : QBoxLayout(LeftToRight, parent)
1354 {
1355 }
1356 
1363 {
1364 }
1365 
1366 
1367 
1368 #ifdef QT3_SUPPORT
1369 
1379  int spacing, const char *name)
1380  : QBoxLayout(LeftToRight, parent)
1381 {
1382  setMargin(margin);
1385 }
1386 
1395 QHBoxLayout::QHBoxLayout(QLayout *parentLayout, int spacing,
1396  const char *name)
1398 {
1401  if (parentLayout) {
1402  setParent(parentLayout);
1403  parentLayout->addItem(this);
1404  }
1405 }
1406 
1415 QHBoxLayout::QHBoxLayout(int spacing, const char *name)
1417 {
1420 }
1421 #endif
1422 
1423 
1430 {
1431 }
1432 
1471  : QBoxLayout(TopToBottom, parent)
1472 {
1473 }
1474 
1482 {
1483 }
1484 
1485 #ifdef QT3_SUPPORT
1486 
1496  const char *name)
1497  : QBoxLayout(TopToBottom, parent)
1498 {
1499  setMargin(margin);
1502 }
1503 
1512 QVBoxLayout::QVBoxLayout(QLayout *parentLayout, int spacing,
1513  const char *name)
1515 {
1518  if (parentLayout) {
1519  setParent(parentLayout);
1520  parentLayout->addItem(this);
1521  }
1522 }
1523 
1532 QVBoxLayout::QVBoxLayout(int spacing, const char *name)
1534 {
1537 }
1538 
1539 
1540 #endif
1541 
1548 {
1549 }
1550 
double d
Definition: qnumeric_p.h:62
QVBoxLayout()
Constructs a new vertical box.
static Qt::LayoutDirection layoutDirection()
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
void effectiveMargins(int *left, int *top, int *right, int *bottom) const
Definition: qboxlayout.cpp:159
QLayoutItem * itemAt(int) const
Reimplemented Function
Definition: qboxlayout.cpp:778
QSize sizeHint() const
Reimplemented Function
int hfw(int w)
Definition: qboxlayout.cpp:74
The QBoxLayout class lines up child widgets horizontally or vertically.
Definition: qboxlayout.h:60
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QHBoxLayout()
Constructs a new horizontal box.
QPointer< QWidget > widget
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
virtual QRect geometry() const =0
Returns the rectangle covered by this layout item.
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
Qt::Orientations expandingDirections() const
Reimplemented Function
#define it(className, varName)
QBoxLayoutItem(QLayoutItem *it, int stretch_=0)
Definition: qboxlayout.cpp:70
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QList< QBoxLayoutItem * > list
Definition: qboxlayout.cpp:122
virtual bool isEmpty() const =0
Implemented in subclasses to return whether this item is empty, i.
~QBoxLayout()
Destroys this box layout.
Definition: qboxlayout.cpp:632
QVector< QLayoutStruct > geomArray
Definition: qboxlayout.cpp:123
static C reverse(const C &l)
void addLayout(QLayout *layout, int stretch=0)
Adds layout to the end of the box, with serial stretch factor stretch.
virtual void addItem(QLayoutItem *)=0
Implemented in subclasses to add an item.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual Qt::Orientations expandingDirections() const =0
Returns whether this layout item can make use of more space than sizeHint().
QSizePolicy::ControlTypes controlTypes() const
Returns the control type(s) for the layout item.
QSize expandedTo(const QSize &) const
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition: qsize.h:187
void insertStretch(int index, int stretch=0)
Inserts a stretchable space (a QSpacerItem) at position index, with zero minimum size and stretch fac...
Definition: qboxlayout.cpp:960
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
static bool ignore(const char *test, const char *const *table)
Definition: qaxserver.cpp:660
void insertSpacing(int index, int size)
Inserts a non-stretchable space (a QSpacerItem) at position index, with size size.
Definition: qboxlayout.cpp:929
~QVBoxLayout()
Destroys this box layout.
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
Qt::Orientations expanding
Definition: qboxlayout.cpp:131
QSize minimumSize() const
Reimplemented Function
Definition: qboxlayout.cpp:688
void addSpacerItem(QSpacerItem *spacerItem)
Adds spacerItem to the end of this box layout.
virtual void setGeometry(const QRect &)
Reimplemented Function
Definition: qlayout.cpp:655
long ASN1_INTEGER_get ASN1_INTEGER * a
void invalidate()
Reimplemented Function
Definition: qlayout.cpp:673
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
virtual QSize minimumSize() const =0
Implemented in subclasses to return the minimum size of this item.
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
virtual bool hasHeightForWidth() const
Returns true if this layout&#39;s preferred height depends on its width; otherwise returns false...
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
bool empty() const
This function is provided for STL compatibility.
Definition: qvector.h:285
#define Q_D(Class)
Definition: qglobal.h:2482
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=0)
Adds widget to the end of this box layout, with a stretch factor of stretch and alignment alignment...
QSize boundedTo(const QSize &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:192
int horizontalStretch() const
Definition: qsizepolicy.h:144
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void addItem(QLayoutItem *)
Reimplemented Function
Definition: qboxlayout.cpp:894
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
QStyle * style() const
Definition: qwidget.cpp:2742
virtual QSpacerItem * spacerItem()
If this item is a QSpacerItem, it is returned as a QSpacerItem; otherwise 0 is returned.
void setStretch(int index, int stretch)
Sets the stretch factor at position index.
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define Q_Q(Class)
Definition: qglobal.h:2483
void invalidate()
Resets cached information.
Definition: qboxlayout.cpp:759
void setWidth(int w)
Sets the width to the given width.
Definition: qsize.h:132
QWidget * parentWidget() const
Returns the parent widget of this layout, or 0 if this layout is not installed on any widget...
Definition: qlayout.cpp:616
void addStretch(int stretch=0)
Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end...
void setDirection(Direction)
Sets the direction of this layout to direction.
#define QT_RETHROW
Definition: qglobal.h:1539
int width() const
Returns the width.
Definition: qsize.h:126
void setAlignment(Qt::Alignment a)
Sets the alignment of this item to alignment.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Direction
This type is used to determine the direction of a box layout.
Definition: qboxlayout.h:65
The QLayoutItem class provides an abstract item that a QLayout manipulates.
Definition: qlayoutitem.h:64
QLayout()
Constructs a new child QLayout.
Definition: qlayout.cpp:127
bool adoptLayout(QLayout *layout)
Definition: qlayout.cpp:987
The QSpacerItem class provides blank space in a layout.
Definition: qlayoutitem.h:96
Qt::Orientations expandingDirections() const
Reimplemented Function
Definition: qboxlayout.cpp:811
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
void insertWidget(int index, QWidget *widget, int stretch=0, Qt::Alignment alignment=0)
Inserts widget at position index, with stretch factor stretch and alignment alignment.
int count() const
Reimplemented Function
Definition: qboxlayout.cpp:769
void insertLayout(int index, QLayout *layout, int stretch=0)
Inserts layout at position index, with stretch factor stretch.
The QLayout class is the base class of geometry managers.
Definition: qlayout.h:90
const char * name
int minimumHeightForWidth(int) const
Reimplemented Function
Definition: qboxlayout.cpp:747
const char * layout
LayoutDirection
Definition: qnamespace.h:1580
static bool checkWidget(QLayout *layout, QWidget *widget)
Definition: qboxlayout.cpp:58
~QHBoxLayout()
Destroys this box layout.
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
int verticalStretch() const
Definition: qsizepolicy.h:145
void addStrut(int)
Limits the perpendicular dimension of the box (e.g.
static bool horz(QBoxLayout::Direction dir)
Definition: qboxlayout.cpp:149
static QSpacerItem * createSpacerItem(const QLayout *layout, int w, int h, QSizePolicy::Policy hPolicy=QSizePolicy::Minimum, QSizePolicy::Policy vPolicy=QSizePolicy::Minimum)
Definition: qlayout.cpp:198
Direction direction() const
Returns the direction of the box.
The QWidgetItem class is a layout item that represents a widget.
Definition: qlayoutitem.h:122
#define QT_CATCH(A)
Definition: qglobal.h:1537
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
bool setStretchFactor(QWidget *w, int stretch)
Sets the stretch factor for widget to stretch and returns true if widget is found in this layout (not...
QBoxLayout::Direction dir
Definition: qboxlayout.cpp:134
QRect alignmentRect(const QRect &) const
Returns the rectangle that should be covered when the geometry of this layout is set to r...
Definition: qlayout.cpp:1459
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
static QWidget * parentWidget(const QWidget *w)
virtual void setGeometry(const QRect &)=0
Implemented in subclasses to set this item&#39;s geometry to r.
int mhfw(int w)
Definition: qboxlayout.cpp:81
Qt::LayoutDirection layoutDirection
the layout direction for this widget
Definition: qwidget.h:216
void qGeomCalc(QVector< QLayoutStruct > &chain, int start, int count, int pos, int space, int spacer)
static QWidgetItem * createWidgetItem(const QLayout *layout, QWidget *widget)
Definition: qlayout.cpp:190
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
Q_GUI_EXPORT int qSmartSpacing(const QLayout *layout, QStyle::PixelMetric pm)
QLayoutItem * takeAt(int)
Reimplemented Function
Definition: qboxlayout.cpp:787
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
void addChildWidget(QWidget *w)
This function is called from addWidget() functions in subclasses to add w as a managed widget of a la...
Definition: qlayout.cpp:1045
virtual QSize sizeHint() const =0
Implemented in subclasses to return the preferred size of this item.
virtual QLayout * layout()
If this item is a QLayout, it is returned as a QLayout; otherwise 0 is returned.
QSize maximumSize() const
Reimplemented Function
Definition: qboxlayout.cpp:699
void addSpacing(int size)
Adds a non-stretchable space (a QSpacerItem) with size size to the end of this box layout...
void setSpacing(int spacing)
Reimplements QLayout::setSpacing().
Definition: qboxlayout.cpp:667
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QSizePolicy sizePolicy
the default layout behavior of the widget
Definition: qwidget.h:171
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
virtual QSize maximumSize() const =0
Implemented in subclasses to return the maximum size of this item.
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
const char * className() const
Returns the class name.
Definition: qobjectdefs.h:491
QString objectName() const
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void insertItem(int index, QLayoutItem *)
Inserts item into this box layout at position index.
Definition: qboxlayout.cpp:909
void setGeometry(const QRect &)
Reimplemented Function
Definition: qboxlayout.cpp:822
QLayoutItem * item
Definition: qboxlayout.cpp:103
QFactoryLoader * l
Qt::Alignment alignment() const
Returns the alignment of this item.
Definition: qlayoutitem.h:85
QBoxLayout(Direction, QWidget *parent=0)
Constructs a new QBoxLayout with direction dir and parent widget parent.
Definition: qboxlayout.cpp:558
quint16 index
int spacing() const
Reimplements QLayout::spacing().
Definition: qboxlayout.cpp:649
virtual QWidget * widget()
If this item is a QWidget, it is returned as a QWidget; otherwise 0 is returned.
QSize sizeHint() const
Reimplemented Function
Definition: qboxlayout.cpp:677
virtual int heightForWidth(int) const
Returns the preferred height for this layout item, given the width w.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isRightToLeft() const
Definition: qwidget.h:428
bool hasHeightForWidth() const
Reimplemented Function
Definition: qboxlayout.cpp:717
QRect geometry() const
Reimplemented Function
Definition: qlayout.cpp:664
int margin() const
QLayout * layout()
Reimplemented Function
static const int QLAYOUTSIZE_MAX
Definition: qlayoutitem.h:56
int stretch(int index) const
Returns the stretch factor at position index.
void insertSpacerItem(int index, QSpacerItem *spacerItem)
Inserts spacerItem at position index, with zero minimum size and stretch factor.
Definition: qboxlayout.cpp:990
void setMargin(int)
Definition: qlayout.cpp:464
static void qMaxExpCalc(int &max, bool &exp, bool &empty, int boxmax, bool boxexp, bool boxempty)
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition: qwidget.h:158
#define QT_TRY
Definition: qglobal.h:1536
void changeSize(int w, int h, QSizePolicy::Policy hData=QSizePolicy::Minimum, QSizePolicy::Policy vData=QSizePolicy::Minimum)
Changes this spacer item to have preferred width w, preferred height h, horizontal size policy hPolic...
int combinedLayoutSpacing(QSizePolicy::ControlTypes controls1, QSizePolicy::ControlTypes controls2, Qt::Orientation orientation, QStyleOption *option=0, QWidget *widget=0) const
Returns the spacing that should be used between controls1 and controls2 in a layout.
Definition: qstyle.cpp:2438
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
int heightForWidth(int) const
Reimplemented Function
Definition: qboxlayout.cpp:728
The QList class is a template class that provides lists.
Definition: qdatastream.h:62