1/*
2 * highlighter.h
3 *
4 * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org>
5 * SPDX-FileCopyrightText: 2013 Martin Sandsmark <martin.sandsmark@kde.org>
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 */
9#ifndef SONNET_HIGHLIGHTER_H
10#define SONNET_HIGHLIGHTER_H
11
12#include "sonnetui_export.h"
13#include <QStringList>
14#include <QSyntaxHighlighter>
15
16#include <memory>
17
18class QTextEdit;
19class QPlainTextEdit;
20
21namespace Sonnet
22{
23class HighlighterPrivate;
24
25/*!
26 * \class Sonnet::Highlighter
27 * \inheaderfile Sonnet/Highlighter
28 * \inmodule SonnetUi
29 * \brief The sonnet Highlighter.
30 *
31 * Used for drawing pretty red lines in text fields
32 */
33class SONNETUI_EXPORT Highlighter : public QSyntaxHighlighter
34{
35 Q_OBJECT
36public:
37 /*!
38 */
39 explicit Highlighter(QTextEdit *textEdit, const QColor &col = QColor());
40
41 /*!
42 * Highlighter.
43 *
44 * \a col define spellchecking color
45 *
46 * \since 5.12
47 */
48 explicit Highlighter(QPlainTextEdit *textEdit, const QColor &col = QColor());
49 ~Highlighter() override;
50
51 /*!
52 * Returns whether a spell checking backend with support for the
53 * currentLanguage() was found.
54 *
55 * Returns true if spell checking is supported for the current language.
56 */
57 bool spellCheckerFound() const;
58
59 /*!
60 * Returns the language code for the current language.
61 */
62 QString currentLanguage() const;
63
64 /*!
65 * \brief Enable/Disable spell checking.
66 *
67 * If \a active is true then spell checking is enabled; otherwise it
68 * is disabled. Note that you have to disable automatic (de)activation
69 * with \l setAutomatic() before you change the state of spell
70 * checking if you want to persistently enable/disable spell
71 * checking.
72 *
73 * \a active if true, then spell checking is enabled
74 *
75 * \sa isActive(), setAutomatic()
76 */
77 void setActive(bool active);
78
79 /*!
80 * Returns the state of spell checking.
81 *
82 * Returns true if spell checking is active
83 *
84 * \sa setActive()
85 */
86 bool isActive() const;
87
88 /*!
89 * Returns the state of the automatic disabling of spell checking.
90 *
91 * Returns true if spell checking is automatically disabled if there's
92 * too many errors
93 */
94 bool automatic() const;
95
96 /*!
97 * Sets whether to automatically disable spell checking if there's too
98 * many errors.
99 *
100 * \a automatic if true, spell checking will be disabled if there's
101 * a significant amount of errors.
102 */
103 void setAutomatic(bool automatic);
104
105 /*!
106 * Returns whether the automatic language detection is disabled,
107 * overriding the Sonnet settings.
108 *
109 * Returns true if the automatic language detection is disabled
110 * \since 5.71
111 */
112 bool autoDetectLanguageDisabled() const;
113
114 /*!
115 * Sets whether to disable the automatic language detection.
116 *
117 * \a autoDetectDisabled if true, the language will not be
118 * detected automatically by the spell checker, even if the option
119 * is enabled in the Sonnet settings.
120 * \since 5.71
121 */
122 void setAutoDetectLanguageDisabled(bool autoDetectDisabled);
123
124 /*!
125 * Adds the given word permanently to the dictionary. It will never
126 * be marked as misspelled again, even after restarting the application.
127 *
128 * \a word the word which will be added to the dictionary
129 * \since 4.1
130 */
131 void addWordToDictionary(const QString &word);
132
133 /*!
134 * Ignores the given word. This word will not be marked misspelled for
135 * this session. It will again be marked as misspelled when creating
136 * new highlighters.
137 *
138 * \a word the word which will be ignored
139 * \since 4.1
140 */
141 void ignoreWord(const QString &word);
142
143 /*!
144 * Returns a list of suggested replacements for the given misspelled word.
145 * If the word is not misspelled, the list will be empty.
146 *
147 * \a word the misspelled word
148 *
149 * \a max at most this many suggestions will be returned. If this is
150 * -1, as many suggestions as the spell backend supports will
151 * be returned.
152 *
153 * Returns a list of suggested replacements for the word
154 * \since 4.1
155 */
156 QStringList suggestionsForWord(const QString &word, int max = 10);
157
158 /*!
159 * Returns a list of suggested replacements for the given misspelled word.
160 * If the word is not misspelled, the list will be empty.
161 *
162 * \a word the misspelled word
163 *
164 * \a cursor the cursor pointing to the beginning of that word. This is used
165 * to determine the language to use, when AutoDetectLanguage is enabled.
166 *
167 * \a max at most this many suggestions will be returned. If this is
168 * -1, as many suggestions as the spell backend supports will
169 * be returned.
170 *
171 * Returns a list of suggested replacements for the word
172 * \since 5.42
173 */
174 QStringList suggestionsForWord(const QString &word, const QTextCursor &cursor, int max = 10);
175
176 /*!
177 * Checks if a given word is marked as misspelled by the highlighter.
178 *
179 * \a word the word to be checked
180 *
181 * Returns true if the given word is misspelled.
182 * \since 4.1
183 */
184 bool isWordMisspelled(const QString &word);
185
186 /*!
187 * Sets the color in which the highlighter underlines misspelled words.
188 * \since 4.2
189 */
190 void setMisspelledColor(const QColor &color);
191
192 /*!
193 * Return true if checker is enabled by default
194 * \since 4.5
195 */
196 bool checkerEnabledByDefault() const;
197
198 /*!
199 * Set a new \l QTextDocument for this highlighter to operate on.
200 *
201 * \a document the new document to operate on.
202 */
203 void setDocument(QTextDocument *document);
204
205Q_SIGNALS:
206
207 /*!
208 * Emitted when as-you-type spell checking is enabled or disabled.
209 *
210 * \a description is a i18n description of the new state,
211 * with an optional reason
212 */
213 void activeChanged(const QString &description);
214
215protected:
216 void highlightBlock(const QString &text) override;
217 /*!
218 */
219 virtual void setMisspelled(int start, int count);
220 /*!
221 */
222 virtual void unsetMisspelled(int start, int count);
223
224 bool eventFilter(QObject *o, QEvent *e) override;
225 /*!
226 */
227 bool intraWordEditing() const;
228 /*!
229 */
230 void setIntraWordEditing(bool editing);
231
232public Q_SLOTS:
233 /*!
234 * Set language to use for spell checking.
235 *
236 * \a language the language code for the new language to use.
237 */
238 void setCurrentLanguage(const QString &language);
239
240 /*!
241 * Run auto detection, disabling spell checking if too many errors are found.
242 */
243 void slotAutoDetection();
244
245 /*!
246 * Force a new highlighting.
247 */
248 void slotRehighlight();
249
250private Q_SLOTS:
251 SONNETUI_NO_EXPORT void contentsChange(int pos, int added, int removed);
252
253private:
254 std::unique_ptr<HighlighterPrivate> const d;
255 Q_DISABLE_COPY(Highlighter)
256};
257}
258
259#endif
260

source code of sonnet/src/ui/highlighter.h