Add option for the flow layout to keep a fixed grid size

This commit is contained in:
baldurk
2016-11-21 12:35:38 +01:00
parent f336643e37
commit 2483fd776a
3 changed files with 32 additions and 6 deletions
+26 -6
View File
@@ -43,13 +43,13 @@
#include "FlowLayout.h"
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing), m_fixedGrid(false)
{
setContentsMargins(margin, margin, margin, margin);
}
FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing)
: m_hSpace(hSpacing), m_vSpace(vSpacing)
: m_hSpace(hSpacing), m_vSpace(vSpacing), m_fixedGrid(false)
{
setContentsMargins(margin, margin, margin, margin);
}
@@ -84,6 +84,16 @@ int FlowLayout::verticalSpacing() const
}
}
bool FlowLayout::fixedGrid() const
{
return m_fixedGrid;
}
void FlowLayout::setFixedGrid(bool fixedgrid)
{
m_fixedGrid = fixedgrid;
}
int FlowLayout::count() const
{
return itemList.size();
@@ -151,10 +161,20 @@ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
QSize fixedSize;
QLayoutItem *item;
if(m_fixedGrid) {
foreach (item, itemList) {
fixedSize = fixedSize.expandedTo(item->sizeHint());
}
}
foreach (item, itemList) {
QWidget *wid = item->widget();
QSize size = m_fixedGrid ? fixedSize : item->sizeHint();
int spaceX = horizontalSpacing();
if (spaceX == -1)
spaceX = wid->style()->layoutSpacing(
@@ -163,11 +183,11 @@ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
if (spaceY == -1)
spaceY = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
int nextX = x + item->sizeHint().width() + spaceX;
int nextX = x + size.width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
x = effectiveRect.x();
y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
nextX = x + size.width() + spaceX;
lineHeight = 0;
}
@@ -175,7 +195,7 @@ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
lineHeight = qMax(lineHeight, size.height());
}
return y + lineHeight - rect.y() + bottom;
}
+4
View File
@@ -64,11 +64,15 @@ public:
QSize sizeHint() const Q_DECL_OVERRIDE;
QLayoutItem *takeAt(int index) Q_DECL_OVERRIDE;
bool fixedGrid() const;
void setFixedGrid(bool fixedgrid);
private:
int doLayout(const QRect &rect, bool testOnly) const;
int smartSpacing(QStyle::PixelMetric pm) const;
QList<QLayoutItem *> itemList;
bool m_fixedGrid;
int m_hSpace;
int m_vSpace;
};
@@ -126,6 +126,8 @@ CaptureDialog::CaptureDialog(CaptureContext *ctx, OnCaptureMethod captureCallbac
FlowLayout *optionsFlow = new FlowLayout(ui->optionsGroup, -1, 3, 3);
optionsFlow->setFixedGrid(true);
for(QObject *o : options)
optionsFlow->addWidget(qobject_cast<QWidget *>(o));