Sonnet

spellcheckhighlighter.h
1 // SPDX-FileCopyrightText: 2004 Zack Rusin <[email protected]>
2 // SPDX-FileCopyrightText: 2013 Martin Sandsmark <[email protected]>
3 // SPDX-FileCopyrightText: 2013 Aurélien Gâteau <[email protected]>
4 // SPDX-FileCopyrightText: 2020 Christian Mollekopf <[email protected]>
5 // SPDX-FileCopyrightText: 2021 Carl Schwan <[email protected]>
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 
8 #pragma once
9 
10 // TODO KF6 create AbstractSpellcheckHighlighter and make the QtQuick and QtWidget inherit
11 // from it.
12 
13 #include <QQuickTextDocument>
14 #include <QSyntaxHighlighter>
15 
16 class HighlighterPrivate;
17 
18 /// \brief The Sonnet Highlighter class, used for drawing red lines in text fields
19 /// when detecting spelling mistakes.
20 ///
21 /// SpellcheckHighlighter is adapted for QML applications. In usual Kirigami/QQC2-desktop-style
22 /// applications, this can be used directly by adding `Kirigami.SpellChecking.enabled: true` on
23 /// a TextArea/TextField.
24 ///
25 /// On other QML applications, you can add the SpellcheckHighlighter as a child of a TextArea/TextField.
26 ///
27 /// \code{.qml}
28 /// TextArea {
29 /// id: textArea
30 /// Sonnet.SpellcheckHighlighter {
31 /// id: spellcheckhighlighter
32 /// document: textArea.textDocument
33 /// cursorPosition: textArea.cursorPosition
34 /// selectionStart: textArea.selectionStart
35 /// selectionEnd: textArea.selectionEnd
36 /// misspelledColor: Kirigami.Theme.negativeTextColor
37 /// active: true
38 ///
39 /// onChangeCursorPosition: {
40 /// textArea.cursorPosition = start;
41 /// textArea.moveCursorSelection(end, TextEdit.SelectCharacters);
42 /// }
43 /// }
44 /// }
45 /// \endcode
46 ///
47 /// Additionally SpellcheckHighlighter provides some convenient methods to create
48 /// a context menu with suggestions. \see suggestions
49 ///
50 /// \since 5.88
52 {
53  Q_OBJECT
54  /// This property holds the underneath document from a QML TextEdit.
55  /// \since 5.88
56  Q_PROPERTY(QQuickTextDocument *document READ quickDocument WRITE setQuickDocument NOTIFY documentChanged)
57 
58  /// This property holds the current cursor position.
59  /// \since 5.88
60  Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
61 
62  /// This property holds the start of the selection.
63  /// \since 5.88
64  Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
65 
66  /// This property holds the end of the selection.
67  /// \since 5.88
68  Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
69 
70  /// This property holds whether the current word under the mouse is misspelled.
71  /// \since 5.88
72  Q_PROPERTY(bool wordIsMisspelled READ wordIsMisspelled NOTIFY wordIsMisspelledChanged)
73 
74  /// This property holds the current word under the mouse.
75  /// \since 5.88
76  Q_PROPERTY(QString wordUnderMouse READ wordUnderMouse NOTIFY wordUnderMouseChanged)
77 
78  /// This property holds the spell color. By default, it's red.
79  /// \since 5.88
80  Q_PROPERTY(QColor misspelledColor READ misspelledColor WRITE setMisspelledColor NOTIFY misspelledColorChanged)
81 
82  /// This property holds the current language used for spell checking.
83  /// \since 5.88
84  Q_PROPERTY(QString currentLanguage READ currentLanguage NOTIFY currentLanguageChanged)
85 
86  /// This property holds whether a spell checking backend with support for the
87  /// \ref currentLanguage was found.
88  /// \since 5.88
90 
91  /// \brief This property holds whether spell checking is enabled.
92  ///
93  /// If \p active is true then spell checking is enabled; otherwise it
94  /// is disabled. Note that you have to disable automatic (de)activation
95  /// with \ref automatic before you change the state of spell
96  /// checking if you want to persistently enable/disable spell
97  /// checking.
98  ///
99  /// \see automatic
100  /// \since 5.88
101  Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
102 
103  /// This property holds whether spell checking is automatically disabled
104  /// if there's too many errors.
105  /// \since 5.88
106  Q_PROPERTY(bool automatic READ automatic WRITE setAutomatic NOTIFY automaticChanged)
107 
108  /// This property holds whether the automatic language detection is disabled
109  /// overriding the Sonnet global settings.
110  /// \since 5.88
111  Q_PROPERTY(bool autoDetectLanguageDisabled READ autoDetectLanguageDisabled WRITE setAutoDetectLanguageDisabled NOTIFY autoDetectLanguageDisabledChanged)
112 
113 public:
114  explicit SpellcheckHighlighter(QObject *parent = nullptr);
115 
116  /// Returns a list of suggested replacements for the given misspelled word.
117  /// If the word is not misspelled, the list will be empty.
118  ///
119  /// \param word the misspelled word
120  /// \param max at most this many suggestions will be returned. If this is
121  /// -1, as many suggestions as the spell backend supports will
122  /// be returned.
123  /// \return a list of suggested replacements for the word
124  /// \since 5.88
125  Q_INVOKABLE QStringList suggestions(int position, int max = 5);
126 
127  /// Ignores the given word. This word will not be marked misspelled for
128  /// this session. It will again be marked as misspelled when creating
129  /// new highlighters.
130  ///
131  /// \param word the word which will be ignored
132  /// \since 5.88
133  Q_INVOKABLE void ignoreWord(const QString &word);
134 
135  /// Adds the given word permanently to the dictionary. It will never
136  /// be marked as misspelled again, even after restarting the application.
137  ///
138  /// \param word the word which will be added to the dictionary
139  /// \since 5.88
140  Q_INVOKABLE void addWordToDictionary(const QString &word);
141 
142  /// Replace word at the current cursor position.
143  /// \since 5.88
144  Q_INVOKABLE void replaceWord(const QString &word);
145 
146  /// Checks if a given word is marked as misspelled by the highlighter.
147  ///
148  /// \param word the word to be checked
149  /// \return true if the given word is misspelled.
150  /// \since 5.88
151  Q_INVOKABLE bool isWordMisspelled(const QString &word);
152 
153  Q_REQUIRED_RESULT QQuickTextDocument *quickDocument() const;
154  void setQuickDocument(QQuickTextDocument *document);
155  Q_REQUIRED_RESULT int cursorPosition() const;
156  void setCursorPosition(int position);
157  Q_REQUIRED_RESULT int selectionStart() const;
158  void setSelectionStart(int position);
159  Q_REQUIRED_RESULT int selectionEnd() const;
160  void setSelectionEnd(int position);
161  Q_REQUIRED_RESULT bool wordIsMisspelled() const;
162  Q_REQUIRED_RESULT QString wordUnderMouse() const;
163  Q_REQUIRED_RESULT bool spellCheckerFound() const;
164  Q_REQUIRED_RESULT QString currentLanguage() const;
165  void setActive(bool active);
166  Q_REQUIRED_RESULT bool active() const;
167  void setAutomatic(bool automatic);
168  Q_REQUIRED_RESULT bool automatic() const;
169  void setAutoDetectLanguageDisabled(bool autoDetectDisabled);
170  Q_REQUIRED_RESULT bool autoDetectLanguageDisabled() const;
171  void setMisspelledColor(const QColor &color);
172  Q_REQUIRED_RESULT QColor misspelledColor() const;
173  void setQuoteColor(const QColor &color);
174  Q_REQUIRED_RESULT QColor quoteColor() const;
175 
176  /// Return true if checker is enabled by default
177  /// \since 5.88
178  bool checkerEnabledByDefault() const;
179 
180  /// Set a new @ref QTextDocument for this highlighter to operate on.
181  /// \param document the new document to operate on.
182  /// \since 5.88
184 
185 Q_SIGNALS:
186  void documentChanged();
187  void cursorPositionChanged();
188  void selectionStartChanged();
189  void selectionEndChanged();
190  void wordIsMisspelledChanged();
191  void wordUnderMouseChanged();
192  void changeCursorPosition(int start, int end);
193  void activeChanged();
194  void misspelledColorChanged();
195  void autoDetectLanguageDisabledChanged();
196  void automaticChanged();
197  void currentLanguageChanged();
198 
199  /// Emitted when as-you-type spell checking is enabled or disabled.
200  ///
201  /// \param description is a i18n description of the new state,
202  /// with an optional reason
203  /// \since 5.88
204  void activeChanged(const QString &description);
205 
206 protected:
207  void highlightBlock(const QString &text) override;
208  virtual void setMisspelled(int start, int count);
209  virtual void unsetMisspelled(int start, int count);
210  bool eventFilter(QObject *o, QEvent *e) override;
211 
212  bool intraWordEditing() const;
213  void setIntraWordEditing(bool editing);
214 
215 public Q_SLOTS:
216  /// Set language to use for spell checking.
217  ///
218  /// \param language the language code for the new language to use.
219  /// \since 5.88
220  void setCurrentLanguage(const QString &language);
221 
222  /// Run auto detection, disabling spell checking if too many errors are found.
223  /// \since 5.88
224  void slotAutoDetection();
225 
226  /// Force a new highlighting.
227  /// \since 5.88
228  void slotRehighlight();
229 
230 private:
231  Q_REQUIRED_RESULT QTextCursor textCursor() const;
232  Q_REQUIRED_RESULT QTextDocument *textDocument() const;
233  void contentsChange(int pos, int add, int rem);
234 
235  void autodetectLanguage(const QString &sentence);
236 
237  HighlighterPrivate *const d;
239 };
Q_OBJECTQ_OBJECT
void slotRehighlight()
Force a new highlighting.
Q_PROPERTY(...)
Q_INVOKABLE void replaceWord(const QString &word)
Replace word at the current cursor position.
void setCurrentLanguage(const QString &language)
Set language to use for spell checking.
Q_SLOTSQ_SLOTS
Q_INVOKABLE void addWordToDictionary(const QString &word)
Adds the given word permanently to the dictionary.
bool wordIsMisspelled
This property holds whether the current word under the mouse is misspelled.
Q_SCRIPTABLE Q_NOREPLY void start()
QString currentLanguage
This property holds the current language used for spell checking.
QString wordUnderMouse
This property holds the current word under the mouse.
int selectionStart
This property holds the start of the selection.
bool active
This property holds whether spell checking is enabled.
int cursorPosition
This property holds the current cursor position.
int selectionEnd
This property holds the end of the selection.
QColor misspelledColor
This property holds the spell color.
Q_INVOKABLE QStringList suggestions(int position, int max=5)
Returns a list of suggested replacements for the given misspelled word.
QQuickTextDocument document
This property holds the underneath document from a QML TextEdit.
void setDocument(QTextDocument *document)
Set a new QTextDocument for this highlighter to operate on.
The Sonnet Highlighter class, used for drawing red lines in text fields when detecting spelling mista...
Q_INVOKABLEQ_INVOKABLE
Q_SIGNALSQ_SIGNALS
Q_INVOKABLE void ignoreWord(const QString &word)
Ignores the given word.
bool checkerEnabledByDefault() const
Return true if checker is enabled by default.
void slotAutoDetection()
Run auto detection, disabling spell checking if too many errors are found.
bool autoDetectLanguageDisabled
This property holds whether the automatic language detection is disabled overriding the Sonnet global...
bool automatic
This property holds whether spell checking is automatically disabled if there's too many errors.
Q_INVOKABLE bool isWordMisspelled(const QString &word)
Checks if a given word is marked as misspelled by the highlighter.
Q_DISABLE_COPY(Class)
QObject * parent() const const
bool spellCheckerFound
This property holds whether a spell checking backend with support for the currentLanguage was found.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 3 2023 03:59:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.