Sonnet

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

KDE's Doxygen guidelines are available online.