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