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