]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/bottombar.cpp
f39431076263d51cb577272b636903b4fdb41401
[dolphin.git] / src / selectionmode / bottombar.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "bottombar.h"
9
10 #include "backgroundcolorhelper.h"
11 #include "bottombarcontentscontainer.h"
12
13 #include <QGridLayout>
14 #include <QResizeEvent>
15 #include <QScrollArea>
16 #include <QStyle>
17 #include <QTimer>
18
19 using namespace SelectionMode;
20
21 BottomBar::BottomBar(KActionCollection *actionCollection, QWidget *parent)
22 : QWidget{parent}
23 {
24 // Showing of this widget is normally animated. We hide it for now and make it small.
25 hide();
26 setMaximumHeight(0);
27
28 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
29 setMinimumWidth(0);
30
31 auto fillParentLayout = new QGridLayout(this);
32 fillParentLayout->setContentsMargins(0, 0, 0, 0);
33
34 // Put the contents into a QScrollArea. This prevents increasing the view width
35 // in case that not enough width for the contents is available. (this trick is also used in dolphinsearchbox.cpp.)
36 m_scrollArea = new QScrollArea(this);
37 fillParentLayout->addWidget(m_scrollArea);
38 m_scrollArea->setFrameShape(QFrame::NoFrame);
39 m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40 m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41 m_scrollArea->setWidgetResizable(true);
42
43 m_contentsContainer = new BottomBarContentsContainer(actionCollection, m_scrollArea);
44 m_scrollArea->setWidget(m_contentsContainer);
45 m_contentsContainer->installEventFilter(this); // Adjusts the height of this bar to the height of the contentsContainer
46 connect(m_contentsContainer, &BottomBarContentsContainer::error, this, &BottomBar::error);
47 connect(m_contentsContainer, &BottomBarContentsContainer::barVisibilityChangeRequested, this, [this](bool visible) {
48 if (!m_allowedToBeVisible && visible) {
49 return;
50 }
51 setVisibleInternal(visible, WithAnimation);
52 });
53 connect(m_contentsContainer, &BottomBarContentsContainer::selectionModeLeavingRequested, this, &BottomBar::selectionModeLeavingRequested);
54
55 BackgroundColorHelper::instance()->controlBackgroundColor(this);
56 }
57
58 void BottomBar::setVisible(bool visible, Animated animated)
59 {
60 m_allowedToBeVisible = visible;
61 setVisibleInternal(visible, animated);
62 }
63
64 void BottomBar::setVisibleInternal(bool visible, Animated animated)
65 {
66 Q_ASSERT_X(animated == WithAnimation, "SelectionModeBottomBar::setVisible", "This wasn't implemented.");
67 if (!visible && contents() == PasteContents) {
68 return; // The bar with PasteContents should not be hidden or users might not know how to paste what they just copied.
69 // Set contents to anything else to circumvent this prevention mechanism.
70 }
71 if (visible && !m_contentsContainer->hasSomethingToShow()) {
72 return; // There is nothing on the bar that we want to show. We keep it invisible and only show it when the selection or the contents change.
73 }
74
75 setEnabled(visible);
76 if (m_heightAnimation) {
77 m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
78 }
79 m_heightAnimation = new QPropertyAnimation(this, "maximumHeight");
80 m_heightAnimation->setDuration(2 * style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) * GlobalConfig::animationDurationFactor());
81 m_heightAnimation->setStartValue(height());
82 m_heightAnimation->setEasingCurve(QEasingCurve::OutCubic);
83 if (visible) {
84 show();
85 m_heightAnimation->setEndValue(sizeHint().height());
86 connect(m_heightAnimation, &QAbstractAnimation::finished, this, [this]() {
87 setMaximumHeight(sizeHint().height());
88 });
89 } else {
90 m_heightAnimation->setEndValue(0);
91 connect(m_heightAnimation, &QAbstractAnimation::finished, this, &QWidget::hide);
92 }
93
94 m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped);
95 }
96
97 QSize BottomBar::sizeHint() const
98 {
99 return QSize{1, m_contentsContainer->sizeHint().height()};
100 // 1 as width because this widget should never be the reason the DolphinViewContainer is made wider.
101 }
102
103 void BottomBar::slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl)
104 {
105 m_contentsContainer->slotSelectionChanged(selection, baseUrl);
106 }
107
108 void BottomBar::slotSplitTabDisabled()
109 {
110 switch (contents()) {
111 case CopyToOtherViewContents:
112 case MoveToOtherViewContents:
113 Q_EMIT selectionModeLeavingRequested();
114 default:
115 return;
116 }
117 }
118
119 void BottomBar::resetContents(BottomBar::Contents contents)
120 {
121 m_contentsContainer->resetContents(contents);
122
123 if (m_allowedToBeVisible) {
124 setVisibleInternal(true, WithAnimation);
125 }
126 }
127
128 BottomBar::Contents BottomBar::contents() const
129 {
130 return m_contentsContainer->contents();
131 }
132
133 bool BottomBar::eventFilter(QObject *watched, QEvent *event)
134 {
135 Q_ASSERT(qobject_cast<QWidget *>(watched)); // This evenfFilter is only implemented for QWidgets.
136
137 switch (event->type()) {
138 case QEvent::ChildAdded:
139 case QEvent::ChildRemoved:
140 QTimer::singleShot(0, this, [this]() {
141 // The necessary height might have changed because of the added/removed child so we change the height manually.
142 if (isVisibleTo(parentWidget()) && isEnabled() && (!m_heightAnimation || m_heightAnimation->state() != QAbstractAnimation::Running)) {
143 setMaximumHeight(sizeHint().height());
144 }
145 });
146 // Fall through.
147 default:
148 return false;
149 }
150 }
151
152 void BottomBar::resizeEvent(QResizeEvent *resizeEvent)
153 {
154 if (resizeEvent->oldSize().width() == resizeEvent->size().width()) {
155 // The width() didn't change so our custom override isn't needed.
156 return QWidget::resizeEvent(resizeEvent);
157 }
158
159 m_contentsContainer->adaptToNewBarWidth(width());
160
161 return QWidget::resizeEvent(resizeEvent);
162 }