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 "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 <QPushButton>
22 #include <QToolButton>
23 #include <QVBoxLayout>
25 #include <unordered_set>
27 using namespace SelectionMode
;
29 BottomBarContentsContainer::BottomBarContentsContainer(KActionCollection
*actionCollection
, QWidget
*parent
) :
31 m_actionCollection
{actionCollection
}
33 // We will mostly interact with m_layout when changing the contents and not care about the other internal hierarchy.
34 m_layout
= new QHBoxLayout(this);
37 void BottomBarContentsContainer::resetContents(BottomBar::Contents contents
)
41 // A label is added in many of the methods below. We only know its size a bit later and if it should be hidden.
42 QTimer::singleShot(10, this, [this](){ updateExplanatoryLabelVisibility(); });
44 Q_CHECK_PTR(m_actionCollection
);
45 m_contents
= contents
;
47 case BottomBar::CopyContents
:
48 return addCopyContents();
49 case BottomBar::CopyLocationContents
:
50 return addCopyLocationContents();
51 case BottomBar::CopyToOtherViewContents
:
52 return addCopyToOtherViewContents();
53 case BottomBar::CutContents
:
54 return addCutContents();
55 case BottomBar::DeleteContents
:
56 return addDeleteContents();
57 case BottomBar::DuplicateContents
:
58 return addDuplicateContents();
59 case BottomBar::GeneralContents
:
60 return addGeneralContents();
61 case BottomBar::PasteContents
:
62 return addPasteContents();
63 case BottomBar::MoveToOtherViewContents
:
64 return addMoveToOtherViewContents();
65 case BottomBar::MoveToTrashContents
:
66 return addMoveToTrashContents();
67 case BottomBar::RenameContents
:
68 return addRenameContents();
72 void BottomBarContentsContainer::adaptToNewBarWidth(int newBarWidth
)
74 m_barWidth
= newBarWidth
;
76 if (m_contents
== BottomBar::GeneralContents
) {
77 Q_ASSERT(m_overflowButton
);
78 if (unusedSpace() < 0) {
79 // The bottom bar is overflowing! We need to hide some of the widgets.
80 for (auto i
= m_generalBarActions
.rbegin(); i
!= m_generalBarActions
.rend(); ++i
) {
81 if (!i
->isWidgetVisible()) {
84 i
->widget()->setVisible(false);
86 // Add the action to the overflow.
87 auto overflowMenu
= m_overflowButton
->menu();
88 if (overflowMenu
->actions().isEmpty()) {
89 overflowMenu
->addAction(i
->action());
91 overflowMenu
->insertAction(overflowMenu
->actions().at(0), i
->action());
93 m_overflowButton
->setVisible(true);
94 if (unusedSpace() >= 0) {
95 break; // All widgets fit now.
99 // We have some unusedSpace(). Let's check if we can maybe add more of the contextual actions' widgets.
100 for (auto i
= m_generalBarActions
.begin(); i
!= m_generalBarActions
.end(); ++i
) {
101 if (i
->isWidgetVisible()) {
106 i
->widget()->setVisible(false);
107 m_layout
->insertWidget(m_layout
->count() - 1, i
->widget()); // Insert before m_overflowButton
109 if (unusedSpace() < i
->widget()->sizeHint().width()) {
110 // It doesn't fit. We keep it invisible.
113 i
->widget()->setVisible(true);
115 // Remove the action from the overflow.
116 auto overflowMenu
= m_overflowButton
->menu();
117 overflowMenu
->removeAction(i
->action());
118 if (overflowMenu
->isEmpty()) {
119 m_overflowButton
->setVisible(false);
125 // Hide the leading explanation if it doesn't fit. The buttons are labeled clear enough that this shouldn't be a big UX problem.
126 updateExplanatoryLabelVisibility();
129 void BottomBarContentsContainer::slotSelectionChanged(const KFileItemList
&selection
, const QUrl
&baseUrl
)
131 if (m_contents
== BottomBar::GeneralContents
) {
132 auto contextActions
= contextActionsFor(selection
, baseUrl
);
133 m_generalBarActions
.clear();
134 if (contextActions
.empty()) {
135 // We have nothing to show
136 Q_ASSERT(qobject_cast
<BottomBar
*>(parentWidget()->parentWidget()->parentWidget()));
137 if (isVisibleTo(parentWidget()->parentWidget()->parentWidget()->parentWidget())) { // is the bar visible
138 Q_EMIT
barVisibilityChangeRequested(false);
141 for (auto i
= contextActions
.begin(); i
!= contextActions
.end(); ++i
) {
142 m_generalBarActions
.emplace_back(ActionWithWidget
{*i
});
144 resetContents(BottomBar::GeneralContents
);
146 Q_EMIT
barVisibilityChangeRequested(true);
149 updateMainActionButton(selection
);
152 void BottomBarContentsContainer::addCopyContents()
154 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied."), this);
155 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
156 m_explanatoryLabel
->setWordWrap(true);
157 m_layout
->addWidget(m_explanatoryLabel
);
159 // i18n: Aborts the current step-by-step process to copy files by leaving the selection mode.
160 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Copying"), this);
161 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
162 m_layout
->addWidget(cancelButton
);
164 auto *copyButton
= new QPushButton(this);
165 // We claim to have PasteContents already so triggering the copy action next won't instantly hide the bottom bar.
166 connect(copyButton
, &QAbstractButton::clicked
, [this]() {
167 if (GeneralSettings::showPasteBarAfterCopying()) {
168 m_contents
= BottomBar::Contents::PasteContents
; // prevents hiding
171 // Connect the copy action as a second step.
172 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)), copyButton
);
173 // Finally connect the lambda that actually changes the contents to the PasteContents.
174 connect(copyButton
, &QAbstractButton::clicked
, [this]() {
175 if (GeneralSettings::showPasteBarAfterCopying()) {
176 resetContents(BottomBar::Contents::PasteContents
); // resetContents() needs to be connected last because
177 // it instantly deletes the button and then the other slots won't be called.
179 Q_EMIT
leaveSelectionModeRequested();
181 updateMainActionButton(KFileItemList());
182 m_layout
->addWidget(copyButton
);
185 void BottomBarContentsContainer::addCopyLocationContents()
187 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select one file or folder whose location should be copied."), this);
188 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
189 m_explanatoryLabel
->setWordWrap(true);
190 m_layout
->addWidget(m_explanatoryLabel
);
192 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
193 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Copying"), this);
194 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
195 m_layout
->addWidget(cancelButton
);
197 auto *copyLocationButton
= new QPushButton(this);
198 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("copy_location")), copyLocationButton
);
199 updateMainActionButton(KFileItemList());
200 m_layout
->addWidget(copyLocationButton
);
203 void BottomBarContentsContainer::addCopyToOtherViewContents()
205 // i18n: "Copy over" refers to copying to the other split view area that is currently visible to the user.
206 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied over."), this);
207 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
208 m_explanatoryLabel
->setWordWrap(true);
209 m_layout
->addWidget(m_explanatoryLabel
);
211 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
212 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Copying"), this);
213 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
214 m_layout
->addWidget(cancelButton
);
216 auto *copyToOtherViewButton
= new QPushButton(this);
217 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("copy_to_inactive_split_view")), copyToOtherViewButton
);
218 updateMainActionButton(KFileItemList());
219 m_layout
->addWidget(copyToOtherViewButton
);
222 void BottomBarContentsContainer::addCutContents()
224 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be cut."), this);
225 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
226 m_explanatoryLabel
->setWordWrap(true);
227 m_layout
->addWidget(m_explanatoryLabel
);
229 // i18n: Aborts the current step-by-step process to cut files by leaving the selection mode.
230 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Cutting"), this);
231 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
232 m_layout
->addWidget(cancelButton
);
234 auto *cutButton
= new QPushButton(this);
235 // We claim to have PasteContents already so triggering the cut action next won't instantly hide the bottom bar.
236 connect(cutButton
, &QAbstractButton::clicked
, [this]() {
237 if (GeneralSettings::showPasteBarAfterCopying()) {
238 m_contents
= BottomBar::Contents::PasteContents
; // prevents hiding
241 // Connect the cut action as a second step.
242 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)), cutButton
);
243 // Finally connect the lambda that actually changes the contents to the PasteContents.
244 connect(cutButton
, &QAbstractButton::clicked
, [this](){
245 if (GeneralSettings::showPasteBarAfterCopying()) {
246 resetContents(BottomBar::Contents::PasteContents
); // resetContents() needs to be connected last because
247 // it instantly deletes the button and then the other slots won't be called.
249 Q_EMIT
leaveSelectionModeRequested();
251 updateMainActionButton(KFileItemList());
252 m_layout
->addWidget(cutButton
);
255 void BottomBarContentsContainer::addDeleteContents()
257 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be permanently deleted."), this);
258 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
259 m_explanatoryLabel
->setWordWrap(true);
260 m_layout
->addWidget(m_explanatoryLabel
);
262 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
263 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort"), this);
264 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
265 m_layout
->addWidget(cancelButton
);
267 auto *deleteButton
= new QPushButton(this);
268 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::DeleteFile
)), deleteButton
);
269 updateMainActionButton(KFileItemList());
270 m_layout
->addWidget(deleteButton
);
273 void BottomBarContentsContainer::addDuplicateContents()
275 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be duplicated here."), this);
276 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
277 m_explanatoryLabel
->setWordWrap(true);
278 m_layout
->addWidget(m_explanatoryLabel
);
280 // i18n: Aborts the current step-by-step process to duplicate files by leaving the selection mode.
281 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Duplicating"), this);
282 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
283 m_layout
->addWidget(cancelButton
);
285 auto *duplicateButton
= new QPushButton(this);
286 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("duplicate")), duplicateButton
);
287 updateMainActionButton(KFileItemList());
288 m_layout
->addWidget(duplicateButton
);
291 void BottomBarContentsContainer::addGeneralContents()
293 if (!m_overflowButton
) {
294 m_overflowButton
= new QToolButton
{this};
295 // i18n: This button appears in a bar if there isn't enough horizontal space to fit all the other buttons.
296 // The small icon-only button opens a menu that contains the actions that didn't fit on the bar.
297 // Since this is an icon-only button this text will only appear as a tooltip and as accessibility text.
298 m_overflowButton
->setToolTip(i18nc("@action", "More"));
299 m_overflowButton
->setAccessibleName(m_overflowButton
->toolTip());
300 m_overflowButton
->setIcon(QIcon::fromTheme(QStringLiteral("view-more-horizontal-symbolic")));
301 m_overflowButton
->setMenu(new QMenu
{m_overflowButton
});
302 m_overflowButton
->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup
);
303 m_overflowButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::MinimumExpanding
); // Makes sure it has the same height as the labeled buttons.
304 m_layout
->addWidget(m_overflowButton
);
306 m_overflowButton
->menu()->actions().clear();
307 // The overflowButton should be part of the calculation for needed space so we set it visible in regards to unusedSpace().
308 m_overflowButton
->setVisible(true);
311 // We first add all the m_generalBarActions to the bar until the bar is full.
312 auto i
= m_generalBarActions
.begin();
313 for (; i
!= m_generalBarActions
.end(); ++i
) {
314 if (i
->action()->isVisible()) {
316 i
->widget()->setEnabled(i
->action()->isEnabled());
319 i
->widget()->setVisible(false);
320 m_layout
->insertWidget(m_layout
->count() - 1, i
->widget()); // Insert before m_overflowButton
322 if (unusedSpace() < i
->widget()->sizeHint().width()) {
323 break; // The bar is too full already. We keep it invisible.
325 i
->widget()->setVisible(true);
329 // We are done adding widgets to the bar so either we were able to fit all the actions in there
330 m_overflowButton
->setVisible(false);
331 // …or there are more actions left which need to be put into m_overflowButton.
332 for (; i
!= m_generalBarActions
.end(); ++i
) {
333 m_overflowButton
->menu()->addAction(i
->action());
335 // The overflowButton is set visible if there is actually an action in it.
336 if (!m_overflowButton
->isVisible() && i
->action()->isVisible() && !i
->action()->isSeparator()) {
337 m_overflowButton
->setVisible(true);
342 void BottomBarContentsContainer::addMoveToOtherViewContents()
344 // i18n: "Move over" refers to moving to the other split view area that is currently visible to the user.
345 m_explanatoryLabel
= new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved over."), this);
346 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
347 m_explanatoryLabel
->setWordWrap(true);
348 m_layout
->addWidget(m_explanatoryLabel
);
350 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
351 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort Moving"), this);
352 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
353 m_layout
->addWidget(cancelButton
);
355 auto *moveToOtherViewButton
= new QPushButton(this);
356 m_mainAction
= ActionWithWidget(m_actionCollection
->action(QStringLiteral("move_to_inactive_split_view")), moveToOtherViewButton
);
357 updateMainActionButton(KFileItemList());
358 m_layout
->addWidget(moveToOtherViewButton
);
361 void BottomBarContentsContainer::addMoveToTrashContents()
363 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);
364 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
365 m_explanatoryLabel
->setWordWrap(true);
366 m_layout
->addWidget(m_explanatoryLabel
);
368 // i18n: Aborts the current step-by-step process of moving files to the trash by leaving the selection mode.
369 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Abort"), this);
370 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
371 m_layout
->addWidget(cancelButton
);
373 auto *moveToTrashButton
= new QPushButton(this);
374 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)), moveToTrashButton
);
375 updateMainActionButton(KFileItemList());
376 m_layout
->addWidget(moveToTrashButton
);
379 void BottomBarContentsContainer::addPasteContents()
381 m_explanatoryLabel
= new QLabel(xi18n("<para>The selected files and folders were added to the Clipboard. "
382 "Now the <emphasis>Paste</emphasis> action can be used to transfer them from the Clipboard "
383 "to any other location. They can even be transferred to other applications by using their "
384 "respective <emphasis>Paste</emphasis> actions.</para>"), this);
385 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
386 m_explanatoryLabel
->setWordWrap(true);
387 m_layout
->addWidget(m_explanatoryLabel
);
389 auto *vBoxLayout
= new QVBoxLayout(this);
390 m_layout
->addLayout(vBoxLayout
);
392 /** We are in "PasteContents" mode which means hiding the bottom bar is impossible.
393 * So we first have to claim that we have different contents before requesting to leave selection mode. */
394 auto actuallyLeaveSelectionMode
= [this]() {
395 m_contents
= BottomBar::Contents::CopyLocationContents
;
396 Q_EMIT
leaveSelectionModeRequested();
399 auto *pasteButton
= new QPushButton(this);
400 copyActionDataToButton(pasteButton
, m_actionCollection
->action(KStandardAction::name(KStandardAction::Paste
)));
401 pasteButton
->setText(i18nc("@action A more elaborate and clearly worded version of the Paste action", "Paste from Clipboard"));
402 connect(pasteButton
, &QAbstractButton::clicked
, this, actuallyLeaveSelectionMode
);
403 vBoxLayout
->addWidget(pasteButton
);
405 auto *dismissButton
= new QToolButton(this);
406 dismissButton
->setText(i18nc("@action Dismisses a bar explaining how to use the Paste action", "Dismiss This Reminder"));
407 connect(dismissButton
, &QAbstractButton::clicked
, this, actuallyLeaveSelectionMode
);
408 auto *dontRemindAgainAction
= new QAction(i18nc("@action Dismisses an explanatory area and never shows it again", "Don't Remind Me Again"), this);
409 connect(dontRemindAgainAction
, &QAction::triggered
, this, []() {
410 GeneralSettings::setShowPasteBarAfterCopying(false);
412 connect(dontRemindAgainAction
, &QAction::triggered
, this, actuallyLeaveSelectionMode
);
413 auto *dismissButtonMenu
= new QMenu(dismissButton
);
414 dismissButtonMenu
->addAction(dontRemindAgainAction
);
415 dismissButton
->setMenu(dismissButtonMenu
);
416 dismissButton
->setPopupMode(QToolButton::MenuButtonPopup
);
417 vBoxLayout
->addWidget(dismissButton
);
419 m_explanatoryLabel
->setMaximumHeight(pasteButton
->sizeHint().height() + dismissButton
->sizeHint().height() + m_explanatoryLabel
->fontMetrics().height());
422 void BottomBarContentsContainer::addRenameContents()
424 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);
425 m_explanatoryLabel
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Preferred
);
426 m_explanatoryLabel
->setWordWrap(true);
427 m_layout
->addWidget(m_explanatoryLabel
);
429 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
430 auto *cancelButton
= new QPushButton(i18nc("@action:button", "Stop Renaming"), this);
431 connect(cancelButton
, &QAbstractButton::clicked
, this, &BottomBarContentsContainer::leaveSelectionModeRequested
);
432 m_layout
->addWidget(cancelButton
);
434 auto *renameButton
= new QPushButton(this);
435 m_mainAction
= ActionWithWidget(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)), renameButton
);
436 updateMainActionButton(KFileItemList());
437 m_layout
->addWidget(renameButton
);
440 void BottomBarContentsContainer::emptyBarContents()
443 while ((child
= m_layout
->takeAt(0)) != nullptr) {
444 if (auto *childLayout
= child
->layout()) {
445 QLayoutItem
*grandChild
;
446 while ((grandChild
= childLayout
->takeAt(0)) != nullptr) {
447 delete grandChild
->widget(); // delete the widget
448 delete grandChild
; // delete the layout item
451 delete child
->widget(); // delete the widget
452 delete child
; // delete the layout item
456 std::vector
<QAction
*> BottomBarContentsContainer::contextActionsFor(const KFileItemList
& selectedItems
, const QUrl
& baseUrl
)
458 if (selectedItems
.isEmpty()) {
459 // There are no contextual actions to show for these items.
460 // We might even want to hide this bar in this case. To make this clear, we reset m_internalContextMenu.
461 m_internalContextMenu
.release()->deleteLater();
462 return std::vector
<QAction
*>{};
465 std::vector
<QAction
*> contextActions
;
467 // We always want to show the most important actions at the beginning
468 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::Copy
)));
469 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::Cut
)));
470 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::RenameFile
)));
471 contextActions
.emplace_back(m_actionCollection
->action(KStandardAction::name(KStandardAction::MoveToTrash
)));
473 // We are going to add the actions from the right-click context menu for the selected items.
474 auto *dolphinMainWindow
= qobject_cast
<DolphinMainWindow
*>(window());
475 Q_CHECK_PTR(dolphinMainWindow
);
476 if (!m_fileItemActions
) {
477 m_fileItemActions
= new KFileItemActions(this);
478 m_fileItemActions
->setParentWidget(dolphinMainWindow
);
479 connect(m_fileItemActions
, &KFileItemActions::error
, this, &BottomBarContentsContainer::error
);
481 m_internalContextMenu
= std::make_unique
<DolphinContextMenu
>(dolphinMainWindow
, selectedItems
.constFirst(), selectedItems
, baseUrl
, m_fileItemActions
);
482 auto internalContextMenuActions
= m_internalContextMenu
->actions();
484 // There are some actions which we wouldn't want to add. We remember them in the actionsThatShouldntBeAdded set.
485 // We don't want to add the four basic actions again which were already added to the top.
486 std::unordered_set
<QAction
*> actionsThatShouldntBeAdded
{contextActions
.begin(), contextActions
.end()};
487 // "Delete" isn't really necessary to add because we have "Move to Trash" already. It is also more dangerous so let's exclude it.
488 actionsThatShouldntBeAdded
.insert(m_actionCollection
->action(KStandardAction::name(KStandardAction::DeleteFile
)));
490 // KHamburgerMenu would only be visible if there is no menu available anywhere on the user interface. This might be useful for recovery from
491 // such a situation in theory but a bar with context dependent actions doesn't really seem like the right place for it.
492 Q_ASSERT(internalContextMenuActions
.first()->icon().name() == m_actionCollection
->action(KStandardAction::name(KStandardAction::HamburgerMenu
))->icon().name());
493 internalContextMenuActions
.removeFirst();
495 for (auto it
= internalContextMenuActions
.constBegin(); it
!= internalContextMenuActions
.constEnd(); ++it
) {
496 if (actionsThatShouldntBeAdded
.count(*it
)) {
497 continue; // Skip this action.
499 if (!qobject_cast
<DolphinRemoveAction
*>(*it
)) { // We already have a "Move to Trash" action so we don't want a DolphinRemoveAction.
500 // We filter duplicate separators here so we won't have to deal with them later.
501 if (!contextActions
.back()->isSeparator() || !(*it
)->isSeparator()) {
502 contextActions
.emplace_back((*it
));
506 return contextActions
;
509 int BottomBarContentsContainer::unusedSpace() const
511 int sumOfPreferredWidths
= m_layout
->contentsMargins().left() + m_layout
->contentsMargins().right();
512 if (m_overflowButton
) {
513 sumOfPreferredWidths
+= m_overflowButton
->sizeHint().width();
515 for (int i
= 0; i
< m_layout
->count(); ++i
) {
516 auto widget
= m_layout
->itemAt(i
)->widget();
517 if (widget
&& !widget
->isVisibleTo(widget
->parentWidget())) {
518 continue; // We don't count invisible widgets.
520 sumOfPreferredWidths
+= m_layout
->itemAt(i
)->sizeHint().width() + m_layout
->spacing();
522 return m_barWidth
- sumOfPreferredWidths
- 20; // We consider all space used when there are only 20 pixels left
523 // so there is some room to breath and not too much wonkyness while resizing.
526 void BottomBarContentsContainer::updateExplanatoryLabelVisibility()
528 if (!m_explanatoryLabel
) {
531 if (m_explanatoryLabel
->isVisible()) {
532 m_explanatoryLabel
->setVisible(unusedSpace() > 0);
534 // We only want to re-show the label when it fits comfortably so the computation below adds another "+20".
535 m_explanatoryLabel
->setVisible(unusedSpace() > m_explanatoryLabel
->sizeHint().width() + 20);
539 void BottomBarContentsContainer::updateMainActionButton(const KFileItemList
& selectedItems
)
541 if (!m_mainAction
.widget()) {
544 Q_ASSERT(qobject_cast
<QAbstractButton
*>(m_mainAction
.widget()));
546 // Users are nudged towards selecting items by having the button disabled when nothing is selected.
547 m_mainAction
.widget()->setEnabled(selectedItems
.count() > 0 && m_mainAction
.action()->isEnabled());
548 QFontMetrics fontMetrics
= m_mainAction
.widget()->fontMetrics();
551 switch (m_contents
) {
552 case BottomBar::CopyContents
:
553 // i18n: A more elaborate and clearly worded version of the Copy action
554 // %2 is a textual representation of the currently selected files or folders. This can be the name of
555 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
556 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
557 // and a fallback will be used.
558 buttonText
= i18ncp("@action", "Copy %2 to the Clipboard", "Copy %2 to the Clipboard", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
559 // All these long lines can not be broken up with line breaks becaue the i18n call should be completely
560 // in the line following the "i18n:" comment without any line breaks within the i18n call
561 // or the comment might not be correctly extracted. See: https://commits.kde.org/kxmlgui/a31135046e1b3335b5d7bbbe6aa9a883ce3284c1
563 case BottomBar::CopyLocationContents
:
564 // i18n: A more elaborate and clearly worded version of the Copy Location action
565 // %2 is a textual representation of the currently selected files or folders. This can be the name of
566 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
567 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
568 // and a fallback will be used.
569 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
));
571 case BottomBar::CutContents
:
572 // i18n: A more elaborate and clearly worded version of the Cut action
573 // %2 is a textual representation of the currently selected files or folders. This can be the name of
574 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
575 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
576 // and a fallback will be used.
577 buttonText
= i18ncp("@action", "Cut %2 to the Clipboard", "Cut %2 to the Clipboard", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
579 case BottomBar::DeleteContents
:
580 // i18n: A more elaborate and clearly worded version of the Delete action
581 // %2 is a textual representation of the currently selected files or folders. This can be the name of
582 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
583 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
584 // and a fallback will be used.
585 buttonText
= i18ncp("@action", "Permanently Delete %2", "Permanently Delete %2", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
587 case BottomBar::DuplicateContents
:
588 // i18n: A more elaborate and clearly worded version of the Duplicate action
589 // %2 is a textual representation of the currently selected files or folders. This can be the name of
590 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
591 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
592 // and a fallback will be used.
593 buttonText
= i18ncp("@action", "Duplicate %2", "Duplicate %2", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
595 case BottomBar::MoveToTrashContents
:
596 // i18n: A more elaborate and clearly worded version of the Trash action
597 // %2 is a textual representation of the currently selected files or folders. This can be the name of
598 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
599 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
600 // and a fallback will be used.
601 buttonText
= i18ncp("@action", "Move %2 to the Trash", "Move %2 to the Trash", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
603 case BottomBar::RenameContents
:
604 // i18n: A more elaborate and clearly worded version of the Rename action
605 // %2 is a textual representation of the currently selected files or folders. This can be the name of
606 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
607 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
608 // and a fallback will be used.
609 buttonText
= i18ncp("@action", "Rename %2", "Rename %2", selectedItems
.count(), fileItemListToString(selectedItems
, fontMetrics
.averageCharWidth() * 20, fontMetrics
));
614 if (buttonText
!= QStringLiteral("NULL")) {
615 static_cast<QAbstractButton
*>(m_mainAction
.widget())->setText(buttonText
);
617 // The width of the button has changed. We might want to hide the label so the full button text fits on the bar.
618 updateExplanatoryLabelVisibility();