]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/selectionmodebottombar.cpp
d10a8581bab025604e6c468e4f33cdf578668a43
[dolphin.git] / src / selectionmode / selectionmodebottombar.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 "selectionmodebottombar.h"
9
10 #include "backgroundcolorhelper.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphincontextmenu.h"
13 #include "dolphinmainwindow.h"
14 #include "dolphinremoveaction.h"
15 #include "global.h"
16 #include "kitemviews/kfileitemlisttostring.h"
17
18 #include <KActionCollection>
19 #include <KColorScheme>
20 #include <KFileItem>
21 #include <KFileItemListProperties>
22 #include <KLocalizedString>
23 #include <KStandardAction>
24
25 #include <QFontMetrics>
26 #include <QGuiApplication>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QLayout>
30 #include <QMenu>
31 #include <QPushButton>
32 #include <QResizeEvent>
33 #include <QScrollArea>
34 #include <QStyle>
35 #include <QToolButton>
36 #include <QtGlobal>
37 #include <QVBoxLayout>
38
39 #include <unordered_set>
40
41 SelectionModeBottomBar::SelectionModeBottomBar(KActionCollection *actionCollection, QWidget *parent) :
42 QWidget{parent},
43 m_actionCollection{actionCollection}
44 {
45 // Showing of this widget is normally animated. We hide it for now and make it small.
46 hide();
47 setMaximumHeight(0);
48
49 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
50 setMinimumWidth(0);
51
52 auto fillParentLayout = new QGridLayout(this);
53 fillParentLayout->setContentsMargins(0, 0, 0, 0);
54
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);
63
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
67
68 BackgroundColorHelper::instance()->controlBackgroundColor(this);
69
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);
72 }
73
74 void SelectionModeBottomBar::setVisible(bool visible, Animated animated)
75 {
76 m_allowedToBeVisible = visible;
77 setVisibleInternal(visible, animated);
78 }
79
80 void SelectionModeBottomBar::setVisibleInternal(bool visible, Animated animated)
81 {
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.
86 }
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.
89 }
90
91 setEnabled(visible);
92 if (m_heightAnimation) {
93 m_heightAnimation->stop(); // deletes because of QAbstractAnimation::DeleteWhenStopped.
94 }
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());
99
100 m_heightAnimation->setStartValue(height());
101 m_heightAnimation->setEasingCurve(QEasingCurve::OutCubic);
102 if (visible) {
103 show();
104 m_heightAnimation->setEndValue(sizeHint().height());
105 connect(m_heightAnimation, &QAbstractAnimation::finished,
106 this, [this](){ setMaximumHeight(sizeHint().height()); });
107 } else {
108 m_heightAnimation->setEndValue(0);
109 connect(m_heightAnimation, &QAbstractAnimation::finished,
110 this, &QWidget::hide);
111 }
112
113 m_heightAnimation->start(QAbstractAnimation::DeleteWhenStopped);
114 }
115
116 QSize SelectionModeBottomBar::sizeHint() const
117 {
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.
120 }
121
122 void SelectionModeBottomBar::slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl)
123 {
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);
130 }
131 } else {
132 for (auto i = contextActions.begin(); i != contextActions.end(); ++i) {
133 m_generalBarActions.emplace_back(ActionWithWidget{*i});
134 }
135 resetContents(GeneralContents);
136
137 if (m_allowedToBeVisible) {
138 setVisibleInternal(true, WithAnimation);
139 }
140 }
141 }
142 updateMainActionButton(selection);
143 }
144
145 void SelectionModeBottomBar::slotSplitTabDisabled()
146 {
147 switch (m_contents) {
148 case CopyToOtherViewContents:
149 case MoveToOtherViewContents:
150 Q_EMIT leaveSelectionModeRequested();
151 default:
152 return;
153 }
154 }
155
156 void SelectionModeBottomBar::resetContents(SelectionModeBottomBar::Contents contents)
157 {
158 emptyBarContents();
159
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(); });
162
163 Q_CHECK_PTR(m_actionCollection);
164 m_contents = contents;
165 switch (contents) {
166 case CopyContents:
167 addCopyContents();
168 break;
169 case CopyLocationContents:
170 addCopyLocationContents();
171 break;
172 case CopyToOtherViewContents:
173 addCopyToOtherViewContents();
174 break;
175 case CutContents:
176 addCutContents();
177 break;
178 case DeleteContents:
179 addDeleteContents();
180 break;
181 case DuplicateContents:
182 addDuplicateContents();
183 break;
184 case GeneralContents:
185 addGeneralContents();
186 break;
187 case PasteContents:
188 addPasteContents();
189 break;
190 case MoveToOtherViewContents:
191 addMoveToOtherViewContents();
192 break;
193 case MoveToTrashContents:
194 addMoveToTrashContents();
195 break;
196 case RenameContents:
197 return addRenameContents();
198 }
199
200 if (m_allowedToBeVisible) {
201 setVisibleInternal(true, WithAnimation);
202 }
203 }
204
205 bool SelectionModeBottomBar::eventFilter(QObject *watched, QEvent *event)
206 {
207 Q_ASSERT(qobject_cast<QWidget *>(watched)); // This evenfFilter is only implemented for QWidgets.
208
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());
216 }
217 });
218 // Fall through.
219 default:
220 return false;
221 }
222 }
223
224 void SelectionModeBottomBar::resizeEvent(QResizeEvent *resizeEvent)
225 {
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);
229 }
230 m_layout->parentWidget()->setFixedWidth(resizeEvent->size().width());
231
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()) {
238 continue;
239 }
240 i->widget()->setVisible(false);
241
242 // Add the action to the overflow.
243 auto overflowMenu = m_overflowButton->menu();
244 if (overflowMenu->actions().isEmpty()) {
245 overflowMenu->addAction(i->action());
246 } else {
247 overflowMenu->insertAction(overflowMenu->actions().at(0), i->action());
248 }
249 m_overflowButton->setVisible(true);
250 if (unusedSpace() >= 0) {
251 break; // All widgets fit now.
252 }
253 }
254 } else {
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()) {
258 continue;
259 }
260 if (!i->widget()) {
261 i->newWidget(this);
262 i->widget()->setVisible(false);
263 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton
264 }
265 if (unusedSpace() < i->widget()->sizeHint().width()) {
266 // It doesn't fit. We keep it invisible.
267 break;
268 }
269 i->widget()->setVisible(true);
270
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);
276 }
277 }
278 }
279 }
280
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);
284 }
285
286 void SelectionModeBottomBar::addCopyContents()
287 {
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);
292
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);
297
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;
303 }
304 });
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.
312 }
313 Q_EMIT leaveSelectionModeRequested();
314 });
315 updateMainActionButton(KFileItemList());
316 m_layout->addWidget(copyButton);
317 }
318
319 void SelectionModeBottomBar::addCopyLocationContents()
320 {
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);
325
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);
330
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);
335 }
336
337 void SelectionModeBottomBar::addCopyToOtherViewContents()
338 {
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);
344
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);
349
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);
354 }
355
356 void SelectionModeBottomBar::addCutContents()
357 {
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);
362
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);
367
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;
373 }
374 });
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.
382 }
383 Q_EMIT leaveSelectionModeRequested();
384 });
385 updateMainActionButton(KFileItemList());
386 m_layout->addWidget(cutButton);
387 }
388
389 void SelectionModeBottomBar::addDeleteContents()
390 {
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);
395
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);
400
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);
405 }
406
407 void SelectionModeBottomBar::addDuplicateContents()
408 {
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);
413
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);
418
419 auto *duplicateButton = new QPushButton(this);
420 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("duplicate")), duplicateButton);
421 updateMainActionButton(KFileItemList());
422 m_layout->addWidget(duplicateButton);
423 }
424
425 void SelectionModeBottomBar::addGeneralContents()
426 {
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);
439 } else {
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);
443 }
444
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()) {
449 if (i->widget()) {
450 i->widget()->setEnabled(i->action()->isEnabled());
451 } else {
452 i->newWidget(this);
453 i->widget()->setVisible(false);
454 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton
455 }
456 if (unusedSpace() < i->widget()->sizeHint().width()) {
457 break; // The bar is too full already. We keep it invisible.
458 } else {
459 i->widget()->setVisible(true);
460 }
461 }
462 }
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());
468
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);
472 }
473 }
474 }
475
476 void SelectionModeBottomBar::addMoveToOtherViewContents()
477 {
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);
483
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);
488
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);
493 }
494
495 void SelectionModeBottomBar::addMoveToTrashContents()
496 {
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);
501
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);
506
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);
511 }
512
513 void SelectionModeBottomBar::addPasteContents()
514 {
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);
522
523 auto *vBoxLayout = new QVBoxLayout(this);
524 m_layout->addLayout(vBoxLayout);
525
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();
531 };
532
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);
538
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);
545 });
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);
552
553 m_explanatoryLabel->setMaximumHeight(pasteButton->sizeHint().height() + dismissButton->sizeHint().height() + m_explanatoryLabel->fontMetrics().height());
554 }
555
556 void SelectionModeBottomBar::addRenameContents()
557 {
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);
562
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);
567
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);
572 }
573
574 void SelectionModeBottomBar::emptyBarContents()
575 {
576 QLayoutItem *child;
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
583 }
584 }
585 delete child->widget(); // delete the widget
586 delete child; // delete the layout item
587 }
588 }
589
590 std::vector<QAction *> SelectionModeBottomBar::contextActionsFor(const KFileItemList& selectedItems, const QUrl& baseUrl)
591 {
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 *>{};
597 }
598
599 std::vector<QAction *> contextActions;
600
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)));
606
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);
614 }
615 m_internalContextMenu = std::make_unique<DolphinContextMenu>(dolphinMainWindow, selectedItems.constFirst(), selectedItems, baseUrl, m_fileItemActions);
616 auto internalContextMenuActions = m_internalContextMenu->actions();
617
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)));
623
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();
628
629 for (auto it = internalContextMenuActions.constBegin(); it != internalContextMenuActions.constEnd(); ++it) {
630 if (actionsThatShouldntBeAdded.count(*it)) {
631 continue; // Skip this action.
632 }
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));
637 }
638 }
639 }
640 return contextActions;
641 }
642
643 int SelectionModeBottomBar::unusedSpace() const
644 {
645 int sumOfPreferredWidths = m_layout->contentsMargins().left() + m_layout->contentsMargins().right();
646 if (m_overflowButton) {
647 sumOfPreferredWidths += m_overflowButton->sizeHint().width();
648 }
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.
653 }
654 sumOfPreferredWidths += m_layout->itemAt(i)->sizeHint().width() + m_layout->spacing();
655 }
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.
658 }
659
660 void SelectionModeBottomBar::updateExplanatoryLabelVisibility()
661 {
662 if (!m_explanatoryLabel) {
663 return;
664 }
665 if (m_explanatoryLabel->isVisible()) {
666 m_explanatoryLabel->setVisible(unusedSpace() > 0);
667 } else {
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);
670 }
671 }
672
673 void SelectionModeBottomBar::updateMainActionButton(const KFileItemList& selection)
674 {
675 if (!m_mainAction.widget()) {
676 return;
677 }
678 Q_ASSERT(qobject_cast<QAbstractButton *>(m_mainAction.widget()));
679
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();
683
684 QString buttonText;
685 switch (m_contents) {
686 case CopyContents:
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));
690 break;
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));
695 break;
696 case CutContents:
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));
700 break;
701 case DeleteContents:
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));
705 break;
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));
710 break;
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));
715 break;
716 case RenameContents:
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));
720 break;
721 default:
722 return;
723 }
724 if (buttonText != QStringLiteral("NULL")) {
725 static_cast<QAbstractButton *>(m_mainAction.widget())->setText(buttonText);
726
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();
729 }
730 }