| 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 "flowlayout.h" | 
|---|
| 43 | #include <QtGui/qwidget.h> | 
|---|
| 44 | #include <QtCore/qmath.h> | 
|---|
| 45 |  | 
|---|
| 46 | FlowLayout::FlowLayout() | 
|---|
| 47 | { | 
|---|
| 48 | m_spacing[0] = 6; | 
|---|
| 49 | m_spacing[1] = 6; | 
|---|
| 50 | QSizePolicy sp = sizePolicy(); | 
|---|
| 51 | sp.setHeightForWidth(true); | 
|---|
| 52 | setSizePolicy(sp); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item) | 
|---|
| 56 | { | 
|---|
| 57 | item->setParentLayoutItem(this); | 
|---|
| 58 | if (uint(index) > uint(m_items.count())) | 
|---|
| 59 | index = m_items.count(); | 
|---|
| 60 | m_items.insert(index, item); | 
|---|
| 61 | invalidate(); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | int FlowLayout::count() const | 
|---|
| 65 | { | 
|---|
| 66 | return m_items.count(); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | QGraphicsLayoutItem *FlowLayout::itemAt(int index) const | 
|---|
| 70 | { | 
|---|
| 71 | return m_items.value(index); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | void FlowLayout::removeAt(int index) | 
|---|
| 75 | { | 
|---|
| 76 | m_items.removeAt(index); | 
|---|
| 77 | invalidate(); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | qreal FlowLayout::spacing(Qt::Orientation o) const | 
|---|
| 81 | { | 
|---|
| 82 | return m_spacing[int(o) - 1]; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void FlowLayout::setSpacing(Qt::Orientations o, qreal spacing) | 
|---|
| 86 | { | 
|---|
| 87 | if (o & Qt::Horizontal) | 
|---|
| 88 | m_spacing[0] = spacing; | 
|---|
| 89 | if (o & Qt::Vertical) | 
|---|
| 90 | m_spacing[1] = spacing; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void FlowLayout::setGeometry(const QRectF &geom) | 
|---|
| 94 | { | 
|---|
| 95 | QGraphicsLayout::setGeometry(geom); | 
|---|
| 96 | doLayout(geom, true); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const | 
|---|
| 100 | { | 
|---|
| 101 | qreal left, top, right, bottom; | 
|---|
| 102 | getContentsMargins(&left, &top, &right, &bottom); | 
|---|
| 103 | const qreal maxw = geom.width() - left - right; | 
|---|
| 104 |  | 
|---|
| 105 | qreal x = 0; | 
|---|
| 106 | qreal y = 0; | 
|---|
| 107 | qreal maxRowHeight = 0; | 
|---|
| 108 | QSizeF pref; | 
|---|
| 109 | for (int i = 0; i < m_items.count(); ++i) { | 
|---|
| 110 | QGraphicsLayoutItem *item = m_items.at(i); | 
|---|
| 111 | pref = item->effectiveSizeHint(Qt::PreferredSize); | 
|---|
| 112 | maxRowHeight = qMax(maxRowHeight, pref.height()); | 
|---|
| 113 |  | 
|---|
| 114 | qreal next_x; | 
|---|
| 115 | next_x = x + pref.width(); | 
|---|
| 116 | if (next_x > maxw) { | 
|---|
| 117 | if (x == 0) { | 
|---|
| 118 | pref.setWidth(maxw); | 
|---|
| 119 | } else { | 
|---|
| 120 | x = 0; | 
|---|
| 121 | next_x = pref.width(); | 
|---|
| 122 | } | 
|---|
| 123 | y += maxRowHeight + spacing(Qt::Vertical); | 
|---|
| 124 | maxRowHeight = 0; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | if (applyNewGeometry) | 
|---|
| 128 | item->setGeometry(QRectF(QPointF(left + x, top + y), pref)); | 
|---|
| 129 | x = next_x + spacing(Qt::Horizontal); | 
|---|
| 130 | } | 
|---|
| 131 | maxRowHeight = qMax(maxRowHeight, pref.height()); | 
|---|
| 132 | return top + y + maxRowHeight + bottom; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | QSizeF FlowLayout::minSize(const QSizeF &constraint) const | 
|---|
| 136 | { | 
|---|
| 137 | QSizeF size(0, 0); | 
|---|
| 138 | qreal left, top, right, bottom; | 
|---|
| 139 | getContentsMargins(&left, &top, &right, &bottom); | 
|---|
| 140 | if (constraint.width() >= 0) {   // height for width | 
|---|
| 141 | const qreal height = doLayout(QRectF(QPointF(0,0), constraint), false); | 
|---|
| 142 | size = QSizeF(constraint.width(), height); | 
|---|
| 143 | } else if (constraint.height() >= 0) {  // width for height? | 
|---|
| 144 | // not supported | 
|---|
| 145 | } else { | 
|---|
| 146 | QGraphicsLayoutItem *item; | 
|---|
| 147 | foreach (item, m_items) | 
|---|
| 148 | size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); | 
|---|
| 149 | size += QSize(left + right, top + bottom); | 
|---|
| 150 | } | 
|---|
| 151 | return size; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | QSizeF FlowLayout::prefSize() const | 
|---|
| 155 | { | 
|---|
| 156 | qreal left, right; | 
|---|
| 157 | getContentsMargins(&left, 0, &right, 0); | 
|---|
| 158 |  | 
|---|
| 159 | QGraphicsLayoutItem *item; | 
|---|
| 160 | qreal maxh = 0; | 
|---|
| 161 | qreal totalWidth = 0; | 
|---|
| 162 | foreach (item, m_items) { | 
|---|
| 163 | if (totalWidth > 0) | 
|---|
| 164 | totalWidth += spacing(Qt::Horizontal); | 
|---|
| 165 | QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); | 
|---|
| 166 | totalWidth += pref.width(); | 
|---|
| 167 | maxh = qMax(maxh, pref.height()); | 
|---|
| 168 | } | 
|---|
| 169 | maxh += spacing(Qt::Vertical); | 
|---|
| 170 |  | 
|---|
| 171 | const qreal goldenAspectRatio = 1.61803399; | 
|---|
| 172 | qreal w = qSqrt(totalWidth * maxh * goldenAspectRatio) + left + right; | 
|---|
| 173 |  | 
|---|
| 174 | return minSize(QSizeF(w, -1)); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | QSizeF FlowLayout::maxSize() const | 
|---|
| 178 | { | 
|---|
| 179 | QGraphicsLayoutItem *item; | 
|---|
| 180 | qreal totalWidth = 0; | 
|---|
| 181 | qreal totalHeight = 0; | 
|---|
| 182 | foreach (item, m_items) { | 
|---|
| 183 | if (totalWidth > 0) | 
|---|
| 184 | totalWidth += spacing(Qt::Horizontal); | 
|---|
| 185 | if (totalHeight > 0) | 
|---|
| 186 | totalHeight += spacing(Qt::Vertical); | 
|---|
| 187 | QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); | 
|---|
| 188 | totalWidth += pref.width(); | 
|---|
| 189 | totalHeight += pref.height(); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | qreal left, top, right, bottom; | 
|---|
| 193 | getContentsMargins(&left, &top, &right, &bottom); | 
|---|
| 194 | return QSizeF(left + totalWidth + right, top + totalHeight + bottom); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | QSizeF FlowLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | 
|---|
| 198 | { | 
|---|
| 199 | QSizeF sh = constraint; | 
|---|
| 200 | switch (which) { | 
|---|
| 201 | case Qt::PreferredSize: | 
|---|
| 202 | sh = prefSize(); | 
|---|
| 203 | break; | 
|---|
| 204 | case Qt::MinimumSize: | 
|---|
| 205 | sh = minSize(constraint); | 
|---|
| 206 | break; | 
|---|
| 207 | case Qt::MaximumSize: | 
|---|
| 208 | sh = maxSize(); | 
|---|
| 209 | break; | 
|---|
| 210 | default: | 
|---|
| 211 | break; | 
|---|
| 212 | } | 
|---|
| 213 | return sh; | 
|---|
| 214 | } | 
|---|