2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
10 #include "backgroundcolorhelper.h"
12 #include <KColorScheme>
13 #include <KLocalizedString>
14 #include <KToolTipHelper>
16 #include <QHBoxLayout>
18 #include <QPushButton>
19 #include <QScrollArea>
22 using namespace SelectionMode
;
24 TopBar::TopBar(QWidget
*parent
)
27 // Showing of this widget is normally animated. We hide it for now and make it small.
31 setToolTip(KToolTipHelper::whatsThisHintOnly());
32 setWhatsThis(xi18nc("@info:whatsthis",
33 "<title>Selection Mode</title><para>Select files or folders to manage or manipulate them."
34 "<list><item>Press on a file or folder to select it.</item><item>Press on an already selected file or folder to deselect it.</item>"
35 "<item>Pressing an empty area does <emphasis>not</emphasis> clear the selection.</item>"
36 "<item>Selection rectangles (created by dragging from an empty area) invert the selection status of items within.</item></list></para>"
37 "<para>The available action buttons at the bottom change depending on the current selection.</para>"));
39 auto fillParentLayout
= new QGridLayout(this);
40 fillParentLayout
->setContentsMargins(0, 0, 0, 0);
42 // Put the contents into a QScrollArea. This prevents increasing the view width
43 // in case that not enough width for the contents is available. (this trick is also used in bottombar.cpp.)
44 auto scrollArea
= new QScrollArea(this);
45 fillParentLayout
->addWidget(scrollArea
);
46 scrollArea
->setFrameShape(QFrame::NoFrame
);
47 scrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
48 scrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
49 scrollArea
->setWidgetResizable(true);
51 auto contentsContainer
= new QWidget(scrollArea
);
52 scrollArea
->setWidget(contentsContainer
);
54 BackgroundColorHelper::instance()->controlBackgroundColor(this);
58 m_fullLabelString
= i18nc("@info label above the view explaining the state", "Selection Mode: Click on files or folders to select or deselect them.");
59 m_shortLabelString
= i18nc("@info label above the view explaining the state", "Selection Mode");
60 m_label
= new QLabel(contentsContainer
);
61 m_label
->setMinimumWidth(0);
62 BackgroundColorHelper::instance()->controlBackgroundColor(m_label
);
64 m_closeButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contentsContainer
);
65 m_closeButton
->setText(i18nc("@action:button", "Exit Selection Mode"));
66 m_closeButton
->setFlat(true);
67 connect(m_closeButton
, &QAbstractButton::clicked
, this, &TopBar::selectionModeLeavingRequested
);
69 QHBoxLayout
*layout
= new QHBoxLayout(contentsContainer
);
70 auto contentsMargins
= layout
->contentsMargins();
71 m_preferredHeight
= contentsMargins
.top() + m_label
->sizeHint().height() + contentsMargins
.bottom();
72 scrollArea
->setMaximumHeight(m_preferredHeight
);
73 m_closeButton
->setFixedHeight(m_preferredHeight
);
74 layout
->setContentsMargins(0, 0, 0, 0);
77 layout
->addWidget(m_label
);
79 layout
->addWidget(m_closeButton
);
82 void TopBar::setVisible(bool visible
, Animated animated
)
84 Q_ASSERT_X(animated
== WithAnimation
, "SelectionModeTopBar::setVisible", "This wasn't implemented.");
86 if (m_heightAnimation
) {
87 m_heightAnimation
->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
89 m_heightAnimation
= new QPropertyAnimation(this, "maximumHeight");
90 m_heightAnimation
->setDuration(2 * style()->styleHint(QStyle::SH_Widget_Animation_Duration
, nullptr, this) * GlobalConfig::animationDurationFactor());
92 m_heightAnimation
->setStartValue(height());
93 m_heightAnimation
->setEasingCurve(QEasingCurve::OutCubic
);
96 m_heightAnimation
->setEndValue(m_preferredHeight
);
98 m_heightAnimation
->setEndValue(0);
99 connect(m_heightAnimation
, &QAbstractAnimation::finished
, this, &QWidget::hide
);
102 m_heightAnimation
->start(QAbstractAnimation::DeleteWhenStopped
);
105 void TopBar::resizeEvent(QResizeEvent
*resizeEvent
)
108 return QWidget::resizeEvent(resizeEvent
);
111 void TopBar::updateLabelString()
113 QFontMetrics fontMetrics
= m_label
->fontMetrics();
114 if (fontMetrics
.horizontalAdvance(m_fullLabelString
) + m_closeButton
->sizeHint().width() + style()->pixelMetric(QStyle::PM_LayoutLeftMargin
) * 2
115 + style()->pixelMetric(QStyle::PM_LayoutRightMargin
) * 2
117 m_label
->setText(m_fullLabelString
);
119 m_label
->setText(m_shortLabelString
);