aboutsummaryrefslogtreecommitdiff
path: root/src/keymap/KeySeqParser.hpp
blob: 2dad0c7204ecc36e37b8d671bd7d13039b23c61f (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
41
42
43
44
#pragma once

#include <QtCore/qnamespace.h>
#include <QtCore>
#include <cmath>

struct KeyChord {
  Qt::KeyboardModifiers mod;
  Qt::Key key;
};
bool operator==(const KeyChord a, const KeyChord b);

typedef QList<KeyChord> KeySequence;

enum KeyMatchType {
  NoMatch,
  Match,
  Pending,
};

class KeySeqParser {
public:
  static KeyMatchType keySequenceMatch(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;
  }

public:
  KeySeqParser();

  KeySequence parse(QString keySequence);

private:
  Qt::Key parseKey(QString keyName);
};