diff options
| author | Jin Liu <[email protected]> | 2024-04-03 12:34:50 +0000 |
|---|---|---|
| committer | Méven Car <[email protected]> | 2024-04-03 12:34:50 +0000 |
| commit | 7d49cb567b595570f59156965332428c9490a04d (patch) | |
| tree | f2a9bf6334aa299cc50f30b2f1fc96811f7ceb1a /src/disabledactionnotifier.h | |
| parent | e75bfea1cb9f6b4cab4334e29bb9d29bb09711ba (diff) | |
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
Diffstat (limited to 'src/disabledactionnotifier.h')
| -rw-r--r-- | src/disabledactionnotifier.h | 60 |
1 files changed, 60 insertions, 0 deletions
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 <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#pragma once + +#include <QAction> +#include <QHash> +#include <QShortcut> + +/** + * @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<QAction *, QShortcut *> m_shortcuts; +}; |
