2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
10 #include "backgroundcolorhelper.h"
11 #include "bottombarcontentscontainer.h"
13 #include <QGridLayout>
14 #include <QResizeEvent>
18 using namespace SelectionMode
;
20 BottomBar::BottomBar(KActionCollection
*actionCollection
, QWidget
*parent
)
21 : AnimatedHeightWidget
{parent
}
23 m_contentsContainer
= new BottomBarContentsContainer(actionCollection
, nullptr);
24 prepareContentsContainer(m_contentsContainer
);
25 m_contentsContainer
->installEventFilter(this); // Adjusts the height of this bar to the height of the contentsContainer
26 connect(m_contentsContainer
, &BottomBarContentsContainer::error
, this, &BottomBar::error
);
27 connect(m_contentsContainer
, &BottomBarContentsContainer::barVisibilityChangeRequested
, this, [this](bool visible
) {
28 if (!m_allowedToBeVisible
&& visible
) {
31 setVisibleInternal(visible
, WithAnimation
);
33 connect(m_contentsContainer
, &BottomBarContentsContainer::selectionModeLeavingRequested
, this, &BottomBar::selectionModeLeavingRequested
);
35 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
36 BackgroundColorHelper::instance()->controlBackgroundColor(this);
39 void BottomBar::setVisible(bool visible
, Animated animated
)
41 m_allowedToBeVisible
= visible
;
42 setVisibleInternal(visible
, animated
);
45 void BottomBar::setVisibleInternal(bool visible
, Animated animated
)
47 if (!visible
&& contents() == PasteContents
) {
48 return; // The bar with PasteContents should not be hidden or users might not know how to paste what they just copied.
49 // Set contents to anything else to circumvent this prevention mechanism.
51 if (visible
&& !m_contentsContainer
->hasSomethingToShow()) {
52 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.
55 AnimatedHeightWidget::setVisible(visible
, animated
);
58 void BottomBar::slotSelectionChanged(const KFileItemList
&selection
, const QUrl
&baseUrl
)
60 m_contentsContainer
->slotSelectionChanged(selection
, baseUrl
);
63 void BottomBar::slotSplitTabDisabled()
66 case CopyToOtherViewContents
:
67 case MoveToOtherViewContents
:
68 Q_EMIT
selectionModeLeavingRequested();
74 void BottomBar::resetContents(BottomBar::Contents contents
)
76 m_contentsContainer
->resetContents(contents
);
78 if (m_allowedToBeVisible
) {
79 setVisibleInternal(true, WithAnimation
);
83 BottomBar::Contents
BottomBar::contents() const
85 return m_contentsContainer
->contents();
88 bool BottomBar::eventFilter(QObject
*watched
, QEvent
*event
)
90 Q_ASSERT(qobject_cast
<QWidget
*>(watched
)); // This evenfFilter is only implemented for QWidgets.
92 switch (event
->type()) {
93 case QEvent::ChildAdded
:
94 case QEvent::ChildRemoved
:
95 QTimer::singleShot(0, this, [this]() {
96 // The necessary height might have changed because of the added/removed child so we change the height manually.
97 if (isVisibleTo(parentWidget()) && isEnabled() && !isAnimationRunning()) {
98 setMaximumHeight(sizeHint().height());
107 void BottomBar::resizeEvent(QResizeEvent
*resizeEvent
)
109 if (resizeEvent
->oldSize().width() == resizeEvent
->size().width()) {
110 // The width() didn't change so our custom override isn't needed.
111 return AnimatedHeightWidget::resizeEvent(resizeEvent
);
114 m_contentsContainer
->adaptToNewBarWidth(width());
116 return AnimatedHeightWidget::resizeEvent(resizeEvent
);
119 int BottomBar::preferredHeight() const
121 return m_contentsContainer
->sizeHint().height();
124 #include "moc_bottombar.cpp"