2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2024 Felix Ernst <felixernst@kde.org>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #include "animatedheightwidget.h"
10 #include <QGridLayout>
11 #include <QPropertyAnimation>
12 #include <QScrollArea>
15 AnimatedHeightWidget::AnimatedHeightWidget(QWidget
*parent
)
18 // Showing of this widget is normally animated. We hide it for now and make it small.
22 auto fillParentLayout
= new QGridLayout(this);
23 fillParentLayout
->setContentsMargins(0, 0, 0, 0);
25 // Put the contents into a QScrollArea. This prevents increasing the view width
26 // in case there is not enough available width for the contents.
27 m_contentsContainerParent
= new QScrollArea(this);
28 fillParentLayout
->addWidget(m_contentsContainerParent
);
29 m_contentsContainerParent
->setFrameShape(QFrame::NoFrame
);
30 m_contentsContainerParent
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
31 m_contentsContainerParent
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
32 m_contentsContainerParent
->setWidgetResizable(true);
37 QSize
AnimatedHeightWidget::sizeHint() const
39 return QSize
{1, preferredHeight()};
40 // 1 as width because this widget should never be the reason the DolphinViewContainer is made wider.
43 void AnimatedHeightWidget::setVisible(bool visible
, Animated animated
)
46 if (m_heightAnimation
) {
47 m_heightAnimation
->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
50 if (animated
== WithAnimation
51 && (style()->styleHint(QStyle::SH_Widget_Animation_Duration
, nullptr, this) < 1 || GlobalConfig::animationDurationFactor() <= 0.0)) {
52 animated
= WithoutAnimation
;
55 if (animated
== WithoutAnimation
) {
56 setMaximumHeight(visible
? preferredHeight() : 0);
61 m_heightAnimation
= new QPropertyAnimation(this, "maximumHeight");
62 m_heightAnimation
->setDuration(2 * style()->styleHint(QStyle::SH_Widget_Animation_Duration
, nullptr, this) * GlobalConfig::animationDurationFactor());
64 m_heightAnimation
->setStartValue(height());
65 m_heightAnimation
->setEasingCurve(QEasingCurve::OutCubic
);
68 m_heightAnimation
->setEndValue(preferredHeight());
70 m_heightAnimation
->setEndValue(0);
71 connect(m_heightAnimation
, &QAbstractAnimation::finished
, this, &QWidget::hide
);
74 m_heightAnimation
->start(QAbstractAnimation::DeleteWhenStopped
);
77 QWidget
*AnimatedHeightWidget::prepareContentsContainer(QWidget
*contentsContainer
)
79 Q_ASSERT_X(!m_contentsContainerParent
->widget(),
80 "AnimatedHeightWidget::prepareContentsContainer",
81 "Another contentsContainer has already been prepared. There can only be one.");
82 contentsContainer
->setParent(m_contentsContainerParent
);
83 m_contentsContainerParent
->setWidget(contentsContainer
);
84 m_contentsContainerParent
->setFocusProxy(contentsContainer
);
85 return contentsContainer
;
88 bool AnimatedHeightWidget::isAnimationRunning() const
90 return m_heightAnimation
&& m_heightAnimation
->state() == QAbstractAnimation::Running
;