more updates

This commit is contained in:
Ikatono
2024-05-26 01:01:46 -05:00
parent 23068db95f
commit 230f382015
6 changed files with 272 additions and 47 deletions

View File

@@ -4,7 +4,8 @@
#include <QtWidgets>
#include "flowlayout.h"
//! [1]
#include "utils.hpp"
FlowLayout::FlowLayout(QWidget* parent, int margin, int hSpacing, int vSpacing)
: QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing)
{
@@ -34,24 +35,33 @@ void FlowLayout::addItem(QLayoutItem *item)
int FlowLayout::horizontalSpacing() const
{
if (m_hSpace >= 0) {
if (m_hSpace >= 0)
{
return m_hSpace;
} else {
}
else
{
return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
}
}
int FlowLayout::verticalSpacing() const
{
if (m_vSpace >= 0) {
if (m_vSpace >= 0)
{
return m_vSpace;
} else {
}
else
{
return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
}
}
int FlowLayout::count() const
{
qInfo() << "getting item list size";
if (itemList.isEmpty())
return 0;
return itemList.size();
}
@@ -98,12 +108,16 @@ QSize FlowLayout::minimumSize() const
{
QSize size;
for (const QLayoutItem* item : std::as_const(itemList))
{
size = size.expandedTo(item->minimumSize());
}
const QMargins margins = contentsMargins();
size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom());
if (size.height() < minimumHeight())
size.rheight() = minimumHeight();
auto minh = minimumHeight();
if (size.height() < minh)
{
size.rheight() = minh;
}
return size;
}
@@ -115,27 +129,33 @@ int FlowLayout::doLayout(const QRect &rect, bool testOnly) const
int x = effectiveRect.x();
int y = effectiveRect.y();
int lineHeight = 0;
for (QLayoutItem* item : std::as_const(itemList)) {
for (QLayoutItem* item : std::as_const(itemList))
{
const QWidget* wid = item->widget();
int spaceX = horizontalSpacing();
if (spaceX == -1)
{
spaceX = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
}
int spaceY = verticalSpacing();
if (spaceY == -1)
{
spaceY = wid->style()->layoutSpacing(
QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
}
int nextX = x + item->sizeHint().width() + spaceX;
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
if (nextX - spaceX > effectiveRect.right() && lineHeight > 0)
{
x = effectiveRect.x();
y = y + lineHeight + spaceY;
nextX = x + item->sizeHint().width() + spaceX;
lineHeight = 0;
}
if (!testOnly)
{
item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));
}
x = nextX;
lineHeight = qMax(lineHeight, item->sizeHint().height());
}
@@ -162,14 +182,23 @@ int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const
void FlowLayout::setIndex(QWidget* wid, int index)
{
if (itemAt(index)->widget() == wid)
qInfo() << "set index" << index;
if (index < count() && itemAt(index)->widget() == wid)
return;
qInfo() << "past first check";
IndexOutOfRangeException::ThrowIf(index, 0, {});
qInfo() << "past second check";
wid->setParent(nullptr);
qInfo() << "cleared parent";
addWidget(wid);
if (index < 0)
return;
for (int i = count()-1; i > index; i--)
qInfo() << "added widget";
IndexOutOfRangeException::ThrowIf(index, {}, count()-1);
qInfo() << "past third check";
for (int i = count()-1; i > +1; i--)
{
qInfo() << "about to swap" << i-1 << "and" << i;
//TODO excessive, should remove later
IndexOutOfRangeException::ThrowIf(index, 0, count()-1);
itemList.swapItemsAt(i-1, i);
}
}