From 7d49cb567b595570f59156965332428c9490a04d Mon Sep 17 00:00:00 2001 From: Jin Liu Date: Wed, 3 Apr 2024 12:34:50 +0000 Subject: DolphinMainWindow: show a banner when the user presses the shortcut of a disabled action Currently, there's no feedback when the user presses a shortcut of a disabled action, e.g. cut / paste in a read-only directory. This patch shows a banner in that case. It's implemented by enabling a QShortcut for each disabled action. the QShortcut is deleted when the action is enabled again. The following actions are included: cut paste rename moveToTrash deleteWithTrashShortcut deleted createDir copyToOtherView moveToOtherView --- src/disabledactionnotifier.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/disabledactionnotifier.cpp (limited to 'src/disabledactionnotifier.cpp') diff --git a/src/disabledactionnotifier.cpp b/src/disabledactionnotifier.cpp new file mode 100644 index 000000000..844e66228 --- /dev/null +++ b/src/disabledactionnotifier.cpp @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2024 Jin Liu + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "disabledactionnotifier.h" + +DisabledActionNotifier::DisabledActionNotifier(QObject *parent) + : QObject(parent) +{ +} + +void DisabledActionNotifier::setDisabledReason(QAction *action, QStringView reason) +{ + if (action->isEnabled()) { + return; + } + + if (m_shortcuts.contains(action)) { + m_shortcuts.take(action)->deleteLater(); + } + + QShortcut *shortcut = new QShortcut(action->shortcut(), parent()); + m_shortcuts.insert(action, shortcut); + + connect(action, &QAction::enabledChanged, this, [this, action](bool enabled) { + if (enabled) { + m_shortcuts.take(action)->deleteLater(); + } + }); + + // Don't capture QStringView, as it may reference a temporary QString + QString reasonString = reason.toString(); + connect(shortcut, &QShortcut::activated, this, [this, action, reasonString]() { + Q_EMIT disabledActionTriggered(action, reasonString); + }); +} + +void DisabledActionNotifier::clearDisabledReason(QAction *action) +{ + if (action->isEnabled()) { + return; + } + + if (m_shortcuts.contains(action)) { + m_shortcuts.take(action)->deleteLater(); + } +} + +#include "moc_disabledactionnotifier.cpp" -- cgit v1.3