Marlin  01.17.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
flowlayout.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2004-2006 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the example classes of the Qt Toolkit.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
13 **
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 **
22 ****************************************************************************/
23 
24 #include <QtGui>
25 
26 #include "flowlayout.h"
27 
28 FlowLayout::FlowLayout(QWidget *parent, int margin, int spacing)
29  : QLayout(parent)
30 {
31  setMargin(margin);
32  setSpacing(spacing);
33 }
34 
36 {
37  setSpacing(spacing);
38 }
39 
41 {
42  QLayoutItem *item;
43  while ((item = takeAt(0)))
44  delete item;
45 }
46 
47 void FlowLayout::addItem(QLayoutItem *item)
48 {
49  itemList.append(item);
50 }
51 
52 int FlowLayout::count() const
53 {
54  return itemList.size();
55 }
56 
57 QLayoutItem *FlowLayout::itemAt(int index) const
58 {
59  return itemList.value(index);
60 }
61 
62 QLayoutItem *FlowLayout::takeAt(int index)
63 {
64  if (index >= 0 && index < itemList.size())
65  return itemList.takeAt(index);
66  else
67  return 0;
68 }
69 
70 Qt::Orientations FlowLayout::expandingDirections() const
71 {
72  return 0;
73 }
74 
76 {
77  return true;
78 }
79 
80 int FlowLayout::heightForWidth(int width) const
81 {
82  int height = doLayout(QRect(0, 0, width, 0), true);
83  return height;
84 }
85 
86 void FlowLayout::setGeometry(const QRect &rect)
87 {
88  QLayout::setGeometry(rect);
89  doLayout(rect, false);
90 }
91 
92 QSize FlowLayout::sizeHint() const
93 {
94  return minimumSize();
95 }
96 
98 {
99  QSize size;
100  QLayoutItem *item;
101  foreach (item, itemList)
102  size = size.expandedTo(item->minimumSize());
103 
104  size += QSize(2*margin(), 2*margin());
105  return size;
106 }
107 
108 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
109 {
110  int x = rect.x();
111  int y = rect.y();
112  int lineHeight = 0;
113 
114  QLayoutItem *item;
115  foreach (item, itemList) {
116  int nextX = x + item->sizeHint().width() + spacing();
117  if (nextX - spacing() > rect.right() && lineHeight > 0) {
118  x = rect.x();
119  y = y + lineHeight + spacing();
120  nextX = x + item->sizeHint().width() + spacing();
121  lineHeight = 0;
122  }
123 
124  if (!testOnly)
125  item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
126 
127  x = nextX;
128  lineHeight = qMax(lineHeight, item->sizeHint().height());
129  }
130  return y + lineHeight - rect.y();
131 }
QLayoutItem * itemAt(int index) const
Definition: flowlayout.cpp:57
int heightForWidth(int) const
Definition: flowlayout.cpp:80
void setGeometry(const QRect &rect)
Definition: flowlayout.cpp:86
void addItem(QLayoutItem *item)
Definition: flowlayout.cpp:47
QSize sizeHint() const
Definition: flowlayout.cpp:92
FlowLayout(QWidget *parent, int margin=0, int spacing=-1)
Definition: flowlayout.cpp:28
QSize minimumSize() const
Definition: flowlayout.cpp:97
QList< QLayoutItem * > itemList
Definition: flowlayout.h:52
Qt::Orientations expandingDirections() const
Definition: flowlayout.cpp:70
QLayoutItem * takeAt(int index)
Definition: flowlayout.cpp:62
int doLayout(const QRect &rect, bool testOnly) const
Definition: flowlayout.cpp:108
bool hasHeightForWidth() const
Definition: flowlayout.cpp:75
int count() const
Definition: flowlayout.cpp:52