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.h | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/disabledactionnotifier.h (limited to 'src/disabledactionnotifier.h') diff --git a/src/disabledactionnotifier.h b/src/disabledactionnotifier.h new file mode 100644 index 000000000..535e9932b --- /dev/null +++ b/src/disabledactionnotifier.h @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: 2024 Jin Liu + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#pragma once + +#include +#include +#include + +/** + * @brief A helper class to display a notification when the user presses the shortcut of a disabled action. + */ +class DisabledActionNotifier : public QObject +{ + Q_OBJECT + +public: + DisabledActionNotifier(QObject *parent = nullptr); + + /** + * Set the reason why the action is disabled. + * + * If the action is enabled, this function does nothing. + * + * Otherwise, it registers a shortcut, so when the user presses the shortcut for the + * disabled action, it emits the disabledActionTriggered() signal with the disabled + * action and the reason. + * + * If a reason has already been set, it will be replaced. + */ + void setDisabledReason(QAction *action, QStringView reason); + + /** + * Clear the reason if it's set before by setDisabledReason(). + * + * If the action is enabled, this function does nothing. + * + * Otherwise, it unregisters any shortcut set by setDisabledReason() on the same action. + * + * When an action is disabled in two cases, but only case A needs to show the reason, + * then case B should call this function. Otherwise, the reason set by case A might be + * shown for case B. + */ + void clearDisabledReason(QAction *action); + +Q_SIGNALS: + /** + * Emitted when the user presses the shortcut of a disabled action. + * + * @param action The disabled action. + * @param reason The reason set in setDisabledReason(). + */ + void disabledActionTriggered(const QAction *action, QString reason); + +private: + QHash m_shortcuts; +}; -- cgit v1.3