aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/hunspellinputmethod.cpp
blob: 09308d05c0c40f9094410767e5fa3a8a9241f4bf (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://www.qt.io
**
** This file is part of the Qt Virtual Keyboard add-on for Qt Enterprise.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io
**
****************************************************************************/

#include "hunspellinputmethod.h"
#include "hunspellworker.h"
#include "declarativeinputcontext.h"
#include <hunspell/hunspell.h>
#include <QStringList>
#include <QFileInfo>
#include "virtualkeyboarddebug.h"
#include <QTextCodec>

class HunspellInputMethodPrivate : public AbstractInputMethodPrivate
{
    Q_DECLARE_PUBLIC(HunspellInputMethod)

public:
    HunspellInputMethodPrivate(HunspellInputMethod *q_ptr) :
        AbstractInputMethodPrivate(),
        q_ptr(q_ptr),
        hunspellWorker(0),
        locale(),
        word(),
        wordCandidates(),
        activeWordIndex(-1),
        wordCompletionPoint(2)
    {
    }

    ~HunspellInputMethodPrivate()
    {
    }

    bool createHunspell(const QString &locale)
    {
        if (this->locale != locale) {
            hunspellWorker.reset(0);
            Hunhandle *hunspell = 0;
            QString hunspellDataPath(QString::fromLatin1(qgetenv("QT_VIRTUALKEYBOARD_HUNSPELL_DATA_PATH").constData()));
            if (hunspellDataPath.isEmpty())
                hunspellDataPath = QT_VIRTUALKEYBOARD_HUNSPELL_DATA_PATH;
            QStringList searchPaths(hunspellDataPath.split(":"));
            foreach (const QString &searchPath, searchPaths) {
                QByteArray affpath(QString("%1/%2.aff").arg(searchPath).arg(locale).toUtf8());
                QByteArray dpath(QString("%1/%2.dic").arg(searchPath).arg(locale).toUtf8());
                if (QFileInfo(dpath).exists()) {
                    hunspell = Hunspell_create(affpath.constData(), dpath.constData());
                    if (hunspell) {
                        /*  Make sure the encoding used by the dictionary is supported
                            by the QTextCodec.
                        */
                        if (QTextCodec::codecForName(Hunspell_get_dic_encoding(hunspell))) {
                            break;
                        } else {
                            qWarning() << "The Hunspell dictionary" << QString("%1/%2.dic").arg(searchPath).arg(locale) << "cannot be used because it uses an unknown text codec" << QString(Hunspell_get_dic_encoding(hunspell));
                            Hunspell_destroy(hunspell);
                            hunspell = 0;
                        }
                    }
                }
            }
            if (!hunspell) {
                qWarning() << "Missing Hunspell dictionary for locale" << locale << "in" << searchPaths;
                this->locale.clear();
                return false;
            }
            this->locale = locale;
            hunspellWorker.reset(new HunspellWorker(hunspell));
            if (!hunspellWorker) {
                Hunspell_destroy(hunspell);
                this->locale.clear();
                return false;
            }
            hunspellWorker->start();
        }
        return true;
    }

    bool updateSuggestions()
    {
        bool wordCandidateListChanged = false;
        if (!word.isEmpty()) {
            if (hunspellWorker)
                hunspellWorker->removeAllTasks();
            if (wordCandidates.isEmpty()) {
                wordCandidates.append(word);
                activeWordIndex = 0;
                wordCandidateListChanged = true;
            } else if (wordCandidates.at(0) != word) {
                wordCandidates.replace(0, word);
                activeWordIndex = 0;
                wordCandidateListChanged = true;
            }
            if (word.length() >= wordCompletionPoint) {
                if (hunspellWorker) {
                    QSharedPointer<HunspellWordList> wordList(new HunspellWordList());
                    QSharedPointer<HunspellBuildSuggestionsTask> buildSuggestionsTask(new HunspellBuildSuggestionsTask());
                    buildSuggestionsTask->word = word;
                    buildSuggestionsTask->wordList = wordList;
                    buildSuggestionsTask->autoCorrect = false;
                    hunspellWorker->addTask(buildSuggestionsTask);
                    QSharedPointer<HunspellUpdateSuggestionsTask> updateSuggestionsTask(new HunspellUpdateSuggestionsTask());
                    updateSuggestionsTask->wordList = wordList;
                    Q_Q(HunspellInputMethod);
                    q->connect(updateSuggestionsTask.data(), SIGNAL(updateSuggestions(QStringList, int)), SLOT(updateSuggestions(QStringList, int)));
                    hunspellWorker->addTask(updateSuggestionsTask);
                }
            } else if (wordCandidates.length() > 1) {
                wordCandidates.clear();
                wordCandidates.append(word);
                activeWordIndex = 0;
                wordCandidateListChanged = true;
            }
        } else {
            wordCandidateListChanged = clearSuggestions();
        }
        return wordCandidateListChanged;
    }

    bool clearSuggestions()
    {
        if (hunspellWorker)
            hunspellWorker->removeAllTasks();
        if (wordCandidates.isEmpty())
            return false;
        wordCandidates.clear();
        activeWordIndex = -1;
        return true;
    }

    bool hasSuggestions() const
    {
        return !wordCandidates.isEmpty();
    }

    bool isAutoSpaceAllowed() const
    {
        Q_Q(const HunspellInputMethod);
        if (q->inputEngine()->inputMode() != DeclarativeInputEngine::Latin)
            return false;
        DeclarativeInputContext *ic = q->inputContext();
        if (!ic)
            return false;
        Qt::InputMethodHints inputMethodHints = ic->inputMethodHints();
        return !inputMethodHints.testFlag(Qt::ImhUrlCharactersOnly) &&
               !inputMethodHints.testFlag(Qt::ImhEmailCharactersOnly);
    }

    HunspellInputMethod *q_ptr;
    QScopedPointer<HunspellWorker> hunspellWorker;
    QString locale;
    QString word;
    QStringList wordCandidates;
    int activeWordIndex;
    int wordCompletionPoint;
};

HunspellInputMethod::HunspellInputMethod(QObject *parent) :
    AbstractInputMethod(*new HunspellInputMethodPrivate(this), parent)
{
}

HunspellInputMethod::~HunspellInputMethod()
{
}

QList<DeclarativeInputEngine::InputMode> HunspellInputMethod::inputModes(const QString &locale)
{
    Q_UNUSED(locale)
    return QList<DeclarativeInputEngine::InputMode>() << DeclarativeInputEngine::Latin << DeclarativeInputEngine::Numeric;
}

bool HunspellInputMethod::setInputMode(const QString &locale, DeclarativeInputEngine::InputMode inputMode)
{
    Q_UNUSED(inputMode)
    Q_D(HunspellInputMethod);
    return d->createHunspell(locale);
}

bool HunspellInputMethod::setTextCase(DeclarativeInputEngine::TextCase textCase)
{
    Q_UNUSED(textCase)
    return true;
}

bool HunspellInputMethod::keyEvent(Qt::Key key, const QString &text, Qt::KeyboardModifiers modifiers)
{
    Q_UNUSED(modifiers)
    Q_D(HunspellInputMethod);
    DeclarativeInputContext *ic = inputContext();
    bool accept = false;
    switch (key) {
    case Qt::Key_Enter:
    case Qt::Key_Return:
    case Qt::Key_Tab:
    case Qt::Key_Space:
        update();
        break;
    case Qt::Key_Backspace:
        if (!d->word.isEmpty()) {
            d->word.remove(d->word.length() - 1, 1);
            ic->setPreeditText(d->word);
            if (d->updateSuggestions()) {
                emit selectionListChanged(DeclarativeSelectionListModel::WordCandidateList);
                emit selectionListActiveItemChanged(DeclarativeSelectionListModel::WordCandidateList, d->activeWordIndex);
            }
            accept = true;
        }
        break;
    default:
        if (ic->inputMethodHints().testFlag(Qt::ImhNoPredictiveText))
            break;
        if (text.length() > 0) {
            QChar c = text.at(0);
            bool addToWord = !c.isPunct() && !c.isSymbol();
            if (!addToWord) {
                if (!d->word.isEmpty()) {
                    if (c == Qt::Key_Apostrophe || c == Qt::Key_Minus)
                        addToWord = true;
                }
            }
            if (addToWord) {
                /*  Automatic space insertion. */
                if (d->word.isEmpty()) {
                    QString surroundingText = ic->surroundingText();
                    int cursorPosition = ic->cursorPosition();
                    /*  Rules for automatic space insertion:
                        - Surrounding text is not empty
                        - Cursor is at the end of the line
                        - No space before the cursor
                        - No spefic characters before the cursor; minus and apostrophe
                    */
                    if (!surroundingText.isEmpty() && cursorPosition == surroundingText.length()) {
                        QChar lastChar = surroundingText.at(cursorPosition - 1);
                        if (!lastChar.isSpace() &&
                            lastChar != Qt::Key_Minus &&
                            lastChar != Qt::Key_Apostrophe &&
                            d->isAutoSpaceAllowed()) {
                            ic->commit(" ");
                        }
                    }
                }
                d->word.append(text);
                ic->setPreeditText(d->word);
                if (d->updateSuggestions()) {
                    emit selectionListChanged(DeclarativeSelectionListModel::WordCandidateList);
                    emit selectionListActiveItemChanged(DeclarativeSelectionListModel::WordCandidateList, d->activeWordIndex);
                }
                accept = true;
            } else if (text.length() > 1) {
                bool addSpace = !d->word.isEmpty();
                update();
                if (addSpace && d->isAutoSpaceAllowed())
                    ic->commit(" ");
                ic->commit(text);
                accept = true;
            } else {
                update();
            }
        }
        break;
    }
    return accept;
}

QList<DeclarativeSelectionListModel::Type> HunspellInputMethod::selectionLists()
{
    return QList<DeclarativeSelectionListModel::Type>() << DeclarativeSelectionListModel::WordCandidateList;
}

int HunspellInputMethod::selectionListItemCount(DeclarativeSelectionListModel::Type type)
{
    Q_UNUSED(type)
    Q_D(HunspellInputMethod);
    return d->wordCandidates.count();
}

QVariant HunspellInputMethod::selectionListData(DeclarativeSelectionListModel::Type type, int index, int role)
{
    QVariant result;
    Q_UNUSED(type)
    Q_D(HunspellInputMethod);
    switch (role) {
    case DeclarativeSelectionListModel::DisplayRole:
        result = QVariant(d->wordCandidates.at(index));
        break;
    case DeclarativeSelectionListModel::WordCompletionLengthRole:
    {
        const QString wordCandidate(d->wordCandidates.at(index));
        int wordCompletionLength = wordCandidate.length() - d->word.length();
        result.setValue((wordCompletionLength > 0 && wordCandidate.startsWith(d->word)) ? wordCompletionLength : 0);
        break;
    }
    default:
        result = AbstractInputMethod::selectionListData(type, index, role);
        break;
    }
    return result;
}

void HunspellInputMethod::selectionListItemSelected(DeclarativeSelectionListModel::Type type, int index)
{
    Q_UNUSED(type)
    Q_D(HunspellInputMethod);
    QString finalWord = d->wordCandidates.at(index);
    reset();
    inputContext()->commit(finalWord);
}

void HunspellInputMethod::reset()
{
    Q_D(HunspellInputMethod);
    if (d->clearSuggestions()) {
        emit selectionListChanged(DeclarativeSelectionListModel::WordCandidateList);
        emit selectionListActiveItemChanged(DeclarativeSelectionListModel::WordCandidateList, d->activeWordIndex);
    }
    d->word.clear();
}

void HunspellInputMethod::update()
{
    Q_D(HunspellInputMethod);
    if (!d->word.isEmpty()) {
        QString finalWord = d->hasSuggestions() ? d->wordCandidates.at(d->activeWordIndex) : d->word;
        reset();
        inputContext()->commit(finalWord);
    }
}

void HunspellInputMethod::updateSuggestions(const QStringList &wordList, int activeWordIndex)
{
    Q_D(HunspellInputMethod);
    d->wordCandidates.clear();
    d->wordCandidates.append(wordList);
    d->activeWordIndex = activeWordIndex;
    emit selectionListChanged(DeclarativeSelectionListModel::WordCandidateList);
    emit selectionListActiveItemChanged(DeclarativeSelectionListModel::WordCandidateList, d->activeWordIndex);
}