File indexing completed on 2024-04-21 05:31:03

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef MODIFIERTRACKER_H
0007 #define MODIFIERTRACKER_H
0008 
0009 // Qt
0010 #include <QHash>
0011 #include <QObject>
0012 #include <QTimer>
0013 
0014 // KDE
0015 #include <KModifierKeyInfo>
0016 
0017 namespace Latte {
0018 namespace ShortcutsPart {
0019 
0020 class ModifierTracker: public QObject {
0021     Q_OBJECT
0022 
0023 public:
0024     ModifierTracker(QObject *parent);
0025     ~ModifierTracker() override;
0026 
0027     //! cancel meta is pressed delayer
0028     void cancelMetaPressed();
0029 
0030     //! none of tracked modifiers is pressed
0031     bool noModifierPressed();
0032 
0033     //! at least one of the modifiers from KeySequence is pressed
0034     bool sequenceModifierPressed(const QKeySequence &seq);
0035 
0036     //! only <key> is pressed and no other modifier
0037     bool singleModifierPressed(Qt::Key key);
0038 
0039     void blockModifierTracking(Qt::Key key);
0040     void unblockModifierTracking(Qt::Key key);
0041 
0042 signals:
0043     void metaModifierPressed();
0044     void modifiersChanged();
0045 
0046 private:
0047     void init();
0048 
0049     //! <key> modifier is tracked for changes
0050     bool modifierIsTracked(Qt::Key key);
0051 
0052     //! adjust key in more general values, e.g. Super_L and Super_R both return Super_L
0053     Qt::Key normalizeKey(Qt::Key key);
0054 
0055 private:
0056     KModifierKeyInfo m_modifierKeyInfo;
0057     QTimer m_metaPressedTimer;
0058 
0059     //! modifiers that the user does not want to track anymore
0060     QList<Qt::Key> m_blockedModifiers;
0061 
0062     //! keep a record for modifiers
0063     QHash<Qt::Key, bool> m_pressed;
0064 
0065 };
0066 
0067 }
0068 }
0069 
0070 #endif