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, or @param at if
143  /// @param at is not -1.
144  /// \since 5.88
145  Q_INVOKABLE void replaceWord(const QString &word, int at = -1);
146 
147  /// Checks if a given word is marked as misspelled by the highlighter.
148  ///
149  /// \param word the word to be checked
150  /// \return true if the given word is misspelled.
151  /// \since 5.88
152  Q_INVOKABLE bool isWordMisspelled(const QString &word);
153 
154  Q_REQUIRED_RESULT QQuickTextDocument *quickDocument() const;
155  void setQuickDocument(QQuickTextDocument *document);
156  Q_REQUIRED_RESULT int cursorPosition() const;
157  void setCursorPosition(int position);
158  Q_REQUIRED_RESULT int selectionStart() const;
159  void setSelectionStart(int position);
160  Q_REQUIRED_RESULT int selectionEnd() const;
161  void setSelectionEnd(int position);
162  Q_REQUIRED_RESULT bool wordIsMisspelled() const;
163  Q_REQUIRED_RESULT QString wordUnderMouse() const;
164  Q_REQUIRED_RESULT bool spellCheckerFound() const;
165  Q_REQUIRED_RESULT QString currentLanguage() const;
166  void setActive(bool active);
167  Q_REQUIRED_RESULT bool active() const;
168  void setAutomatic(bool automatic);
169  Q_REQUIRED_RESULT bool automatic() const;
170  void setAutoDetectLanguageDisabled(bool autoDetectDisabled);
171  Q_REQUIRED_RESULT bool autoDetectLanguageDisabled() const;
172  void setMisspelledColor(const QColor &color);
173  Q_REQUIRED_RESULT QColor misspelledColor() const;
174  void setQuoteColor(const QColor &color);
175  Q_REQUIRED_RESULT QColor quoteColor() const;
176 
177  /// Return true if checker is enabled by default
178  /// \since 5.88
179  bool checkerEnabledByDefault() const;
180 
181  /// Set a new @ref QTextDocument for this highlighter to operate on.
182  /// \param document the new document to operate on.
183  /// \since 5.88
185 
186 Q_SIGNALS:
187  void documentChanged();
188  void cursorPositionChanged();
189  void selectionStartChanged();
190  void selectionEndChanged();
191  void wordIsMisspelledChanged();
192  void wordUnderMouseChanged();
193  void changeCursorPosition(int start, int end);
194  void activeChanged();
195  void misspelledColorChanged();
196  void autoDetectLanguageDisabledChanged();
197  void automaticChanged();
198  void currentLanguageChanged();
199 
200  /// Emitted when as-you-type spell checking is enabled or disabled.
201  ///
202  /// \param description is a i18n description of the new state,
203  /// with an optional reason
204  /// \since 5.88
205  void activeChanged(const QString &description);
206 
207 protected:
208  void highlightBlock(const QString &text) override;
209  virtual void setMisspelled(int start, int count);
210  virtual void setMisspelledSelected(int start, int count);
211  virtual void unsetMisspelled(int start, int count);
212  bool eventFilter(QObject *o, QEvent *e) override;
213 
214  bool intraWordEditing() const;
215  void setIntraWordEditing(bool editing);
216 
217 public Q_SLOTS:
218  /// Set language to use for spell checking.
219  ///
220  /// \param language the language code for the new language to use.
221  /// \since 5.88
222  void setCurrentLanguage(const QString &language);
223 
224  /// Run auto detection, disabling spell checking if too many errors are found.
225  /// \since 5.88
226  void slotAutoDetection();
227 
228  /// Force a new highlighting.
229  /// \since 5.88
230  void slotRehighlight();
231 
232 private:
233  Q_REQUIRED_RESULT QTextCursor textCursor() const;
234  Q_REQUIRED_RESULT QTextDocument *textDocument() const;
235  void contentsChange(int pos, int add, int rem);
236 
237  void autodetectLanguage(const QString &sentence);
238 
239  HighlighterPrivate *const d;
241 };
Q_OBJECTQ_OBJECT
void slotRehighlight()
Force a new highlighting.
Q_PROPERTY(...)
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.
Q_INVOKABLE void replaceWord(const QString &word, int at=-1)
Replace word at the current cursor position, or.
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 Fri Jun 9 2023 03:57:24 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.