1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2016 The Qt Company Ltd. 4 ** Contact: https://www.qt.io/licensing/ 5 ** 6 ** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:GPL$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see https://www.qt.io/terms-conditions. For further 15 ** information use the contact form at https://www.qt.io/contact-us. 16 ** 17 ** GNU General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU 19 ** General Public License version 3 or (at your option) any later version 20 ** approved by the KDE Free Qt Foundation. The licenses are as published by 21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3 22 ** included in the packaging of this file. Please review the following 23 ** information to ensure the GNU General Public License requirements will 24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html. 25 ** 26 ** $QT_END_LICENSE$ 27 ** 28 ****************************************************************************/ 29 30 #ifndef T9WRITEWORKER_H 31 #define T9WRITEWORKER_H 32 33 #include "trace.h" 34 35 #include <QThread> 36 #include <QSemaphore> 37 #include <QMutex> 38 #include <QStringList> 39 #include <QSharedPointer> 40 #include <QPointer> 41 #include <QMap> 42 #include <QVector> 43 44 #include "decuma_hwr.h" 45 46 namespace QtVirtualKeyboard { 47 48 class T9WriteTask : public QObject 49 { 50 Q_OBJECT 51 public: 52 explicit T9WriteTask(QObject *parent = 0); 53 54 virtual void run() = 0; 55 56 void wait(); 57 58 friend class T9WriteWorker; 59 60 protected: 61 DECUMA_SESSION *decumaSession; 62 63 private: 64 QSemaphore runSema; 65 }; 66 67 class T9WriteDictionaryTask : public T9WriteTask 68 { 69 Q_OBJECT 70 public: 71 explicit T9WriteDictionaryTask(const QString &fileUri, 72 const DECUMA_MEM_FUNCTIONS &memFuncs); 73 74 void run(); 75 76 const QString fileUri; 77 const DECUMA_MEM_FUNCTIONS &memFuncs; 78 79 signals: 80 void completed(const QString &fileUri, void *dictionary); 81 }; 82 83 class T9WriteRecognitionResult 84 { 85 Q_DISABLE_COPY(T9WriteRecognitionResult) 86 87 public: 88 explicit T9WriteRecognitionResult(int id, int maxResults, int maxCharsPerWord); 89 90 QVector<DECUMA_HWR_RESULT> results; 91 DECUMA_UINT16 numResults; 92 int instantGesture; 93 const int id; 94 const int maxResults; 95 const int maxCharsPerWord; 96 private: 97 QVector<DECUMA_UNICODE> _chars; 98 QVector<DECUMA_INT16> _symbolChars; 99 QVector<DECUMA_INT16> _symbolStrokes; 100 }; 101 102 class T9WriteRecognitionTask : public T9WriteTask 103 { 104 Q_OBJECT 105 public: 106 explicit T9WriteRecognitionTask(QSharedPointer<T9WriteRecognitionResult> result, 107 const DECUMA_INSTANT_GESTURE_SETTINGS &instantGestureSettings, 108 BOOST_LEVEL boostLevel, 109 const QString &stringStart); 110 111 void run(); 112 bool cancelRecognition(); 113 int resultId() const; 114 115 private: 116 static int shouldAbortRecognize(void *pUserData); 117 friend int shouldAbortRecognize(void *pUserData); 118 119 private: 120 QSharedPointer<T9WriteRecognitionResult> result; 121 DECUMA_INSTANT_GESTURE_SETTINGS instantGestureSettings; 122 BOOST_LEVEL boostLevel; 123 QString stringStart; 124 QMutex stateLock; 125 bool stateCancelled; 126 }; 127 128 class T9WriteRecognitionResultsTask : public T9WriteTask 129 { 130 Q_OBJECT 131 public: 132 explicit T9WriteRecognitionResultsTask(QSharedPointer<T9WriteRecognitionResult> result); 133 134 void run(); 135 136 signals: 137 void resultsAvailable(const QVariantList &resultList); 138 139 private: 140 QSharedPointer<T9WriteRecognitionResult> result; 141 }; 142 143 class T9WriteWorker : public QThread 144 { 145 Q_OBJECT 146 public: 147 explicit T9WriteWorker(DECUMA_SESSION *decumaSession, QObject *parent = 0); 148 ~T9WriteWorker(); 149 150 void addTask(QSharedPointer<T9WriteTask> task); 151 int removeTask(QSharedPointer<T9WriteTask> task); 152 int removeAllTasks(); 153 154 template <class X> removeAllTasks()155 int removeAllTasks() { 156 QMutexLocker guard(&taskLock); 157 int count = 0; 158 for (int i = 0; i < taskList.size();) { 159 QSharedPointer<X> task(taskList[i].objectCast<X>()); 160 if (task) { 161 taskList.removeAt(i); 162 ++count; 163 } else { 164 ++i; 165 } 166 } 167 return count; 168 } 169 170 protected: 171 void run(); 172 173 private: 174 QList<QSharedPointer<T9WriteTask> > taskList; 175 QSemaphore taskSema; 176 QMutex taskLock; 177 DECUMA_SESSION *decumaSession; 178 bool abort; 179 }; 180 181 } // namespace QtVirtualKeyboard 182 183 #endif // T9WRITEWORKER_H 184