]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/topbar.cpp
Merge remote-tracking branch 'upstream/master' into work/zakharafoniam/useful-groups
[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 <KLocalizedString>
14 #include <KToolTipHelper>
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 setToolTip(KToolTipHelper::whatsThisHintOnly());
27 setWhatsThis(xi18nc("@info:whatsthis",
28 "<title>Selection Mode</title><para>Select files or folders to manage or manipulate them."
29 "<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>"
30 "<item>Pressing an empty area does <emphasis>not</emphasis> clear the selection.</item>"
31 "<item>Selection rectangles (created by dragging from an empty area) invert the selection status of items within.</item></list></para>"
32 "<para>The available action buttons at the bottom change depending on the current selection.</para>"));
33
34 QWidget *contentsContainer = prepareContentsContainer();
35
36 BackgroundColorHelper::instance()->controlBackgroundColor(this);
37
38 m_fullLabelString = i18nc("@info label above the view explaining the state", "Selection Mode: Click on files or folders to select or deselect them.");
39 m_shortLabelString = i18nc("@info label above the view explaining the state", "Selection Mode");
40 m_label = new QLabel(contentsContainer);
41 m_label->setMinimumWidth(0);
42 BackgroundColorHelper::instance()->controlBackgroundColor(m_label);
43
44 m_closeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contentsContainer);
45 m_closeButton->setText(i18nc("@action:button", "Exit Selection Mode"));
46 m_closeButton->setFlat(true);
47 connect(m_closeButton, &QAbstractButton::clicked, this, &TopBar::selectionModeLeavingRequested);
48
49 QHBoxLayout *layout = new QHBoxLayout(contentsContainer);
50 auto contentsMargins = layout->contentsMargins();
51 m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom();
52 m_closeButton->setFixedHeight(m_preferredHeight);
53 layout->setContentsMargins(0, 0, 0, 0);
54
55 layout->addStretch();
56 layout->addWidget(m_label);
57 layout->addStretch();
58 layout->addWidget(m_closeButton);
59 }
60
61 void TopBar::resizeEvent(QResizeEvent *resizeEvent)
62 {
63 updateLabelString();
64 return AnimatedHeightWidget::resizeEvent(resizeEvent);
65 }
66
67 void TopBar::updateLabelString()
68 {
69 QFontMetrics fontMetrics = m_label->fontMetrics();
70 if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_closeButton->sizeHint().width() + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2
71 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2
72 < width()) {
73 m_label->setText(m_fullLabelString);
74 } else {
75 m_label->setText(m_shortLabelString);
76 }
77 }
78
79 #include "moc_topbar.cpp"