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