1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00
transmission/qt/ColumnResizer.cc
Charles Kerr ebb2ab6aee
chore: update license spdx abbreviations (#2582)
Use SPDX license list 3.0 terminology: replace deprecated identifiers
GPL-2.0" and "GPL-3.0" with "GPL-3.0-only" and "GPL-3.0-only".
2022-02-07 10:25:02 -06:00

87 lines
1.9 KiB
C++

// This file Copyright © 2015-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <QEvent>
#include <QGridLayout>
#include "ColumnResizer.h"
namespace
{
int itemColumnSpan(QGridLayout const* layout, QLayoutItem const* item)
{
for (int i = 0, count = layout->count(); i < count; ++i)
{
if (layout->itemAt(i) != item)
{
continue;
}
int row = {};
int column = {};
int row_span = {};
int column_span = {};
layout->getItemPosition(i, &row, &column, &row_span, &column_span);
return column_span;
}
return 0;
}
} // namespace
ColumnResizer::ColumnResizer(QObject* parent)
: QObject(parent)
{
timer_.setSingleShot(true);
connect(&timer_, &QTimer::timeout, this, &ColumnResizer::update);
}
void ColumnResizer::addLayout(QGridLayout* layout)
{
layouts_ << layout;
scheduleUpdate();
}
bool ColumnResizer::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Resize)
{
scheduleUpdate();
}
return QObject::eventFilter(object, event);
}
void ColumnResizer::update() const
{
int max_width = 0;
for (QGridLayout const* const layout : layouts_)
{
for (int i = 0, count = layout->rowCount(); i < count; ++i)
{
QLayoutItem const* const item = layout->itemAtPosition(i, 0);
if (item == nullptr || itemColumnSpan(layout, item) > 1)
{
continue;
}
max_width = qMax(max_width, item->sizeHint().width());
}
}
for (QGridLayout* const layout : layouts_)
{
layout->setColumnMinimumWidth(0, max_width);
}
}
void ColumnResizer::scheduleUpdate()
{
timer_.start(0);
}