2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #include "bottombarcontentscontainer.h"
10 #include "dolphin_generalsettings.h"
11 #include "dolphincontextmenu.h"
12 #include "dolphinmainwindow.h"
13 #include "dolphinremoveaction.h"
14 #include "kitemviews/kfileitemlisttostring.h"
16 #include <KLocalizedString>
18 #include <QHBoxLayout>
21 #include <QToolButton>
22 #include <QVBoxLayout>
24 #include <unordered_set>
26 using namespace SelectionMode
;
28 BottomBarContentsContainer::BottomBarContentsContainer(KActionCollection
*actionCollection
, QWidget
*parent
)
30 , m_actionCollection
{actionCollection
}
32 // We will mostly interact with m_layout when changing the contents and not care about the other internal hierarchy.
33 m_layout
= new QHBoxLayout(this);
36 void BottomBarContentsContainer::resetContents(BottomBar::Contents contents
)
40 // A label is added in many of the methods below. We only know its size a bit later and if it should be hidden.
41 QTimer::singleShot(10, this, [this]() {
42 updateExplanatoryLabelVisibility();
45 Q_CHECK_PTR(m_actionCollection
);
46 m_contents
= contents
;
48 case BottomBar::CopyContents
:
49 return addCopyContents();
50 case BottomBar::CopyLocationContents
:
51 return addCopyLocationContents();
52 case BottomBar::CopyToOtherViewContents
:
53 return addCopyToOtherViewContents();
54 case BottomBar::CutContents
:
55 return addCutContents();
56 case BottomBar::DeleteContents
:
57 return addDeleteContents();
58 case BottomBar::DuplicateContents
:
59 return addDuplicateContents();
60 case BottomBar::GeneralContents
:
61 return addGeneralContents();
62 case BottomBar::PasteContents
:
63 return addPasteContents();
64 case BottomBar::MoveToOtherViewContents
:
65 return addMoveToOtherViewContents();
66 case BottomBar::MoveToTrashContents
:
67 return addMoveToTrashContents();
68 case BottomBar::RenameContents
:
69 return addRenameContents();
73 void BottomBarContentsContainer::adaptToNewBarWidth(int newBarWidth
)
75 m_barWidth
= newBarWidth
;
77 if (m_contents
== BottomBar::GeneralContents
) {
78 Q_ASSERT(m_overflowButton
);
79 if (unusedSpace() < 0) {
80 // The bottom bar is overflowing! We need to hide some of the widgets.
81 for (auto i
= m_generalBarActions
.rbegin(); i
!= m_generalBarActions
.rend(); ++i
) {
82 if (!i
->isWidgetVisible()) {
85 i
->widget()->setVisible(false);
87 // Add the action to the overflow.
88 auto overflowMenu
= m_overflowButton
->menu();
89 if (overflowMenu
->actions().isEmpty()) {
90 overflowMenu
->addAction(i
->action());
92 overflowMenu
->insertAction(overflowMenu
->actions().at(0), i
->action());
94 m_overflowButton
->setVisible(true);
95 if (unusedSpace() >= 0) {
96 break; // All widgets fit now.
100 // We have some unusedSpace(). Let's check if we can maybe add more of the contextual actions' widgets.
101 for (auto i
= m_generalBarActions
.begin(); i
!= m_generalBarActions
.end(); ++i
) {
102 if (i
->isWidgetVisible()) {
107 i
->widget()->setVisible(false);
108 m_layout
->insertWidget(m_layout
->count() - 1, i
->widget()); // Insert before m_overflowButton
110 if (unusedSpace() < i
->widget()->sizeHint().width()) {
111 // It doesn't fit. We keep it invisible.
114 i
->widget()->setVisible(true);
116 // Remove the action from the overflow.
117 auto overflowMenu
= m_overflowButton
->menu();
118 overflowMenu
->removeAction(i
->action());
119 if (overflowMenu
->isEmpty()) {
120 m_overflowButton
->setVisible(false);
126 // Hide the leading explanation if it doesn't fit. The buttons are labeled clear enough that this shouldn't be a big UX problem.
127 updateExplanatoryLabelVisibility();
130 void BottomBarContentsContainer::slotSelectionChanged(const KFileItemList
&selection
, const QUrl
&baseUrl
)
132 if (m_contents
== BottomBar::GeneralContents
) {
133 auto contextActions
= contextActionsFor(selection
, baseUrl
);
134 m_generalBarActions
.clear();
135 if (contextActions
.empty()) {
136 // We have nothing to show
137 Q_ASSERT(qobject_cast
<BottomBar
*>(parentWidget()->parentWidget()->parentWidget()));
138 if (isVisibleTo(parentWidget()->parentWidget()->parentWidget()->parentWidget())) { // is the bar visible
139 Q_EMIT
barVisibilityChangeRequested(false);
142 for (auto i
= contextActions
.begin(); i
!= contextActions
.end(); ++i
) {
143 m_generalBarActions
.emplace_back(ActionWithWidget
{*i
});
145 resetContents(BottomBar::GeneralContents
);
147 Q_EMIT
barVisibilityChangeRequested(true);
150 updateMainActionButton(selection
);
153 void BottomBarContentsContainer::addCopyContents()
155 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied."), this);
156 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
157 m_explanatoryLabel
->setWordWrap(true);
158 m_layout
->addWidget(m_explanatoryLabel
);
161 // i18n: Aborts the current step-by-step process to copy files by leaving the selection mode.
162 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this);
164 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
165 m_layout
->addWidget(cancelButton
);
167 auto *copyButton
= new QPushButton(this);
168 // We claim to have PasteContents already so triggering the copy action next won't instantly hide the bottom bar.
169 connect(copyButton
, &QAbstractButton::clicked
, this, [this]() {
170 if (GeneralSettings::showPasteBarAfterCopying()) {
171 m_contents
= BottomBar::Contents::PasteContents
; // prevents hiding
174 // Connect the copy action as a second step.
175 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)), copyButton
);
176 // Finally connect the lambda that actually changes the contents to the PasteContents.
177 connect(copyButton
, &QAbstractButton::clicked
, this, [this]() {
178 if (GeneralSettings::showPasteBarAfterCopying()) {
179 resetContents(BottomBar::Contents::PasteContents
); // resetContents() needs to be connected last because
180 // it instantly deletes the button and then the other slots won't be called.
182 Q_EMIT
selectionModeLeavingRequested();
184 updateMainActionButton(KFileItemList());
185 m_layout
->addWidget(copyButton
);
188 void BottomBarContentsContainer::addCopyLocationContents()
190 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select one file or folder whose location should be copied."), this);
191 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
192 m_explanatoryLabel
->setWordWrap(true);
193 m_layout
->addWidget(m_explanatoryLabel
);
196 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
197 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this);
199 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
200 m_layout
->addWidget(cancelButton
);
202 auto *copyLocationButton
= new QPushButton(this);
203 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("copy_location")), copyLocationButton
);
204 updateMainActionButton(KFileItemList());
205 m_layout
->addWidget(copyLocationButton
);
208 void BottomBarContentsContainer::addCopyToOtherViewContents()
211 // i18n: "Copy over" refers to copying to the other split view area that is currently visible to the user.
212 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied over."), this);
214 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
215 m_explanatoryLabel
->setWordWrap(true);
216 m_layout
->addWidget(m_explanatoryLabel
);
219 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
220 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this);
222 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
223 m_layout
->addWidget(cancelButton
);
225 auto *copyToOtherViewButton
= new QPushButton(this);
226 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("copy_to_inactive_split_view")), copyToOtherViewButton
);
227 updateMainActionButton(KFileItemList());
228 m_layout
->addWidget(copyToOtherViewButton
);
231 void BottomBarContentsContainer::addCutContents()
233 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be cut."), this);
234 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
235 m_explanatoryLabel
->setWordWrap(true);
236 m_layout
->addWidget(m_explanatoryLabel
);
239 // i18n: Aborts the current step-by-step process to cut files by leaving the selection mode.
240 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Cutting"), this);
242 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
243 m_layout
->addWidget(cancelButton
);
245 auto *cutButton
= new QPushButton(this);
246 // We claim to have PasteContents already so triggering the cut action next won't instantly hide the bottom bar.
247 connect(cutButton
, &QAbstractButton::clicked
, this, [this]() {
248 if (GeneralSettings::showPasteBarAfterCopying()) {
249 m_contents
= BottomBar::Contents::PasteContents
; // prevents hiding
252 // Connect the cut action as a second step.
253 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)), cutButton
);
254 // Finally connect the lambda that actually changes the contents to the PasteContents.
255 connect(cutButton
, &QAbstractButton::clicked
, this, [this]() {
256 if (GeneralSettings::showPasteBarAfterCopying()) {
257 resetContents(BottomBar::Contents::PasteContents
); // resetContents() needs to be connected last because
258 // it instantly deletes the button and then the other slots won't be called.
260 Q_EMIT
selectionModeLeavingRequested();
262 updateMainActionButton(KFileItemList());
263 m_layout
->addWidget(cutButton
);
266 void BottomBarContentsContainer::addDeleteContents()
269 new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be permanently deleted."), this);
270 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
271 m_explanatoryLabel
->setWordWrap(true);
272 m_layout
->addWidget(m_explanatoryLabel
);
275 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
276 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel"), this);
278 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
279 m_layout
->addWidget(cancelButton
);
281 auto *deleteButton
= new QPushButton(this);
282 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::DeleteFile
)), deleteButton
);
283 updateMainActionButton(KFileItemList());
284 m_layout
->addWidget(deleteButton
);
287 void BottomBarContentsContainer::addDuplicateContents()
289 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be duplicated here."), this);
290 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
291 m_explanatoryLabel
->setWordWrap(true);
292 m_layout
->addWidget(m_explanatoryLabel
);
295 // i18n: Aborts the current step-by-step process to duplicate files by leaving the selection mode.
296 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Duplicating"), this);
298 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
299 m_layout
->addWidget(cancelButton
);
301 auto *duplicateButton
= new QPushButton(this);
302 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("duplicate")), duplicateButton
);
303 updateMainActionButton(KFileItemList());
304 m_layout
->addWidget(duplicateButton
);
307 void BottomBarContentsContainer::addGeneralContents()
309 if (!m_overflowButton
) {
311 // i18n: This button appears in a bar if there isn't enough horizontal space to fit all the other buttons so please keep it short.
312 // The small button opens a menu that contains the actions that didn't fit on the bar.
313 m_overflowButton
= new QPushButton
{QIcon::fromTheme(QStringLiteral("view-more-symbolic")), i18nc("@action keep short", "More"), this};
315 m_overflowButton
->setMenu(new QMenu
{m_overflowButton
});
316 m_overflowButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::MinimumExpanding
); // Makes sure it has the same height as the labeled buttons.
317 m_layout
->addWidget(m_overflowButton
);
319 m_overflowButton
->menu()->actions().clear();
320 // The overflowButton should be part of the calculation for needed space so we set it visible in regards to unusedSpace().
321 m_overflowButton
->setVisible(true);
324 // We first add all the m_generalBarActions to the bar until the bar is full.
325 auto i
= m_generalBarActions
.begin();
326 for (; i
!= m_generalBarActions
.end(); ++i
) {
327 if (i
->action()->isVisible()) {
329 i
->widget()->setEnabled(i
->action()->isEnabled());
332 i
->widget()->setVisible(false);
333 m_layout
->insertWidget(m_layout
->count() - 1, i
->widget()); // Insert before m_overflowButton
335 if (unusedSpace() < i
->widget()->sizeHint().width()) {
336 break; // The bar is too full already. We keep it invisible.
338 i
->widget()->setVisible(true);
342 // We are done adding widgets to the bar so either we were able to fit all the actions in there
343 m_overflowButton
->setVisible(false);
344 // …or there are more actions left which need to be put into m_overflowButton.
345 for (; i
!= m_generalBarActions
.end(); ++i
) {
346 m_overflowButton
->menu()->addAction(i
->action());
348 // The overflowButton is set visible if there is actually an action in it.
349 if (!m_overflowButton
->isVisible() && i
->action()->isVisible() && !i
->action()->isSeparator()) {
350 m_overflowButton
->setVisible(true);
355 void BottomBarContentsContainer::addMoveToOtherViewContents()
358 // i18n: "Move over" refers to moving to the other split view area that is currently visible to the user.
359 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved over."), this);
361 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
362 m_explanatoryLabel
->setWordWrap(true);
363 m_layout
->addWidget(m_explanatoryLabel
);
366 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
367 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Moving"), this);
369 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
370 m_layout
->addWidget(cancelButton
);
372 auto *moveToOtherViewButton
= new QPushButton(this);
373 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("move_to_inactive_split_view")), moveToOtherViewButton
);
374 updateMainActionButton(KFileItemList());
375 m_layout
->addWidget(moveToOtherViewButton
);
378 void BottomBarContentsContainer::addMoveToTrashContents()
381 new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved to the Trash."), this);
382 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
383 m_explanatoryLabel
->setWordWrap(true);
384 m_layout
->addWidget(m_explanatoryLabel
);
387 // i18n: Aborts the current step-by-step process of moving files to the trash by leaving the selection mode.
388 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel"), this);
390 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
391 m_layout
->addWidget(cancelButton
);
393 auto *moveToTrashButton
= new QPushButton(this);
394 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)), moveToTrashButton
);
395 updateMainActionButton(KFileItemList());
396 m_layout
->addWidget(moveToTrashButton
);
399 void BottomBarContentsContainer::addPasteContents()
401 m_explanatoryLabel
= new QLabel(xi18n("<para>The selected files and folders were added to the Clipboard. "
402 "Now the <emphasis>Paste</emphasis> action can be used to transfer them from the Clipboard "
403 "to any other location. They can even be transferred to other applications by using their "
404 "respective <emphasis>Paste</emphasis> actions.</para>"),
406 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
407 m_explanatoryLabel
->setWordWrap(true);
408 m_layout
->addWidget(m_explanatoryLabel
);
410 auto *vBoxLayout
= new QVBoxLayout(this);
411 m_layout
->addLayout(vBoxLayout
);
413 /** We are in "PasteContents" mode which means hiding the bottom bar is impossible.
414 * So we first have to claim that we have different contents before requesting to leave selection mode. */
415 auto actuallyLeaveSelectionMode
= [this]() {
416 m_contents
= BottomBar::Contents::CopyLocationContents
;
417 Q_EMIT
barVisibilityChangeRequested(false);
420 auto *pasteButton
= new QPushButton(this);
421 copyActionDataToButton(pasteButton
, m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
422 pasteButton
->setText(i18nc("@action A more elaborate and clearly worded version of the Paste action", "Paste from Clipboard"));
423 connect(pasteButton
, &QAbstractButton::clicked
, this, actuallyLeaveSelectionMode
);
424 vBoxLayout
->addWidget(pasteButton
);
426 auto *dismissButton
= new QToolButton(this);
427 dismissButton
->setText(i18nc("@action Dismisses a bar explaining how to use the Paste action", "Dismiss This Reminder"));
428 connect(dismissButton
, &QAbstractButton::clicked
, this, actuallyLeaveSelectionMode
);
429 auto *dontRemindAgainAction
= new QAction(i18nc("@action Dismisses an explanatory area and never shows it again", "Don't Remind Me Again"), this);
430 connect(dontRemindAgainAction
, &QAction::triggered
, this, []() {
431 GeneralSettings::setShowPasteBarAfterCopying(false);
433 connect(dontRemindAgainAction
, &QAction::triggered
, this, actuallyLeaveSelectionMode
);
434 auto *dismissButtonMenu
= new QMenu(dismissButton
);
435 dismissButtonMenu
->addAction(dontRemindAgainAction
);
436 dismissButton
->setMenu(dismissButtonMenu
);
437 dismissButton
->setPopupMode(QToolButton::MenuButtonPopup
);
438 vBoxLayout
->addWidget(dismissButton
);
440 m_explanatoryLabel
->setMaximumHeight(pasteButton
->sizeHint().height() + dismissButton
->sizeHint().height() + m_explanatoryLabel
->fontMetrics().height());
443 void BottomBarContentsContainer::addRenameContents()
445 m_explanatoryLabel
= new QLabel(i18nc("@info explains the next step in a process",
446 "Select the file or folder that should be renamed.\nBulk renaming is possible when multiple items are selected."),
448 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
449 m_explanatoryLabel
->setWordWrap(true);
450 m_layout
->addWidget(m_explanatoryLabel
);
453 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
454 auto *cancelButton
= new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Renaming"), this);
456 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::selectionModeLeavingRequested
);
457 m_layout
->addWidget(cancelButton
);
459 auto *renameButton
= new QPushButton(this);
460 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)), renameButton
);
461 updateMainActionButton(KFileItemList());
462 m_layout
->addWidget(renameButton
);
465 void BottomBarContentsContainer::emptyBarContents()
468 while ((child
= m_layout
->takeAt(0)) != nullptr) {
469 if (auto *childLayout
= child
->layout()) {
470 QLayoutItem
*grandChild
;
471 while ((grandChild
= childLayout
->takeAt(0)) != nullptr) {
472 delete grandChild
->widget(); // delete the widget
473 delete grandChild
; // delete the layout item
476 delete child
->widget(); // delete the widget
477 delete child
; // delete the layout item
481 std::vector
<QAction
*> BottomBarContentsContainer::contextActionsFor(const KFileItemList
&selectedItems
, const QUrl
&baseUrl
)
483 if (selectedItems
.isEmpty()) {
484 // There are no contextual actions to show for these items.
485 // We might even want to hide this bar in this case. To make this clear, we reset m_internalContextMenu.
486 m_internalContextMenu
.release()->deleteLater();
487 return std::vector
<QAction
*>{};
490 std::vector
<QAction
*> contextActions
;
492 // We always want to show the most important actions at the beginning
493 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
494 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
495 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
496 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
498 // We are going to add the actions from the right-click context menu for the selected items.
499 auto *dolphinMainWindow
= qobject_cast
<DolphinMainWindow
*>(window());
500 Q_CHECK_PTR(dolphinMainWindow
);
501 if (!m_fileItemActions
) {
502 m_fileItemActions
= new KFileItemActions(this);
503 m_fileItemActions
->setParentWidget(dolphinMainWindow
);
504 connect(m_fileItemActions
, &KFileItemActions::error
, this, &BottomBarContentsContainer::error
);
506 m_internalContextMenu
= std::make_unique
<DolphinContextMenu
>(dolphinMainWindow
, selectedItems
.constFirst(), selectedItems
, baseUrl
, m_fileItemActions
);
507 auto internalContextMenuActions
= m_internalContextMenu
->actions();
509 // There are some actions which we wouldn't want to add. We remember them in the actionsThatShouldntBeAdded set.
510 // We don't want to add the four basic actions again which were already added to the top.
511 std::unordered_set
<QAction
*> actionsThatShouldntBeAdded
{contextActions
.begin(), contextActions
.end()};
512 // "Delete" isn't really necessary to add because we have "Move to Trash" already. It is also more dangerous so let's exclude it.
513 actionsThatShouldntBeAdded
.insert(m_actionCollection
->action(KStandardAction::name(KStandardAction::DeleteFile
)));
515 // KHamburgerMenu would only be visible if there is no menu available anywhere on the user interface. This might be useful for recovery from
516 // such a situation in theory but a bar with context dependent actions doesn't really seem like the right place for it.
517 Q_ASSERT(internalContextMenuActions
.first()->icon().name()
518 == m_actionCollection
->action(KStandardAction::name(KStandardAction::HamburgerMenu
))->icon().name());
519 internalContextMenuActions
.removeFirst();
521 for (auto it
= internalContextMenuActions
.constBegin(); it
!= internalContextMenuActions
.constEnd(); ++it
) {
522 if (actionsThatShouldntBeAdded
.count(*it
)) {
523 continue; // Skip this action.
525 if (!qobject_cast
<DolphinRemoveAction
*>(*it
)) { // We already have a "Move to Trash" action so we don't want a DolphinRemoveAction.
526 // We filter duplicate separators here so we won't have to deal with them later.
527 if (!contextActions
.back()->isSeparator() || !(*it
)->isSeparator()) {
528 contextActions
.emplace_back((*it
));
533 auto separator
= new QAction(m_internalContextMenu
.get());
534 separator
->setSeparator(true);
535 contextActions
.emplace_back(separator
);
537 // Add "Invert Selection" and "Select All" at the very end for better usability while in selection mode.
538 // Design-wise this decision is slightly questionable because the other actions in the bar apply to the selected items while
539 // the "select" actions apply to the view instead but we decided that there are more benefits than drawbacks to this.
540 auto invertSelectionAction
= m_actionCollection
->action(QStringLiteral("invert_selection"));
541 Q_ASSERT(invertSelectionAction
&& !internalContextMenuActions
.contains(invertSelectionAction
));
542 contextActions
.emplace_back(invertSelectionAction
);
543 auto selectAllAction
= m_actionCollection
->action(KStandardAction::name(KStandardAction::SelectAll
));
544 Q_ASSERT(selectAllAction
&& !internalContextMenuActions
.contains(selectAllAction
));
545 contextActions
.emplace_back(selectAllAction
);
547 return contextActions
;
550 int BottomBarContentsContainer::unusedSpace() const
552 int sumOfPreferredWidths
= m_layout
->contentsMargins().left() + m_layout
->contentsMargins().right();
553 if (m_overflowButton
) {
554 sumOfPreferredWidths
+= m_overflowButton
->sizeHint().width();
556 for (int i
= 0; i
< m_layout
->count(); ++i
) {
557 auto widget
= m_layout
->itemAt(i
)->widget();
558 if (widget
&& !widget
->isVisibleTo(widget
->parentWidget())) {
559 continue; // We don't count invisible widgets.
561 sumOfPreferredWidths
+= m_layout
->itemAt(i
)->sizeHint().width() + m_layout
->spacing();
563 return m_barWidth
- sumOfPreferredWidths
- 20; // We consider all space used when there are only 20 pixels left
564 // so there is some room to breath and not too much wonkyness while resizing.
567 void BottomBarContentsContainer::updateExplanatoryLabelVisibility()
569 if (!m_explanatoryLabel
) {
572 if (m_explanatoryLabel
->isVisible()) {
573 m_explanatoryLabel
->setVisible(unusedSpace() > 0);
575 // We only want to re-show the label when it fits comfortably so the computation below adds another "+20".
576 m_explanatoryLabel
->setVisible(unusedSpace() > m_explanatoryLabel
->sizeHint().width() + 20);
580 void BottomBarContentsContainer::updateMainActionButton(const KFileItemList
& selectedItems
)
582 if (!m_mainAction
.widget()) {
585 Q_ASSERT(qobject_cast
<QAbstractButton
*>(m_mainAction
.widget()));
587 // Users are nudged towards selecting items by having the button disabled when nothing is selected.
588 m_mainAction
.widget()->setEnabled(selectedItems
.count() > 0 && m_mainAction
.action()->isEnabled());
589 QFontMetrics fontMetrics
= m_mainAction
.widget()->fontMetrics();
592 switch (m_contents
) {
593 case BottomBar::CopyContents
:
594 // i18n: A more elaborate and clearly worded version of the Copy action
595 // %2 is a textual representation of the currently selected files or folders. This can be the name of
596 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
597 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
598 // and a fallback will be used.
599 buttonText
= i18ncp("@action", "Copy %2 to the Clipboard", "Copy %2 to the Clipboard", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
600 // All these long lines can not be broken up with line breaks becaue the i18n call should be completely
601 // in the line following the "i18n:" comment without any line breaks within the i18n call
602 // or the comment might not be correctly extracted. See: https://commits.kde.org/kxmlgui/a31135046e1b3335b5d7bbbe6aa9a883ce3284c1
604 case BottomBar::CopyLocationContents
:
605 // i18n: A more elaborate and clearly worded version of the Copy Location action
606 // %2 is a textual representation of the currently selected files or folders. This can be the name of
607 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
608 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
609 // and a fallback will be used.
610 buttonText
= i18ncp("@action", "Copy the Location of %2 to the Clipboard", "Copy the Location of %2 to the Clipboard", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
612 case BottomBar::CutContents
:
613 // i18n: A more elaborate and clearly worded version of the Cut action
614 // %2 is a textual representation of the currently selected files or folders. This can be the name of
615 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
616 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
617 // and a fallback will be used.
618 buttonText
= i18ncp("@action", "Cut %2 to the Clipboard", "Cut %2 to the Clipboard", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
620 case BottomBar::DeleteContents
:
621 // i18n: A more elaborate and clearly worded version of the Delete action
622 // %2 is a textual representation of the currently selected files or folders. This can be the name of
623 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
624 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
625 // and a fallback will be used.
626 buttonText
= i18ncp("@action", "Permanently Delete %2", "Permanently Delete %2", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
628 case BottomBar::DuplicateContents
:
629 // i18n: A more elaborate and clearly worded version of the Duplicate action
630 // %2 is a textual representation of the currently selected files or folders. This can be the name of
631 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
632 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
633 // and a fallback will be used.
634 buttonText
= i18ncp("@action", "Duplicate %2", "Duplicate %2", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
636 case BottomBar::MoveToTrashContents
:
637 // i18n: A more elaborate and clearly worded version of the Trash action
638 // %2 is a textual representation of the currently selected files or folders. This can be the name of
639 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
640 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
641 // and a fallback will be used.
642 buttonText
= i18ncp("@action", "Move %2 to the Trash", "Move %2 to the Trash", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
644 case BottomBar::RenameContents
:
645 // i18n: A more elaborate and clearly worded version of the Rename action
646 // %2 is a textual representation of the currently selected files or folders. This can be the name of
647 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
648 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
649 // and a fallback will be used.
650 buttonText
= i18ncp("@action", "Rename %2", "Rename %2", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
655 if (buttonText
!= QStringLiteral("NULL")) {
656 static_cast<QAbstractButton
*>(m_mainAction
.widget())->setText(buttonText
);
658 // The width of the button has changed. We might want to hide the label so the full button text fits on the bar.
659 updateExplanatoryLabelVisibility();
664 #include "moc_bottombarcontentscontainer.cpp"