2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <fe.a.ernst@gmail.com>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #include "selectionmodetopbar.h"
10 #include "backgroundcolorhelper.h"
12 #include <KColorScheme>
13 #include <KLocalizedString>
14 #include <KToolTipHelper>
16 #include <QHBoxLayout>
19 #include <QPropertyAnimation>
20 #include <QPushButton>
21 #include <QScrollArea>
25 #include <type_traits>
28 SelectionModeTopBar::SelectionModeTopBar(QWidget
*parent
) :
31 // Showing of this widget is normally animated. We hide it for now and make it small.
35 setToolTip(KToolTipHelper::whatsThisHintOnly());
36 setWhatsThis(xi18nc("@info:whatsthis", "<title>Selection Mode</title><para>Select files or folders to manage or manipulate them."
37 "<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>"
38 "<item>Pressing an empty area does <emphasis>not</emphasis> clear the selection.</item>"
39 "<item>Selection rectangles (created by dragging from an empty area) invert the selection status of items within.</item></list></para>"
40 "<para>The available action buttons at the bottom change depending on the current selection.</para>"));
42 auto fillParentLayout
= new QGridLayout(this);
43 fillParentLayout
->setContentsMargins(0, 0, 0, 0);
45 // Put the contents into a QScrollArea. This prevents increasing the view width
46 // in case that not enough width for the contents is available. (this trick is also used in selectionmodebottombar.cpp.)
47 auto scrollArea
= new QScrollArea(this);
48 fillParentLayout
->addWidget(scrollArea
);
49 scrollArea
->setFrameShape(QFrame::NoFrame
);
50 scrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
51 scrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
52 scrollArea
->setWidgetResizable(true);
54 auto contentsContainer
= new QWidget(scrollArea
);
55 scrollArea
->setWidget(contentsContainer
);
57 BackgroundColorHelper::instance()->controlBackgroundColor(this);
61 m_fullLabelString
= i18nc("@info label above the view explaining the state",
62 "Selection Mode: Click on files or folders to select or deselect them.");
63 m_shortLabelString
= i18nc("@info label above the view explaining the state",
65 m_label
= new QLabel(contentsContainer
);
66 m_label
->setMinimumWidth(0);
67 BackgroundColorHelper::instance()->controlBackgroundColor(m_label
);
69 m_closeButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contentsContainer
);
70 m_closeButton
->setToolTip(i18nc("@action:button", "Exit Selection Mode"));
71 m_closeButton
->setAccessibleName(m_closeButton
->toolTip());
72 m_closeButton
->setFlat(true);
73 connect(m_closeButton
, &QAbstractButton::pressed
,
74 this, &SelectionModeTopBar::leaveSelectionModeRequested
);
76 QHBoxLayout
*layout
= new QHBoxLayout(contentsContainer
);
77 auto contentsMargins
= layout
->contentsMargins();
78 m_preferredHeight
= contentsMargins
.top() + m_label
->sizeHint().height() + contentsMargins
.bottom();
79 scrollArea
->setMaximumHeight(m_preferredHeight
);
80 m_closeButton
->setFixedHeight(m_preferredHeight
);
81 layout
->setContentsMargins(0, 0, 0, 0);
84 layout
->addWidget(m_label
);
86 layout
->addWidget(m_closeButton
);
89 void SelectionModeTopBar::setVisible(bool visible
, Animated animated
)
91 Q_ASSERT_X(animated
== WithAnimation
, "SelectionModeTopBar::setVisible", "This wasn't implemented.");
93 if (!m_heightAnimation
) {
94 m_heightAnimation
= new QPropertyAnimation(this, "maximumHeight");
96 disconnect(m_heightAnimation
, &QAbstractAnimation::finished
,
97 this, &QWidget::hide
);
98 m_heightAnimation
->setDuration(2 *
99 style()->styleHint(QStyle::SH_Widget_Animation_Duration
, nullptr, this) *
100 GlobalConfig::animationDurationFactor());
104 m_heightAnimation
->setStartValue(0);
105 m_heightAnimation
->setEndValue(m_preferredHeight
);
106 m_heightAnimation
->setEasingCurve(QEasingCurve::OutCubic
);
108 m_heightAnimation
->setStartValue(height());
109 m_heightAnimation
->setEndValue(0);
110 m_heightAnimation
->setEasingCurve(QEasingCurve::OutCubic
);
111 connect(m_heightAnimation
, &QAbstractAnimation::finished
,
112 this, &QWidget::hide
);
115 m_heightAnimation
->start();
118 void SelectionModeTopBar::resizeEvent(QResizeEvent */
* resizeEvent */
)
123 void SelectionModeTopBar::updateLabelString()
125 QFontMetrics fontMetrics
= m_label
->fontMetrics();
126 if (fontMetrics
.horizontalAdvance(m_fullLabelString
) + m_closeButton
->sizeHint().width() + style()->pixelMetric(QStyle::PM_LayoutLeftMargin
) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin
) * 2 < width()) {
127 m_label
->setText(m_fullLabelString
);
129 m_label
->setText(m_shortLabelString
);