comparison widgets/FlowLayout.cpp @ 164:635b837e2801

add FlowLayout.h
author lbajardsilogic
date Thu, 15 Nov 2007 16:58:36 +0000
parents
children 0442224a9553
comparison
equal deleted inserted replaced
163:77f4ef621c54 164:635b837e2801
1 /****************************************************************************
2 **
3 ** Copyright (C) 2004-2007 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
35 FlowLayout::FlowLayout(int spacing)
36 {
37 setSpacing(spacing);
38 }
39
40 FlowLayout::~FlowLayout()
41 {
42 QLayoutItem *item;
43 while ((item = takeAt(0)))
44 delete item;
45 }
46
47 void FlowLayout::addItem(QLayoutItem *item)
48 {
49 m_itemList.append(item);
50 }
51
52 int FlowLayout::count() const
53 {
54 return m_itemList.size();
55 }
56
57 QLayoutItem *FlowLayout::itemAt(int index) const
58 {
59 return m_itemList.value(index);
60 }
61
62 QLayoutItem *FlowLayout::takeAt(int index)
63 {
64 if (index >= 0 && index < m_itemList.size())
65 return m_itemList.takeAt(index);
66 else
67 return 0;
68 }
69
70 Qt::Orientations FlowLayout::expandingDirections() const
71 {
72 return 0;
73 }
74
75 bool FlowLayout::hasHeightForWidth() const
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
97 QSize FlowLayout::minimumSize() const
98 {
99 QSize size;
100 QLayoutItem *item;
101 QLayoutItem *itemXMax = 0;
102 QLayoutItem *itemYMax = 0;
103 foreach (item, m_itemList)
104 {
105 //init itemXMax and itemYMax
106 if ((itemXMax == 0) && (itemYMax == 0))
107 {
108 itemXMax = item;
109 itemYMax = item;
110 }
111
112 //find XMax
113 if (itemXMax->geometry().x() < item->geometry().x())
114 {
115 itemXMax = item;
116 }
117
118 //find YMax
119 if (itemYMax->geometry().y() < item->geometry().y())
120 {
121 itemYMax = item;
122 }
123 }
124
125 if ((itemXMax != 0) && (itemYMax != 0))
126 size = QSize(itemXMax->geometry().x() + itemXMax->minimumSize().width(), itemYMax->geometry().y() + itemYMax->minimumSize().height());
127
128 size += QSize(2*margin(), 2*margin());
129 return size;
130 }
131
132 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
133 {
134 int x = rect.x();
135 int y = rect.y();
136 int lineHeight = 0;
137
138 QLayoutItem *item;
139 foreach (item, m_itemList) {
140 int nextX = x + item->sizeHint().width() + spacing();
141 if (nextX - spacing() > rect.right() && lineHeight > 0) {
142 x = rect.x();
143 y = y + lineHeight + spacing();
144 nextX = x + item->sizeHint().width() + spacing();
145 lineHeight = 0;
146 }
147
148 if (!testOnly)
149 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
150
151 x = nextX;
152 lineHeight = qMax(lineHeight, item->sizeHint().height());
153 }
154
155 return y + lineHeight - rect.y();
156 }