Browse Source

Improve FlowLayout to support vertical alignment

adaptive-webui-19844
Vladimir Golovnev (Glassez) 2 years ago
parent
commit
d629c77184
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 30
      src/gui/flowlayout.cpp
  2. 1
      src/gui/flowlayout.h

30
src/gui/flowlayout.cpp

@ -29,6 +29,7 @@
#include "flowlayout.h" #include "flowlayout.h"
#include <QHash>
#include <QWidget> #include <QWidget>
#include "base/global.h" #include "base/global.h"
@ -142,6 +143,7 @@ int FlowLayout::doLayout(const QRect &rect, const bool testOnly) const
int y = effectiveRect.y(); int y = effectiveRect.y();
int lineHeight = 0; int lineHeight = 0;
QHash<QLayoutItem *, QPoint> lineItems;
for (QLayoutItem *item : asConst(m_itemList)) for (QLayoutItem *item : asConst(m_itemList))
{ {
const QWidget *wid = item->widget(); const QWidget *wid = item->widget();
@ -163,6 +165,12 @@ int FlowLayout::doLayout(const QRect &rect, const bool testOnly) const
int nextX = x + item->sizeHint().width() + spaceX; int nextX = x + item->sizeHint().width() + spaceX;
if (((nextX - spaceX) > effectiveRect.right()) && (lineHeight > 0)) if (((nextX - spaceX) > effectiveRect.right()) && (lineHeight > 0))
{ {
if (!testOnly)
{
applyItemsGeometry(lineItems, lineHeight);
lineItems.clear();
}
x = effectiveRect.x(); x = effectiveRect.x();
y = y + lineHeight + spaceY; y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX; nextX = x + item->sizeHint().width() + spaceX;
@ -170,12 +178,15 @@ int FlowLayout::doLayout(const QRect &rect, const bool testOnly) const
} }
if (!testOnly) if (!testOnly)
item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); lineItems[item] = QPoint(x, y);
x = nextX; x = nextX;
lineHeight = std::max(lineHeight, item->sizeHint().height()); lineHeight = std::max(lineHeight, item->sizeHint().height());
} }
if (!testOnly)
applyItemsGeometry(lineItems, lineHeight);
return y + lineHeight - rect.y() + bottom; return y + lineHeight - rect.y() + bottom;
} }
@ -193,3 +204,20 @@ int FlowLayout::smartSpacing(const QStyle::PixelMetric pm) const
return static_cast<QLayout *>(parent)->spacing(); return static_cast<QLayout *>(parent)->spacing();
} }
void FlowLayout::applyItemsGeometry(const QHash<QLayoutItem *, QPoint> &items, const int lineHeight) const
{
for (auto it = items.cbegin(); it != items.cend(); ++it)
{
QLayoutItem *item = it.key();
QPoint point = it.value();
const auto alignment = item->alignment();
const int vSpace = lineHeight - item->sizeHint().height();
if (alignment & Qt::AlignVCenter)
point.ry() += vSpace / 2;
else if (alignment & Qt::AlignBottom)
point.ry() += vSpace;
item->setGeometry(QRect(point, item->sizeHint()));
}
}

1
src/gui/flowlayout.h

@ -56,6 +56,7 @@ public:
private: private:
int doLayout(const QRect &rect, bool testOnly) const; int doLayout(const QRect &rect, bool testOnly) const;
int smartSpacing(QStyle::PixelMetric pm) const; int smartSpacing(QStyle::PixelMetric pm) const;
void applyItemsGeometry(const QHash<QLayoutItem *, QPoint> &items, int lineHeight) const;
QList<QLayoutItem *> m_itemList; QList<QLayoutItem *> m_itemList;
int m_hSpace; int m_hSpace;

Loading…
Cancel
Save