2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #include "selectionmodebottombar.h"
10 #include "backgroundcolorhelper.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphincontextmenu.h"
13 #include "dolphinmainwindow.h"
14 #include "dolphinremoveaction.h"
16 #include "kitemviews/kfileitemlisttostring.h"
18 #include <KActionCollection>
19 #include <KColorScheme>
21 #include <KFileItemListProperties>
22 #include <KLocalizedString>
23 #include <KStandardAction>
25 #include <QFontMetrics>
26 #include <QGuiApplication>
27 #include <QHBoxLayout>
31 #include <QPushButton>
32 #include <QResizeEvent>
33 #include <QScrollArea>
35 #include <QToolButton>
37 #include <QVBoxLayout>
39 #include <unordered_set>
41 SelectionModeBottomBar::SelectionModeBottomBar(KActionCollection
*actionCollection
, QWidget
*parent
) :
43 m_actionCollection
{actionCollection
}
45 // Showing of this widget is normally animated. We hide it for now and make it small.
49 setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
52 auto fillParentLayout
= new QGridLayout(this);
53 fillParentLayout
->setContentsMargins(0, 0, 0, 0);
55 // Put the contents into a QScrollArea. This prevents increasing the view width
56 // in case that not enough width for the contents is available. (this trick is also used in dolphinsearchbox.cpp.)
57 auto scrollArea
= new QScrollArea(this);
58 fillParentLayout
->addWidget(scrollArea
);
59 scrollArea
->setFrameShape(QFrame::NoFrame
);
60 scrollArea
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
61 scrollArea
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
62 scrollArea
->setWidgetResizable(true);
64 auto contentsContainer
= new QWidget(scrollArea
);
65 scrollArea
->setWidget(contentsContainer
);
66 contentsContainer
->installEventFilter(this); // Adjusts the height of this bar to the height of the contentsContainer
68 BackgroundColorHelper::instance()->controlBackgroundColor(this);
70 // We will mostly interact with m_layout when changing the contents and not care about the other internal hierarchy.
71 m_layout
= new QHBoxLayout(contentsContainer
);
74 void SelectionModeBottomBar::setVisible(bool visible
, Animated animated
)
76 m_allowedToBeVisible
= visible
;
77 setVisibleInternal(visible
, animated
);
80 void SelectionModeBottomBar::setVisibleInternal(bool visible
, Animated animated
)
82 Q_ASSERT_X(animated
== WithAnimation
, "SelectionModeBottomBar::setVisible", "This wasn't implemented.");
83 if (!visible
&& m_contents
== PasteContents
) {
84 return; // The bar with PasteContents should not be hidden or users might not know how to paste what they just copied.
85 // Set m_contents to anything else to circumvent this prevention mechanism.
87 if (visible
&& m_contents
== GeneralContents
&& !m_internalContextMenu
) {
88 return; // There is nothing on the bar that we want to show. We keep it invisible and only show it when the selection or the contents change.
92 if (m_heightAnimation
) {
93 m_heightAnimation
->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
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());
100 m_heightAnimation
->setStartValue(height());
101 m_heightAnimation
->setEasingCurve(QEasingCurve::OutCubic
);
104 m_heightAnimation
->setEndValue(sizeHint().height());
105 connect(m_heightAnimation
, &QAbstractAnimation::finished
,
106 this, [this](){ setMaximumHeight(sizeHint().height()); });
108 m_heightAnimation
->setEndValue(0);
109 connect(m_heightAnimation
, &QAbstractAnimation::finished
,
110 this, &QWidget::hide
);
113 m_heightAnimation
->start(QAbstractAnimation::DeleteWhenStopped
);
116 QSize
SelectionModeBottomBar::sizeHint() const
118 return QSize
{1, m_layout
->parentWidget()->sizeHint().height()};
119 // 1 as width because this widget should never be the reason the DolphinViewContainer is made wider.
122 void SelectionModeBottomBar::slotSelectionChanged(const KFileItemList
&selection
, const QUrl
&baseUrl
)
124 if (m_contents
== GeneralContents
) {
125 auto contextActions
= contextActionsFor(selection
, baseUrl
);
126 m_generalBarActions
.clear();
127 if (contextActions
.empty()) {
128 if (isVisibleTo(parentWidget())) {
129 setVisibleInternal(false, WithAnimation
);
132 for (auto i
= contextActions
.begin(); i
!= contextActions
.end(); ++i
) {
133 m_generalBarActions
.emplace_back(ActionWithWidget
{*i
});
135 resetContents(GeneralContents
);
137 if (m_allowedToBeVisible
) {
138 setVisibleInternal(true, WithAnimation
);
142 updateMainActionButton(selection
);
145 void SelectionModeBottomBar::slotSplitTabDisabled()
147 switch (m_contents
) {
148 case CopyToOtherViewContents
:
149 case MoveToOtherViewContents
:
150 Q_EMIT
leaveSelectionModeRequested();
156 void SelectionModeBottomBar::resetContents(SelectionModeBottomBar::Contents contents
)
160 // A label is added in many of the methods below. We only know its size a bit later and if it should be hidden.
161 QTimer::singleShot(10, this, [this](){ updateExplanatoryLabelVisibility(); });
163 Q_CHECK_PTR(m_actionCollection
);
164 m_contents
= contents
;
169 case CopyLocationContents
:
170 addCopyLocationContents();
172 case CopyToOtherViewContents
:
173 addCopyToOtherViewContents();
181 case DuplicateContents
:
182 addDuplicateContents();
184 case GeneralContents
:
185 addGeneralContents();
190 case MoveToOtherViewContents
:
191 addMoveToOtherViewContents();
193 case MoveToTrashContents
:
194 addMoveToTrashContents();
197 return addRenameContents();
200 if (m_allowedToBeVisible
) {
201 setVisibleInternal(true, WithAnimation
);
205 bool SelectionModeBottomBar::eventFilter(QObject
*watched
, QEvent
*event
)
207 Q_ASSERT(qobject_cast
<QWidget
*>(watched
)); // This evenfFilter is only implemented for QWidgets.
209 switch (event
->type()) {
210 case QEvent::ChildAdded
:
211 case QEvent::ChildRemoved
:
212 QTimer::singleShot(0, this, [this]() {
213 // The necessary height might have changed because of the added/removed child so we change the height manually.
214 if (isVisibleTo(parentWidget()) && isEnabled() && (!m_heightAnimation
|| m_heightAnimation
->state() != QAbstractAnimation::Running
)) {
215 setMaximumHeight(sizeHint().height());
224 void SelectionModeBottomBar::resizeEvent(QResizeEvent
*resizeEvent
)
226 if (resizeEvent
->oldSize().width() == resizeEvent
->size().width()) {
227 // The width() didn't change so our custom override isn't needed.
228 return QWidget::resizeEvent(resizeEvent
);
230 m_layout
->parentWidget()->setFixedWidth(resizeEvent
->size().width());
232 if (m_contents
== GeneralContents
) {
233 Q_ASSERT(m_overflowButton
);
234 if (unusedSpace() < 0) {
235 // The bottom bar is overflowing! We need to hide some of the widgets.
236 for (auto i
= m_generalBarActions
.rbegin(); i
!= m_generalBarActions
.rend(); ++i
) {
237 if (!i
->isWidgetVisible()) {
240 i
->widget()->setVisible(false);
242 // Add the action to the overflow.
243 auto overflowMenu
= m_overflowButton
->menu();
244 if (overflowMenu
->actions().isEmpty()) {
245 overflowMenu
->addAction(i
->action());
247 overflowMenu
->insertAction(overflowMenu
->actions().at(0), i
->action());
249 m_overflowButton
->setVisible(true);
250 if (unusedSpace() >= 0) {
251 break; // All widgets fit now.
255 // We have some unusedSpace(). Let's check if we can maybe add more of the contextual action's widgets.
256 for (auto i
= m_generalBarActions
.begin(); i
!= m_generalBarActions
.end(); ++i
) {
257 if (i
->isWidgetVisible()) {
262 i
->widget()->setVisible(false);
263 m_layout
->insertWidget(m_layout
->count() - 1, i
->widget()); // Insert before m_overflowButton
265 if (unusedSpace() < i
->widget()->sizeHint().width()) {
266 // It doesn't fit. We keep it invisible.
269 i
->widget()->setVisible(true);
271 // Remove the action from the overflow.
272 auto overflowMenu
= m_overflowButton
->menu();
273 overflowMenu
->removeAction(i
->action());
274 if (overflowMenu
->isEmpty()) {
275 m_overflowButton
->setVisible(false);
281 // Hide the leading explanation if it doesn't fit. The buttons are labeled clear enough that this shouldn't be a big UX problem.
282 updateExplanatoryLabelVisibility();
283 return QWidget::resizeEvent(resizeEvent
);
286 void SelectionModeBottomBar::addCopyContents()
288 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied."), this);
289 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
290 m_explanatoryLabel
->setWordWrap(true);
291 m_layout
->addWidget(m_explanatoryLabel
);
293 // i18n: Aborts the current step-by-step process to copy files by leaving the selection mode.
294 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Copying"), this);
295 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
296 m_layout
->addWidget(cancelButton
);
298 auto *copyButton
= new QPushButton(this);
299 // We claim to have PasteContents already so triggering the copy action next won't instantly hide the bottom bar.
300 connect(copyButton
, &QAbstractButton::clicked
, [this]() {
301 if (GeneralSettings::showPasteBarAfterCopying()) {
302 m_contents
= Contents::PasteContents
;
305 // Connect the copy action as a second step.
306 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)), copyButton
);
307 // Finally connect the lambda that actually changes the contents to the PasteContents.
308 connect(copyButton
, &QAbstractButton::clicked
, [this]() {
309 if (GeneralSettings::showPasteBarAfterCopying()) {
310 resetContents(Contents::PasteContents
); // resetContents() needs to be connected last because
311 // it instantly deletes the button and then the other slots won't be called.
313 Q_EMIT
leaveSelectionModeRequested();
315 updateMainActionButton(KFileItemList());
316 m_layout
->addWidget(copyButton
);
319 void SelectionModeBottomBar::addCopyLocationContents()
321 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select one file or folder whose location should be copied."), this);
322 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
323 m_explanatoryLabel
->setWordWrap(true);
324 m_layout
->addWidget(m_explanatoryLabel
);
326 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
327 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Copying"), this);
328 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
329 m_layout
->addWidget(cancelButton
);
331 auto *copyLocationButton
= new QPushButton(this);
332 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("copy_location")), copyLocationButton
);
333 updateMainActionButton(KFileItemList());
334 m_layout
->addWidget(copyLocationButton
);
337 void SelectionModeBottomBar::addCopyToOtherViewContents()
339 // i18n: "Copy over" refers to copying to the other split view area that is currently visible to the user.
340 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied over."), this);
341 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
342 m_explanatoryLabel
->setWordWrap(true);
343 m_layout
->addWidget(m_explanatoryLabel
);
345 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
346 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Copying"), this);
347 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
348 m_layout
->addWidget(cancelButton
);
350 auto *copyToOtherViewButton
= new QPushButton(this);
351 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("copy_to_inactive_split_view")), copyToOtherViewButton
);
352 updateMainActionButton(KFileItemList());
353 m_layout
->addWidget(copyToOtherViewButton
);
356 void SelectionModeBottomBar::addCutContents()
358 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be cut."), this);
359 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
360 m_explanatoryLabel
->setWordWrap(true);
361 m_layout
->addWidget(m_explanatoryLabel
);
363 // i18n: Aborts the current step-by-step process to cut files by leaving the selection mode.
364 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Cutting"), this);
365 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
366 m_layout
->addWidget(cancelButton
);
368 auto *cutButton
= new QPushButton(this);
369 // We claim to have PasteContents already so triggering the cut action next won't instantly hide the bottom bar.
370 connect(cutButton
, &QAbstractButton::clicked
, [this]() {
371 if (GeneralSettings::showPasteBarAfterCopying()) {
372 m_contents
= Contents::PasteContents
;
375 // Connect the cut action as a second step.
376 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)), cutButton
);
377 // Finally connect the lambda that actually changes the contents to the PasteContents.
378 connect(cutButton
, &QAbstractButton::clicked
, [this](){
379 if (GeneralSettings::showPasteBarAfterCopying()) {
380 resetContents(Contents::PasteContents
); // resetContents() needs to be connected last because
381 // it instantly deletes the button and then the other slots won't be called.
383 Q_EMIT
leaveSelectionModeRequested();
385 updateMainActionButton(KFileItemList());
386 m_layout
->addWidget(cutButton
);
389 void SelectionModeBottomBar::addDeleteContents()
391 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be permanently deleted."), this);
392 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
393 m_explanatoryLabel
->setWordWrap(true);
394 m_layout
->addWidget(m_explanatoryLabel
);
396 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
397 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort"), this);
398 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
399 m_layout
->addWidget(cancelButton
);
401 auto *deleteButton
= new QPushButton(this);
402 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::DeleteFile
)), deleteButton
);
403 updateMainActionButton(KFileItemList());
404 m_layout
->addWidget(deleteButton
);
407 void SelectionModeBottomBar::addDuplicateContents()
409 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be duplicated here."), this);
410 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
411 m_explanatoryLabel
->setWordWrap(true);
412 m_layout
->addWidget(m_explanatoryLabel
);
414 // i18n: Aborts the current step-by-step process to duplicate files by leaving the selection mode.
415 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Duplicating"), this);
416 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
417 m_layout
->addWidget(cancelButton
);
419 auto *duplicateButton
= new QPushButton(this);
420 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("duplicate")), duplicateButton
);
421 updateMainActionButton(KFileItemList());
422 m_layout
->addWidget(duplicateButton
);
425 void SelectionModeBottomBar::addGeneralContents()
427 if (!m_overflowButton
) {
428 m_overflowButton
= new QToolButton
{this};
429 // i18n: This button appears in a bar if there isn't enough horizontal space to fit all the other buttons.
430 // The small icon-only button opens a menu that contains the actions that didn't fit on the bar.
431 // Since this is an icon-only button this text will only appear as a tooltip and as accessibility text.
432 m_overflowButton
->setToolTip(i18nc("@action", "More"));
433 m_overflowButton
->setAccessibleName(m_overflowButton
->toolTip());
434 m_overflowButton
->setIcon(QIcon::fromTheme(QStringLiteral("view-more-horizontal-symbolic")));
435 m_overflowButton
->setMenu(new QMenu
{m_overflowButton
});
436 m_overflowButton
->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup
);
437 m_overflowButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::MinimumExpanding
); // Makes sure it has the same height as the labeled buttons.
438 m_layout
->addWidget(m_overflowButton
);
440 m_overflowButton
->menu()->actions().clear();
441 // The overflowButton should be part of the calculation for needed space so we set it visible in regards to unusedSpace().
442 m_overflowButton
->setVisible(true);
445 // We first add all the m_generalBarActions to the bar until the bar is full.
446 auto i
= m_generalBarActions
.begin();
447 for (; i
!= m_generalBarActions
.end(); ++i
) {
448 if (i
->action()->isVisible()) {
450 i
->widget()->setEnabled(i
->action()->isEnabled());
453 i
->widget()->setVisible(false);
454 m_layout
->insertWidget(m_layout
->count() - 1, i
->widget()); // Insert before m_overflowButton
456 if (unusedSpace() < i
->widget()->sizeHint().width()) {
457 break; // The bar is too full already. We keep it invisible.
459 i
->widget()->setVisible(true);
463 // We are done adding widgets to the bar so either we were able to fit all the actions in there
464 m_overflowButton
->setVisible(false);
465 // …or there are more actions left which need to be put into m_overflowButton.
466 for (; i
!= m_generalBarActions
.end(); ++i
) {
467 m_overflowButton
->menu()->addAction(i
->action());
469 // The overflowButton is set visible if there is actually an action in it.
470 if (!m_overflowButton
->isVisible() && i
->action()->isVisible() && !i
->action()->isSeparator()) {
471 m_overflowButton
->setVisible(true);
476 void SelectionModeBottomBar::addMoveToOtherViewContents()
478 // i18n: "Move over" refers to moving to the other split view area that is currently visible to the user.
479 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved over."), this);
480 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
481 m_explanatoryLabel
->setWordWrap(true);
482 m_layout
->addWidget(m_explanatoryLabel
);
484 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
485 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Moving"), this);
486 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
487 m_layout
->addWidget(cancelButton
);
489 auto *moveToOtherViewButton
= new QPushButton(this);
490 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("move_to_inactive_split_view")), moveToOtherViewButton
);
491 updateMainActionButton(KFileItemList());
492 m_layout
->addWidget(moveToOtherViewButton
);
495 void SelectionModeBottomBar::addMoveToTrashContents()
497 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved to the Trash."), this);
498 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
499 m_explanatoryLabel
->setWordWrap(true);
500 m_layout
->addWidget(m_explanatoryLabel
);
502 // i18n: Aborts the current step-by-step process of moving files to the trash by leaving the selection mode.
503 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort"), this);
504 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
505 m_layout
->addWidget(cancelButton
);
507 auto *moveToTrashButton
= new QPushButton(this);
508 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)), moveToTrashButton
);
509 updateMainActionButton(KFileItemList());
510 m_layout
->addWidget(moveToTrashButton
);
513 void SelectionModeBottomBar::addPasteContents()
515 m_explanatoryLabel
= new QLabel(xi18n("<para>The selected files and folders were added to the Clipboard. "
516 "Now the <emphasis>Paste</emphasis> action can be used to transfer them from the Clipboard "
517 "to any other location. They can even be transferred to other applications by using their "
518 "respective <emphasis>Paste</emphasis> actions.</para>"), this);
519 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
520 m_explanatoryLabel
->setWordWrap(true);
521 m_layout
->addWidget(m_explanatoryLabel
);
523 auto *vBoxLayout
= new QVBoxLayout(this);
524 m_layout
->addLayout(vBoxLayout
);
526 /** We are in "PasteContents" mode which means hiding the bottom bar is impossible.
527 * So we first have to claim that we have different contents before requesting to leave selection mode. */
528 auto actuallyLeaveSelectionMode
= [this]() {
529 m_contents
= Contents::CopyLocationContents
;
530 Q_EMIT
leaveSelectionModeRequested();
533 auto *pasteButton
= new QPushButton(this);
534 copyActionDataToButton(pasteButton
, m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
535 pasteButton
->setText(i18nc("@action A more elaborate and clearly worded version of the Paste action", "Paste from Clipboard"));
536 connect(pasteButton
, &QAbstractButton::clicked
, this, actuallyLeaveSelectionMode
);
537 vBoxLayout
->addWidget(pasteButton
);
539 auto *dismissButton
= new QToolButton(this);
540 dismissButton
->setText(i18nc("@action Dismisses a bar explaining how to use the Paste action", "Dismiss this Reminder"));
541 connect(dismissButton
, &QAbstractButton::clicked
, this, actuallyLeaveSelectionMode
);
542 auto *dontRemindAgainAction
= new QAction(i18nc("@action Dismisses an explanatory area and never shows it again", "Don't remind me again"), this);
543 connect(dontRemindAgainAction
, &QAction::triggered
, this, []() {
544 GeneralSettings::setShowPasteBarAfterCopying(false);
546 connect(dontRemindAgainAction
, &QAction::triggered
, this, actuallyLeaveSelectionMode
);
547 auto *dismissButtonMenu
= new QMenu(dismissButton
);
548 dismissButtonMenu
->addAction(dontRemindAgainAction
);
549 dismissButton
->setMenu(dismissButtonMenu
);
550 dismissButton
->setPopupMode(QToolButton::MenuButtonPopup
);
551 vBoxLayout
->addWidget(dismissButton
);
553 m_explanatoryLabel
->setMaximumHeight(pasteButton
->sizeHint().height() + dismissButton
->sizeHint().height() + m_explanatoryLabel
->fontMetrics().height());
556 void SelectionModeBottomBar::addRenameContents()
558 m_explanatoryLabel
= new QLabel(i18nc("@info explains the next step in a process", "Select the file or folder that should be renamed.\nBulk renaming is possible when multiple items are selected."), this);
559 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
560 m_explanatoryLabel
->setWordWrap(true);
561 m_layout
->addWidget(m_explanatoryLabel
);
563 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
564 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Stop Renaming"), this);
565 connect(cancelButton
, &QAbstractButton::clicked
, this, &SelectionModeBottomBar::leaveSelectionModeRequested
);
566 m_layout
->addWidget(cancelButton
);
568 auto *renameButton
= new QPushButton(this);
569 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)), renameButton
);
570 updateMainActionButton(KFileItemList());
571 m_layout
->addWidget(renameButton
);
574 void SelectionModeBottomBar::emptyBarContents()
577 while ((child
= m_layout
->takeAt(0)) != nullptr) {
578 if (auto *childLayout
= child
->layout()) {
579 QLayoutItem
*grandChild
;
580 while ((grandChild
= childLayout
->takeAt(0)) != nullptr) {
581 delete grandChild
->widget(); // delete the widget
582 delete grandChild
; // delete the layout item
585 delete child
->widget(); // delete the widget
586 delete child
; // delete the layout item
590 std::vector
<QAction
*> SelectionModeBottomBar::contextActionsFor(const KFileItemList
& selectedItems
, const QUrl
& baseUrl
)
592 if (selectedItems
.isEmpty()) {
593 // There are no contextual actions to show for these items.
594 // We might even want to hide this bar in this case. To make this clear, we reset m_internalContextMenu.
595 m_internalContextMenu
.release()->deleteLater();
596 return std::vector
<QAction
*>{};
599 std::vector
<QAction
*> contextActions
;
601 // We always want to show the most important actions at the beginning
602 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
603 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
604 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
605 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
607 // We are going to add the actions from the right-click context menu for the selected items.
608 auto *dolphinMainWindow
= qobject_cast
<DolphinMainWindow
*>(window());
609 Q_CHECK_PTR(dolphinMainWindow
);
610 if (!m_fileItemActions
) {
611 m_fileItemActions
= new KFileItemActions(this);
612 m_fileItemActions
->setParentWidget(dolphinMainWindow
);
613 connect(m_fileItemActions
, &KFileItemActions::error
, this, &SelectionModeBottomBar::error
);
615 m_internalContextMenu
= std::make_unique
<DolphinContextMenu
>(dolphinMainWindow
, selectedItems
.constFirst(), selectedItems
, baseUrl
, m_fileItemActions
);
616 auto internalContextMenuActions
= m_internalContextMenu
->actions();
618 // There are some actions which we wouldn't want to add. We remember them in the actionsThatShouldntBeAdded set.
619 // We don't want to add the four basic actions again which were already added to the top.
620 std::unordered_set
<QAction
*> actionsThatShouldntBeAdded
{contextActions
.begin(), contextActions
.end()};
621 // "Delete" isn't really necessary to add because we have "Move to Trash" already. It is also more dangerous so let's exclude it.
622 actionsThatShouldntBeAdded
.insert(m_actionCollection
->action(KStandardAction::name(KStandardAction::DeleteFile
)));
624 // KHamburgerMenu would only be visible if there is no menu available anywhere on the user interface. This might be useful for recovery from
625 // such a situation in theory but a bar with context dependent actions doesn't really seem like the right place for it.
626 Q_ASSERT(internalContextMenuActions
.first()->icon().name() == m_actionCollection
->action(KStandardAction::name(KStandardAction::HamburgerMenu
))->icon().name());
627 internalContextMenuActions
.removeFirst();
629 for (auto it
= internalContextMenuActions
.constBegin(); it
!= internalContextMenuActions
.constEnd(); ++it
) {
630 if (actionsThatShouldntBeAdded
.count(*it
)) {
631 continue; // Skip this action.
633 if (!qobject_cast
<DolphinRemoveAction
*>(*it
)) { // We already have a "Move to Trash" action so we don't want a DolphinRemoveAction.
634 // We filter duplicate separators here so we won't have to deal with them later.
635 if (!contextActions
.back()->isSeparator() || !(*it
)->isSeparator()) {
636 contextActions
.emplace_back((*it
));
640 return contextActions
;
643 int SelectionModeBottomBar::unusedSpace() const
645 int sumOfPreferredWidths
= m_layout
->contentsMargins().left() + m_layout
->contentsMargins().right();
646 if (m_overflowButton
) {
647 sumOfPreferredWidths
+= m_overflowButton
->sizeHint().width();
649 for (int i
= 0; i
< m_layout
->count(); ++i
) {
650 auto widget
= m_layout
->itemAt(i
)->widget();
651 if (widget
&& !widget
->isVisibleTo(widget
->parentWidget())) {
652 continue; // We don't count invisible widgets.
654 sumOfPreferredWidths
+= m_layout
->itemAt(i
)->sizeHint().width() + m_layout
->spacing();
656 return width() - sumOfPreferredWidths
- 20; // We consider all space used when there are only 20 pixels left
657 // so there is some room to breath and not too much wonkyness while resizing.
660 void SelectionModeBottomBar::updateExplanatoryLabelVisibility()
662 if (!m_explanatoryLabel
) {
665 if (m_explanatoryLabel
->isVisible()) {
666 m_explanatoryLabel
->setVisible(unusedSpace() > 0);
668 // We only want to re-show the label when it fits comfortably so the computation below adds another "+20".
669 m_explanatoryLabel
->setVisible(unusedSpace() > m_explanatoryLabel
->sizeHint().width() + 20);
673 void SelectionModeBottomBar::updateMainActionButton(const KFileItemList
& selection
)
675 if (!m_mainAction
.widget()) {
678 Q_ASSERT(qobject_cast
<QAbstractButton
*>(m_mainAction
.widget()));
680 // Users are nudged towards selecting items by having the button disabled when nothing is selected.
681 m_mainAction
.widget()->setEnabled(selection
.count() > 0 && m_mainAction
.action()->isEnabled());
682 QFontMetrics fontMetrics
= m_mainAction
.widget()->fontMetrics();
685 switch (m_contents
) {
687 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Copy action",
688 "Copy %2 to the Clipboard", "Copy %2 to the Clipboard", selection
.count(),
689 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
691 case CopyLocationContents
:
692 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Copy Location action",
693 "Copy the Location of %2 to the Clipboard", "Copy the Location of %2 to the Clipboard", selection
.count(),
694 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
697 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Cut action",
698 "Cut %2 to the Clipboard", "Cut %2 to the Clipboard", selection
.count(),
699 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
702 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Delete action",
703 "Permanently Delete %2", "Permanently Delete %2", selection
.count(),
704 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
706 case DuplicateContents
:
707 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Duplicate action",
708 "Duplicate %2", "Duplicate %2", selection
.count(),
709 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
711 case MoveToTrashContents
:
712 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Trash action",
713 "Move %2 to the Trash", "Move %2 to the Trash", selection
.count(),
714 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
717 buttonText
= i18ncp("@action A more elaborate and clearly worded version of the Rename action",
718 "Rename %2", "Rename %2", selection
.count(),
719 fileItemListToString(selection
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
724 if (buttonText
!= QStringLiteral("NULL")) {
725 static_cast<QAbstractButton
*>(m_mainAction
.widget())->setText(buttonText
);
727 // The width of the button has changed. We might want to hide the label so the full button text fits on the bar.
728 updateExplanatoryLabelVisibility();