2023-06-08 21:33:16 +03:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kspellhighlighter.h"
|
|
|
|
#include "kspeller.h"
|
2023-06-10 01:23:08 +03:00
|
|
|
#include "kcolorscheme.h"
|
2023-06-08 21:33:16 +03:00
|
|
|
#include "kdebug.h"
|
|
|
|
|
|
|
|
class KSpellHighlighterPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KSpellHighlighterPrivate(KConfig *config);
|
|
|
|
|
|
|
|
KSpeller speller;
|
2024-04-07 18:46:21 +03:00
|
|
|
QTextCharFormat emptyformat;
|
|
|
|
QTextCharFormat spellformat;
|
|
|
|
|
2023-06-08 21:33:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
KSpellHighlighterPrivate::KSpellHighlighterPrivate(KConfig *config)
|
|
|
|
: speller(config)
|
|
|
|
{
|
2024-04-07 18:46:21 +03:00
|
|
|
spellformat.setFontUnderline(true);
|
|
|
|
spellformat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
|
2023-06-10 01:23:08 +03:00
|
|
|
// NOTE: same as the default of Kate
|
2024-04-07 18:46:21 +03:00
|
|
|
spellformat.setUnderlineColor(KColorScheme(QPalette::Active, KColorScheme::View).foreground(KColorScheme::NegativeText).color());
|
2023-06-08 21:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
KSpellHighlighter::KSpellHighlighter(KConfig *config, QTextEdit *parent)
|
|
|
|
: QSyntaxHighlighter(parent),
|
|
|
|
d(new KSpellHighlighterPrivate(config))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KSpellHighlighter::~KSpellHighlighter()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KSpellHighlighter::currentLanguage() const
|
|
|
|
{
|
|
|
|
return d->speller.dictionary();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSpellHighlighter::setCurrentLanguage(const QString &lang)
|
|
|
|
{
|
|
|
|
d->speller.setDictionary(lang);
|
2024-04-10 00:05:43 +03:00
|
|
|
rehighlight();
|
2023-06-08 21:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void KSpellHighlighter::addWordToDictionary(const QString &word)
|
|
|
|
{
|
|
|
|
d->speller.addToPersonal(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSpellHighlighter::ignoreWord(const QString &word)
|
|
|
|
{
|
|
|
|
d->speller.addToSession(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList KSpellHighlighter::suggestionsForWord(const QString &word, int max)
|
|
|
|
{
|
|
|
|
QStringList result = d->speller.suggest(word);
|
|
|
|
// qDebug() << Q_FUNC_INFO << result << max;
|
|
|
|
while (result.size() > max) {
|
|
|
|
result.removeLast();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KSpellHighlighter::isWordMisspelled(const QString &word)
|
|
|
|
{
|
|
|
|
return !d->speller.check(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KSpellHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
|
|
|
// qDebug() << Q_FUNC_INFO << text.size() << d->speller.dictionary();
|
|
|
|
if (text.isEmpty() || d->speller.dictionary().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-13 00:39:32 +03:00
|
|
|
|
2024-04-07 18:46:21 +03:00
|
|
|
int wordstart = -1;
|
|
|
|
int counter = 0;
|
|
|
|
while (counter < text.size()) {
|
|
|
|
const bool atseparator = KSpeller::isWordSeparator(text.at(counter));
|
|
|
|
if (!atseparator && wordstart == -1) {
|
|
|
|
wordstart = counter;
|
2024-04-11 14:39:22 +03:00
|
|
|
} else if ((atseparator || (counter + 1) == text.size()) && wordstart != -1) {
|
|
|
|
const QString word = text.mid(wordstart, counter - wordstart + (atseparator ? 0 : 1));
|
2023-06-13 00:39:32 +03:00
|
|
|
// not worth checking if it is less than two characters
|
2024-04-07 18:46:21 +03:00
|
|
|
if (word.size() >= 2) {
|
|
|
|
if (!d->speller.check(word)) {
|
|
|
|
setFormat(wordstart, word.size(), d->spellformat);
|
|
|
|
} else {
|
|
|
|
setFormat(wordstart, word.size(), d->emptyformat);
|
|
|
|
}
|
2023-06-08 21:33:16 +03:00
|
|
|
}
|
2024-04-07 18:46:21 +03:00
|
|
|
wordstart = -1;
|
2023-06-08 21:33:16 +03:00
|
|
|
}
|
2024-04-07 18:46:21 +03:00
|
|
|
counter++;
|
2023-06-08 21:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_kspellhighlighter.cpp"
|