| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 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:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "flowlayout.h"
|
|---|
| 45 | //! [1]
|
|---|
| 46 | FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
|
|---|
| 47 | : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
|
|---|
| 48 | {
|
|---|
| 49 | setContentsMargins(margin, margin, margin, margin);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
|
|---|
| 53 | : m_hSpace(hSpacing), m_vSpace(vSpacing)
|
|---|
| 54 | {
|
|---|
| 55 | setContentsMargins(margin, margin, margin, margin);
|
|---|
| 56 | }
|
|---|
| 57 | //! [1]
|
|---|
| 58 |
|
|---|
| 59 | //! [2]
|
|---|
| 60 | FlowLayout::~FlowLayout()
|
|---|
| 61 | {
|
|---|
| 62 | QLayoutItem *item;
|
|---|
| 63 | while ((item = takeAt(0)))
|
|---|
| 64 | delete item;
|
|---|
| 65 | }
|
|---|
| 66 | //! [2]
|
|---|
| 67 |
|
|---|
| 68 | //! [3]
|
|---|
| 69 | void FlowLayout::addItem(QLayoutItem *item)
|
|---|
| 70 | {
|
|---|
| 71 | itemList.append(item);
|
|---|
| 72 | }
|
|---|
| 73 | //! [3]
|
|---|
| 74 |
|
|---|
| 75 | //! [4]
|
|---|
| 76 | int FlowLayout::horizontalSpacing() const
|
|---|
| 77 | {
|
|---|
| 78 | if (m_hSpace >= 0) {
|
|---|
| 79 | return m_hSpace;
|
|---|
| 80 | } else {
|
|---|
| 81 | return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | int FlowLayout::verticalSpacing() const
|
|---|
| 86 | {
|
|---|
| 87 | if (m_vSpace >= 0) {
|
|---|
| 88 | return m_vSpace;
|
|---|
| 89 | } else {
|
|---|
| 90 | return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | //! [4]
|
|---|
| 94 |
|
|---|
| 95 | //! [5]
|
|---|
| 96 | int FlowLayout::count() const
|
|---|
| 97 | {
|
|---|
| 98 | return itemList.size();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | QLayoutItem *FlowLayout::itemAt(int index) const
|
|---|
| 102 | {
|
|---|
| 103 | return itemList.value(index);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | QLayoutItem *FlowLayout::takeAt(int index)
|
|---|
| 107 | {
|
|---|
| 108 | if (index >= 0 && index < itemList.size())
|
|---|
| 109 | return itemList.takeAt(index);
|
|---|
| 110 | else
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|
| 113 | //! [5]
|
|---|
| 114 |
|
|---|
| 115 | //! [6]
|
|---|
| 116 | Qt::Orientations FlowLayout::expandingDirections() const
|
|---|
| 117 | {
|
|---|
| 118 | return 0;
|
|---|
| 119 | }
|
|---|
| 120 | //! [6]
|
|---|
| 121 |
|
|---|
| 122 | //! [7]
|
|---|
| 123 | bool FlowLayout::hasHeightForWidth() const
|
|---|
| 124 | {
|
|---|
| 125 | return true;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int FlowLayout::heightForWidth(int width) const
|
|---|
| 129 | {
|
|---|
| 130 | int height = doLayout(QRect(0, 0, width, 0), true);
|
|---|
| 131 | return height;
|
|---|
| 132 | }
|
|---|
| 133 | //! [7]
|
|---|
| 134 |
|
|---|
| 135 | //! [8]
|
|---|
| 136 | void FlowLayout::setGeometry(const QRect &rect)
|
|---|
| 137 | {
|
|---|
| 138 | QLayout::setGeometry(rect);
|
|---|
| 139 | doLayout(rect, false);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | QSize FlowLayout::sizeHint() const
|
|---|
| 143 | {
|
|---|
| 144 | return minimumSize();
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | QSize FlowLayout::minimumSize() const
|
|---|
| 148 | {
|
|---|
| 149 | QSize size;
|
|---|
| 150 | QLayoutItem *item;
|
|---|
| 151 | foreach (item, itemList)
|
|---|
| 152 | size = size.expandedTo(item->minimumSize());
|
|---|
| 153 |
|
|---|
| 154 | size += QSize(2*margin(), 2*margin());
|
|---|
| 155 | return size;
|
|---|
| 156 | }
|
|---|
| 157 | //! [8]
|
|---|
| 158 |
|
|---|
| 159 | //! [9]
|
|---|
| 160 | int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
|
|---|
| 161 | {
|
|---|
| 162 | int left, top, right, bottom;
|
|---|
| 163 | getContentsMargins(&left, &top, &right, &bottom);
|
|---|
| 164 | QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
|
|---|
| 165 | int x = effectiveRect.x();
|
|---|
| 166 | int y = effectiveRect.y();
|
|---|
| 167 | int lineHeight = 0;
|
|---|
| 168 | //! [9]
|
|---|
| 169 |
|
|---|
| 170 | //! [10]
|
|---|
| 171 | QLayoutItem *item;
|
|---|
| 172 | foreach (item, itemList) {
|
|---|
| 173 | QWidget *wid = item->widget();
|
|---|
| 174 | int spaceX = horizontalSpacing();
|
|---|
| 175 | if (spaceX == -1)
|
|---|
| 176 | spaceX = wid->style()->layoutSpacing(
|
|---|
| 177 | QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
|
|---|
| 178 | int spaceY = verticalSpacing();
|
|---|
| 179 | if (spaceY == -1)
|
|---|
| 180 | spaceY = wid->style()->layoutSpacing(
|
|---|
| 181 | QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
|
|---|
| 182 | //! [10]
|
|---|
| 183 | //! [11]
|
|---|
| 184 | int nextX = x + item->sizeHint().width() + spaceX;
|
|---|
| 185 | if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
|
|---|
| 186 | x = effectiveRect.x();
|
|---|
| 187 | y = y + lineHeight + spaceY;
|
|---|
| 188 | nextX = x + item->sizeHint().width() + spaceX;
|
|---|
| 189 | lineHeight = 0;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | if (!testOnly)
|
|---|
| 193 | item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
|
|---|
| 194 |
|
|---|
| 195 | x = nextX;
|
|---|
| 196 | lineHeight = qMax(lineHeight, item->sizeHint().height());
|
|---|
| 197 | }
|
|---|
| 198 | return y + lineHeight - rect.y() + bottom;
|
|---|
| 199 | }
|
|---|
| 200 | //! [11]
|
|---|
| 201 | //! [12]
|
|---|
| 202 | int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
|
|---|
| 203 | {
|
|---|
| 204 | QObject *parent = this->parent();
|
|---|
| 205 | if (!parent) {
|
|---|
| 206 | return -1;
|
|---|
| 207 | } else if (parent->isWidgetType()) {
|
|---|
| 208 | QWidget *pw = static_cast<QWidget *>(parent);
|
|---|
| 209 | return pw->style()->pixelMetric(pm, 0, pw);
|
|---|
| 210 | } else {
|
|---|
| 211 | return static_cast<QLayout *>(parent)->spacing();
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | //! [12]
|
|---|