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