1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
This file is part of the KDE project
SPDX-FileCopyrightText: 2024 Felix Ernst <[email protected]>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "animatedheightwidget.h"
#include <QEvent>
#include <QGridLayout>
#include <QKeyEvent>
#include <QPropertyAnimation>
#include <QScrollArea>
#include <QScrollBar>
#include <QStyle>
AnimatedHeightWidget::AnimatedHeightWidget(QWidget *parent)
: QWidget{parent}
{
// Showing of this widget is normally animated. We hide it for now and make it small.
hide();
setMaximumHeight(0);
auto fillParentLayout = new QGridLayout(this);
fillParentLayout->setContentsMargins(0, 0, 0, 0);
// Put the contents into a QScrollArea. This prevents increasing the view width
// in case there is not enough available width for the contents.
m_contentsContainerParent = new QScrollArea(this);
fillParentLayout->addWidget(m_contentsContainerParent);
m_contentsContainerParent->setFrameShape(QFrame::NoFrame);
m_contentsContainerParent->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_contentsContainerParent->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_contentsContainerParent->setWidgetResizable(true);
// Disables manual scrolling, for example with mouse scrollwheel.
m_contentsContainerParent->verticalScrollBar()->setEnabled(false);
m_contentsContainerParent->horizontalScrollBar()->setEnabled(false);
setMinimumWidth(0);
}
QSize AnimatedHeightWidget::sizeHint() const
{
return QSize{1, preferredHeight()};
// 1 as width because this widget should never be the reason the DolphinViewContainer is made wider.
}
void AnimatedHeightWidget::setVisible(bool visible, Animated animated)
{
setEnabled(visible);
if (m_heightAnimation) {
m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
}
if (animated == WithAnimation
&& (style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) < 1 || GlobalConfig::animationDurationFactor() <= 0.0)) {
animated = WithoutAnimation;
}
if (animated == WithoutAnimation) {
setMaximumHeight(visible ? preferredHeight() : 0);
setVisible(visible);
return;
}
m_heightAnimation = new QPropertyAnimation(this, "maximumHeight");
m_heightAnimation->setDuration(2 * style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) * GlobalConfig::animationDurationFactor());
m_heightAnimation->setStartValue(height());
m_heightAnimation->setEasingCurve(QEasingCurve::OutCubic);
if (visible) {
show();
m_heightAnimation->setEndValue(preferredHeight());
} else {
m_heightAnimation->setEndValue(0);
connect(m_heightAnimation, &QAbstractAnimation::finished, this, &QWidget::hide);
}
m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
QWidget *AnimatedHeightWidget::prepareContentsContainer(QWidget *contentsContainer)
{
Q_ASSERT_X(!m_contentsContainerParent->widget(),
"AnimatedHeightWidget::prepareContentsContainer",
"Another contentsContainer has already been prepared. There can only be one.");
contentsContainer->setParent(m_contentsContainerParent);
m_contentsContainerParent->setWidget(contentsContainer);
m_contentsContainerParent->setFocusProxy(contentsContainer);
contentsContainer->installEventFilter(this);
return contentsContainer;
}
bool AnimatedHeightWidget::isAnimationRunning() const
{
return m_heightAnimation && m_heightAnimation->state() == QAbstractAnimation::Running;
}
bool AnimatedHeightWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
auto *keyEvent = static_cast<QKeyEvent *>(event);
// Ignore PageUp/PageDown to prevent QScrollArea (invisible scrollbar) from scrolling
if (keyEvent->key() == Qt::Key_PageUp || keyEvent->key() == Qt::Key_PageDown) {
keyEvent->accept();
return true;
}
}
return QWidget::eventFilter(obj, event);
}
|