]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/topbar.cpp
Improve naming consistency and leave mode on Escape
[dolphin.git] / src / selectionmode / topbar.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
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 <QPalette>
19 #include <QPropertyAnimation>
20 #include <QPushButton>
21 #include <QScrollArea>
22 #include <QStyle>
23 #include <QtGlobal>
24
25 using namespace SelectionMode;
26
27 TopBar::TopBar(QWidget *parent) :
28 QWidget{parent}
29 {
30 // Showing of this widget is normally animated. We hide it for now and make it small.
31 hide();
32 setMaximumHeight(0);
33
34 setToolTip(KToolTipHelper::whatsThisHintOnly());
35 setWhatsThis(xi18nc("@info:whatsthis", "<title>Selection Mode</title><para>Select files or folders to manage or manipulate them."
36 "<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>"
37 "<item>Pressing an empty area does <emphasis>not</emphasis> clear the selection.</item>"
38 "<item>Selection rectangles (created by dragging from an empty area) invert the selection status of items within.</item></list></para>"
39 "<para>The available action buttons at the bottom change depending on the current selection.</para>"));
40
41 auto fillParentLayout = new QGridLayout(this);
42 fillParentLayout->setContentsMargins(0, 0, 0, 0);
43
44 // Put the contents into a QScrollArea. This prevents increasing the view width
45 // in case that not enough width for the contents is available. (this trick is also used in bottombar.cpp.)
46 auto scrollArea = new QScrollArea(this);
47 fillParentLayout->addWidget(scrollArea);
48 scrollArea->setFrameShape(QFrame::NoFrame);
49 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
51 scrollArea->setWidgetResizable(true);
52
53 auto contentsContainer = new QWidget(scrollArea);
54 scrollArea->setWidget(contentsContainer);
55
56 BackgroundColorHelper::instance()->controlBackgroundColor(this);
57
58 setMinimumWidth(0);
59
60 m_fullLabelString = i18nc("@info label above the view explaining the state",
61 "Selection Mode: Click on files or folders to select or deselect them.");
62 m_shortLabelString = i18nc("@info label above the view explaining the state",
63 "Selection Mode");
64 m_label = new QLabel(contentsContainer);
65 m_label->setMinimumWidth(0);
66 BackgroundColorHelper::instance()->controlBackgroundColor(m_label);
67
68 m_closeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contentsContainer);
69 m_closeButton->setToolTip(i18nc("@action:button", "Exit Selection Mode"));
70 m_closeButton->setAccessibleName(m_closeButton->toolTip());
71 m_closeButton->setFlat(true);
72 connect(m_closeButton, &QAbstractButton::pressed,
73 this, &TopBar::selectionModeLeavingRequested);
74
75 QHBoxLayout *layout = new QHBoxLayout(contentsContainer);
76 auto contentsMargins = layout->contentsMargins();
77 m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom();
78 scrollArea->setMaximumHeight(m_preferredHeight);
79 m_closeButton->setFixedHeight(m_preferredHeight);
80 layout->setContentsMargins(0, 0, 0, 0);
81
82 layout->addStretch();
83 layout->addWidget(m_label);
84 layout->addStretch();
85 layout->addWidget(m_closeButton);
86 }
87
88 void TopBar::setVisible(bool visible, Animated animated)
89 {
90 Q_ASSERT_X(animated == WithAnimation, "SelectionModeTopBar::setVisible", "This wasn't implemented.");
91
92 if (m_heightAnimation) {
93 m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
94 }
95 m_heightAnimation = new QPropertyAnimation(this, "maximumHeight");
96 m_heightAnimation->setDuration(2 *
97 style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) *
98 GlobalConfig::animationDurationFactor());
99
100 m_heightAnimation->setStartValue(height());
101 m_heightAnimation->setEasingCurve(QEasingCurve::OutCubic);
102 if (visible) {
103 show();
104 m_heightAnimation->setEndValue(m_preferredHeight);
105 } else {
106 m_heightAnimation->setEndValue(0);
107 connect(m_heightAnimation, &QAbstractAnimation::finished,
108 this, &QWidget::hide);
109 }
110
111 m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped);
112 }
113
114 void TopBar::resizeEvent(QResizeEvent *resizeEvent)
115 {
116 updateLabelString();
117 return QWidget::resizeEvent(resizeEvent);
118 }
119
120 void TopBar::updateLabelString()
121 {
122 QFontMetrics fontMetrics = m_label->fontMetrics();
123 if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_closeButton->sizeHint().width() + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2 < width()) {
124 m_label->setText(m_fullLabelString);
125 } else {
126 m_label->setText(m_shortLabelString);
127 }
128 }