summaryrefslogtreecommitdiffstats
path: root/kspell2/ui/highlighter.cpp
blob: 0ecbb9802d558e7261c607eb3a83fdfd6c52b15f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * highlighter.cpp
 *
 * Copyright (C)  2004  Zack Rusin <zack@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "highlighter.h"
#include "broker.h"
#include "dictionary.h"
#include "settings.h"

#include <kconfig.h>
#include <kdebug.h>

#include <qtextedit.h>
#include <qtimer.h>
#include <qcolor.h>
#include <qdict.h>

namespace KSpell2 {

class Highlighter::Private
{
public:
    Filter     *filter;
    Broker::Ptr broker;
    Dictionary *dict;
    QDict<Dictionary>  dictCache;
};

Highlighter::Highlighter( QTextEdit *textEdit,
                          const QString& configFile,
                          Filter *filter)
    : QSyntaxHighlighter( textEdit )
{
    d = new Private;
    d->filter = filter;
    if ( !configFile.isEmpty() )
        d->broker = Broker::openBroker( KSharedConfig::openConfig( configFile ) );
    else
        d->broker = Broker::openBroker();

    d->filter->setSettings( d->broker->settings() );
    d->dict   = d->broker->dictionary();
    Q_ASSERT( d->dict );
    d->dictCache.insert( d->broker->settings()->defaultLanguage(),
                         d->dict );
}

Highlighter::~Highlighter()
{
    delete d; d = 0;
}

int Highlighter::highlightParagraph( const QString& text,
                                     int endStateOfLastPara )
{
    Q_UNUSED( endStateOfLastPara );
    int para, index;
    textEdit()->getCursorPosition( &para, &index );
    const int lengthPosition = text.length() - 1;

    if ( index != lengthPosition ||
         ( lengthPosition > 0 && !text[lengthPosition-1].isLetter() ) ) {
        d->filter->setBuffer( text );
        Word w = d->filter->nextWord();
        while ( !w.end ) {
            if ( !d->dict->check( w.word ) ) {
                setMisspelled( w.start, w.word.length() );
            } else
                unsetMisspelled( w.start, w.word.length() );
            w = d->filter->nextWord();
        }
    }
    //QTimer::singleShot( 0, this, SLOT(checkWords()) );

    return 0;
}

Filter *Highlighter::currentFilter() const
{
    return d->filter;
}

void Highlighter::setCurrentFilter( Filter *filter )
{
    d->filter = filter;
    d->filter->setSettings( d->broker->settings() );
}

QString Highlighter::currentLanguage() const
{
    return d->dict->language();
}

void Highlighter::setCurrentLanguage( const QString& lang )
{
    if ( !d->dictCache.find( lang ) ) {
        Dictionary *dict = d->broker->dictionary( lang );
        if ( dict ) {
            d->dictCache.insert( lang, dict );
        } else {
            kdDebug()<<"No dictionary for \""
                     <<lang
                     <<"\" staying with the current language."
                     <<endl;
            return;
        }
    }
    d->dict = d->dictCache[lang];
}

void Highlighter::setMisspelled( int start, int count )
{
    setFormat( start , count, Qt::red );
}

void Highlighter::unsetMisspelled( int start, int count )
{
    setFormat( start, count, Qt::black );
}

/*
void Highlighter::checkWords()
{
    Word w = d->filter->nextWord();
    if ( !w.end ) {
        if ( !d->dict->check( w.word ) ) {
            setFormat( w.start, w.word.length(),
                       Qt::red );
        }
    }
}*/

}