2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2015-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-01-21 21:14:00 +00:00
|
|
|
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QGridLayout>
|
|
|
|
|
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
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
int itemColumnSpan(QGridLayout const* 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
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
int row = {};
|
|
|
|
int column = {};
|
|
|
|
int row_span = {};
|
|
|
|
int column_span = {};
|
2020-05-27 21:53:12 +00:00
|
|
|
layout->getItemPosition(i, &row, &column, &row_span, &column_span);
|
|
|
|
return column_span;
|
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
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ColumnResizer::ColumnResizer(QObject* parent)
|
|
|
|
: QObject(parent)
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2020-10-31 18:56:12 +00:00
|
|
|
timer_.setSingleShot(true);
|
|
|
|
connect(&timer_, &QTimer::timeout, this, &ColumnResizer::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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
layouts_ << layout;
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
void ColumnResizer::update() const
|
2015-01-21 21:14:00 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
int max_width = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
for (QGridLayout const* const layout : layouts_)
|
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
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
QLayoutItem const* const item = layout->itemAtPosition(i, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (item == nullptr || itemColumnSpan(layout, item) > 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
max_width = qMax(max_width, item->sizeHint().width());
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QGridLayout* const layout : layouts_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
layout->setColumnMinimumWidth(0, max_width);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
{
|
2020-10-31 18:56:12 +00:00
|
|
|
timer_.start(0);
|
2015-01-21 21:14:00 +00:00
|
|
|
}
|