]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/bottombarcontentscontainer.cpp
2c924415ac7a391599085deacfc5604bea263c14
[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(i18nc("@action:button", "Abort Copying"), this);
161 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
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 leaveSelectionModeRequested();
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(i18nc("@action:button", "Abort Copying"), this);
194 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
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(i18nc("@action:button", "Abort Copying"), this);
213 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
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(i18nc("@action:button", "Abort Cutting"), this);
231 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
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 leaveSelectionModeRequested();
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(i18nc("@action:button", "Abort"), this);
264 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
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(i18nc("@action:button", "Abort Duplicating"), this);
282 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
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 m_overflowButton = new QToolButton{this};
295 // i18n: This button appears in a bar if there isn't enough horizontal space to fit all the other buttons.
296 // The small icon-only button opens a menu that contains the actions that didn't fit on the bar.
297 // Since this is an icon-only button this text will only appear as a tooltip and as accessibility text.
298 m_overflowButton->setToolTip(i18nc("@action", "More"));
299 m_overflowButton->setAccessibleName(m_overflowButton->toolTip());
300 m_overflowButton->setIcon(QIcon::fromTheme(QStringLiteral("view-more-horizontal-symbolic")));
301 m_overflowButton->setMenu(new QMenu{m_overflowButton});
302 m_overflowButton->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);
303 m_overflowButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); // Makes sure it has the same height as the labeled buttons.
304 m_layout->addWidget(m_overflowButton);
305 } else {
306 m_overflowButton->menu()->actions().clear();
307 // The overflowButton should be part of the calculation for needed space so we set it visible in regards to unusedSpace().
308 m_overflowButton->setVisible(true);
309 }
310
311 // We first add all the m_generalBarActions to the bar until the bar is full.
312 auto i = m_generalBarActions.begin();
313 for (; i != m_generalBarActions.end(); ++i) {
314 if (i->action()->isVisible()) {
315 if (i->widget()) {
316 i->widget()->setEnabled(i->action()->isEnabled());
317 } else {
318 i->newWidget(this);
319 i->widget()->setVisible(false);
320 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton
321 }
322 if (unusedSpace() < i->widget()->sizeHint().width()) {
323 break; // The bar is too full already. We keep it invisible.
324 } else {
325 i->widget()->setVisible(true);
326 }
327 }
328 }
329 // We are done adding widgets to the bar so either we were able to fit all the actions in there
330 m_overflowButton->setVisible(false);
331 // …or there are more actions left which need to be put into m_overflowButton.
332 for (; i != m_generalBarActions.end(); ++i) {
333 m_overflowButton->menu()->addAction(i->action());
334
335 // The overflowButton is set visible if there is actually an action in it.
336 if (!m_overflowButton->isVisible() && i->action()->isVisible() && !i->action()->isSeparator()) {
337 m_overflowButton->setVisible(true);
338 }
339 }
340 }
341
342 void BottomBarContentsContainer::addMoveToOtherViewContents()
343 {
344 // i18n: "Move over" refers to moving to the other split view area that is currently visible to the user.
345 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved over."), this);
346 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
347 m_explanatoryLabel->setWordWrap(true);
348 m_layout->addWidget(m_explanatoryLabel);
349
350 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode.
351 auto *cancelButton = new QPushButton(i18nc("@action:button", "Abort Moving"), this);
352 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
353 m_layout->addWidget(cancelButton);
354
355 auto *moveToOtherViewButton = new QPushButton(this);
356 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("move_to_inactive_split_view")), moveToOtherViewButton);
357 updateMainActionButton(KFileItemList());
358 m_layout->addWidget(moveToOtherViewButton);
359 }
360
361 void BottomBarContentsContainer::addMoveToTrashContents()
362 {
363 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved to the Trash."), this);
364 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
365 m_explanatoryLabel->setWordWrap(true);
366 m_layout->addWidget(m_explanatoryLabel);
367
368 // i18n: Aborts the current step-by-step process of moving files to the trash by leaving the selection mode.
369 auto *cancelButton = new QPushButton(i18nc("@action:button", "Abort"), this);
370 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
371 m_layout->addWidget(cancelButton);
372
373 auto *moveToTrashButton = new QPushButton(this);
374 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)), moveToTrashButton);
375 updateMainActionButton(KFileItemList());
376 m_layout->addWidget(moveToTrashButton);
377 }
378
379 void BottomBarContentsContainer::addPasteContents()
380 {
381 m_explanatoryLabel = new QLabel(xi18n("<para>The selected files and folders were added to the Clipboard. "
382 "Now the <emphasis>Paste</emphasis> action can be used to transfer them from the Clipboard "
383 "to any other location. They can even be transferred to other applications by using their "
384 "respective <emphasis>Paste</emphasis> actions.</para>"), this);
385 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
386 m_explanatoryLabel->setWordWrap(true);
387 m_layout->addWidget(m_explanatoryLabel);
388
389 auto *vBoxLayout = new QVBoxLayout(this);
390 m_layout->addLayout(vBoxLayout);
391
392 /** We are in "PasteContents" mode which means hiding the bottom bar is impossible.
393 * So we first have to claim that we have different contents before requesting to leave selection mode. */
394 auto actuallyLeaveSelectionMode = [this]() {
395 m_contents = BottomBar::Contents::CopyLocationContents;
396 Q_EMIT leaveSelectionModeRequested();
397 };
398
399 auto *pasteButton = new QPushButton(this);
400 copyActionDataToButton(pasteButton, m_actionCollection->action(KStandardAction::name(KStandardAction::Paste)));
401 pasteButton->setText(i18nc("@action A more elaborate and clearly worded version of the Paste action", "Paste from Clipboard"));
402 connect(pasteButton, &QAbstractButton::clicked, this, actuallyLeaveSelectionMode);
403 vBoxLayout->addWidget(pasteButton);
404
405 auto *dismissButton = new QToolButton(this);
406 dismissButton->setText(i18nc("@action Dismisses a bar explaining how to use the Paste action", "Dismiss This Reminder"));
407 connect(dismissButton, &QAbstractButton::clicked, this, actuallyLeaveSelectionMode);
408 auto *dontRemindAgainAction = new QAction(i18nc("@action Dismisses an explanatory area and never shows it again", "Don't Remind Me Again"), this);
409 connect(dontRemindAgainAction, &QAction::triggered, this, []() {
410 GeneralSettings::setShowPasteBarAfterCopying(false);
411 });
412 connect(dontRemindAgainAction, &QAction::triggered, this, actuallyLeaveSelectionMode);
413 auto *dismissButtonMenu = new QMenu(dismissButton);
414 dismissButtonMenu->addAction(dontRemindAgainAction);
415 dismissButton->setMenu(dismissButtonMenu);
416 dismissButton->setPopupMode(QToolButton::MenuButtonPopup);
417 vBoxLayout->addWidget(dismissButton);
418
419 m_explanatoryLabel->setMaximumHeight(pasteButton->sizeHint().height() + dismissButton->sizeHint().height() + m_explanatoryLabel->fontMetrics().height());
420 }
421
422 void BottomBarContentsContainer::addRenameContents()
423 {
424 m_explanatoryLabel = new QLabel(i18nc("@info explains the next step in a process", "Select the file or folder that should be renamed.\nBulk renaming is possible when multiple items are selected."), this);
425 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
426 m_explanatoryLabel->setWordWrap(true);
427 m_layout->addWidget(m_explanatoryLabel);
428
429 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode.
430 auto *cancelButton = new QPushButton(i18nc("@action:button", "Stop Renaming"), this);
431 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::leaveSelectionModeRequested);
432 m_layout->addWidget(cancelButton);
433
434 auto *renameButton = new QPushButton(this);
435 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)), renameButton);
436 updateMainActionButton(KFileItemList());
437 m_layout->addWidget(renameButton);
438 }
439
440 void BottomBarContentsContainer::emptyBarContents()
441 {
442 QLayoutItem *child;
443 while ((child = m_layout->takeAt(0)) != nullptr) {
444 if (auto *childLayout = child->layout()) {
445 QLayoutItem *grandChild;
446 while ((grandChild = childLayout->takeAt(0)) != nullptr) {
447 delete grandChild->widget(); // delete the widget
448 delete grandChild; // delete the layout item
449 }
450 }
451 delete child->widget(); // delete the widget
452 delete child; // delete the layout item
453 }
454 }
455
456 std::vector<QAction *> BottomBarContentsContainer::contextActionsFor(const KFileItemList& selectedItems, const QUrl& baseUrl)
457 {
458 if (selectedItems.isEmpty()) {
459 // There are no contextual actions to show for these items.
460 // We might even want to hide this bar in this case. To make this clear, we reset m_internalContextMenu.
461 m_internalContextMenu.release()->deleteLater();
462 return std::vector<QAction *>{};
463 }
464
465 std::vector<QAction *> contextActions;
466
467 // We always want to show the most important actions at the beginning
468 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::Copy)));
469 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::Cut)));
470 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)));
471 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)));
472
473 // We are going to add the actions from the right-click context menu for the selected items.
474 auto *dolphinMainWindow = qobject_cast<DolphinMainWindow *>(window());
475 Q_CHECK_PTR(dolphinMainWindow);
476 if (!m_fileItemActions) {
477 m_fileItemActions = new KFileItemActions(this);
478 m_fileItemActions->setParentWidget(dolphinMainWindow);
479 connect(m_fileItemActions, &KFileItemActions::error, this, &BottomBarContentsContainer::error);
480 }
481 m_internalContextMenu = std::make_unique<DolphinContextMenu>(dolphinMainWindow, selectedItems.constFirst(), selectedItems, baseUrl, m_fileItemActions);
482 auto internalContextMenuActions = m_internalContextMenu->actions();
483
484 // There are some actions which we wouldn't want to add. We remember them in the actionsThatShouldntBeAdded set.
485 // We don't want to add the four basic actions again which were already added to the top.
486 std::unordered_set<QAction *> actionsThatShouldntBeAdded{contextActions.begin(), contextActions.end()};
487 // "Delete" isn't really necessary to add because we have "Move to Trash" already. It is also more dangerous so let's exclude it.
488 actionsThatShouldntBeAdded.insert(m_actionCollection->action(KStandardAction::name(KStandardAction::DeleteFile)));
489
490 // KHamburgerMenu would only be visible if there is no menu available anywhere on the user interface. This might be useful for recovery from
491 // such a situation in theory but a bar with context dependent actions doesn't really seem like the right place for it.
492 Q_ASSERT(internalContextMenuActions.first()->icon().name() == m_actionCollection->action(KStandardAction::name(KStandardAction::HamburgerMenu))->icon().name());
493 internalContextMenuActions.removeFirst();
494
495 for (auto it = internalContextMenuActions.constBegin(); it != internalContextMenuActions.constEnd(); ++it) {
496 if (actionsThatShouldntBeAdded.count(*it)) {
497 continue; // Skip this action.
498 }
499 if (!qobject_cast<DolphinRemoveAction *>(*it)) { // We already have a "Move to Trash" action so we don't want a DolphinRemoveAction.
500 // We filter duplicate separators here so we won't have to deal with them later.
501 if (!contextActions.back()->isSeparator() || !(*it)->isSeparator()) {
502 contextActions.emplace_back((*it));
503 }
504 }
505 }
506 return contextActions;
507 }
508
509 int BottomBarContentsContainer::unusedSpace() const
510 {
511 int sumOfPreferredWidths = m_layout->contentsMargins().left() + m_layout->contentsMargins().right();
512 if (m_overflowButton) {
513 sumOfPreferredWidths += m_overflowButton->sizeHint().width();
514 }
515 for (int i = 0; i < m_layout->count(); ++i) {
516 auto widget = m_layout->itemAt(i)->widget();
517 if (widget && !widget->isVisibleTo(widget->parentWidget())) {
518 continue; // We don't count invisible widgets.
519 }
520 sumOfPreferredWidths += m_layout->itemAt(i)->sizeHint().width() + m_layout->spacing();
521 }
522 return m_barWidth - sumOfPreferredWidths - 20; // We consider all space used when there are only 20 pixels left
523 // so there is some room to breath and not too much wonkyness while resizing.
524 }
525
526 void BottomBarContentsContainer::updateExplanatoryLabelVisibility()
527 {
528 if (!m_explanatoryLabel) {
529 return;
530 }
531 if (m_explanatoryLabel->isVisible()) {
532 m_explanatoryLabel->setVisible(unusedSpace() > 0);
533 } else {
534 // We only want to re-show the label when it fits comfortably so the computation below adds another "+20".
535 m_explanatoryLabel->setVisible(unusedSpace() > m_explanatoryLabel->sizeHint().width() + 20);
536 }
537 }
538
539 void BottomBarContentsContainer::updateMainActionButton(const KFileItemList& selectedItems)
540 {
541 if (!m_mainAction.widget()) {
542 return;
543 }
544 Q_ASSERT(qobject_cast<QAbstractButton *>(m_mainAction.widget()));
545
546 // Users are nudged towards selecting items by having the button disabled when nothing is selected.
547 m_mainAction.widget()->setEnabled(selectedItems.count() > 0 && m_mainAction.action()->isEnabled());
548 QFontMetrics fontMetrics = m_mainAction.widget()->fontMetrics();
549
550 QString buttonText;
551 switch (m_contents) {
552 case BottomBar::CopyContents:
553 // i18n: A more elaborate and clearly worded version of the Copy action
554 // %2 is a textual representation of the currently selected files or folders. This can be the name of
555 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
556 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
557 // and a fallback will be used.
558 buttonText = i18ncp("@action", "Copy %2 to the Clipboard", "Copy %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
559 // All these long lines can not be broken up with line breaks becaue the i18n call should be completely
560 // in the line following the "i18n:" comment without any line breaks within the i18n call
561 // or the comment might not be correctly extracted. See: https://commits.kde.org/kxmlgui/a31135046e1b3335b5d7bbbe6aa9a883ce3284c1
562 break;
563 case BottomBar::CopyLocationContents:
564 // i18n: A more elaborate and clearly worded version of the Copy Location action
565 // %2 is a textual representation of the currently selected files or folders. This can be the name of
566 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
567 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
568 // and a fallback will be used.
569 buttonText = i18ncp("@action", "Copy the Location of %2 to the Clipboard", "Copy the Location of %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
570 break;
571 case BottomBar::CutContents:
572 // i18n: A more elaborate and clearly worded version of the Cut action
573 // %2 is a textual representation of the currently selected files or folders. This can be the name of
574 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
575 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
576 // and a fallback will be used.
577 buttonText = i18ncp("@action", "Cut %2 to the Clipboard", "Cut %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
578 break;
579 case BottomBar::DeleteContents:
580 // i18n: A more elaborate and clearly worded version of the Delete action
581 // %2 is a textual representation of the currently selected files or folders. This can be the name of
582 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
583 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
584 // and a fallback will be used.
585 buttonText = i18ncp("@action", "Permanently Delete %2", "Permanently Delete %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
586 break;
587 case BottomBar::DuplicateContents:
588 // i18n: A more elaborate and clearly worded version of the Duplicate action
589 // %2 is a textual representation of the currently selected files or folders. This can be the name of
590 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
591 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
592 // and a fallback will be used.
593 buttonText = i18ncp("@action", "Duplicate %2", "Duplicate %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
594 break;
595 case BottomBar::MoveToTrashContents:
596 // i18n: A more elaborate and clearly worded version of the Trash action
597 // %2 is a textual representation of the currently selected files or folders. This can be the name of
598 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
599 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
600 // and a fallback will be used.
601 buttonText = i18ncp("@action", "Move %2 to the Trash", "Move %2 to the Trash", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
602 break;
603 case BottomBar::RenameContents:
604 // i18n: A more elaborate and clearly worded version of the Rename action
605 // %2 is a textual representation of the currently selected files or folders. This can be the name of
606 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders".
607 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes)
608 // and a fallback will be used.
609 buttonText = i18ncp("@action", "Rename %2", "Rename %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics));
610 break;
611 default:
612 return;
613 }
614 if (buttonText != QStringLiteral("NULL")) {
615 static_cast<QAbstractButton *>(m_mainAction.widget())->setText(buttonText);
616
617 // The width of the button has changed. We might want to hide the label so the full button text fits on the bar.
618 updateExplanatoryLabelVisibility();
619 }
620 }