24#include "highlighter.moc"
37#include <QTextCharFormat>
44#include <QApplication>
48class Highlighter::Private
59 bool completeRehighlightRequired;
60 bool intraWordEditing;
61 bool spellCheckerFound;
62 int disablePercentage;
64 int wordCount, errorCount;
65 QTimer *rehighlightRequest;
67 int suggestionListeners;
70Highlighter::Private::~Private()
72 qDeleteAll(dictCache);
77 const QString& configFile,
88 d->intraWordEditing =
false;
89 d->completeRehighlightRequired =
false;
90 d->spellColor = _col.isValid() ? _col : Qt::red;
91 d->suggestionListeners = 0;
93 textEdit->installEventFilter(
this );
94 textEdit->viewport()->installEventFilter(
this );
101 if (!configFile.isEmpty()) {
104 d->loader->settings()->restore(&conf);
105 d->filter->setSettings(d->loader->settings());
110 d->spellCheckerFound = d->dict->isValid();
111 d->rehighlightRequest =
new QTimer(
this);
112 connect( d->rehighlightRequest, SIGNAL(
timeout()),
115 if(!d->spellCheckerFound)
118 d->dictCache.insert(d->dict->language(), d->dict);
120 d->disablePercentage = d->loader->settings()->disablePercentageWordError();
121 d->disableWordCount = d->loader->settings()->disableWordErrorCount();
125 for ( QStringList::ConstIterator it = l.begin(); it != l.end(); ++it ) {
126 d->dict->addToSession( *it );
128 d->completeRehighlightRequired =
true;
129 d->rehighlightRequest->setInterval(0);
130 d->rehighlightRequest->setSingleShot(
true);
131 d->rehighlightRequest->start();
141 return d->spellCheckerFound;
147void Highlighter::connectNotify(
const char* signal)
149 if (QLatin1String(signal) == SIGNAL(
newSuggestions(QString,QStringList)))
150 ++d->suggestionListeners;
151 QSyntaxHighlighter::connectNotify(signal);
154void Highlighter::disconnectNotify(
const char* signal)
156 if (QLatin1String(signal) == SIGNAL(
newSuggestions(QString,QStringList)))
157 --d->suggestionListeners;
158 QSyntaxHighlighter::disconnectNotify(signal);
163 kDebug(0) <<
"Highlighter::slotRehighlight()";
164 if (d->completeRehighlightRequired) {
171 QTextCursor cursor = d->edit->textCursor();
172 cursor.insertText(
"" );
184 l.append(
"KOrganizer" );
185 l.append(
"KAddressBook" );
189 l.append(
"Konqueror" );
190 l.append(
"Sonnet" );
191 l.append(
"Kontact" );
203 return d->intraWordEditing;
208 d->intraWordEditing = editing;
224 bool savedActive = d->active;
227 if (d->automatic && d->wordCount >= 10) {
229 bool tme = (d->errorCount >= d->disableWordCount) && (
230 d->errorCount * 100 >= d->disablePercentage * d->wordCount);
231 if (d->active && tme) {
233 }
else if (!d->active && !tme) {
238 if (d->active != savedActive) {
243 "As-you-type spell checking disabled."));
246 d->completeRehighlightRequired =
true;
247 d->rehighlightRequest->setInterval(100);
248 d->rehighlightRequest->setSingleShot(
true);
249 kDebug()<<
" Highlighter::slotAutoDetection :"<<d->active;
256 if ( active == d->active )
275 if (text.isEmpty() || !d->active || !d->spellCheckerFound)
278 d->filter->setBuffer( text );
279 Word w = d->filter->nextWord();
282 if (d->dict->isMisspelled(w.
word)) {
285 if (d->suggestionListeners)
289 w = d->filter->nextWord();
292 setCurrentBlockState(0);
297 return d->dict->language();
302 if (!d->dictCache.contains(lang)) {
303 d->dict =
new Speller(*d->dict);
304 d->dict->setLanguage(lang);
305 if (d->dict->isValid()) {
306 d->dictCache.insert(lang, d->dict);
308 d->spellCheckerFound =
false;
309 kDebug()<<
"No dictionary for \""
311 <<
"\" staying with the current language."
316 d->dict = d->dictCache[lang];
317 d->spellCheckerFound = d->dict->isValid();
326 QTextCharFormat format;
327 format.setFontUnderline(
true);
328 format.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
329 format.setUnderlineColor(d->spellColor);
330 setFormat(start, count, format);
335 setFormat(start, count, QTextCharFormat());
341 if (o == textEdit() && (e->type() == QEvent::FocusIn)) {
342 if ( d->globalConfig ) {
343 QString skey = spellKey();
344 if ( d->spell && d->spellKey != skey ) {
346 KDictSpellingHighlighter::dictionaryChanged();
351 if (!d->spellCheckerFound)
353 if (o == d->edit && (e->type() == QEvent::KeyPress)) {
354 QKeyEvent *k =
static_cast<QKeyEvent *
>(e);
356 if (d->rehighlightRequest->isActive())
357 d->rehighlightRequest->start( 500 );
358 if ( k->key() == Qt::Key_Enter ||
359 k->key() == Qt::Key_Return ||
360 k->key() == Qt::Key_Up ||
361 k->key() == Qt::Key_Down ||
362 k->key() == Qt::Key_Left ||
363 k->key() == Qt::Key_Right ||
364 k->key() == Qt::Key_PageUp ||
365 k->key() == Qt::Key_PageDown ||
366 k->key() == Qt::Key_Home ||
367 k->key() == Qt::Key_End ||
368 (( k->modifiers()== Qt::ControlModifier ) &&
369 ((k->key() == Qt::Key_A) ||
370 (k->key() == Qt::Key_B) ||
371 (k->key() == Qt::Key_E) ||
372 (k->key() == Qt::Key_N) ||
373 (k->key() == Qt::Key_P))) ) {
376 d->completeRehighlightRequired =
true;
377 d->rehighlightRequest->setInterval(500);
378 d->rehighlightRequest->setSingleShot(
true);
379 d->rehighlightRequest->start();
382 if (d->checksDone != d->checksRequested) {
385 d->completeRehighlightRequired =
true;
386 d->rehighlightRequest->start( 500,
true );
392 if ( k->key() == Qt::Key_Space ||
393 k->key() == Qt::Key_Enter ||
394 k->key() == Qt::Key_Return ) {
399 else if ( o == d->edit->viewport() &&
400 ( e->type() == QEvent::MouseButtonPress )) {
404 d->completeRehighlightRequired =
true;
405 d->rehighlightRequest->setInterval(0);
406 d->rehighlightRequest->setSingleShot(
true);
407 d->rehighlightRequest->start();
415 d->dict->addToPersonal(word);
420 d->dict->addToSession(word);
425 QStringList suggestions = d->dict->suggest(word);
427 while ( suggestions.count() > max )
428 suggestions.removeLast();
435 return d->dict->isMisspelled(word);
440 d->spellColor = color;
445 return d->loader->settings()->checkerEnabledByDefault();
bool hasGroup(const char *group) const
static Filter * defaultFilter()
void addWordToDictionary(const QString &word)
Adds the given word permanently to the dictionary.
void activeChanged(const QString &description)
Emitted when as-you-type spell checking is enabled or disabled.
Highlighter(QTextEdit *textEdit, const QString &configFile=QString(), const QColor &col=QColor())
static QStringList personalWords()
QString currentLanguage() const
void ignoreWord(const QString &word)
Ignores the given word.
bool isActive() const
Returns the state of spell checking.
bool isWordMisspelled(const QString &word)
Checks if a given word is marked as misspelled by the highlighter.
QStringList suggestionsForWord(const QString &word, int max=10)
Returns a list of suggested replacements for the given misspelled word.
bool eventFilter(QObject *o, QEvent *e)
QT_MOC_COMPAT void newSuggestions(const QString &originalWord, const QStringList &suggestions)
void setActive(bool active)
Enable/Disable spell checking.
virtual void highlightBlock(const QString &text)
void setIntraWordEditing(bool editing)
bool spellCheckerFound() const
void setMisspelledColor(const QColor &color)
Sets the color in which the highlighter underlines misspelled words.
void setAutomatic(bool automatic)
bool checkerEnabledByDefault() const
Return true if checker is enabled by default.
virtual void setMisspelled(int start, int count)
virtual void unsetMisspelled(int start, int count)
bool intraWordEditing() const
void setCurrentLanguage(const QString &lang)
static Loader * openLoader()
QString i18n(const char *text)