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