File indexing completed on 2024-04-21 05:45:27
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "bottombarcontentscontainer.h" 0009 0010 #include "dolphin_generalsettings.h" 0011 #include "dolphincontextmenu.h" 0012 #include "dolphinmainwindow.h" 0013 #include "dolphinremoveaction.h" 0014 #include "kitemviews/kfileitemlisttostring.h" 0015 0016 #include <KLocalizedString> 0017 0018 #include <QHBoxLayout> 0019 #include <QLabel> 0020 #include <QMenu> 0021 #include <QToolButton> 0022 #include <QVBoxLayout> 0023 0024 #include <unordered_set> 0025 0026 using namespace SelectionMode; 0027 0028 BottomBarContentsContainer::BottomBarContentsContainer(KActionCollection *actionCollection, QWidget *parent) 0029 : QWidget{parent} 0030 , m_actionCollection{actionCollection} 0031 { 0032 // We will mostly interact with m_layout when changing the contents and not care about the other internal hierarchy. 0033 m_layout = new QHBoxLayout(this); 0034 } 0035 0036 void BottomBarContentsContainer::resetContents(BottomBar::Contents contents) 0037 { 0038 emptyBarContents(); 0039 0040 // A label is added in many of the methods below. We only know its size a bit later and if it should be hidden. 0041 QTimer::singleShot(10, this, [this]() { 0042 updateExplanatoryLabelVisibility(); 0043 }); 0044 0045 Q_CHECK_PTR(m_actionCollection); 0046 m_contents = contents; 0047 switch (contents) { 0048 case BottomBar::CopyContents: 0049 return addCopyContents(); 0050 case BottomBar::CopyLocationContents: 0051 return addCopyLocationContents(); 0052 case BottomBar::CopyToOtherViewContents: 0053 return addCopyToOtherViewContents(); 0054 case BottomBar::CutContents: 0055 return addCutContents(); 0056 case BottomBar::DeleteContents: 0057 return addDeleteContents(); 0058 case BottomBar::DuplicateContents: 0059 return addDuplicateContents(); 0060 case BottomBar::GeneralContents: 0061 return addGeneralContents(); 0062 case BottomBar::PasteContents: 0063 return addPasteContents(); 0064 case BottomBar::MoveToOtherViewContents: 0065 return addMoveToOtherViewContents(); 0066 case BottomBar::MoveToTrashContents: 0067 return addMoveToTrashContents(); 0068 case BottomBar::RenameContents: 0069 return addRenameContents(); 0070 } 0071 } 0072 0073 void BottomBarContentsContainer::adaptToNewBarWidth(int newBarWidth) 0074 { 0075 m_barWidth = newBarWidth; 0076 0077 if (m_contents == BottomBar::GeneralContents) { 0078 Q_ASSERT(m_overflowButton); 0079 if (unusedSpace() < 0) { 0080 // The bottom bar is overflowing! We need to hide some of the widgets. 0081 for (auto i = m_generalBarActions.rbegin(); i != m_generalBarActions.rend(); ++i) { 0082 if (!i->isWidgetVisible()) { 0083 continue; 0084 } 0085 i->widget()->setVisible(false); 0086 0087 // Add the action to the overflow. 0088 auto overflowMenu = m_overflowButton->menu(); 0089 if (overflowMenu->actions().isEmpty()) { 0090 overflowMenu->addAction(i->action()); 0091 } else { 0092 overflowMenu->insertAction(overflowMenu->actions().at(0), i->action()); 0093 } 0094 m_overflowButton->setVisible(true); 0095 if (unusedSpace() >= 0) { 0096 break; // All widgets fit now. 0097 } 0098 } 0099 } else { 0100 // We have some unusedSpace(). Let's check if we can maybe add more of the contextual actions' widgets. 0101 for (auto i = m_generalBarActions.begin(); i != m_generalBarActions.end(); ++i) { 0102 if (i->isWidgetVisible()) { 0103 continue; 0104 } 0105 if (!i->widget()) { 0106 i->newWidget(this); 0107 i->widget()->setVisible(false); 0108 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton 0109 } 0110 if (unusedSpace() < i->widget()->sizeHint().width()) { 0111 // It doesn't fit. We keep it invisible. 0112 break; 0113 } 0114 i->widget()->setVisible(true); 0115 0116 // Remove the action from the overflow. 0117 auto overflowMenu = m_overflowButton->menu(); 0118 overflowMenu->removeAction(i->action()); 0119 if (overflowMenu->isEmpty()) { 0120 m_overflowButton->setVisible(false); 0121 } 0122 } 0123 } 0124 } 0125 0126 // Hide the leading explanation if it doesn't fit. The buttons are labeled clear enough that this shouldn't be a big UX problem. 0127 updateExplanatoryLabelVisibility(); 0128 } 0129 0130 void BottomBarContentsContainer::slotSelectionChanged(const KFileItemList &selection, const QUrl &baseUrl) 0131 { 0132 if (m_contents == BottomBar::GeneralContents) { 0133 auto contextActions = contextActionsFor(selection, baseUrl); 0134 m_generalBarActions.clear(); 0135 if (contextActions.empty()) { 0136 // We have nothing to show 0137 Q_ASSERT(qobject_cast<BottomBar *>(parentWidget()->parentWidget()->parentWidget())); 0138 if (isVisibleTo(parentWidget()->parentWidget()->parentWidget()->parentWidget())) { // is the bar visible 0139 Q_EMIT barVisibilityChangeRequested(false); 0140 } 0141 } else { 0142 for (auto i = contextActions.begin(); i != contextActions.end(); ++i) { 0143 m_generalBarActions.emplace_back(ActionWithWidget{*i}); 0144 } 0145 resetContents(BottomBar::GeneralContents); 0146 0147 Q_EMIT barVisibilityChangeRequested(true); 0148 } 0149 } 0150 updateMainActionButton(selection); 0151 } 0152 0153 void BottomBarContentsContainer::addCopyContents() 0154 { 0155 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied."), this); 0156 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0157 m_explanatoryLabel->setWordWrap(true); 0158 m_layout->addWidget(m_explanatoryLabel); 0159 0160 // clang-format off 0161 // i18n: Aborts the current step-by-step process to copy files by leaving the selection mode. 0162 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this); 0163 // clang-format on 0164 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0165 m_layout->addWidget(cancelButton); 0166 0167 auto *copyButton = new QPushButton(this); 0168 // We claim to have PasteContents already so triggering the copy action next won't instantly hide the bottom bar. 0169 connect(copyButton, &QAbstractButton::clicked, this, [this]() { 0170 if (GeneralSettings::showPasteBarAfterCopying()) { 0171 m_contents = BottomBar::Contents::PasteContents; // prevents hiding 0172 } 0173 }); 0174 // Connect the copy action as a second step. 0175 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::Copy)), copyButton); 0176 // Finally connect the lambda that actually changes the contents to the PasteContents. 0177 connect(copyButton, &QAbstractButton::clicked, this, [this]() { 0178 if (GeneralSettings::showPasteBarAfterCopying()) { 0179 resetContents(BottomBar::Contents::PasteContents); // resetContents() needs to be connected last because 0180 // it instantly deletes the button and then the other slots won't be called. 0181 } 0182 Q_EMIT selectionModeLeavingRequested(); 0183 }); 0184 updateMainActionButton(KFileItemList()); 0185 m_layout->addWidget(copyButton); 0186 } 0187 0188 void BottomBarContentsContainer::addCopyLocationContents() 0189 { 0190 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select one file or folder whose location should be copied."), this); 0191 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0192 m_explanatoryLabel->setWordWrap(true); 0193 m_layout->addWidget(m_explanatoryLabel); 0194 0195 // clang-format off 0196 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode. 0197 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this); 0198 // clang-format on 0199 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0200 m_layout->addWidget(cancelButton); 0201 0202 auto *copyLocationButton = new QPushButton(this); 0203 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("copy_location")), copyLocationButton); 0204 updateMainActionButton(KFileItemList()); 0205 m_layout->addWidget(copyLocationButton); 0206 } 0207 0208 void BottomBarContentsContainer::addCopyToOtherViewContents() 0209 { 0210 // clang-format off 0211 // i18n: "Copy over" refers to copying to the other split view area that is currently visible to the user. 0212 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be copied over."), this); 0213 // clang-format on 0214 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0215 m_explanatoryLabel->setWordWrap(true); 0216 m_layout->addWidget(m_explanatoryLabel); 0217 0218 // clang-format off 0219 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode. 0220 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Copying"), this); 0221 // clang-format on 0222 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0223 m_layout->addWidget(cancelButton); 0224 0225 auto *copyToOtherViewButton = new QPushButton(this); 0226 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("copy_to_inactive_split_view")), copyToOtherViewButton); 0227 updateMainActionButton(KFileItemList()); 0228 m_layout->addWidget(copyToOtherViewButton); 0229 } 0230 0231 void BottomBarContentsContainer::addCutContents() 0232 { 0233 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be cut."), this); 0234 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0235 m_explanatoryLabel->setWordWrap(true); 0236 m_layout->addWidget(m_explanatoryLabel); 0237 0238 // clang-format off 0239 // i18n: Aborts the current step-by-step process to cut files by leaving the selection mode. 0240 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Cutting"), this); 0241 // clang-format on 0242 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0243 m_layout->addWidget(cancelButton); 0244 0245 auto *cutButton = new QPushButton(this); 0246 // We claim to have PasteContents already so triggering the cut action next won't instantly hide the bottom bar. 0247 connect(cutButton, &QAbstractButton::clicked, this, [this]() { 0248 if (GeneralSettings::showPasteBarAfterCopying()) { 0249 m_contents = BottomBar::Contents::PasteContents; // prevents hiding 0250 } 0251 }); 0252 // Connect the cut action as a second step. 0253 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::Cut)), cutButton); 0254 // Finally connect the lambda that actually changes the contents to the PasteContents. 0255 connect(cutButton, &QAbstractButton::clicked, this, [this]() { 0256 if (GeneralSettings::showPasteBarAfterCopying()) { 0257 resetContents(BottomBar::Contents::PasteContents); // resetContents() needs to be connected last because 0258 // it instantly deletes the button and then the other slots won't be called. 0259 } 0260 Q_EMIT selectionModeLeavingRequested(); 0261 }); 0262 updateMainActionButton(KFileItemList()); 0263 m_layout->addWidget(cutButton); 0264 } 0265 0266 void BottomBarContentsContainer::addDeleteContents() 0267 { 0268 m_explanatoryLabel = 0269 new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be permanently deleted."), this); 0270 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0271 m_explanatoryLabel->setWordWrap(true); 0272 m_layout->addWidget(m_explanatoryLabel); 0273 0274 // clang-format off 0275 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode. 0276 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel"), this); 0277 // clang-format on 0278 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0279 m_layout->addWidget(cancelButton); 0280 0281 auto *deleteButton = new QPushButton(this); 0282 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::DeleteFile)), deleteButton); 0283 updateMainActionButton(KFileItemList()); 0284 m_layout->addWidget(deleteButton); 0285 } 0286 0287 void BottomBarContentsContainer::addDuplicateContents() 0288 { 0289 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be duplicated here."), this); 0290 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0291 m_explanatoryLabel->setWordWrap(true); 0292 m_layout->addWidget(m_explanatoryLabel); 0293 0294 // clang-format off 0295 // i18n: Aborts the current step-by-step process to duplicate files by leaving the selection mode. 0296 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Duplicating"), this); 0297 // clang-format on 0298 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0299 m_layout->addWidget(cancelButton); 0300 0301 auto *duplicateButton = new QPushButton(this); 0302 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("duplicate")), duplicateButton); 0303 updateMainActionButton(KFileItemList()); 0304 m_layout->addWidget(duplicateButton); 0305 } 0306 0307 void BottomBarContentsContainer::addGeneralContents() 0308 { 0309 if (!m_overflowButton) { 0310 // clang-format off 0311 // 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. 0312 // The small button opens a menu that contains the actions that didn't fit on the bar. 0313 m_overflowButton = new QPushButton{QIcon::fromTheme(QStringLiteral("view-more-symbolic")), i18nc("@action keep short", "More"), this}; 0314 // clang-format on 0315 m_overflowButton->setMenu(new QMenu{m_overflowButton}); 0316 m_overflowButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); // Makes sure it has the same height as the labeled buttons. 0317 m_layout->addWidget(m_overflowButton); 0318 } else { 0319 m_overflowButton->menu()->actions().clear(); 0320 // The overflowButton should be part of the calculation for needed space so we set it visible in regards to unusedSpace(). 0321 m_overflowButton->setVisible(true); 0322 } 0323 0324 // We first add all the m_generalBarActions to the bar until the bar is full. 0325 auto i = m_generalBarActions.begin(); 0326 for (; i != m_generalBarActions.end(); ++i) { 0327 if (i->action()->isVisible()) { 0328 if (i->widget()) { 0329 i->widget()->setEnabled(i->action()->isEnabled()); 0330 } else { 0331 i->newWidget(this); 0332 i->widget()->setVisible(false); 0333 m_layout->insertWidget(m_layout->count() - 1, i->widget()); // Insert before m_overflowButton 0334 } 0335 if (unusedSpace() < i->widget()->sizeHint().width()) { 0336 break; // The bar is too full already. We keep it invisible. 0337 } else { 0338 i->widget()->setVisible(true); 0339 } 0340 } 0341 } 0342 // We are done adding widgets to the bar so either we were able to fit all the actions in there 0343 m_overflowButton->setVisible(false); 0344 // …or there are more actions left which need to be put into m_overflowButton. 0345 for (; i != m_generalBarActions.end(); ++i) { 0346 m_overflowButton->menu()->addAction(i->action()); 0347 0348 // The overflowButton is set visible if there is actually an action in it. 0349 if (!m_overflowButton->isVisible() && i->action()->isVisible() && !i->action()->isSeparator()) { 0350 m_overflowButton->setVisible(true); 0351 } 0352 } 0353 } 0354 0355 void BottomBarContentsContainer::addMoveToOtherViewContents() 0356 { 0357 // clang-format off 0358 // i18n: "Move over" refers to moving to the other split view area that is currently visible to the user. 0359 m_explanatoryLabel = new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved over."), this); 0360 // clang-format on 0361 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0362 m_explanatoryLabel->setWordWrap(true); 0363 m_layout->addWidget(m_explanatoryLabel); 0364 0365 // clang-format off 0366 // i18n: Aborts the current step-by-step process to copy the location of files by leaving the selection mode. 0367 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Moving"), this); 0368 // clang-format on 0369 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0370 m_layout->addWidget(cancelButton); 0371 0372 auto *moveToOtherViewButton = new QPushButton(this); 0373 m_mainAction = ActionWithWidget(m_actionCollection->action(QStringLiteral("move_to_inactive_split_view")), moveToOtherViewButton); 0374 updateMainActionButton(KFileItemList()); 0375 m_layout->addWidget(moveToOtherViewButton); 0376 } 0377 0378 void BottomBarContentsContainer::addMoveToTrashContents() 0379 { 0380 m_explanatoryLabel = 0381 new QLabel(i18nc("@info explaining the next step in a process", "Select the files and folders that should be moved to the Trash."), this); 0382 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0383 m_explanatoryLabel->setWordWrap(true); 0384 m_layout->addWidget(m_explanatoryLabel); 0385 0386 // clang-format off 0387 // i18n: Aborts the current step-by-step process of moving files to the trash by leaving the selection mode. 0388 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel"), this); 0389 // clang-format on 0390 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0391 m_layout->addWidget(cancelButton); 0392 0393 auto *moveToTrashButton = new QPushButton(this); 0394 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)), moveToTrashButton); 0395 updateMainActionButton(KFileItemList()); 0396 m_layout->addWidget(moveToTrashButton); 0397 } 0398 0399 void BottomBarContentsContainer::addPasteContents() 0400 { 0401 m_explanatoryLabel = new QLabel(xi18n("<para>The selected files and folders were added to the Clipboard. " 0402 "Now the <emphasis>Paste</emphasis> action can be used to transfer them from the Clipboard " 0403 "to any other location. They can even be transferred to other applications by using their " 0404 "respective <emphasis>Paste</emphasis> actions.</para>"), 0405 this); 0406 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0407 m_explanatoryLabel->setWordWrap(true); 0408 m_layout->addWidget(m_explanatoryLabel); 0409 0410 auto *vBoxLayout = new QVBoxLayout(this); 0411 m_layout->addLayout(vBoxLayout); 0412 0413 /** We are in "PasteContents" mode which means hiding the bottom bar is impossible. 0414 * So we first have to claim that we have different contents before requesting to leave selection mode. */ 0415 auto actuallyLeaveSelectionMode = [this]() { 0416 m_contents = BottomBar::Contents::CopyLocationContents; 0417 Q_EMIT barVisibilityChangeRequested(false); 0418 }; 0419 0420 auto *pasteButton = new QPushButton(this); 0421 copyActionDataToButton(pasteButton, m_actionCollection->action(KStandardAction::name(KStandardAction::Paste))); 0422 pasteButton->setText(i18nc("@action A more elaborate and clearly worded version of the Paste action", "Paste from Clipboard")); 0423 connect(pasteButton, &QAbstractButton::clicked, this, actuallyLeaveSelectionMode); 0424 vBoxLayout->addWidget(pasteButton); 0425 0426 auto *dismissButton = new QToolButton(this); 0427 dismissButton->setText(i18nc("@action Dismisses a bar explaining how to use the Paste action", "Dismiss This Reminder")); 0428 connect(dismissButton, &QAbstractButton::clicked, this, actuallyLeaveSelectionMode); 0429 auto *dontRemindAgainAction = new QAction(i18nc("@action Dismisses an explanatory area and never shows it again", "Don't Remind Me Again"), this); 0430 connect(dontRemindAgainAction, &QAction::triggered, this, []() { 0431 GeneralSettings::setShowPasteBarAfterCopying(false); 0432 }); 0433 connect(dontRemindAgainAction, &QAction::triggered, this, actuallyLeaveSelectionMode); 0434 auto *dismissButtonMenu = new QMenu(dismissButton); 0435 dismissButtonMenu->addAction(dontRemindAgainAction); 0436 dismissButton->setMenu(dismissButtonMenu); 0437 dismissButton->setPopupMode(QToolButton::MenuButtonPopup); 0438 vBoxLayout->addWidget(dismissButton); 0439 0440 m_explanatoryLabel->setMaximumHeight(pasteButton->sizeHint().height() + dismissButton->sizeHint().height() + m_explanatoryLabel->fontMetrics().height()); 0441 } 0442 0443 void BottomBarContentsContainer::addRenameContents() 0444 { 0445 m_explanatoryLabel = new QLabel(i18nc("@info explains the next step in a process", 0446 "Select the file or folder that should be renamed.\nBulk renaming is possible when multiple items are selected."), 0447 this); 0448 m_explanatoryLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); 0449 m_explanatoryLabel->setWordWrap(true); 0450 m_layout->addWidget(m_explanatoryLabel); 0451 0452 // clang-format off 0453 // i18n: Aborts the current step-by-step process to delete files by leaving the selection mode. 0454 auto *cancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), i18nc("@action:button", "Cancel Renaming"), this); 0455 // clang-format on 0456 connect(cancelButton, &QAbstractButton::clicked, this, &BottomBarContentsContainer::selectionModeLeavingRequested); 0457 m_layout->addWidget(cancelButton); 0458 0459 auto *renameButton = new QPushButton(this); 0460 m_mainAction = ActionWithWidget(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)), renameButton); 0461 updateMainActionButton(KFileItemList()); 0462 m_layout->addWidget(renameButton); 0463 } 0464 0465 void BottomBarContentsContainer::emptyBarContents() 0466 { 0467 QLayoutItem *child; 0468 while ((child = m_layout->takeAt(0)) != nullptr) { 0469 if (auto *childLayout = child->layout()) { 0470 QLayoutItem *grandChild; 0471 while ((grandChild = childLayout->takeAt(0)) != nullptr) { 0472 delete grandChild->widget(); // delete the widget 0473 delete grandChild; // delete the layout item 0474 } 0475 } 0476 delete child->widget(); // delete the widget 0477 delete child; // delete the layout item 0478 } 0479 } 0480 0481 std::vector<QAction *> BottomBarContentsContainer::contextActionsFor(const KFileItemList &selectedItems, const QUrl &baseUrl) 0482 { 0483 if (selectedItems.isEmpty()) { 0484 // There are no contextual actions to show for these items. 0485 // We might even want to hide this bar in this case. To make this clear, we reset m_internalContextMenu. 0486 m_internalContextMenu.release()->deleteLater(); 0487 return std::vector<QAction *>{}; 0488 } 0489 0490 std::vector<QAction *> contextActions; 0491 0492 // We always want to show the most important actions at the beginning 0493 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::Copy))); 0494 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::Cut))); 0495 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile))); 0496 contextActions.emplace_back(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash))); 0497 0498 // We are going to add the actions from the right-click context menu for the selected items. 0499 auto *dolphinMainWindow = qobject_cast<DolphinMainWindow *>(window()); 0500 Q_CHECK_PTR(dolphinMainWindow); 0501 if (!m_fileItemActions) { 0502 m_fileItemActions = new KFileItemActions(this); 0503 m_fileItemActions->setParentWidget(dolphinMainWindow); 0504 connect(m_fileItemActions, &KFileItemActions::error, this, &BottomBarContentsContainer::error); 0505 } 0506 m_internalContextMenu = std::make_unique<DolphinContextMenu>(dolphinMainWindow, selectedItems.constFirst(), selectedItems, baseUrl, m_fileItemActions); 0507 auto internalContextMenuActions = m_internalContextMenu->actions(); 0508 0509 // There are some actions which we wouldn't want to add. We remember them in the actionsThatShouldntBeAdded set. 0510 // We don't want to add the four basic actions again which were already added to the top. 0511 std::unordered_set<QAction *> actionsThatShouldntBeAdded{contextActions.begin(), contextActions.end()}; 0512 // "Delete" isn't really necessary to add because we have "Move to Trash" already. It is also more dangerous so let's exclude it. 0513 actionsThatShouldntBeAdded.insert(m_actionCollection->action(KStandardAction::name(KStandardAction::DeleteFile))); 0514 0515 // KHamburgerMenu would only be visible if there is no menu available anywhere on the user interface. This might be useful for recovery from 0516 // such a situation in theory but a bar with context dependent actions doesn't really seem like the right place for it. 0517 Q_ASSERT(internalContextMenuActions.first()->icon().name() 0518 == m_actionCollection->action(KStandardAction::name(KStandardAction::HamburgerMenu))->icon().name()); 0519 internalContextMenuActions.removeFirst(); 0520 0521 for (auto it = internalContextMenuActions.constBegin(); it != internalContextMenuActions.constEnd(); ++it) { 0522 if (actionsThatShouldntBeAdded.count(*it)) { 0523 continue; // Skip this action. 0524 } 0525 if (!qobject_cast<DolphinRemoveAction *>(*it)) { // We already have a "Move to Trash" action so we don't want a DolphinRemoveAction. 0526 // We filter duplicate separators here so we won't have to deal with them later. 0527 if (!contextActions.back()->isSeparator() || !(*it)->isSeparator()) { 0528 contextActions.emplace_back((*it)); 0529 } 0530 } 0531 } 0532 0533 auto separator = new QAction(m_internalContextMenu.get()); 0534 separator->setSeparator(true); 0535 contextActions.emplace_back(separator); 0536 0537 // Add "Invert Selection" and "Select All" at the very end for better usability while in selection mode. 0538 // Design-wise this decision is slightly questionable because the other actions in the bar apply to the selected items while 0539 // the "select" actions apply to the view instead but we decided that there are more benefits than drawbacks to this. 0540 auto invertSelectionAction = m_actionCollection->action(QStringLiteral("invert_selection")); 0541 Q_ASSERT(invertSelectionAction && !internalContextMenuActions.contains(invertSelectionAction)); 0542 contextActions.emplace_back(invertSelectionAction); 0543 auto selectAllAction = m_actionCollection->action(KStandardAction::name(KStandardAction::SelectAll)); 0544 Q_ASSERT(selectAllAction && !internalContextMenuActions.contains(selectAllAction)); 0545 contextActions.emplace_back(selectAllAction); 0546 0547 return contextActions; 0548 } 0549 0550 int BottomBarContentsContainer::unusedSpace() const 0551 { 0552 int sumOfPreferredWidths = m_layout->contentsMargins().left() + m_layout->contentsMargins().right(); 0553 if (m_overflowButton) { 0554 sumOfPreferredWidths += m_overflowButton->sizeHint().width(); 0555 } 0556 for (int i = 0; i < m_layout->count(); ++i) { 0557 auto widget = m_layout->itemAt(i)->widget(); 0558 if (widget && !widget->isVisibleTo(widget->parentWidget())) { 0559 continue; // We don't count invisible widgets. 0560 } 0561 sumOfPreferredWidths += m_layout->itemAt(i)->sizeHint().width() + m_layout->spacing(); 0562 } 0563 return m_barWidth - sumOfPreferredWidths - 20; // We consider all space used when there are only 20 pixels left 0564 // so there is some room to breath and not too much wonkyness while resizing. 0565 } 0566 0567 void BottomBarContentsContainer::updateExplanatoryLabelVisibility() 0568 { 0569 if (!m_explanatoryLabel) { 0570 return; 0571 } 0572 if (m_explanatoryLabel->isVisible()) { 0573 m_explanatoryLabel->setVisible(unusedSpace() > 0); 0574 } else { 0575 // We only want to re-show the label when it fits comfortably so the computation below adds another "+20". 0576 m_explanatoryLabel->setVisible(unusedSpace() > m_explanatoryLabel->sizeHint().width() + 20); 0577 } 0578 } 0579 // clang-format off 0580 void BottomBarContentsContainer::updateMainActionButton(const KFileItemList& selectedItems) 0581 { 0582 if (!m_mainAction.widget()) { 0583 return; 0584 } 0585 Q_ASSERT(qobject_cast<QAbstractButton *>(m_mainAction.widget())); 0586 0587 // Users are nudged towards selecting items by having the button disabled when nothing is selected. 0588 m_mainAction.widget()->setEnabled(selectedItems.count() > 0 && m_mainAction.action()->isEnabled()); 0589 QFontMetrics fontMetrics = m_mainAction.widget()->fontMetrics(); 0590 0591 QString buttonText; 0592 switch (m_contents) { 0593 case BottomBar::CopyContents: 0594 // i18n: A more elaborate and clearly worded version of the Copy action 0595 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0596 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0597 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0598 // and a fallback will be used. 0599 buttonText = i18ncp("@action", "Copy %2 to the Clipboard", "Copy %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics)); 0600 // All these long lines can not be broken up with line breaks becaue the i18n call should be completely 0601 // in the line following the "i18n:" comment without any line breaks within the i18n call 0602 // or the comment might not be correctly extracted. See: https://commits.kde.org/kxmlgui/a31135046e1b3335b5d7bbbe6aa9a883ce3284c1 0603 break; 0604 case BottomBar::CopyLocationContents: 0605 // i18n: A more elaborate and clearly worded version of the Copy Location action 0606 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0607 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0608 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0609 // and a fallback will be used. 0610 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)); 0611 break; 0612 case BottomBar::CutContents: 0613 // i18n: A more elaborate and clearly worded version of the Cut action 0614 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0615 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0616 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0617 // and a fallback will be used. 0618 buttonText = i18ncp("@action", "Cut %2 to the Clipboard", "Cut %2 to the Clipboard", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics)); 0619 break; 0620 case BottomBar::DeleteContents: 0621 // i18n: A more elaborate and clearly worded version of the Delete action 0622 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0623 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0624 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0625 // and a fallback will be used. 0626 buttonText = i18ncp("@action", "Permanently Delete %2", "Permanently Delete %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics)); 0627 break; 0628 case BottomBar::DuplicateContents: 0629 // i18n: A more elaborate and clearly worded version of the Duplicate action 0630 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0631 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0632 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0633 // and a fallback will be used. 0634 buttonText = i18ncp("@action", "Duplicate %2", "Duplicate %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics)); 0635 break; 0636 case BottomBar::MoveToTrashContents: 0637 // i18n: A more elaborate and clearly worded version of the Trash action 0638 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0639 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0640 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0641 // and a fallback will be used. 0642 buttonText = i18ncp("@action", "Move %2 to the Trash", "Move %2 to the Trash", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics)); 0643 break; 0644 case BottomBar::RenameContents: 0645 // i18n: A more elaborate and clearly worded version of the Rename action 0646 // %2 is a textual representation of the currently selected files or folders. This can be the name of 0647 // the file/files like "file1" or "file1, file2 and file3" or an aggregate like "8 Selected Folders". 0648 // If this sort of word puzzle can not be correctly translated in your language, translate it as "NULL" (without the quotes) 0649 // and a fallback will be used. 0650 buttonText = i18ncp("@action", "Rename %2", "Rename %2", selectedItems.count(), fileItemListToString(selectedItems, fontMetrics.averageCharWidth() * 20, fontMetrics)); 0651 break; 0652 default: 0653 return; 0654 } 0655 if (buttonText != QStringLiteral("NULL")) { 0656 static_cast<QAbstractButton *>(m_mainAction.widget())->setText(buttonText); 0657 0658 // The width of the button has changed. We might want to hide the label so the full button text fits on the bar. 0659 updateExplanatoryLabelVisibility(); 0660 } 0661 } 0662 // clang-format on 0663 0664 #include "moc_bottombarcontentscontainer.cpp"