File indexing completed on 2024-05-05 04:49:26

0001 /****************************************************************************************
0002  * Copyright (c) 2004-2008 Trolltech ASA <copyright@trolltech.com>                      *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) version 3 or any    *
0007  * later version publicly approved by Trolltech ASA (or its successor, if any) and the  *
0008  * KDE Free Qt Foundation.                                                              *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  *                                                                                      *
0017  * In addition, Trolltech gives you certain additional rights as described in the       *
0018  * Trolltech GPL Exception version 1.2 which can be found at                            *
0019  * http://www.trolltech.com/products/qt/gplexception/                                   *
0020  ****************************************************************************************/
0021 
0022 #include "FlowLayout.h"
0023 
0024  FlowLayout::FlowLayout(QWidget *parent, int margin, int spacing)
0025     : QLayout(parent)
0026 {
0027     setMargin(margin);
0028     setSpacing(spacing);
0029 }
0030 
0031  FlowLayout::FlowLayout(int spacing)
0032 {
0033     setSpacing(spacing);
0034 }
0035 
0036  FlowLayout::~FlowLayout()
0037 {
0038     QLayoutItem *item;
0039     while ((item = takeAt(0)))
0040         delete item;
0041 }
0042 
0043  void FlowLayout::addItem(QLayoutItem *item)
0044 {
0045     itemList.append(item);
0046 }
0047 
0048  int FlowLayout::count() const
0049 {
0050     return itemList.size();
0051 }
0052 
0053  QLayoutItem *FlowLayout::itemAt(int index) const
0054 {
0055     return itemList.value(index);
0056 }
0057 
0058  QLayoutItem *FlowLayout::takeAt(int index)
0059 {
0060     if (index >= 0 && index < itemList.size())
0061         return itemList.takeAt(index);
0062     else
0063         return nullptr;
0064 }
0065 
0066  Qt::Orientations FlowLayout::expandingDirections() const
0067 {
0068     return {};
0069 }
0070 
0071  bool FlowLayout::hasHeightForWidth() const
0072 {
0073     return true;
0074 }
0075 
0076  int FlowLayout::heightForWidth(int width) const
0077 {
0078     int height = doLayout(QRect(0, 0, width, 0), true);
0079     return height;
0080 }
0081 
0082  void FlowLayout::setGeometry(const QRect &rect)
0083 {
0084     QLayout::setGeometry(rect);
0085     doLayout(rect, false);
0086 }
0087 
0088  QSize FlowLayout::sizeHint() const
0089 {
0090     return minimumSize();
0091 }
0092 
0093  QSize FlowLayout::minimumSize() const
0094 {
0095     QSize size;
0096     QLayoutItem *item;
0097     foreach (item, itemList)
0098         size = size.expandedTo(item->minimumSize());
0099 
0100     size += QSize(2*margin(), 2*margin());
0101     return size;
0102 }
0103 
0104  int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
0105 {
0106     int x = rect.x();
0107     int y = rect.y();
0108     int lineHeight = 0;
0109 
0110     QLayoutItem *item;
0111     foreach (item, itemList) {
0112         int nextX = x + item->sizeHint().width() + spacing();
0113         if (nextX - spacing() > rect.right() && lineHeight > 0) {
0114             x = rect.x();
0115             y = y + lineHeight + spacing();
0116             nextX = x + item->sizeHint().width() + spacing();
0117             lineHeight = 0;
0118         }
0119 
0120         if (!testOnly)
0121             item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
0122 
0123         x = nextX;
0124         lineHeight = qMax(lineHeight, item->sizeHint().height());
0125     }
0126     return y + lineHeight - rect.y();
0127 }
0128