]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/bottombar.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / selectionmode / bottombar.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
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 <QStyle>
16 #include <QTimer>
17
18 using namespace SelectionMode;
19
20 BottomBar::BottomBar(KActionCollection *actionCollection, QWidget *parent)
21 : AnimatedHeightWidget{parent}
22 {
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) {
29 return;
30 }
31 setVisibleInternal(visible, WithAnimation);
32 });
33 connect(m_contentsContainer, &BottomBarContentsContainer::selectionModeLeavingRequested, this, &BottomBar::selectionModeLeavingRequested);
34
35 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
36 BackgroundColorHelper::instance()->controlBackgroundColor(this);
37 }
38
39 void BottomBar::setVisible(bool visible, Animated animated)
40 {
41 m_allowedToBeVisible = visible;
42 setVisibleInternal(visible, animated);
43 }
44
45 void BottomBar::setVisibleInternal(bool visible, Animated animated)
46 {
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.
50 }
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.
53 }
54
55 AnimatedHeightWidget::setVisible(visible, animated);
56 }
57
58 void BottomBar::slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl)
59 {
60 m_contentsContainer->slotSelectionChanged(selection, baseUrl);
61 }
62
63 void BottomBar::slotSplitTabDisabled()
64 {
65 switch (contents()) {
66 case CopyToOtherViewContents:
67 case MoveToOtherViewContents:
68 Q_EMIT selectionModeLeavingRequested();
69 default:
70 return;
71 }
72 }
73
74 void BottomBar::resetContents(BottomBar::Contents contents)
75 {
76 m_contentsContainer->resetContents(contents);
77
78 if (m_allowedToBeVisible) {
79 setVisibleInternal(true, WithAnimation);
80 }
81 }
82
83 BottomBar::Contents BottomBar::contents() const
84 {
85 return m_contentsContainer->contents();
86 }
87
88 bool BottomBar::eventFilter(QObject *watched, QEvent *event)
89 {
90 Q_ASSERT(qobject_cast<QWidget *>(watched)); // This evenfFilter is only implemented for QWidgets.
91
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());
99 }
100 });
101 // Fall through.
102 default:
103 return false;
104 }
105 }
106
107 void BottomBar::resizeEvent(QResizeEvent *resizeEvent)
108 {
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);
112 }
113
114 m_contentsContainer->adaptToNewBarWidth(width());
115
116 return AnimatedHeightWidget::resizeEvent(resizeEvent);
117 }
118
119 int BottomBar::preferredHeight() const
120 {
121 return m_contentsContainer->sizeHint().height();
122 }
123
124 #include "moc_bottombar.cpp"