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