-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputhandling.cpp
163 lines (135 loc) · 4.04 KB
/
inputhandling.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "inputhandling.h"
#include <QKeyEvent>
#include <QMetaEnum>
#include <QDebug>
#include "utils.h"
InputHandling::InputHandling(QObject *parent)
:
QObject(parent)
{
m_stringToSignal.insert("change_station_up", &InputHandling::stationUp);
m_stringToSignal.insert("change_station_down", &InputHandling::stationDown);
m_stringToSignal.insert("volume_up", &InputHandling::volumeUp);
m_stringToSignal.insert("volume_down", &InputHandling::volumeDown);
m_stringToSignal.insert("power_off", &InputHandling::powerOff);
m_waitForKeys.setInterval(1000);
m_waitForKeys.setSingleShot(true);
connect(&m_waitForKeys, SIGNAL(timeout()), this, SLOT(finishedWritingStationNumber()));
loadKeys();
}
void InputHandling::setPossibleNumbers(const QSet<QString>& numbers)
{
m_possibleNumbers = numbers;
}
int InputHandling::startsWith(const QString& text)
{
int count = 0;
for (const QString& number : m_possibleNumbers)
{
if (number.startsWith(text))
{
count++;
}
}
return count;
}
void InputHandling::finishedWritingStationNumber()
{
if (m_possibleNumbers.contains(m_currentInput))
{
emit changeToStation(m_currentInput);
m_currentInput.clear();
}
else
{
m_currentInput.clear();
}
}
QString InputHandling::loadKeys()
{
Utils::ErrorOrValue errorOrvalue = Utils::readJsonToVariant("configuration/keys.json");
m_keyToSignal.clear();
if (!errorOrvalue.error.isEmpty())
{
return errorOrvalue.error;
}
QMap<QString, QVariant> keys = errorOrvalue.value.toMap();
// TODO: refactor this 3 fors
for (const QString& key : keys.keys())
{
SignalType signalName = m_stringToSignal.value(key);
for (const QVariant& configurationKeys : keys.values(key))
{
for (const QVariant& configurationKey : configurationKeys.toList())
{
Qt::Key key;
bool ok;
int configurationKeyInt = configurationKey.toInt(&ok);
// The key in the configuration file was an int
if (ok)
{
key = static_cast<Qt::Key>(configurationKeyInt);
m_keyToSignal.insert(key, signalName);
continue;
}
// The key in the configuration file was a QString very likely representing a Qt::Key
QMetaEnum metaEnum = QMetaEnum::fromType<Qt::Key>();
key = static_cast<Qt::Key>(metaEnum.keyToValue(configurationKey.toString().toUtf8().constData(), &ok));
if (ok)
{
m_keyToSignal.insert(key, signalName);
continue;
}
}
}
}
return QString();
}
bool InputHandling::eventFilter(QObject* object, QEvent* event)
{
Q_UNUSED(object);
QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
if (!keyEvent || keyEvent->type() != QEvent::KeyPress)
{
return false;
}
Qt::Key key = static_cast<Qt::Key>(keyEvent->key());
qDebug() << "==== key pressed:" << key;
QString text = keyEvent->text();
bool ok;
text.toInt(&ok);
if (ok)
{
m_currentInput.append(text);
int startsWithCount = startsWith(m_currentInput);
if (startsWithCount == 0)
{
m_currentInput.clear();
return true;
}
else if (startsWithCount == 1)
{
emit changeToStation(m_currentInput);
m_currentInput.clear();
return true;
}
else if (startsWithCount > 1)
{
m_waitForKeys.start();
return true;
}
if (startsWith(m_currentInput) == 0)
{
m_currentInput.clear();
}
m_waitForKeys.start();
return true;
}
if (m_keyToSignal.contains(key))
{
SignalType signalName = m_keyToSignal.value(key);
(this->*signalName)();
return true;
}
return false;
}