]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/topbar.cpp
DolphinView: Remove -1 interval, add setAutoActivationEnabled
[dolphin.git] / src / selectionmode / topbar.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 "topbar.h"
9
10 #include "backgroundcolorhelper.h"
11
12 #include <KColorScheme>
13 #include <KContextualHelpButton>
14 #include <KLocalizedString>
15
16 #include <QHBoxLayout>
17 #include <QLabel>
18 #include <QPushButton>
19 #include <QStyle>
20
21 using namespace SelectionMode;
22
23 TopBar::TopBar(QWidget *parent)
24 : AnimatedHeightWidget{parent}
25 {
26 QWidget *contentsContainer = prepareContentsContainer();
27
28 BackgroundColorHelper::instance()->controlBackgroundColor(this);
29
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);
36
37 m_contextualHelpButton = new KContextualHelpButton{
38 xi18nc("@info",
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)),
49 nullptr,
50 contentsContainer};
51
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);
56
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);
62
63 layout->addStretch();
64 layout->addWidget(m_label);
65 layout->addWidget(m_contextualHelpButton);
66 layout->addStretch();
67 layout->addWidget(m_closeButton);
68 }
69
70 void TopBar::resizeEvent(QResizeEvent *resizeEvent)
71 {
72 updateLabelString();
73 return AnimatedHeightWidget::resizeEvent(resizeEvent);
74 }
75
76 void TopBar::updateLabelString()
77 {
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
81 < width()) {
82 m_label->setText(m_fullLabelString);
83 } else {
84 m_label->setText(m_shortLabelString);
85 }
86 }
87
88 #include "moc_topbar.cpp"