source: trunk/examples/layouts/flowlayout/flowlayout.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "flowlayout.h"
44//! [1]
45FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
46 : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
47{
48 setContentsMargins(margin, margin, margin, margin);
49}
50
51FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
52 : m_hSpace(hSpacing), m_vSpace(vSpacing)
53{
54 setContentsMargins(margin, margin, margin, margin);
55}
56//! [1]
57
58//! [2]
59FlowLayout::~FlowLayout()
60{
61 QLayoutItem *item;
62 while ((item = takeAt(0)))
63 delete item;
64}
65//! [2]
66
67//! [3]
68void FlowLayout::addItem(QLayoutItem *item)
69{
70 itemList.append(item);
71}
72//! [3]
73
74//! [4]
75int FlowLayout::horizontalSpacing() const
76{
77 if (m_hSpace >= 0) {
78 return m_hSpace;
79 } else {
80 return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
81 }
82}
83
84int FlowLayout::verticalSpacing() const
85{
86 if (m_vSpace >= 0) {
87 return m_vSpace;
88 } else {
89 return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
90 }
91}
92//! [4]
93
94//! [5]
95int FlowLayout::count() const
96{
97 return itemList.size();
98}
99
100QLayoutItem *FlowLayout::itemAt(int index) const
101{
102 return itemList.value(index);
103}
104
105QLayoutItem *FlowLayout::takeAt(int index)
106{
107 if (index >= 0 && index < itemList.size())
108 return itemList.takeAt(index);
109 else
110 return 0;
111}
112//! [5]
113
114//! [6]
115Qt::Orientations FlowLayout::expandingDirections() const
116{
117 return 0;
118}
119//! [6]
120
121//! [7]
122bool FlowLayout::hasHeightForWidth() const
123{
124 return true;
125}
126
127int FlowLayout::heightForWidth(int width) const
128{
129 int height = doLayout(QRect(0, 0, width, 0), true);
130 return height;
131}
132//! [7]
133
134//! [8]
135void FlowLayout::setGeometry(const QRect &rect)
136{
137 QLayout::setGeometry(rect);
138 doLayout(rect, false);
139}
140
141QSize FlowLayout::sizeHint() const
142{
143 return minimumSize();
144}
145
146QSize FlowLayout::minimumSize() const
147{
148 QSize size;
149 QLayoutItem *item;
150 foreach (item, itemList)
151 size = size.expandedTo(item->minimumSize());
152
153 size += QSize(2*margin(), 2*margin());
154 return size;
155}
156//! [8]
157
158//! [9]
159int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
160{
161 int left, top, right, bottom;
162 getContentsMargins(&left, &top, &right, &bottom);
163 QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
164 int x = effectiveRect.x();
165 int y = effectiveRect.y();
166 int lineHeight = 0;
167//! [9]
168
169//! [10]
170 QLayoutItem *item;
171 foreach (item, itemList) {
172 QWidget *wid = item->widget();
173 int spaceX = horizontalSpacing();
174 if (spaceX == -1)
175 spaceX = wid->style()->layoutSpacing(
176 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
177 int spaceY = verticalSpacing();
178 if (spaceY == -1)
179 spaceY = wid->style()->layoutSpacing(
180 QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
181//! [10]
182//! [11]
183 int nextX = x + item->sizeHint().width() + spaceX;
184 if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
185 x = effectiveRect.x();
186 y = y + lineHeight + spaceY;
187 nextX = x + item->sizeHint().width() + spaceX;
188 lineHeight = 0;
189 }
190
191 if (!testOnly)
192 item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
193
194 x = nextX;
195 lineHeight = qMax(lineHeight, item->sizeHint().height());
196 }
197 return y + lineHeight - rect.y() + bottom;
198}
199//! [11]
200//! [12]
201int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
202{
203 QObject *parent = this->parent();
204 if (!parent) {
205 return -1;
206 } else if (parent->isWidgetType()) {
207 QWidget *pw = static_cast<QWidget *>(parent);
208 return pw->style()->pixelMetric(pm, 0, pw);
209 } else {
210 return static_cast<QLayout *>(parent)->spacing();
211 }
212}
213//! [12]
Note: See TracBrowser for help on using the repository browser.