]> cloud.milkyroute.net Git - dolphin.git/blob - src/disabledactionnotifier.cpp
Fix build for KIO version < 6.14
[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 clearDisabledReason(action);
22 }
23
24 QShortcut *shortcut = new QShortcut(action->shortcut(), parent());
25 m_shortcuts.insert(action, shortcut);
26
27 connect(
28 action,
29 &QAction::enabledChanged,
30 this,
31 [this, action](bool enabled) {
32 if (enabled && m_shortcuts.contains(action)) {
33 m_shortcuts.take(action)->deleteLater();
34 }
35 },
36 Qt::SingleShotConnection);
37
38 // Don't capture QStringView, as it may reference a temporary QString
39 QString reasonString = reason.toString();
40 connect(shortcut, &QShortcut::activated, this, [this, action, reasonString]() {
41 Q_EMIT disabledActionTriggered(action, reasonString);
42 });
43 }
44
45 void DisabledActionNotifier::clearDisabledReason(QAction *action)
46 {
47 if (action->isEnabled()) {
48 return;
49 }
50
51 action->disconnect(this);
52 if (m_shortcuts.contains(action)) {
53 m_shortcuts.take(action)->deleteLater();
54 }
55 }
56
57 #include "moc_disabledactionnotifier.cpp"