136 lines
2.9 KiB
C++
Executable File
136 lines
2.9 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* GridLaout with auto column layouter.
|
|
* Layouts all widgets depending on the maxColumn value
|
|
* releayouts if maxColumn changed
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
|
|
#include "lts_grid_layout.h"
|
|
#include <QResizeEvent>
|
|
|
|
namespace LtsWidgets
|
|
{
|
|
|
|
LtsGridLayout::LtsGridLayout(LtsWidgets::ButtonSize buttonSize, QWidget *parent, int maxColumn)
|
|
: QGridLayout(parent)
|
|
, m_buttonSize(buttonSize)
|
|
, m_defaultMaxColumn(maxColumn)
|
|
, m_maxColumn(maxColumn)
|
|
, m_colSize(0)
|
|
, m_autoSize(maxColumn < 0)
|
|
{
|
|
setSizeConstraint(SizeConstraint::SetNoConstraint);
|
|
//setHorizontalSpacing(1);
|
|
setVerticalSpacing(1);
|
|
setMargin(0);
|
|
}
|
|
|
|
void LtsGridLayout::add_widget(QWidget *widget)
|
|
{
|
|
for (auto it = m_widgetList.cbegin(); it != m_widgetList.cend(); ++it)
|
|
{
|
|
if (*it == widget)
|
|
return;
|
|
}
|
|
connect(widget, &QWidget::destroyed, this, &LtsGridLayout::onWidgetDestroyed);
|
|
addWidgetInternal(widget);
|
|
m_widgetList.push_back(widget);
|
|
}
|
|
|
|
void LtsGridLayout::setAutoSize(bool enable)
|
|
{
|
|
m_autoSize = enable;
|
|
if (m_autoSize == false)
|
|
setMaxColumn(1);
|
|
else
|
|
{
|
|
m_maxColumn = -1;
|
|
m_oldSize = QSize();
|
|
m_oldGeo = QRect();
|
|
QResizeEvent e(sizeHint(), sizeHint());
|
|
onParentResize(&e);
|
|
setGeometry(QRect(0, 0, sizeHint().width(), sizeHint().height()));
|
|
}
|
|
}
|
|
|
|
void LtsGridLayout::setMaxColumn(int max)
|
|
{
|
|
m_maxColumn = max;
|
|
m_defaultMaxColumn = max;
|
|
|
|
//resetInternalSize
|
|
m_colSize = 0;
|
|
|
|
for (QWidget *wid : qAsConst(m_widgetList))
|
|
removeWidget(wid);
|
|
|
|
for (QWidget *wid : qAsConst(m_widgetList))
|
|
addWidgetInternal(wid);
|
|
|
|
}
|
|
|
|
void LtsGridLayout::addWidgetInternal(QWidget *widget)
|
|
{
|
|
int current_row = 0;
|
|
int current_column = 0;
|
|
|
|
while (QLayoutItem *item = itemAtPosition(current_row, current_column))
|
|
{
|
|
if (!item->widget())
|
|
{
|
|
removeItem(item);
|
|
continue;
|
|
}
|
|
|
|
if (current_column == (m_maxColumn - 1))
|
|
{
|
|
current_column = 0;
|
|
++current_row;
|
|
}
|
|
else
|
|
++current_column;
|
|
}
|
|
|
|
if (current_row == 0)
|
|
m_colSize += 1;
|
|
|
|
QGridLayout::addWidget(widget, current_row, current_column);
|
|
}
|
|
|
|
|
|
void LtsGridLayout::onParentResize(QResizeEvent *e)
|
|
{
|
|
if (m_autoSize && m_oldSize != e->size())
|
|
{
|
|
bool bigger = (e->size().width() > m_oldSize.width());
|
|
m_oldSize = e->size();
|
|
int newWidth = m_oldSize.width();
|
|
int maxIconsInRow = newWidth / (static_cast<int>(m_buttonSize) + 4);
|
|
|
|
if (!bigger && newWidth <= (static_cast<int>(m_buttonSize)*m_colSize - static_cast<int>(m_buttonSize) / 2))
|
|
maxIconsInRow--;
|
|
if (m_maxColumn != maxIconsInRow)
|
|
setMaxColumn(maxIconsInRow);
|
|
}
|
|
}
|
|
|
|
void LtsGridLayout::onWidgetDestroyed()
|
|
{
|
|
QWidget *s = qobject_cast<QWidget*>(sender());
|
|
for (auto it = m_widgetList.begin(); it != m_widgetList.end(); ++it)
|
|
{
|
|
if (*it == s)
|
|
{
|
|
QGridLayout::removeWidget(s);
|
|
m_widgetList.erase(it);
|
|
setMaxColumn(m_maxColumn); // resort, also remove the layoutitem
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace LtsWidgets
|