File indexing completed on 2024-04-14 15:49:28

0001 /*
0002  * SPDX-FileCopyrightText: 2013 Dawit Alemayehu <adawit@kde.org>
0003  * SPDX-FileCopyrightText: 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "dolphinremoveaction.h"
0009 
0010 #include <QApplication>
0011 
0012 DolphinRemoveAction::DolphinRemoveAction(QObject *parent, KActionCollection *collection)
0013     : QAction(parent)
0014     , m_collection(collection)
0015 {
0016     update();
0017     connect(this, &DolphinRemoveAction::triggered, this, &DolphinRemoveAction::slotRemoveActionTriggered);
0018 }
0019 
0020 void DolphinRemoveAction::slotRemoveActionTriggered()
0021 {
0022     if (m_action) {
0023         m_action->trigger();
0024     }
0025 }
0026 
0027 void DolphinRemoveAction::update(ShiftState shiftState)
0028 {
0029     if (!m_collection) {
0030         m_action = nullptr;
0031         return;
0032     }
0033 
0034     if (shiftState == ShiftState::Unknown) {
0035         shiftState = QGuiApplication::keyboardModifiers() & Qt::ShiftModifier ? ShiftState::Pressed : ShiftState::Released;
0036     }
0037 
0038     switch (shiftState) {
0039     case ShiftState::Pressed: {
0040         m_action = m_collection->action(KStandardAction::name(KStandardAction::DeleteFile));
0041         // Make sure we show Shift+Del in the context menu.
0042         auto deleteShortcuts = m_action->shortcuts();
0043         deleteShortcuts.removeAll(Qt::SHIFT | Qt::Key_Delete);
0044         deleteShortcuts.prepend(Qt::SHIFT | Qt::Key_Delete);
0045         m_collection->setDefaultShortcuts(this, deleteShortcuts);
0046         break;
0047     }
0048     case ShiftState::Released: {
0049         m_action = m_collection->action(KStandardAction::name(KStandardAction::MoveToTrash));
0050         // Make sure we show Del in the context menu.
0051         auto trashShortcuts = m_action->shortcuts();
0052         trashShortcuts.removeAll(QKeySequence::Delete);
0053         trashShortcuts.prepend(QKeySequence::Delete);
0054         m_collection->setDefaultShortcuts(this, trashShortcuts);
0055         break;
0056     }
0057     case ShiftState::Unknown:
0058         Q_UNREACHABLE();
0059         break;
0060     }
0061 
0062     if (m_action) {
0063         setText(m_action->text());
0064         setIcon(m_action->icon());
0065         setEnabled(m_action->isEnabled());
0066     }
0067 }
0068 
0069 #include "moc_dolphinremoveaction.cpp"