2015-01-21 21:14:00 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2015 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QTimer>
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "ColumnResizer.h"
|
2015-01-21 21:14:00 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int itemColumnSpan(QGridLayout* layout, QLayoutItem const* item)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
for (int i = 0, count = layout->count(); i < count; ++i)
|
|
|
|
{
|
|
|
|
if (layout->itemAt(i) != item)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-21 21:14:00 +00:00
|
|
|
|
|
|
|
int row, column, rowSpan, columnSpan;
|
2017-04-19 12:04:45 +00:00
|
|
|
layout->getItemPosition(i, &row, &column, &rowSpan, &columnSpan);
|
2015-01-21 21:14:00 +00:00
|
|
|
return columnSpan;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-01-21 21:14:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ColumnResizer::ColumnResizer(QObject* parent) :
|
|
|
|
QObject(parent),
|
|
|
|
myTimer(new QTimer(this)),
|
|
|
|
myLayouts()
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myTimer->setSingleShot(true);
|
|
|
|
connect(myTimer, SIGNAL(timeout()), SLOT(update()));
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void ColumnResizer::addLayout(QGridLayout* layout)
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myLayouts << layout;
|
|
|
|
scheduleUpdate();
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ColumnResizer::eventFilter(QObject* object, QEvent* event)
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (event->type() == QEvent::Resize)
|
|
|
|
{
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QObject::eventFilter(object, event);
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void ColumnResizer::update()
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int maxWidth = 0;
|
|
|
|
|
|
|
|
for (QGridLayout* const layout : myLayouts)
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = 0, count = layout->rowCount(); i < count; ++i)
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QLayoutItem* item = layout->itemAtPosition(i, 0);
|
|
|
|
|
|
|
|
if (item == nullptr || itemColumnSpan(layout, item) > 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxWidth = qMax(maxWidth, item->sizeHint().width());
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (QGridLayout* const layout : myLayouts)
|
|
|
|
{
|
|
|
|
layout->setColumnMinimumWidth(0, maxWidth);
|
|
|
|
}
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void ColumnResizer::scheduleUpdate()
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myTimer->start(0);
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|