1 /**************************************************************************************** 2 * Copyright (c) 2004-2008 Trolltech ASA <copyright@trolltech.com> * 3 * * 4 * This program is free software; you can redistribute it and/or modify it under * 5 * the terms of the GNU General Public License as published by the Free Software * 6 * Foundation; either version 2 of the License, or (at your option) version 3 or any * 7 * later version publicly approved by Trolltech ASA (or its successor, if any) and the * 8 * KDE Free Qt Foundation. * 9 * * 10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 12 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 13 * * 14 * You should have received a copy of the GNU General Public License along with * 15 * this program. If not, see <http://www.gnu.org/licenses/>. * 16 * * 17 * In addition, Trolltech gives you certain additional rights as described in the * 18 * Trolltech GPL Exception version 1.2 which can be found at * 19 * http://www.trolltech.com/products/qt/gplexception/ * 20 ****************************************************************************************/ 21 22 #include "FlowLayout.h" 23 FlowLayout(QWidget * parent,int margin,int spacing)24 FlowLayout::FlowLayout(QWidget *parent, int margin, int spacing) 25 : QLayout(parent) 26 { 27 setMargin(margin); 28 setSpacing(spacing); 29 } 30 FlowLayout(int spacing)31 FlowLayout::FlowLayout(int spacing) 32 { 33 setSpacing(spacing); 34 } 35 ~FlowLayout()36 FlowLayout::~FlowLayout() 37 { 38 QLayoutItem *item; 39 while ((item = takeAt(0))) 40 delete item; 41 } 42 addItem(QLayoutItem * item)43 void FlowLayout::addItem(QLayoutItem *item) 44 { 45 itemList.append(item); 46 } 47 count() const48 int FlowLayout::count() const 49 { 50 return itemList.size(); 51 } 52 itemAt(int index) const53 QLayoutItem *FlowLayout::itemAt(int index) const 54 { 55 return itemList.value(index); 56 } 57 takeAt(int index)58 QLayoutItem *FlowLayout::takeAt(int index) 59 { 60 if (index >= 0 && index < itemList.size()) 61 return itemList.takeAt(index); 62 else 63 return 0; 64 } 65 expandingDirections() const66 Qt::Orientations FlowLayout::expandingDirections() const 67 { 68 return 0; 69 } 70 hasHeightForWidth() const71 bool FlowLayout::hasHeightForWidth() const 72 { 73 return true; 74 } 75 heightForWidth(int width) const76 int FlowLayout::heightForWidth(int width) const 77 { 78 int height = doLayout(QRect(0, 0, width, 0), true); 79 return height; 80 } 81 setGeometry(const QRect & rect)82 void FlowLayout::setGeometry(const QRect &rect) 83 { 84 QLayout::setGeometry(rect); 85 doLayout(rect, false); 86 } 87 sizeHint() const88 QSize FlowLayout::sizeHint() const 89 { 90 return minimumSize(); 91 } 92 minimumSize() const93 QSize FlowLayout::minimumSize() const 94 { 95 QSize size; 96 QLayoutItem *item; 97 foreach (item, itemList) 98 size = size.expandedTo(item->minimumSize()); 99 100 size += QSize(2*margin(), 2*margin()); 101 return size; 102 } 103 doLayout(const QRect & rect,bool testOnly) const104 int FlowLayout::doLayout(const QRect &rect, bool testOnly) const 105 { 106 int x = rect.x(); 107 int y = rect.y(); 108 int lineHeight = 0; 109 110 QLayoutItem *item; 111 foreach (item, itemList) { 112 int nextX = x + item->sizeHint().width() + spacing(); 113 if (nextX - spacing() > rect.right() && lineHeight > 0) { 114 x = rect.x(); 115 y = y + lineHeight + spacing(); 116 nextX = x + item->sizeHint().width() + spacing(); 117 lineHeight = 0; 118 } 119 120 if (!testOnly) 121 item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); 122 123 x = nextX; 124 lineHeight = qMax(lineHeight, item->sizeHint().height()); 125 } 126 return y + lineHeight - rect.y(); 127 } 128 129