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"
12 #include <KColorScheme>
13 #include <KContextualHelpButton>
14 #include <KLocalizedString>
16 #include <QHBoxLayout>
18 #include <QPushButton>
21 using namespace SelectionMode
;
23 TopBar::TopBar(QWidget
*parent
)
24 : AnimatedHeightWidget
{parent
}
26 QWidget
*contentsContainer
= prepareContentsContainer();
28 BackgroundColorHelper::instance()->controlBackgroundColor(this);
30 m_fullLabelString
= i18nc("@info label above the view explaining the state", "Selection Mode: Click on files or folders to select or deselect them.");
31 m_shortLabelString
= i18nc("@info label above the view explaining the state", "Selection Mode");
32 m_label
= new QLabel(contentsContainer
);
33 m_label
->setMinimumWidth(0);
34 m_label
->setTextInteractionFlags(Qt::TextSelectableByMouse
| Qt::TextSelectableByKeyboard
| Qt::LinksAccessibleByKeyboard
); // for keyboard accessibility
35 BackgroundColorHelper::instance()->controlBackgroundColor(m_label
);
37 m_contextualHelpButton
= new KContextualHelpButton
{
39 "<title>Selection Mode</title><para>Select files or folders to manage or manipulate them."
40 "<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>"
41 "<item>Pressing an empty area does <emphasis>not</emphasis> clear the selection.</item>"
42 "<item>Selection rectangles (created by dragging from an empty area) invert the selection status of items within.</item>"
43 "<item>Moving with <shortcut>arrow keys</shortcut> does <emphasis>not</emphasis> change the selection.</item>"
44 "<item>Pressing <shortcut>%1</shortcut>, <shortcut>%2</shortcut>, or <shortcut>%3</shortcut> toggles the selection.</item></list></para>"
45 "<para>The available action buttons at the bottom change depending on the current selection.</para>",
46 QKeySequence
{Qt::Key_Enter
}.toString(QKeySequence::NativeText
),
47 QKeySequence
{Qt::Key_Return
}.toString(QKeySequence::NativeText
),
48 QKeySequence
{Qt::CTRL
| Qt::Key_Space
}.toString(QKeySequence::NativeText
)),
52 m_closeButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contentsContainer
);
53 m_closeButton
->setText(i18nc("@action:button", "Exit Selection Mode"));
54 m_closeButton
->setFlat(true);
55 connect(m_closeButton
, &QAbstractButton::clicked
, this, &TopBar::selectionModeLeavingRequested
);
57 QHBoxLayout
*layout
= new QHBoxLayout(contentsContainer
);
58 auto contentsMargins
= layout
->contentsMargins();
59 m_preferredHeight
= contentsMargins
.top() + m_label
->sizeHint().height() + contentsMargins
.bottom();
60 m_closeButton
->setFixedHeight(m_preferredHeight
);
61 layout
->setContentsMargins(0, 0, 0, 0);
64 layout
->addWidget(m_label
);
65 layout
->addWidget(m_contextualHelpButton
);
67 layout
->addWidget(m_closeButton
);
70 void TopBar::resizeEvent(QResizeEvent
*resizeEvent
)
73 return AnimatedHeightWidget::resizeEvent(resizeEvent
);
76 void TopBar::updateLabelString()
78 QFontMetrics fontMetrics
= m_label
->fontMetrics();
79 if (fontMetrics
.horizontalAdvance(m_fullLabelString
) + m_contextualHelpButton
->sizeHint().width() + m_closeButton
->sizeHint().width()
80 + style()->pixelMetric(QStyle::PM_LayoutLeftMargin
) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin
) * 2
82 m_label
->setText(m_fullLabelString
);
84 m_label
->setText(m_shortLabelString
);
88 #include "moc_topbar.cpp"