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
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\example webenginewidgets/spellchecker
\title WebEngine Widgets Spellchecker Example
\ingroup webengine-widgetexamples
\brief Integrates a spellchecker into a simple HTML form.
\examplecategory {Web Technologies}
\image spellchecker-example.png
\e {Spellchecker} demonstrates how to integrate spellchecking support into
an HTML form that enables users to submit spellchecked messages.
\include examples-run.qdocinc
\section1 Dictionaries
To be able to check the spelling, we need to provide the spellchecker with
dictionaries. The \QWE spellchecker supports dictionaries provided by the
\l {Hunspell project} on all platforms and native dictionaries provided by macOS.
In this example, we want to support the English and German languages.
For Hunspell dictionaries to be supported they have to be compiled into a special binary format.
A Hunspell dictionary consists of two files:
\list
\li A \c .dic file that is a dictionary containing words for the
language
\li An \c .aff file that defines the meaning of special flags in the
dictionary
\endlist
These two files can be converted into the \c bdic format by using the
\c qwebengine_convert_dict tool that is shipped together with Qt.
In this example, we are going to compile en_US and de_DE dictionaries.
However, the real full dictionaries would take too much space for the
purposes of this example. Therefore, we have created two dummy dictionaries
that contain the following words and can be used to demonstrate the
conversion process:
\list
\li English dictionary: I, you, he, she, it, we, they, love, loves, qt
\li German dictionary: ich, du, er, sie, es, wir, ihr, sie, Sie, liebe,
liebst, liebt, lieben, liebt, qt
\endlist
Each word in a dictionary can be prefixed with \c q. For more information
about how to create \c dic and \c aff files, see the Hunspell dictionary
file format specification in the \l{Hunspell Project}.
See the \l {Spellchecker}{Spellchecker feature documentation} for how
dictionary files are searched.
We specify the QMAKE_EXTRA_COMPILERS parameter in the project file to add a
conversion step to the build process:
\quotefromfile webenginewidgets/spellchecker/spellchecker.pro
\skipto CONVERT_TOOL
\printuntil QMAKE_EXTRA_COMPILERS
To set up a dictionary, we run \c qwebengine_convert_dict passing the
file path of the dictionary \c dic and \c bdic files. The \c aff file and
optional \c delta file are also picked up by the \c convert process.
The output \c bdic file is placed into the \e qtwebengine_dictionaries local
directory (or Resources directory), which the application binary will run from.
\section1 Setting the Spellchecker
The constructor of our class is trivial.
\quotefromfile webenginewidgets/spellchecker/webview.cpp
\skipto WebView::WebView
\printuntil }
We define simple mapping between our dictionary filenames and
the actual language display name. We will use that mapping to display names
of dictionaries in the context menu. Spellchecking is disabled by default.
Therefore we also enable spellchecker and set the \e English dictionary.
When \QWE's spellcheck service initializes, it will try to load the
\c bdict dictionaries and to check them for consistency.
Any errors are logged by using the qWarning() function.
\section1 Switching the Spellchecking Language
The current language used for spellchecking is defined per profile, and can
get set using the QWebEngineProfile::setSpellCheckLanguage method. When the
user clicks on an underlined misspelled word, the default context menu
displays up to four suggestions. Selecting one will replace the misspelled
word. We could reimplement a number of suggestions, by overriding
QWebEngineView::contextMenuEvent and using
QWebEngineContextMenuData::spellCheckerSuggestions, but we will demonstrate
how to add langague options in the context menu instead:
\quotefromfile webenginewidgets/spellchecker/webview.cpp
\skipto void WebView::contextMenuEvent
\printuntil menu->popup
\printline }
Above, we get the QWebEngineContextMenuData instance using the
QWebEnginePage::contextMenuData method. We use it to be notified when the
user clicks on an editable field and show the \uicontrol {Check Spelling}
item in the context menu. Moreover, if spellchecking is enabled, we also
add the \uicontrol {Select Language} submenu with the supported languages.
When an action is triggered, we set the language with the
QWebEngineProfile::setSpellCheckLanguage call.
*/
|