From aedfa16e422b9ba07777e10ba5fadb0699014186 Mon Sep 17 00:00:00 2001 From: Trevor SANDY Date: Wed, 7 Sep 2022 17:58:26 +0200 Subject: [PATCH] Improve shortcuts dialog interactive resize --- qt/lc_qutils.cpp | 30 +++++++++++++++++++----------- qt/lc_qutils.h | 5 +++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/qt/lc_qutils.cpp b/qt/lc_qutils.cpp index c08f21e2..f843353d 100644 --- a/qt/lc_qutils.cpp +++ b/qt/lc_qutils.cpp @@ -47,40 +47,48 @@ float lcParseValueLocalized(const QString& Value) // Resize all columns to content except for one stretching column. (taken from QT creator) lcQTreeWidgetColumnStretcher::lcQTreeWidgetColumnStretcher(QTreeWidget *treeWidget, int columnToStretch) - : QObject(treeWidget->header()), m_columnToStretch(columnToStretch) + : QObject(treeWidget->header()), m_columnToStretch(columnToStretch), m_stretchWidth(0) { parent()->installEventFilter(this); + connect(treeWidget->header(), SIGNAL(sectionResized(int, int, int)), SLOT(sectionResized(int, int, int))); QHideEvent fake; lcQTreeWidgetColumnStretcher::eventFilter(parent(), &fake); } +void lcQTreeWidgetColumnStretcher::sectionResized(int LogicalIndex, int OldSize, int NewSize) +{ + Q_UNUSED(OldSize) + + if (LogicalIndex == m_columnToStretch) + m_stretchWidth = NewSize; +} + bool lcQTreeWidgetColumnStretcher::eventFilter(QObject* Object, QEvent* Event) { if (Object == parent()) { + QHeaderView* HeaderView = qobject_cast(Object); + if (Event->type() == QEvent::Show) { - QHeaderView* HeaderView = qobject_cast(Object); - for (int i = 0; i < HeaderView->count(); ++i) HeaderView->setSectionResizeMode(i, QHeaderView::Interactive); + + m_stretchWidth = HeaderView->sectionSize(m_columnToStretch); + } else if (Event->type() == QEvent::Hide) { - QHeaderView* HeaderView = qobject_cast(Object); - for (int i = 0; i < HeaderView->count(); ++i) HeaderView->setSectionResizeMode(i, i == m_columnToStretch ? QHeaderView::Stretch : QHeaderView::ResizeToContents); } else if (Event->type() == QEvent::Resize) { - QHeaderView* HeaderView = qobject_cast(Object); + if (HeaderView->sectionResizeMode(m_columnToStretch) == QHeaderView::Interactive) { - if (HeaderView->sectionResizeMode(m_columnToStretch) == QHeaderView::Interactive) - { - const QResizeEvent* ResizeEvent = reinterpret_cast(Event); - const int Diff = ResizeEvent->size().width() - ResizeEvent->oldSize().width() ; - HeaderView->resizeSection(m_columnToStretch, qMax(32, HeaderView->sectionSize(1) + Diff)); + const int StretchWidth = HeaderView->isVisible() ? m_stretchWidth : 32; + + HeaderView->resizeSection(m_columnToStretch, StretchWidth); } } } diff --git a/qt/lc_qutils.h b/qt/lc_qutils.h index 1b229160..f0bfecfe 100644 --- a/qt/lc_qutils.h +++ b/qt/lc_qutils.h @@ -15,7 +15,12 @@ public: bool eventFilter(QObject* Object, QEvent* Event) override; +private slots: + void sectionResized(int LogicalIndex, int OldSize, int NewSize); + +private: const int m_columnToStretch; + int m_stretchWidth; }; class lcSmallLineEdit : public QLineEdit