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