-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundInputProcessor.cpp
79 lines (63 loc) · 1.6 KB
/
BackgroundInputProcessor.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
#include "BackgroundInputProcessor.h"
#include <QGuiApplication>
#include <iostream>
BackgroundInputProcessor::BackgroundInputProcessor(QObject* parent)
: QThread(parent)
{
parser = new InputParser(this);
}
BackgroundInputProcessor* BackgroundInputProcessor::getInstance()
{
static BackgroundInputProcessor* obj = new BackgroundInputProcessor();
return obj;
}
void BackgroundInputProcessor::processInput(const QString& input)
{
lastInput = input;
if (isWaiting)
condition.wakeAll();
else
queued = true;
}
void BackgroundInputProcessor::run()
{
// TODO: do not find when input wasn't changed
while (_isProcessing) {
emit processingStarted();
isWaiting = false;
emit processingFinished(process());
isWaiting = true;
if (queued) {
queued = false;
continue;
}
QMutexLocker locker(&mutex);
condition.wait(&mutex);
}
}
QList<QObject*> BackgroundInputProcessor::process()
{
QString input = lastInput;
QList<QObject*> result = parser->parse(input);
for (QObject* item : result)
item->moveToThread(QGuiApplication::instance()->thread());
return result;
}
bool BackgroundInputProcessor::isProcessing() const
{
return _isProcessing;
}
void BackgroundInputProcessor::setIsProcessing(bool isProcessing)
{
_isProcessing = isProcessing;
condition.wakeAll();
}
void BackgroundInputProcessor::stop(int waitingTime)
{
setIsProcessing(false);
if (isRunning() && waitingTime > 0) {
if (!wait(waitingTime)) {
terminate();
}
}
}