File indexing completed on 2025-01-26 05:06:23

0001 /*
0002     SPDX-FileCopyrightText: Ken <https://stackoverflow.com/users/1568857/ken>
0003     SPDX-FileCopyrightText: 2016 Leslie Zhai <xiangzhai83@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "shortcut.h"
0009 
0010 #include <KStandardShortcut>
0011 
0012 #include <QKeyEvent>
0013 
0014 ShortCut::ShortCut(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 void ShortCut::installAsEventFilterFor(QObject *target)
0020 {
0021     if (target) {
0022         target->installEventFilter(this);
0023     }
0024 }
0025 
0026 bool ShortCut::eventFilter(QObject *obj, QEvent *e)
0027 {
0028     if (e->type() == QEvent::KeyPress) {
0029         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
0030         const int keyInt = keyEvent->modifiers() & ~Qt::KeypadModifier | keyEvent->key();
0031         if (KStandardShortcut::deleteFile().contains(QKeySequence(keyInt))) {
0032             Q_EMIT deleteFile();
0033             return true;
0034         }
0035         if (KStandardShortcut::renameFile().contains(QKeySequence(keyInt))) {
0036             Q_EMIT renameFile();
0037             return true;
0038         }
0039         if (KStandardShortcut::moveToTrash().contains(QKeySequence(keyInt))) {
0040             Q_EMIT moveToTrash();
0041             return true;
0042         }
0043         if (KStandardShortcut::createFolder().contains(QKeySequence(keyInt))) {
0044             Q_EMIT createFolder();
0045             return true;
0046         }
0047     }
0048 
0049     return QObject::eventFilter(obj, e);
0050 }