aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cerence/hwr/plugin/t9writeworker_p.h
blob: 34c29afed56bc0d67760072de7e67cc5a487e42c (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef T9WRITEWORKER_H
#define T9WRITEWORKER_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtVirtualKeyboard/qvirtualkeyboardtrace.h>

#include <QThread>
#include <QSemaphore>
#include <QMutex>
#include <QStringList>
#include <QSharedPointer>
#include <QPointer>
#include <QMap>
#include <QList>
#include <QElapsedTimer>

#include "cerence_hwr_p.h"
#include "t9writedictionary_p.h"

QT_BEGIN_NAMESPACE
namespace QtVirtualKeyboard {

class T9WriteTask : public QObject
{
    Q_OBJECT
public:
    explicit T9WriteTask(QObject *parent = nullptr);

    virtual void run() = 0;

    void wait();

    friend class T9WriteWorker;

protected:
    DECUMA_SESSION *decumaSession;
    bool cjk;

private:
    QSemaphore runSema;
};

class T9WriteDictionaryTask : public T9WriteTask
{
    Q_OBJECT
public:
    explicit T9WriteDictionaryTask(QSharedPointer<T9WriteAbstractDictionary> dictionary,
                                   bool convertDictionary);

    void run();

    QSharedPointer<T9WriteAbstractDictionary> dictionary;
    QSharedPointer<T9WriteAbstractSource> source;
    const QString dictionaryFileName;
    bool convertDictionary;

signals:
    void completed(QSharedPointer<T9WriteAbstractDictionary> dictionary);
};

class T9WriteAddArcTask : public T9WriteTask
{
    Q_OBJECT
public:
    explicit T9WriteAddArcTask(QVirtualKeyboardTrace *trace);

    void run();

private:
    QVirtualKeyboardTrace *trace;
};

class T9WriteRecognitionResult
{
    Q_DISABLE_COPY(T9WriteRecognitionResult)

public:
    explicit T9WriteRecognitionResult(int id, int maxResults, int maxCharsPerWord);

    DECUMA_STATUS status;
    QList<DECUMA_HWR_RESULT> results;
    DECUMA_UINT16 numResults;
    int instantGesture;
    const int id;
    const int maxResults;
    const int maxCharsPerWord;

private:
    QList<DECUMA_UNICODE> _chars;
    QList<DECUMA_INT16> _symbolChars;
    QList<DECUMA_INT16> _symbolStrokes;
};

class T9WriteRecognitionTask : public T9WriteTask
{
    Q_OBJECT
public:
    explicit T9WriteRecognitionTask(QSharedPointer<T9WriteRecognitionResult> result);

    void run();
    bool cancelRecognition();
    bool checkCancelled();
    int resultId() const;

private:
    void waitForBackgroundRecognition();

private:
    QSharedPointer<T9WriteRecognitionResult> result;
    QMutex stateLock;
    bool stateCancelled;
    QElapsedTimer perf;
};

class T9WriteRecognitionResultsTask : public T9WriteTask
{
    Q_OBJECT
public:
    explicit T9WriteRecognitionResultsTask(QSharedPointer<T9WriteRecognitionResult> result);

    void run();

signals:
    void resultsAvailable(const QVariantList &resultList);
    void recognitionError(int status);

private:
    QSharedPointer<T9WriteRecognitionResult> result;
};

class T9WriteDlmWordTask : public T9WriteTask
{
    Q_OBJECT
public:
    explicit T9WriteDlmWordTask(QSharedPointer<T9WriteAbstractDictionary> dlmDictionary, const QString &word, const QString &stringStart);

    void run();

protected:
    void persist();

protected:
    QSharedPointer<T9WriteAbstractDictionary> dlmDictionary;
    const QString word;
    const QString stringStart;
};

class T9WriteDlmRemoveWordTask : public T9WriteDlmWordTask
{
    Q_OBJECT
public:
    explicit T9WriteDlmRemoveWordTask(QSharedPointer<T9WriteAbstractDictionary> dlmDictionary, const QString &word);

    void run();
};

class T9WriteWorker : public QThread
{
    Q_OBJECT
public:
    explicit T9WriteWorker(DECUMA_SESSION *decumaSession, const bool cjk, QObject *parent = nullptr);
    ~T9WriteWorker();

    void addTask(QSharedPointer<T9WriteTask> task);
    int removeTask(QSharedPointer<T9WriteTask> task);
    int removeAllTasks();
    void waitForAllTasks();
    int numberOfPendingTasks();

    template <class X>
    int removeAllTasks() {
        QMutexLocker guard(&taskLock);
        int count = 0;
        for (int i = 0; i < taskList.size();) {
            QSharedPointer<X> task(taskList[i].objectCast<X>());
            if (task) {
                taskList.removeAt(i);
                ++count;
            } else {
                ++i;
            }
        }
        return count;
    }

    template <class X>
    void waitForAllTasksOfType() {
        QSharedPointer<X> task;
        {
            QMutexLocker guard(&taskLock);
            for (int i = taskList.size() - 1; i >= 0; --i) {
                task = taskList[i].objectCast<X>();
                if (task)
                    break;
            }
        }
        if (task)
            task->wait();
    }

protected:
    void run();

private:
    QList<QSharedPointer<T9WriteTask> > taskList;
    QSemaphore idleSema;
    QSemaphore taskSema;
    QMutex taskLock;
    DECUMA_SESSION *decumaSession;
    QBasicAtomicInt abort;
    const bool cjk;
};

} // namespace QtVirtualKeyboard
QT_END_NAMESPACE

#endif // T9WRITEWORKER_H