From 0b2e725471b71768ed26c6a7a96053c9e7dd1532 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Mon, 14 Feb 2022 21:49:39 +0100 Subject: [PATCH] Do not run collapse animation when calling SetContent() --- src/headers/section.hpp | 3 ++- src/macro-action-edit.cpp | 2 +- src/macro-condition-edit.cpp | 2 +- src/section.cpp | 41 ++++++++++++++++++++++++------------ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/headers/section.hpp b/src/headers/section.hpp index 62c0de03..ef5f6b23 100644 --- a/src/headers/section.hpp +++ b/src/headers/section.hpp @@ -14,7 +14,8 @@ public: explicit Section(const int animationDuration = 300, QWidget *parent = 0); - void SetContent(QWidget *w, bool collapsed = true); + void SetContent(QWidget *w); + void SetContent(QWidget *w, bool collapsed); void AddHeaderWidget(QWidget *); void SetCollapsed(bool); diff --git a/src/macro-action-edit.cpp b/src/macro-action-edit.cpp index f827c1cb..f59966e3 100644 --- a/src/macro-action-edit.cpp +++ b/src/macro-action-edit.cpp @@ -111,7 +111,7 @@ void MacroActionEdit::ActionSelectionChanged(const QString &text) auto widget = MacroActionFactory::CreateWidget(id, this, *_entryData); QWidget::connect(widget, SIGNAL(HeaderInfoChanged(const QString &)), this, SLOT(HeaderInfoChanged(const QString &))); - _section->SetContent(widget, false); + _section->SetContent(widget); SetFocusPolicyOfWidgets(); } diff --git a/src/macro-condition-edit.cpp b/src/macro-condition-edit.cpp index 03d197ba..be8f74db 100644 --- a/src/macro-condition-edit.cpp +++ b/src/macro-condition-edit.cpp @@ -212,7 +212,7 @@ void MacroConditionEdit::ConditionSelectionChanged(const QString &text) MacroConditionFactory::CreateWidget(id, this, *_entryData); QWidget::connect(widget, SIGNAL(HeaderInfoChanged(const QString &)), this, SLOT(HeaderInfoChanged(const QString &))); - _section->SetContent(widget, false); + _section->SetContent(widget); _dur->setVisible(MacroConditionFactory::UsesDurationConstraint(id)); SetFocusPolicyOfWidgets(); } diff --git a/src/section.cpp b/src/section.cpp index efc980fd..bbf75e2d 100644 --- a/src/section.cpp +++ b/src/section.cpp @@ -42,17 +42,21 @@ Section::Section(const int animationDuration, QWidget *parent) void Section::Collapse(bool collapse) { _toggleButton->setChecked(collapse); - _toggleButton->setArrowType(!collapse ? Qt::ArrowType::DownArrow - : Qt::ArrowType::RightArrow); - _toggleAnimation->setDirection(!collapse - ? QAbstractAnimation::Forward - : QAbstractAnimation::Backward); + _toggleButton->setArrowType(collapse ? Qt::ArrowType::RightArrow + : Qt::ArrowType::DownArrow); + _toggleAnimation->setDirection(collapse ? QAbstractAnimation::Backward + : QAbstractAnimation::Forward); _transitioning = true; _collapsed = collapse; _toggleAnimation->start(); emit Collapsed(collapse); } +void Section::SetContent(QWidget *w) +{ + SetContent(w, _collapsed); +} + void Section::SetContent(QWidget *w, bool collapsed) { CleanUpPreviousContent(); @@ -67,8 +71,8 @@ void Section::SetContent(QWidget *w, bool collapsed) _contentArea->setMaximumHeight(0); _contentArea->setMinimumHeight(0); - w->installEventFilter(this); _content = w; + _content->installEventFilter(this); auto newLayout = new QVBoxLayout(); newLayout->setContentsMargins(0, 0, 0, 0); newLayout->addWidget(w); @@ -76,7 +80,9 @@ void Section::SetContent(QWidget *w, bool collapsed) _mainLayout->addWidget(_contentArea, 1, 0, 1, 3); _headerHeight = sizeHint().height() - _contentArea->maximumHeight(); - _contentHeight = newLayout->sizeHint().height(); + _contentHeight = _content->sizeHint().height(); + + SetupAnimations(); if (collapsed) { this->setMinimumHeight(_headerHeight); @@ -85,8 +91,11 @@ void Section::SetContent(QWidget *w, bool collapsed) this->setMinimumHeight(_headerHeight + _contentHeight); _contentArea->setMaximumHeight(_contentHeight); } - SetupAnimations(); - Collapse(collapsed); + const QSignalBlocker b(_toggleButton); + _toggleButton->setChecked(collapsed); + _toggleButton->setArrowType(collapsed ? Qt::ArrowType::RightArrow + : Qt::ArrowType::DownArrow); + _collapsed = collapsed; } void Section::AddHeaderWidget(QWidget *w) @@ -102,11 +111,15 @@ void Section::SetCollapsed(bool collapsed) bool Section::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::Resize && !_transitioning && !_collapsed) { - _contentHeight = _content->sizeHint().height(); - setMaximumHeight(_headerHeight + _contentHeight); - setMinimumHeight(_headerHeight + _contentHeight); - _contentArea->setMaximumHeight(_contentHeight); - SetupAnimations(); + if (_contentHeight != _content->sizeHint().height()) { + _contentHeight = _content->sizeHint().height(); + setMaximumHeight(_headerHeight + _contentHeight); + setMinimumHeight(_headerHeight + _contentHeight); + _contentArea->setMaximumHeight(_contentHeight); + // Note: Calling this too frequently inside this event + // filter will cause a segfault for some reason + SetupAnimations(); + } } return QObject::eventFilter(obj, event); }