aboutsummaryrefslogtreecommitdiff
path: root/src/keymap/KeySeqParser.hpp
blob: 31680a93b1715ab9e8b067dc26307efe15d16c33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once

#include <QtCore>
#include <cstdint>

struct KeyChord {
  Qt::KeyboardModifiers mod;
  Qt::Key key;

  bool operator==(const KeyChord &chord2) const;
};

using KeySequence = QList<KeyChord>;

enum KeyMatchType : uint8_t {
  NoMatch,
  Match,
  Pending,
};

class KeySeqParser {
public:
  static KeyMatchType key_sequence_match(const KeySequence &target, const KeySequence &current) {
    for (int i = 0; i < target.length(); i++) {
      if (current.length() <= i)
        return KeyMatchType::Pending;

      if (target[i].key != current[i].key || !target[i].mod.testFlags(current[i].mod))
        return KeyMatchType::NoMatch;
    }

    return KeyMatchType::Match;
  }

  KeySeqParser() = default;
  KeySequence parse(QString key_sequence);

private:
  Qt::Key parse_key(const QString &key_name);
};