9#include "backgroundchecker.h"
10#include "backgroundchecker_p.h"
12#include "core_debug.h"
16void BackgroundCheckerPrivate::start()
22void BackgroundCheckerPrivate::continueChecking()
27void BackgroundCheckerPrivate::checkNext()
31 while (sentenceOffset != -1 && words.hasNext()) {
32 Token word = words.next();
33 if (!words.isSpellcheckable()) {
38 if (currentDict.isMisspelled(word.toString())) {
39 lastMisspelled = word;
40 Q_EMIT misspelling(word.toString(), word.position() + sentenceOffset);
47 const bool autodetectLanguage = currentDict.testAttribute(Speller::AutoDetectLanguage);
48 const bool ignoreUpperCase = !currentDict.testAttribute(Speller::CheckUppercase);
49 while (mainTokenizer.hasNext()) {
50 Token sentence = mainTokenizer.next();
51 if (autodetectLanguage && !autoDetectLanguageDisabled) {
52 if (!mainTokenizer.isSpellcheckable()) {
56 currentDict.setLanguage(mainTokenizer.language());
58 sentenceOffset = sentence.position();
59 words.setBuffer(sentence.toString());
60 words.setIgnoreUppercase(ignoreUpperCase);
63 }
while (sentenceOffset != -1);
67BackgroundChecker::BackgroundChecker(
QObject *parent)
69 , d(new BackgroundCheckerPrivate)
72 connect(d.get(), &BackgroundCheckerPrivate::done,
this, &BackgroundChecker::slotEngineDone);
75BackgroundChecker::BackgroundChecker(
const Speller &speller,
QObject *parent)
77 , d(new BackgroundCheckerPrivate)
79 d->currentDict = speller;
81 connect(d.get(), &BackgroundCheckerPrivate::done,
this, &BackgroundChecker::slotEngineDone);
84BackgroundChecker::~BackgroundChecker() =
default;
88 d->mainTokenizer.setBuffer(text);
92void BackgroundChecker::start()
101void BackgroundChecker::stop()
117 return d->autoDetectLanguageDisabled;
122 d->autoDetectLanguageDisabled = autoDetectDisabled;
125void BackgroundChecker::setSpeller(
const Speller &speller)
127 d->currentDict = speller;
130Speller BackgroundChecker::speller()
const
132 return d->currentDict;
135bool BackgroundChecker::checkWord(
const QString &word)
137 return d->currentDict.isCorrect(word);
140bool BackgroundChecker::addWordToPersonal(
const QString &word)
142 return d->currentDict.addToPersonal(word);
147 return d->currentDict.addToSession(word);
152 return d->currentDict.suggest(word);
155void BackgroundChecker::changeLanguage(
const QString &lang)
158 d->currentDict.setLanguage(lang);
163 d->continueChecking();
166void BackgroundChecker::slotEngineDone()
171 if (currentText.
isNull()) {
174 d->mainTokenizer.setBuffer(currentText);
179QString BackgroundChecker::text()
const
181 return d->mainTokenizer.buffer();
184QString BackgroundChecker::currentContext()
const
189 int currentPosition = d->lastMisspelled.position() + d->sentenceOffset;
190 bool begin = ((currentPosition - len / 2) <= 0) ?
true : false;
192 QString buffer = d->mainTokenizer.buffer();
193 buffer.
replace(currentPosition, d->lastMisspelled.length(), QStringLiteral(
"<b>%1</b>").arg(d->lastMisspelled.toString()));
197 context = QStringLiteral(
"%1...").
arg(buffer.
mid(0, len));
199 context = QStringLiteral(
"...%1...").
arg(buffer.
mid(currentPosition - 20, len));
207void Sonnet::BackgroundChecker::replace(
int start,
const QString &oldText,
const QString &newText)
211 d->words.replace(
start - (d->sentenceOffset), oldText.
length(), newText);
212 d->mainTokenizer.replace(
start, oldText.
length(), newText);
215#include "moc_backgroundchecker.cpp"
216#include "moc_backgroundchecker_p.cpp"
virtual void finishedCurrentFeed()
This function will be called whenever the background checker will be finished text which it got from ...
bool addWordToSession(const QString &word)
This method is used to add a word to the session of the speller currently set in BackgroundChecker.
void setText(const QString &text)
This method is used to spell check static text.
void misspelling(const QString &word, int start)
Emitted whenever a misspelled word is found.
void done()
Emitted after the whole text has been spell checked.
bool autoDetectLanguageDisabled() const
Returns whether the automatic language detection is disabled, overriding the Sonnet settings.
virtual QString fetchMoreText()
This function is called to get the text to spell check.
void setAutoDetectLanguageDisabled(bool autoDetectDisabled)
Sets whether to disable the automatic language detection.
virtual void continueChecking()
After emitting misspelling signal the background checker stops.
Q_SCRIPTABLE Q_NOREPLY void start()
const QList< QKeySequence > & begin()
QString arg(Args &&... args) const const
bool isNull() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)