]> cloud.milkyroute.net Git - dolphin.git/blob - src/disabledactionnotifier.cpp
DolphinMainWindow: show a banner when the user presses the shortcut of a disabled...
[dolphin.git] / src / disabledactionnotifier.cpp
1 /*
2 * SPDX-FileCopyrightText: 2024 Jin Liu <m.liu.jin@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "disabledactionnotifier.h"
8
9 DisabledActionNotifier::DisabledActionNotifier(QObject *parent)
10 : QObject(parent)
11 {
12 }
13
14 void DisabledActionNotifier::setDisabledReason(QAction *action, QStringView reason)
15 {
16 if (action->isEnabled()) {
17 return;
18 }
19
20 if (m_shortcuts.contains(action)) {
21 m_shortcuts.take(action)->deleteLater();
22 }
23
24 QShortcut *shortcut = new QShortcut(action->shortcut(), parent());
25 m_shortcuts.insert(action, shortcut);
26
27 connect(action, &QAction::enabledChanged, this, [this, action](bool enabled) {
28 if (enabled) {
29 m_shortcuts.take(action)->deleteLater();
30 }
31 });
32
33 // Don't capture QStringView, as it may reference a temporary QString
34 QString reasonString = reason.toString();
35 connect(shortcut, &QShortcut::activated, this, [this, action, reasonString]() {
36 Q_EMIT disabledActionTriggered(action, reasonString);
37 });
38 }
39
40 void DisabledActionNotifier::clearDisabledReason(QAction *action)
41 {
42 if (action->isEnabled()) {
43 return;
44 }
45
46 if (m_shortcuts.contains(action)) {
47 m_shortcuts.take(action)->deleteLater();
48 }
49 }
50
51 #include "moc_disabledactionnotifier.cpp"