File indexing completed on 2024-04-21 16:17:13

0001 /*
0002 *  Copyright 2019  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock 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 
0020 #ifndef MODIFIERTRACKER_H
0021 #define MODIFIERTRACKER_H
0022 
0023 // Qt
0024 #include <QHash>
0025 #include <QObject>
0026 #include <QTimer>
0027 
0028 // KDE
0029 #include <KModifierKeyInfo>
0030 
0031 namespace Latte {
0032 namespace ShortcutsPart {
0033 
0034 class ModifierTracker: public QObject {
0035     Q_OBJECT
0036 
0037 public:
0038     ModifierTracker(QObject *parent);
0039     ~ModifierTracker() override;
0040 
0041     //! cancel meta is pressed delayer
0042     void cancelMetaPressed();
0043 
0044     //! none of tracked modifiers is pressed
0045     bool noModifierPressed();
0046 
0047     //! at least one of the modifiers from KeySequence is pressed
0048     bool sequenceModifierPressed(const QKeySequence &seq);
0049 
0050     //! only <key> is pressed and no other modifier
0051     bool singleModifierPressed(Qt::Key key);
0052 
0053     void blockModifierTracking(Qt::Key key);
0054     void unblockModifierTracking(Qt::Key key);
0055 
0056 signals:
0057     void metaModifierPressed();
0058     void modifiersChanged();
0059 
0060 private:
0061     void init();
0062 
0063     //! <key> modifier is tracked for changes
0064     bool modifierIsTracked(Qt::Key key);
0065 
0066     //! adjust key in more general values, e.g. Super_L and Super_R both return Super_L
0067     Qt::Key normalizeKey(Qt::Key key);
0068 
0069 private:
0070     KModifierKeyInfo m_modifierKeyInfo;
0071     QTimer m_metaPressedTimer;
0072 
0073     //! modifiers that the user does not want to track anymore
0074     QList<Qt::Key> m_blockedModifiers;
0075 
0076     //! keep a record for modifiers
0077     QHash<Qt::Key, bool> m_pressed;
0078 
0079 };
0080 
0081 }
0082 }
0083 
0084 #endif