]>
cloud.milkyroute.net Git - dolphin.git/blob - src/disabledactionnotifier.cpp
2 * SPDX-FileCopyrightText: 2024 Jin Liu <m.liu.jin@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "disabledactionnotifier.h"
9 DisabledActionNotifier::DisabledActionNotifier(QObject
*parent
)
14 void DisabledActionNotifier::setDisabledReason(QAction
*action
, QStringView reason
)
16 if (action
->isEnabled()) {
20 if (m_shortcuts
.contains(action
)) {
21 clearDisabledReason(action
);
24 QShortcut
*shortcut
= new QShortcut(action
->shortcut(), parent());
25 m_shortcuts
.insert(action
, shortcut
);
29 &QAction::enabledChanged
,
31 [this, action
](bool enabled
) {
32 if (enabled
&& m_shortcuts
.contains(action
)) {
33 m_shortcuts
.take(action
)->deleteLater();
36 Qt::SingleShotConnection
);
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
);
45 void DisabledActionNotifier::clearDisabledReason(QAction
*action
)
47 if (action
->isEnabled()) {
51 action
->disconnect(this);
52 if (m_shortcuts
.contains(action
)) {
53 m_shortcuts
.take(action
)->deleteLater();
57 #include "moc_disabledactionnotifier.cpp"