2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-05-06 15:05:37 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include "flowlayout.h"
|
|
|
|
|
|
2011-01-24 12:29:48 +01:00
|
|
|
#include <QtCore/QRect>
|
|
|
|
|
#include <QtGui/QWidgetItem>
|
2010-03-18 10:59:06 +01:00
|
|
|
|
2011-06-23 14:35:45 +02:00
|
|
|
using namespace Utils;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-06-23 14:35:45 +02:00
|
|
|
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
|
|
|
|
|
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-06-23 14:35:45 +02:00
|
|
|
setContentsMargins(margin, margin, margin, margin);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-06-23 14:35:45 +02:00
|
|
|
FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
|
|
|
|
|
: m_hSpace(hSpacing), m_vSpace(vSpacing)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-06-23 14:35:45 +02:00
|
|
|
setContentsMargins(margin, margin, margin, margin);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FlowLayout::~FlowLayout()
|
|
|
|
|
{
|
|
|
|
|
QLayoutItem *item;
|
|
|
|
|
while ((item = takeAt(0)))
|
|
|
|
|
delete item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlowLayout::addItem(QLayoutItem *item)
|
|
|
|
|
{
|
|
|
|
|
itemList.append(item);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-23 14:35:45 +02:00
|
|
|
int FlowLayout::horizontalSpacing() const
|
|
|
|
|
{
|
|
|
|
|
if (m_hSpace >= 0) {
|
|
|
|
|
return m_hSpace;
|
|
|
|
|
} else {
|
|
|
|
|
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlowLayout::verticalSpacing() const
|
|
|
|
|
{
|
|
|
|
|
if (m_vSpace >= 0) {
|
|
|
|
|
return m_vSpace;
|
|
|
|
|
} else {
|
|
|
|
|
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
int FlowLayout::count() const
|
|
|
|
|
{
|
|
|
|
|
return itemList.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QLayoutItem *FlowLayout::itemAt(int index) const
|
|
|
|
|
{
|
|
|
|
|
return itemList.value(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QLayoutItem *FlowLayout::takeAt(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index >= 0 && index < itemList.size())
|
|
|
|
|
return itemList.takeAt(index);
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Qt::Orientations FlowLayout::expandingDirections() const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlowLayout::hasHeightForWidth() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlowLayout::heightForWidth(int width) const
|
|
|
|
|
{
|
|
|
|
|
int height = doLayout(QRect(0, 0, width, 0), true);
|
|
|
|
|
return height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlowLayout::setGeometry(const QRect &rect)
|
|
|
|
|
{
|
|
|
|
|
QLayout::setGeometry(rect);
|
|
|
|
|
doLayout(rect, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize FlowLayout::sizeHint() const
|
|
|
|
|
{
|
|
|
|
|
return minimumSize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QSize FlowLayout::minimumSize() const
|
|
|
|
|
{
|
|
|
|
|
QSize size;
|
2011-06-23 14:35:45 +02:00
|
|
|
QLayoutItem *item;
|
|
|
|
|
foreach (item, itemList)
|
2008-12-02 12:01:29 +01:00
|
|
|
size = size.expandedTo(item->minimumSize());
|
|
|
|
|
|
2011-06-23 14:35:45 +02:00
|
|
|
size += QSize(2*margin(), 2*margin());
|
2008-12-02 12:01:29 +01:00
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
|
|
|
|
|
{
|
2011-06-23 14:35:45 +02:00
|
|
|
int left, top, right, bottom;
|
|
|
|
|
getContentsMargins(&left, &top, &right, &bottom);
|
|
|
|
|
QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
|
|
|
|
|
int x = effectiveRect.x();
|
|
|
|
|
int y = effectiveRect.y();
|
2008-12-02 12:01:29 +01:00
|
|
|
int lineHeight = 0;
|
|
|
|
|
|
2011-06-23 14:35:45 +02:00
|
|
|
QLayoutItem *item;
|
|
|
|
|
foreach (item, itemList) {
|
|
|
|
|
QWidget *wid = item->widget();
|
|
|
|
|
int spaceX = horizontalSpacing();
|
|
|
|
|
if (spaceX == -1)
|
|
|
|
|
spaceX = wid->style()->layoutSpacing(
|
|
|
|
|
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
|
|
|
|
|
int spaceY = verticalSpacing();
|
|
|
|
|
if (spaceY == -1)
|
|
|
|
|
spaceY = wid->style()->layoutSpacing(
|
|
|
|
|
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
|
|
|
|
|
int nextX = x + item->sizeHint().width() + spaceX;
|
|
|
|
|
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
|
|
|
|
|
x = effectiveRect.x();
|
|
|
|
|
y = y + lineHeight + spaceY;
|
|
|
|
|
nextX = x + item->sizeHint().width() + spaceX;
|
2008-12-02 12:01:29 +01:00
|
|
|
lineHeight = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!testOnly)
|
|
|
|
|
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
|
|
|
|
|
|
|
|
|
|
x = nextX;
|
|
|
|
|
lineHeight = qMax(lineHeight, item->sizeHint().height());
|
|
|
|
|
}
|
2011-06-23 14:35:45 +02:00
|
|
|
return y + lineHeight - rect.y() + bottom;
|
|
|
|
|
}
|
|
|
|
|
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
|
|
|
|
|
{
|
|
|
|
|
QObject *parent = this->parent();
|
|
|
|
|
if (!parent) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (parent->isWidgetType()) {
|
|
|
|
|
QWidget *pw = static_cast<QWidget *>(parent);
|
|
|
|
|
return pw->style()->pixelMetric(pm, 0, pw);
|
|
|
|
|
} else {
|
|
|
|
|
return static_cast<QLayout *>(parent)->spacing();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|