author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "flowlayout.h" |
|
44 |
//! [1] |
|
45 |
FlowLayout::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 |
||
51 |
FlowLayout::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] |
|
59 |
FlowLayout::~FlowLayout() |
|
60 |
{ |
|
61 |
QLayoutItem *item; |
|
62 |
while ((item = takeAt(0))) |
|
63 |
delete item; |
|
64 |
} |
|
65 |
//! [2] |
|
66 |
||
67 |
//! [3] |
|
68 |
void FlowLayout::addItem(QLayoutItem *item) |
|
69 |
{ |
|
70 |
itemList.append(item); |
|
71 |
} |
|
72 |
//! [3] |
|
73 |
||
74 |
//! [4] |
|
75 |
int FlowLayout::horizontalSpacing() const |
|
76 |
{ |
|
77 |
if (m_hSpace >= 0) { |
|
78 |
return m_hSpace; |
|
79 |
} else { |
|
80 |
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
int 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] |
|
95 |
int FlowLayout::count() const |
|
96 |
{ |
|
97 |
return itemList.size(); |
|
98 |
} |
|
99 |
||
100 |
QLayoutItem *FlowLayout::itemAt(int index) const |
|
101 |
{ |
|
102 |
return itemList.value(index); |
|
103 |
} |
|
104 |
||
105 |
QLayoutItem *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] |
|
115 |
Qt::Orientations FlowLayout::expandingDirections() const |
|
116 |
{ |
|
117 |
return 0; |
|
118 |
} |
|
119 |
//! [6] |
|
120 |
||
121 |
//! [7] |
|
122 |
bool FlowLayout::hasHeightForWidth() const |
|
123 |
{ |
|
124 |
return true; |
|
125 |
} |
|
126 |
||
127 |
int 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] |
|
135 |
void FlowLayout::setGeometry(const QRect &rect) |
|
136 |
{ |
|
137 |
QLayout::setGeometry(rect); |
|
138 |
doLayout(rect, false); |
|
139 |
} |
|
140 |
||
141 |
QSize FlowLayout::sizeHint() const |
|
142 |
{ |
|
143 |
return minimumSize(); |
|
144 |
} |
|
145 |
||
146 |
QSize 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] |
|
159 |
int 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] |
|
201 |
int 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] |