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