11#include "kblocklayout.h"
17class KBlockLayout::Private
48KBlockLayout::KBlockLayout(
QWidget *parent,
int margin,
int hSpacing,
int vSpacing)
53 setContentsMargins(margin, margin, margin, margin);
54 setSpacing(hSpacing, vSpacing);
57KBlockLayout::KBlockLayout(
int margin,
int hSpacing,
int vSpacing)
61 setContentsMargins(margin, margin, margin, margin);
62 setSpacing(hSpacing, vSpacing);
65KBlockLayout::~KBlockLayout()
68 while ((item = takeAt(0)))
84int KBlockLayout::horizontalSpacing()
const
86 if (d->m_hSpace >= 0) {
93int KBlockLayout::verticalSpacing()
const
95 if (d->m_vSpace >= 0) {
102void KBlockLayout::setSpacing(
int h,
int v)
114int KBlockLayout::count()
const
116 return d->itemList.
size();
121 return d->itemList.
value(index);
126 if (index >= 0 && index < d->itemList.size())
127 return d->itemList.
takeAt(index);
137bool KBlockLayout::hasHeightForWidth()
const
142int KBlockLayout::heightForWidth(
int width)
const
144 int height = doLayout(
QRect(0, 0, width, 0),
true);
148void KBlockLayout::setGeometry(
const QRect &rect)
151 doLayout(rect,
false);
154int KBlockLayout::getMargin()
const
158 if (left == top && top == right && right == bottom) {
165QSize KBlockLayout::sizeHint()
const
169 for (
QLayoutItem *item : std::as_const(d->itemList)) {
177 size.
rwidth() += horizontalSpacing() * d->itemList.
count();
178 size +=
QSize(2 * getMargin(), 2 * getMargin());
182QSize KBlockLayout::minimumSize()
const
185 for (
QLayoutItem *item : std::as_const(d->itemList)) {
189 size +=
QSize(2 * getMargin(), 2 * getMargin());
206int KBlockLayout::doLayout(
const QRect &rect,
bool testOnly)
const
215 for (
int i = 0; i < d->itemList.
count(); ++i) {
217 int nextX = x + item->
sizeHint().
width() + horizontalSpacing();
218 if (nextX - horizontalSpacing() > rect.
right() && lineHeight > 0) {
219 rows.
append(Row(rowItems, lineHeight, x - horizontalSpacing()));
223 y = y + lineHeight + verticalSpacing();
234 rows.
append(Row(rowItems, lineHeight, x - horizontalSpacing()));
236 int finalHeight = y + lineHeight - rect.
y();
242 for (
const Row &row : std::as_const(rows)) {
245 x += (rect.
width() - row.width);
247 x += (rect.
width() - row.width) / 2;
249 for (
QLayoutItem *item : std::as_const(row.items)) {
252 yy += (row.height - item->
sizeHint().height());
254 yy += (row.height - item->
sizeHint().height()) / 2;
260 x += (rect.
width() - row.width) / qMax(row.items.count() - 1, 1);
263 y = y + row.height + verticalSpacing();
The KBlockLayout arranges widget in rows and columns like a text editor does.
void setAlignment(Qt::Alignment)
Set the alignment to use.
void getContentsMargins(int *left, int *top, int *right, int *bottom) const const
virtual void setGeometry(const QRect &r) override
virtual QSize minimumSize() const const=0
virtual void setGeometry(const QRect &r)=0
virtual QSize sizeHint() const const=0
void append(QList< T > &&value)
qsizetype count() const const
qsizetype size() const const
T value(qsizetype i) const const
QObject * parent() const const
QSize expandedTo(const QSize &otherSize) const const
void setHeight(int height)
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const const=0
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)