]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/bottombarcontentscontainer.cpp
Address Nate's UX feedback: Episode 2
[dolphin.git] / src / selectionmode / bottombarcontentscontainer.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 "bottombarcontentscontainer.h"
9
10 #include "dolphin_generalsettings.h"
11 #include "dolphincontextmenu.h"
12 #include "dolphinmainwindow.h"
13 #include "dolphinremoveaction.h"
14 #include "kitemviews/kfileitemlisttostring.h"
15
16 #include <KLocalizedString>
17
18 #include <QHBoxLayout>
19 #include <QLabel>
20 #include <QMenu>
21 #include <QPushButton>
22 #include <QToolButton>
23 #include <QVBoxLayout>
24
25 #include <unordered_set>
26
27 using namespace SelectionMode;
28
29 BottomBarContentsContainer::BottomBarContentsContainer(KActionCollection *actionCollection, QWidget *parent) :
30 QWidget{parent},
31 m_actionCollection{actionCollection}
32 {
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);
35 }
36
37 void BottomBarContentsContainer::resetContents(BottomBar::Contents contents)
38 {
39 emptyBarContents();
40
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(); });
43
44 Q_CHECK_PTR(m_actionCollection);
45 m_contents = contents;
46 switch (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();
69 }
70 }
71
72 void BottomBarContentsContainer::adaptToNewBarWidth(int newBarWidth)
73 {
74 m_barWidth = newBarWidth;
75
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()) {
82 continue;
83 }
84 i->widget()->setVisible(false);
85
86 // Add the action to the overflow.
87 auto overflowMenu = m_overflowButton->menu();
88 if (overflowMenu->actions().isEmpty()) {
89 overflowMenu->addAction(i->action());
90 } else {
91 overflowMenu->insertAction(overflowMenu->actions().at(0), i->action());
92 }
93 m_overflowButton->setVisible(true);
94 if (unusedSpace() >= 0) {
95 break; // All widgets fit now.
96 }
97 }
98 } else {
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()) {
102 continue;
103 }
104 if (!i->widget()) {
105 i->newWidget(this);
106 i->widget()->setVisible(false);
107 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton
108 }
109 if (unusedSpace() < i->widget()->sizeHint().width()) {
110 // It doesn't fit. We keep it invisible.
111 break;
112 }
113 i->widget()->setVisible(true);
114
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);
120 }
121 }
122 }
123 }
124
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();
127 }
128
129 void BottomBarContentsContainer::slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl)
130 {
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);
139 }
140 } else {
141 for (auto i = contextActions.begin(); i != contextActions.end(); ++i) {
142 m_generalBarActions.emplace_back(ActionWithWidget{*i});
143 }
144 resetContents(BottomBar::GeneralContents);
145
146 Q_EMIT barVisibilityChangeRequested(true);
147 }
148 }
149 updateMainActionButton(selection);
150 }
151
152 void BottomBarContentsContainer::addCopyContents()
153 {
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);
158
159 // i18n: Aborts the current step-by-step process to copy files by leaving the selection mode.
160 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this);
161 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
162 m_layout->addWidget(cancelButton);
163
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
169 }
170 });
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.
178 }
179 Q_EMIT selectionModeLeavingRequested();
180 });
181 updateMainActionButton(KFileItemList());
182 m_layout->addWidget(copyButton);
183 }
184
185 void BottomBarContentsContainer::addCopyLocationContents()
186 {
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);
191
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(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this);
194 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
195 m_layout->addWidget(cancelButton);
196
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);
201 }
202
203 void BottomBarContentsContainer::addCopyToOtherViewContents()
204 {
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);
210
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(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this);
213 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
214 m_layout->addWidget(cancelButton);
215
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);
220 }
221
222 void BottomBarContentsContainer::addCutContents()
223 {
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);
228
229 // i18n: Aborts the current step-by-step process to cut files by leaving the selection mode.
230 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Cutting"), this);
231 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
232 m_layout->addWidget(cancelButton);
233
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
239 }
240 });
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.
248 }
249 Q_EMIT selectionModeLeavingRequested();
250 });
251 updateMainActionButton(KFileItemList());
252 m_layout->addWidget(cutButton);
253 }
254
255 void BottomBarContentsContainer::addDeleteContents()
256 {
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);
261
262 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
263 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel"), this);
264 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
265 m_layout->addWidget(cancelButton);
266
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);
271 }
272
273 void BottomBarContentsContainer::addDuplicateContents()
274 {
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);
279
280 // i18n: Aborts the current step-by-step process to duplicate files by leaving the selection mode.
281 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Duplicating"), this);
282 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
283 m_layout->addWidget(cancelButton);
284
285 auto *duplicateButton = new QPushButton(this);
286 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("duplicate")), duplicateButton);
287 updateMainActionButton(KFileItemList());
288 m_layout->addWidget(duplicateButton);
289 }
290
291 void BottomBarContentsContainer::addGeneralContents()
292 {
293 if (!m_overflowButton) {
294 // 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.
295 // The small button opens a menu that contains the actions that didn't fit on the bar.
296 m_overflowButton = new QPushButton{QIcon::fromTheme(QStringLiteral("view-more-symbolic")), i18nc("@action keep short", "More"), this};
297 m_overflowButton->setMenu(new QMenu{m_overflowButton});
298 m_overflowButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); // Makes sure it has the same height as the labeled buttons.
299 m_layout->addWidget(m_overflowButton);
300 } else {
301 m_overflowButton->menu()->actions().clear();
302 // The overflowButton should be part of the calculation for needed space so we set it visible in regards to unusedSpace().
303 m_overflowButton->setVisible(true);
304 }
305
306 // We first add all the m_generalBarActions to the bar until the bar is full.
307 auto i = m_generalBarActions.begin();
308 for (; i != m_generalBarActions.end(); ++i) {
309 if (i->action()->isVisible()) {
310 if (i->widget()) {
311 i->widget()->setEnabled(i->action()->isEnabled());
312 } else {
313 i->newWidget(this);
314 i->widget()->setVisible(false);
315 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton
316 }
317 if (unusedSpace() < i->widget()->sizeHint().width()) {
318 break; // The bar is too full already. We keep it invisible.
319 } else {
320 i->widget()->setVisible(true);
321 }
322 }
323 }
324 // We are done adding widgets to the bar so either we were able to fit all the actions in there
325 m_overflowButton->setVisible(false);
326 // …or there are more actions left which need to be put into m_overflowButton.
327 for (; i != m_generalBarActions.end(); ++i) {
328 m_overflowButton->menu()->addAction(i->action());
329
330 // The overflowButton is set visible if there is actually an action in it.
331 if (!m_overflowButton->isVisible() && i->action()->isVisible() && !i->action()->isSeparator()) {
332 m_overflowButton->setVisible(true);
333 }
334 }
335 }
336
337 void BottomBarContentsContainer::addMoveToOtherViewContents()
338 {
339 // i18n: "Move over" refers to moving 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 moved 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(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Moving"), this);
347 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
348 m_layout->addWidget(cancelButton);
349
350 auto *moveToOtherViewButton = new QPushButton(this);
351 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("move_to_inactive_split_view")), moveToOtherViewButton);
352 updateMainActionButton(KFileItemList());
353 m_layout->addWidget(moveToOtherViewButton);
354 }
355
356 void BottomBarContentsContainer::addMoveToTrashContents()
357 {
358 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);
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 of moving files to the trash by leaving the selection mode.
364 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel"), this);
365 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
366 m_layout->addWidget(cancelButton);
367
368 auto *moveToTrashButton = new QPushButton(this);
369 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)), moveToTrashButton);
370 updateMainActionButton(KFileItemList());
371 m_layout->addWidget(moveToTrashButton);
372 }
373
374 void BottomBarContentsContainer::addPasteContents()
375 {
376 m_explanatoryLabel = new QLabel(xi18n("<para>The selected files and folders were added to the Clipboard. "
377 "Now the <emphasis>Paste</emphasis> action can be used to transfer them from the Clipboard "
378 "to any other location. They can even be transferred to other applications by using their "
379 "respective <emphasis>Paste</emphasis> actions.</para>"), this);
380 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
381 m_explanatoryLabel->setWordWrap(true);
382 m_layout->addWidget(m_explanatoryLabel);
383
384 auto *vBoxLayout = new QVBoxLayout(this);
385 m_layout->addLayout(vBoxLayout);
386
387 /** We are in "PasteContents" mode which means hiding the bottom bar is impossible.
388 * So we first have to claim that we have different contents before requesting to leave selection mode. */
389 auto actuallyLeaveSelectionMode = [this]() {
390 m_contents = BottomBar::Contents::CopyLocationContents;
391 Q_EMIT selectionModeLeavingRequested();
392 };
393
394 auto *pasteButton = new QPushButton(this);
395 copyActionDataToButton(pasteButton, m_actionCollection->action(KStandardAction::name(KStandardAction::Paste)));
396 pasteButton->setText(i18nc("@action A more elaborate and clearly worded version of the Paste action", "Paste from Clipboard"));
397 connect(pasteButton, &QAbstractButton::clicked, this, actuallyLeaveSelectionMode);
398 vBoxLayout->addWidget(pasteButton);
399
400 auto *dismissButton = new QToolButton(this);
401 dismissButton->setText(i18nc("@action Dismisses a bar explaining how to use the Paste action", "Dismiss This Reminder"));
402 connect(dismissButton, &QAbstractButton::clicked, this, actuallyLeaveSelectionMode);
403 auto *dontRemindAgainAction = new QAction(i18nc("@action Dismisses an explanatory area and never shows it again", "Don't Remind Me Again"), this);
404 connect(dontRemindAgainAction, &QAction::triggered, this, []() {
405 GeneralSettings::setShowPasteBarAfterCopying(false);
406 });
407 connect(dontRemindAgainAction, &QAction::triggered, this, actuallyLeaveSelectionMode);
408 auto *dismissButtonMenu = new QMenu(dismissButton);
409 dismissButtonMenu->addAction(dontRemindAgainAction);
410 dismissButton->setMenu(dismissButtonMenu);
411 dismissButton->setPopupMode(QToolButton::MenuButtonPopup);
412 vBoxLayout->addWidget(dismissButton);
413
414 m_explanatoryLabel->setMaximumHeight(pasteButton->sizeHint().height() + dismissButton->sizeHint().height() + m_explanatoryLabel->fontMetrics().height());
415 }
416
417 void BottomBarContentsContainer::addRenameContents()
418 {
419 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);
420 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
421 m_explanatoryLabel->setWordWrap(true);
422 m_layout->addWidget(m_explanatoryLabel);
423
424 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
425 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Renaming"), this);
426 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested);
427 m_layout->addWidget(cancelButton);
428
429 auto *renameButton = new QPushButton(this);
430 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)), renameButton);
431 updateMainActionButton(KFileItemList());
432 m_layout->addWidget(renameButton);
433 }
434
435 void BottomBarContentsContainer::emptyBarContents()
436 {
437 QLayoutItem *child;
438 while ((child = m_layout->takeAt(0)) != nullptr) {
439 if (auto *childLayout = child->layout()) {
440 QLayoutItem *grandChild;
441 while ((grandChild = childLayout->takeAt(0)) != nullptr) {
442 delete grandChild->widget(); // delete the widget
443 delete grandChild; // delete the layout item
444 }
445 }
446 delete child->widget(); // delete the widget
447 delete child; // delete the layout item
448 }
449 }
450
451 std::vector<QAction *> BottomBarContentsContainer::contextActionsFor(const KFileItemList& selectedItems, const QUrl& baseUrl)
452 {
453 if (selectedItems.isEmpty()) {
454 // There are no contextual actions to show for these items.
455 // We might even want to hide this bar in this case. To make this clear, we reset m_internalContextMenu.
456 m_internalContextMenu.release()->deleteLater();
457 return std::vector<QAction *>{};
458 }
459
460 std::vector<QAction *> contextActions;
461
462 // We always want to show the most important actions at the beginning
463 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::Copy)));
464 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::Cut)));
465 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)));
466 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)));
467
468 // We are going to add the actions from the right-click context menu for the selected items.
469 auto *dolphinMainWindow = qobject_cast<DolphinMainWindow *>(window());
470 Q_CHECK_PTR(dolphinMainWindow);
471 if (!m_fileItemActions) {
472 m_fileItemActions = new KFileItemActions(this);
473 m_fileItemActions->setParentWidget(dolphinMainWindow);
474 connect(m_fileItemActions, &KFileItemActions::error, this, &BottomBarContentsContainer::error);
475 }
476 m_internalContextMenu = std::make_unique<DolphinContextMenu>(dolphinMainWindow, selectedItems.constFirst(), selectedItems, baseUrl, m_fileItemActions);
477 auto internalContextMenuActions = m_internalContextMenu->actions();
478
479 // There are some actions which we wouldn't want to add. We remember them in the actionsThatShouldntBeAdded set.
480 // We don't want to add the four basic actions again which were already added to the top.
481 std::unordered_set<QAction *> actionsThatShouldntBeAdded{contextActions.begin(), contextActions.end()};
482 // "Delete" isn't really necessary to add because we have "Move to Trash" already. It is also more dangerous so let's exclude it.
483 actionsThatShouldntBeAdded.insert(m_actionCollection->action(KStandardAction::name(KStandardAction::DeleteFile)));
484
485 // KHamburgerMenu would only be visible if there is no menu available anywhere on the user interface. This might be useful for recovery from
486 // such a situation in theory but a bar with context dependent actions doesn't really seem like the right place for it.
487 Q_ASSERT(internalContextMenuActions.first()->icon().name() == m_actionCollection->action(KStandardAction::name(KStandardAction::HamburgerMenu))->icon().name());
488 internalContextMenuActions.removeFirst();
489
490 for (auto it = internalContextMenuActions.constBegin(); it != internalContextMenuActions.constEnd(); ++it) {
491 if (actionsThatShouldntBeAdded.count(*it)) {
492 continue; // Skip this action.
493 }
494 if (!qobject_cast<DolphinRemoveAction *>(*it)) { // We already have a "Move to Trash" action so we don't want a DolphinRemoveAction.
495 // We filter duplicate separators here so we won't have to deal with them later.
496 if (!contextActions.back()->isSeparator() || !(*it)->isSeparator()) {
497 contextActions.emplace_back((*it));
498 }
499 }
500 }
501 return contextActions;
502 }
503
504 int BottomBarContentsContainer::unusedSpace() const
505 {
506 int sumOfPreferredWidths = m_layout->contentsMargins().left() + m_layout->contentsMargins().right();
507 if (m_overflowButton) {
508 sumOfPreferredWidths += m_overflowButton->sizeHint().width();
509 }
510 for (int i = 0; i < m_layout->count(); ++i) {
511 auto widget = m_layout->itemAt(i)->widget();
512 if (widget && !widget->isVisibleTo(widget->parentWidget())) {
513 continue; // We don't count invisible widgets.
514 }
515 sumOfPreferredWidths += m_layout->itemAt(i)->sizeHint().width() + m_layout->spacing();
516 }
517 return m_barWidth - sumOfPreferredWidths - 20; // We consider all space used when there are only 20 pixels left
518 // so there is some room to breath and not too much wonkyness while resizing.
519 }
520
521 void BottomBarContentsContainer::updateExplanatoryLabelVisibility()
522 {
523 if (!m_explanatoryLabel) {
524 return;
525 }
526 if (m_explanatoryLabel->isVisible()) {
527 m_explanatoryLabel->setVisible(unusedSpace() > 0);
528 } else {
529 // We only want to re-show the label when it fits comfortably so the computation below adds another "+20".
530 m_explanatoryLabel->setVisible(unusedSpace() > m_explanatoryLabel->sizeHint().width() + 20);
531 }
532 }
533
534 void BottomBarContentsContainer::updateMainActionButton(const KFileItemList& selectedItems)
535 {
536 if (!m_mainAction.widget()) {
537 return;
538 }
539 Q_ASSERT(qobject_cast<QAbstractButton *>(m_mainAction.widget()));
540
541 // Users are nudged towards selecting items by having the button disabled when nothing is selected.
542 m_mainAction.widget()->setEnabled(selectedItems.count() > 0 && m_mainAction.action()->isEnabled());
543 QFontMetrics fontMetrics = m_mainAction.widget()->fontMetrics();
544
545 QString buttonText;
546 switch (m_contents) {
547 case BottomBar::CopyContents:
548 // i18n: A more elaborate and clearly worded version of the Copy action
549 // %2 is a textual representation of the currently selected files or folders. This can be the name of
550 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
551 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
552 // and a fallback will be used.
553 buttonText = i18ncp("@action", "Copy %2 to the Clipboard", "Copy %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
554 // All these long lines can not be broken up with line breaks becaue the i18n call should be completely
555 // in the line following the "i18n:" comment without any line breaks within the i18n call
556 // or the comment might not be correctly extracted. See: https://commits.kde.org/kxmlgui/a31135046e1b3335b5d7bbbe6aa9a883ce3284c1
557 break;
558 case BottomBar::CopyLocationContents:
559 // i18n: A more elaborate and clearly worded version of the Copy Location action
560 // %2 is a textual representation of the currently selected files or folders. This can be the name of
561 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
562 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
563 // and a fallback will be used.
564 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));
565 break;
566 case BottomBar::CutContents:
567 // i18n: A more elaborate and clearly worded version of the Cut action
568 // %2 is a textual representation of the currently selected files or folders. This can be the name of
569 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
570 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
571 // and a fallback will be used.
572 buttonText = i18ncp("@action", "Cut %2 to the Clipboard", "Cut %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
573 break;
574 case BottomBar::DeleteContents:
575 // i18n: A more elaborate and clearly worded version of the Delete action
576 // %2 is a textual representation of the currently selected files or folders. This can be the name of
577 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
578 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
579 // and a fallback will be used.
580 buttonText = i18ncp("@action", "Permanently Delete %2", "Permanently Delete %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
581 break;
582 case BottomBar::DuplicateContents:
583 // i18n: A more elaborate and clearly worded version of the Duplicate action
584 // %2 is a textual representation of the currently selected files or folders. This can be the name of
585 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
586 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
587 // and a fallback will be used.
588 buttonText = i18ncp("@action", "Duplicate %2", "Duplicate %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
589 break;
590 case BottomBar::MoveToTrashContents:
591 // i18n: A more elaborate and clearly worded version of the Trash action
592 // %2 is a textual representation of the currently selected files or folders. This can be the name of
593 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
594 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
595 // and a fallback will be used.
596 buttonText = i18ncp("@action", "Move %2 to the Trash", "Move %2 to the Trash", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
597 break;
598 case BottomBar::RenameContents:
599 // i18n: A more elaborate and clearly worded version of the Rename action
600 // %2 is a textual representation of the currently selected files or folders. This can be the name of
601 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
602 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
603 // and a fallback will be used.
604 buttonText = i18ncp("@action", "Rename %2", "Rename %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
605 break;
606 default:
607 return;
608 }
609 if (buttonText != QStringLiteral("NULL")) {
610 static_cast<QAbstractButton *>(m_mainAction.widget())->setText(buttonText);
611
612 // The width of the button has changed. We might want to hide the label so the full button text fits on the bar.
613 updateExplanatoryLabelVisibility();
614 }
615 }