1 | /* |
2 | * backgroundchecker.h |
3 | * |
4 | * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org> |
5 | * |
6 | * SPDX-License-Identifier: LGPL-2.1-or-later |
7 | */ |
8 | #ifndef SONNET_BACKGROUNDCHECKER_H |
9 | #define SONNET_BACKGROUNDCHECKER_H |
10 | |
11 | #include "speller.h" |
12 | |
13 | #include "sonnetcore_export.h" |
14 | |
15 | #include <QObject> |
16 | |
17 | #include <memory> |
18 | |
19 | /*! |
20 | * \namespace Sonnet |
21 | * \inmodule SonnetCore |
22 | */ |
23 | namespace Sonnet |
24 | { |
25 | class BackgroundCheckerPrivate; |
26 | class Speller; |
27 | |
28 | /*! |
29 | * \class Sonnet::BackgroundChecker |
30 | * \inheaderfile Sonnet/BackgroundChecker |
31 | * \inmodule SonnetCore |
32 | * |
33 | * \brief class used for spell checking in the background. |
34 | * |
35 | * BackgroundChecker is used to perform spell checking without |
36 | * blocking the application. You can use it as is by calling |
37 | * the checkText function or subclass it and reimplement |
38 | * getMoreText function. |
39 | * |
40 | * The misspelling signal is emitted whenever a misspelled word |
41 | * is found. The background checker stops right before emitting |
42 | * the signal. So the parent has to call continueChecking function |
43 | * to resume the checking. |
44 | * |
45 | * The done() signal is emitted when whole text is spell checked. |
46 | */ |
47 | class SONNETCORE_EXPORT BackgroundChecker : public QObject |
48 | { |
49 | Q_OBJECT |
50 | public: |
51 | /*! |
52 | */ |
53 | explicit BackgroundChecker(QObject *parent = nullptr); |
54 | /*! |
55 | */ |
56 | explicit BackgroundChecker(const Speller &speller, QObject *parent = nullptr); |
57 | ~BackgroundChecker() override; |
58 | |
59 | /*! |
60 | * This method is used to spell check static text. |
61 | * It automatically invokes start(). |
62 | * |
63 | * Use fetchMoreText() with start() to spell check a stream. |
64 | */ |
65 | void setText(const QString &text); |
66 | /*! |
67 | */ |
68 | QString text() const; |
69 | |
70 | /*! |
71 | */ |
72 | QString currentContext() const; |
73 | |
74 | /*! |
75 | */ |
76 | Speller speller() const; |
77 | /*! |
78 | */ |
79 | void setSpeller(const Speller &speller); |
80 | |
81 | /*! |
82 | */ |
83 | bool checkWord(const QString &word); |
84 | /*! |
85 | */ |
86 | QStringList suggest(const QString &word) const; |
87 | /*! |
88 | */ |
89 | bool addWordToPersonal(const QString &word); |
90 | |
91 | /*! |
92 | * This method is used to add a word to the session of the |
93 | * speller currently set in BackgroundChecker. |
94 | * |
95 | * \since 5.55 |
96 | */ |
97 | bool addWordToSession(const QString &word); |
98 | |
99 | /*! |
100 | * Returns whether the automatic language detection is disabled, |
101 | * overriding the Sonnet settings. |
102 | * |
103 | * Returns true if the automatic language detection is disabled |
104 | * \since 5.71 |
105 | */ |
106 | bool autoDetectLanguageDisabled() const; |
107 | |
108 | /*! |
109 | * Sets whether to disable the automatic language detection. |
110 | * |
111 | * \a autoDetectDisabled if true, the language will not be |
112 | * detected automatically by the spell checker, even if the option |
113 | * is enabled in the Sonnet settings. |
114 | * \since 5.71 |
115 | */ |
116 | void setAutoDetectLanguageDisabled(bool autoDetectDisabled); |
117 | |
118 | public Q_SLOTS: |
119 | /*! |
120 | */ |
121 | virtual void start(); |
122 | /*! |
123 | */ |
124 | virtual void stop(); |
125 | /*! |
126 | */ |
127 | void replace(int start, const QString &oldText, const QString &newText); |
128 | /*! |
129 | */ |
130 | void changeLanguage(const QString &lang); |
131 | |
132 | /*! |
133 | * After emitting misspelling signal the background |
134 | * checker stops. The catcher is responsible for calling |
135 | * continueChecking function to resume checking. |
136 | */ |
137 | virtual void continueChecking(); |
138 | |
139 | Q_SIGNALS: |
140 | /*! |
141 | * Emitted whenever a misspelled word is found |
142 | */ |
143 | void misspelling(const QString &word, int start); |
144 | |
145 | /*! |
146 | * Emitted after the whole text has been spell checked. |
147 | */ |
148 | void done(); |
149 | |
150 | protected: |
151 | /*! |
152 | * This function is called to get the text to spell check. |
153 | * It will be called continuesly until it returns QString() |
154 | * in which case the done() signal is emitted. |
155 | * \note the start parameter in misspelling() is not a combined |
156 | * position but a position in the last string returned |
157 | * by fetchMoreText. You need to store the state in the derivatives. |
158 | */ |
159 | virtual QString fetchMoreText(); |
160 | |
161 | /*! |
162 | * This function will be called whenever the background checker |
163 | * will be finished text which it got from fetchMoreText. |
164 | */ |
165 | virtual void finishedCurrentFeed(); |
166 | |
167 | protected Q_SLOTS: |
168 | /*! |
169 | */ |
170 | void slotEngineDone(); |
171 | |
172 | private: |
173 | std::unique_ptr<BackgroundCheckerPrivate> const d; |
174 | }; |
175 | } |
176 | |
177 | #endif |
178 | |