Create replacement repo for previous corrupted one
This commit is contained in:
151
src/layout/flowLayout/flowLayout.cpp
Normal file
151
src/layout/flowLayout/flowLayout.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
#include "flowLayout.h"
|
||||
#include <QtWidgets>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing)
|
||||
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
|
||||
{
|
||||
setContentsMargins(margin, margin, margin, margin);
|
||||
}
|
||||
|
||||
FlowLayout::~FlowLayout()
|
||||
{
|
||||
QLayoutItem *item;
|
||||
while ((item = takeAt(0)))
|
||||
delete item;
|
||||
}
|
||||
|
||||
void FlowLayout::addItem(QLayoutItem *item)
|
||||
{
|
||||
itemList.append(item);
|
||||
}
|
||||
|
||||
int FlowLayout::horizontalSpacing() const
|
||||
{
|
||||
if (m_hSpace >= 0)
|
||||
return m_hSpace;
|
||||
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
|
||||
}
|
||||
|
||||
int FlowLayout::verticalSpacing() const
|
||||
{
|
||||
if (m_vSpace >= 0)
|
||||
return m_vSpace;
|
||||
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
|
||||
}
|
||||
|
||||
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);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Qt::Orientations FlowLayout::expandingDirections() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
bool FlowLayout::hasHeightForWidth() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int FlowLayout::heightForWidth(int width) const
|
||||
{
|
||||
return doLayout(QRect(0, 0, width, 0), true);
|
||||
}
|
||||
|
||||
QSize FlowLayout::minimumSize() const
|
||||
{
|
||||
QSize size;
|
||||
|
||||
for (QLayoutItem *item : std::as_const(itemList))
|
||||
size = size.expandedTo(item->minimumSize());
|
||||
|
||||
const auto m = contentsMargins();
|
||||
size += QSize(m.left() + m.right(), m.top() + m.bottom());
|
||||
return size;
|
||||
}
|
||||
|
||||
void FlowLayout::setGeometry(const QRect &rect)
|
||||
{
|
||||
QLayout::setGeometry(rect);
|
||||
doLayout(rect, false);
|
||||
}
|
||||
|
||||
QSize FlowLayout::sizeHint() const
|
||||
{
|
||||
return minimumSize();
|
||||
}
|
||||
|
||||
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
|
||||
{
|
||||
int left, top, right, bottom;
|
||||
getContentsMargins(&left, &top, &right, &bottom);
|
||||
|
||||
QRect effective = rect.adjusted(+left, +top, -right, -bottom);
|
||||
|
||||
int x = effective.x();
|
||||
int y = effective.y();
|
||||
int lineHeight = 0;
|
||||
|
||||
for (QLayoutItem *item : std::as_const(itemList)) {
|
||||
QWidget *widget = item->widget();
|
||||
|
||||
int spaceX = horizontalSpacing();
|
||||
if (spaceX < 0)
|
||||
spaceX = widget->style()->layoutSpacing(
|
||||
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
|
||||
|
||||
int spaceY = verticalSpacing();
|
||||
if (spaceY < 0)
|
||||
spaceY = widget->style()->layoutSpacing(
|
||||
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
|
||||
|
||||
const QSize hint = item->sizeHint();
|
||||
int nextX = x + hint.width() + spaceX;
|
||||
|
||||
if (nextX - spaceX > effective.right() && lineHeight > 0) {
|
||||
// wrap
|
||||
x = effective.x();
|
||||
y += lineHeight + spaceY;
|
||||
nextX = x + hint.width() + spaceX;
|
||||
lineHeight = 0;
|
||||
}
|
||||
|
||||
if (!testOnly)
|
||||
item->setGeometry(QRect(QPoint(x, y), hint));
|
||||
|
||||
x = nextX;
|
||||
lineHeight = qMax(lineHeight, hint.height());
|
||||
}
|
||||
|
||||
return y + lineHeight + bottom - rect.y();
|
||||
}
|
||||
|
||||
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
|
||||
{
|
||||
QObject *parentObj = parent();
|
||||
|
||||
if (!parentObj)
|
||||
return -1;
|
||||
|
||||
if (parentObj->isWidgetType()) {
|
||||
QWidget *w = static_cast<QWidget *>(parentObj);
|
||||
return w->style()->pixelMetric(pm, nullptr, w);
|
||||
}
|
||||
|
||||
return static_cast<QLayout *>(parentObj)->spacing();
|
||||
}
|
||||
35
src/layout/flowLayout/flowLayout.h
Normal file
35
src/layout/flowLayout/flowLayout.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef FLOWLAYOUT_H
|
||||
#define FLOWLAYOUT_H
|
||||
|
||||
#include <QLayout>
|
||||
#include <QRect>
|
||||
#include <QStyle>
|
||||
|
||||
class FlowLayout : public QLayout {
|
||||
public:
|
||||
explicit FlowLayout(QWidget *parent = nullptr, int margin = -1, int hSpacing = -1, int vSpacing = -1);
|
||||
~FlowLayout();
|
||||
|
||||
void addItem(QLayoutItem *item) override;
|
||||
int horizontalSpacing() const;
|
||||
int verticalSpacing() const;
|
||||
Qt::Orientations expandingDirections() const override;
|
||||
bool hasHeightForWidth() const override;
|
||||
int heightForWidth(int) const override;
|
||||
int count() const override;
|
||||
QLayoutItem *itemAt(int index) const override;
|
||||
QSize minimumSize() const override;
|
||||
void setGeometry(const QRect &rect) override;
|
||||
QSize sizeHint() const override;
|
||||
QLayoutItem *takeAt(int index) override;
|
||||
|
||||
private:
|
||||
int doLayout(const QRect &rect, bool testOnly) const;
|
||||
int smartSpacing(QStyle::PixelMetric pm) const;
|
||||
|
||||
QList<QLayoutItem *> itemList;
|
||||
int m_hSpace;
|
||||
int m_vSpace;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user