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