55#include "flowlayout.h"
57FlowLayout::FlowLayout(
QWidget *parent,
int margin,
int hSpacing,
int vSpacing)
58 :
QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
60 setContentsMargins(margin, margin, margin, margin);
63FlowLayout::FlowLayout(
int margin,
int hSpacing,
int vSpacing)
64 : m_hSpace(hSpacing), m_vSpace(vSpacing)
66 setContentsMargins(margin, margin, margin, margin);
69FlowLayout::~FlowLayout()
72 while ((item = takeAt(0)))
81int FlowLayout::horizontalSpacing()
const
90int FlowLayout::verticalSpacing()
const
99int FlowLayout::count()
const
101 return itemList.
size();
106 return itemList.
value(index);
111 if (index >= 0 && index < itemList.
size())
112 return itemList.
takeAt(index);
122bool FlowLayout::hasHeightForWidth()
const
127int FlowLayout::heightForWidth(
int width)
const
129 int height = doLayout(
QRect(0, 0, width, 0),
true);
133void FlowLayout::setGeometry(
const QRect &rect)
136 doLayout(rect,
false);
139QSize FlowLayout::sizeHint()
const
141 return minimumSize();
144QSize FlowLayout::minimumSize()
const
148 foreach (item, itemList)
152 size +=
QSize(left + right, top + bottom);
156int FlowLayout::doLayout(
const QRect &rect,
bool testOnly)
const
160 QRect effectiveRect = rect.
adjusted(+left, +top, -right, -bottom);
161 int x = effectiveRect.
x();
162 int y = effectiveRect.
y();
168 for (
int i = 0; i < itemList.
length(); i++) {
169 item = itemList.
at(i);
171 spaceX = horizontalSpacing();
175 int spaceY = verticalSpacing();
181 if (nextX - spaceX > effectiveRect.
right() && lineHeight > 0) {
183 int offset = qFloor((effectiveRect.
right() + spaceX - x) / 2);
184 for(
int j = i-1; j > i-1-rowCount; j--) {
185 auto rowItem = itemList.
at(j);
186 rowItem->
setGeometry(rowItem->geometry().adjusted(offset, 0, offset, 0));
190 x = effectiveRect.
x();
191 y = y + lineHeight + spaceY;
207 int offset = qFloor((effectiveRect.
right() + spaceX - x) / 2);
208 for (
int j = itemList.
length()-1; j > itemList.
length()-1-rowCount; j--) {
209 auto rowItem = itemList.
at(j);
210 rowItem->
setGeometry(rowItem->geometry().adjusted(offset, 0, offset, 0));
214 return y + lineHeight - rect.
y() + bottom;
void getContentsMargins(int *left, int *top, int *right, int *bottom) const const
virtual void setGeometry(const QRect &r) override
Qt::Alignment alignment() const const
virtual QSize minimumSize() const const=0
virtual void setGeometry(const QRect &r)=0
virtual QSize sizeHint() const const=0
void append(const T &value)
const T & at(int i) const const
T value(int i) const const
QObject * parent() const const
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
QSize expandedTo(const QSize &otherSize) const const
PM_LayoutHorizontalSpacing
virtual int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption *option, const QWidget *widget) const const=0
virtual int pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)