File indexing completed on 2024-04-28 04:18:50
0001 // vim: set tabstop=4 shiftwidth=4 expandtab: 0002 /* 0003 Gwenview: an image viewer 0004 Copyright 2014 Aurélien Gâteau <agateau@kde.org> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License 0008 as published by the Free Software Foundation; either version 2 0009 of the License, or (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 // Self 0020 #include <disabledactionshortcutmonitor.h> 0021 0022 // Local 0023 0024 // KF 0025 0026 // Qt 0027 #include <QAction> 0028 #include <QEvent> 0029 #include <QShortcut> 0030 #include <QWidget> 0031 0032 namespace Gwenview 0033 { 0034 class DisabledActionShortcutMonitorPrivate 0035 { 0036 public: 0037 QShortcut *mShortcut = nullptr; 0038 }; 0039 DisabledActionShortcutMonitor::DisabledActionShortcutMonitor(QAction *action, QObject *parent) 0040 : QObject(parent) 0041 , d(new DisabledActionShortcutMonitorPrivate) 0042 { 0043 d->mShortcut = new QShortcut(parent); 0044 connect(d->mShortcut, &QShortcut::activated, this, &DisabledActionShortcutMonitor::activated); 0045 action->installEventFilter(this); 0046 } 0047 0048 DisabledActionShortcutMonitor::~DisabledActionShortcutMonitor() 0049 { 0050 delete d->mShortcut; 0051 delete d; 0052 } 0053 0054 bool DisabledActionShortcutMonitor::eventFilter(QObject *object, QEvent *event) 0055 { 0056 if (event->type() == QEvent::ActionChanged) { 0057 auto action = static_cast<QAction *>(object); 0058 if (action->isEnabled()) { 0059 // Unset the shortcut otherwise we get a dialog complaining about 0060 // ambiguous shortcuts when the user tries to trigger the action 0061 d->mShortcut->setKey(QKeySequence()); 0062 } else { 0063 d->mShortcut->setKey(action->shortcut()); 0064 } 0065 } 0066 return false; 0067 } 0068 0069 } // namespace 0070 0071 #include "moc_disabledactionshortcutmonitor.cpp"