┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/disabledactionnotifier.h
blob: 535e9932b89601c29be3a3b875fa4f0442b1e100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
};