File indexing completed on 2024-04-28 05:45:15
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "topbar.h" 0009 0010 #include "backgroundcolorhelper.h" 0011 0012 #include <KColorScheme> 0013 #include <KLocalizedString> 0014 #include <KToolTipHelper> 0015 0016 #include <QHBoxLayout> 0017 #include <QLabel> 0018 #include <QPushButton> 0019 #include <QScrollArea> 0020 #include <QStyle> 0021 0022 using namespace SelectionMode; 0023 0024 TopBar::TopBar(QWidget *parent) 0025 : QWidget{parent} 0026 { 0027 // Showing of this widget is normally animated. We hide it for now and make it small. 0028 hide(); 0029 setMaximumHeight(0); 0030 0031 setToolTip(KToolTipHelper::whatsThisHintOnly()); 0032 setWhatsThis(xi18nc("@info:whatsthis", 0033 "<title>Selection Mode</title><para>Select files or folders to manage or manipulate them." 0034 "<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>" 0035 "<item>Pressing an empty area does <emphasis>not</emphasis> clear the selection.</item>" 0036 "<item>Selection rectangles (created by dragging from an empty area) invert the selection status of items within.</item></list></para>" 0037 "<para>The available action buttons at the bottom change depending on the current selection.</para>")); 0038 0039 auto fillParentLayout = new QGridLayout(this); 0040 fillParentLayout->setContentsMargins(0, 0, 0, 0); 0041 0042 // Put the contents into a QScrollArea. This prevents increasing the view width 0043 // in case that not enough width for the contents is available. (this trick is also used in bottombar.cpp.) 0044 auto scrollArea = new QScrollArea(this); 0045 fillParentLayout->addWidget(scrollArea); 0046 scrollArea->setFrameShape(QFrame::NoFrame); 0047 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0048 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0049 scrollArea->setWidgetResizable(true); 0050 0051 auto contentsContainer = new QWidget(scrollArea); 0052 scrollArea->setWidget(contentsContainer); 0053 0054 BackgroundColorHelper::instance()->controlBackgroundColor(this); 0055 0056 setMinimumWidth(0); 0057 0058 m_fullLabelString = i18nc("@info label above the view explaining the state", "Selection Mode: Click on files or folders to select or deselect them."); 0059 m_shortLabelString = i18nc("@info label above the view explaining the state", "Selection Mode"); 0060 m_label = new QLabel(contentsContainer); 0061 m_label->setMinimumWidth(0); 0062 BackgroundColorHelper::instance()->controlBackgroundColor(m_label); 0063 0064 m_closeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("window-close-symbolic")), "", contentsContainer); 0065 m_closeButton->setText(i18nc("@action:button", "Exit Selection Mode")); 0066 m_closeButton->setFlat(true); 0067 connect(m_closeButton, &QAbstractButton::clicked, this, &TopBar::selectionModeLeavingRequested); 0068 0069 QHBoxLayout *layout = new QHBoxLayout(contentsContainer); 0070 auto contentsMargins = layout->contentsMargins(); 0071 m_preferredHeight = contentsMargins.top() + m_label->sizeHint().height() + contentsMargins.bottom(); 0072 scrollArea->setMaximumHeight(m_preferredHeight); 0073 m_closeButton->setFixedHeight(m_preferredHeight); 0074 layout->setContentsMargins(0, 0, 0, 0); 0075 0076 layout->addStretch(); 0077 layout->addWidget(m_label); 0078 layout->addStretch(); 0079 layout->addWidget(m_closeButton); 0080 } 0081 0082 void TopBar::setVisible(bool visible, Animated animated) 0083 { 0084 Q_ASSERT_X(animated == WithAnimation, "SelectionModeTopBar::setVisible", "This wasn't implemented."); 0085 0086 if (m_heightAnimation) { 0087 m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped. 0088 } 0089 m_heightAnimation = new QPropertyAnimation(this, "maximumHeight"); 0090 m_heightAnimation->setDuration(2 * style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, this) * GlobalConfig::animationDurationFactor()); 0091 0092 m_heightAnimation->setStartValue(height()); 0093 m_heightAnimation->setEasingCurve(QEasingCurve::OutCubic); 0094 if (visible) { 0095 show(); 0096 m_heightAnimation->setEndValue(m_preferredHeight); 0097 } else { 0098 m_heightAnimation->setEndValue(0); 0099 connect(m_heightAnimation, &QAbstractAnimation::finished, this, &QWidget::hide); 0100 } 0101 0102 m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped); 0103 } 0104 0105 void TopBar::resizeEvent(QResizeEvent *resizeEvent) 0106 { 0107 updateLabelString(); 0108 return QWidget::resizeEvent(resizeEvent); 0109 } 0110 0111 void TopBar::updateLabelString() 0112 { 0113 QFontMetrics fontMetrics = m_label->fontMetrics(); 0114 if (fontMetrics.horizontalAdvance(m_fullLabelString) + m_closeButton->sizeHint().width() + style()->pixelMetric(QStyle::PM_LayoutLeftMargin) * 2 0115 + style()->pixelMetric(QStyle::PM_LayoutRightMargin) * 2 0116 < width()) { 0117 m_label->setText(m_fullLabelString); 0118 } else { 0119 m_label->setText(m_shortLabelString); 0120 } 0121 } 0122 0123 #include "moc_topbar.cpp"