2
This file is part of RoboJournal.
3
Copyright (c) 2013 by Will Kraft <pwizard@gmail.com>.
6
Will Kraft (11/27/12): This code was originally Jan Sundermeyer's work
7
(sunderme@web.de) I made some minor changes and adapted it for
10
RoboJournal is free software: you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation, either version 3 of the License, or
13
(at your option) any later version.
15
RoboJournal is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with RoboJournal. If not, see <http://www.gnu.org/licenses/>.
26
/****************************************************************************
29
** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
31
** This file is part of the example classes of the Qt Toolkit.
33
** This file may be used under the terms of the GNU General Public
34
** License versions 2.0 or 3.0 as published by the Free Software
35
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
36
** included in the packaging of this file. Alternatively you may (at
37
** your option) use any later version of the GNU General Public
38
** License if such license has been publicly approved by Trolltech ASA
39
** (or its successors, if any) and the KDE Free Qt Foundation. In
40
** addition, as a special exception, Trolltech gives you certain
41
** additional rights. These rights are described in the Trolltech GPL
42
** Exception version 1.1, which can be found at
43
** http://www.trolltech.com/products/qt/gplexception/ and in the file
44
** GPL_EXCEPTION.txt in this package.
46
** Please review the following information to ensure GNU General
47
** Public Licensing requirements will be met:
48
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
49
** you are unsure which license is appropriate for your use, please
50
** review the following information:
51
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
52
** or contact the sales department at sales@trolltech.com.
54
** In addition, as a special exception, Trolltech, as the sole
55
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
56
** Integration plug-in the right for the Qt/Eclipse Integration to
57
** link to functionality provided by Qt Designer and its related
60
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
61
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
62
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
65
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
66
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
68
****************************************************************************/
70
// Will Kraft (11/27/12): This code was originally Jan Sundermeyer's work. I made some minor changes
71
// and adapted it for RoboJournal > =0.4.
75
#include "ui/highlighter.h"
76
#include "core/buffer.h"
78
Highlighter::Highlighter(QTextDocument *parent,QString SpellDic,bool spellCheckState) :
79
QSyntaxHighlighter(parent) {
80
HighlightingRule rule;
83
spellCheckFormat.setUnderlineColor(QColor(Qt::red));
84
spellCheckFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
86
//Settings for online spellchecking
87
if(!SpellDic.isEmpty()){
89
spell_dic=SpellDic.left(SpellDic.length()-4);
91
pChecker = new Hunspell(Buffer::current_dictionary_aff.toLatin1(),Buffer::current_dictionary.toLatin1());
92
spell_encoding=QString(pChecker->get_dic_encoding());
93
codec = QTextCodec::codecForName(spell_encoding.toLatin1());
95
QFileInfo fi(SpellDic);
96
if (fi.exists() && fi.isReadable()) spellCheckActive=true;
97
else spellCheckActive=false;
98
// get user config dictionary
100
QString filePath=Buffer::current_dictionary;
102
//std::cout << qPrintable(filePath) << std::endl;
103
fi=QFileInfo(filePath);
104
if (fi.exists() && fi.isReadable()){
105
pChecker->add_dic(filePath.toLatin1());
109
else spellCheckActive=false;
110
spellerError=!spellCheckActive;
111
spellCheckActive=spellCheckActive && spellCheckState;
114
Highlighter::~Highlighter() {
119
void Highlighter::highlightBlock(const QString &text) {
124
void Highlighter::enableSpellChecking(const bool state)
126
bool old=spellCheckActive;
127
if(!spellerError) spellCheckActive=state;
128
if(old!=spellCheckActive) rehighlight();
131
void Highlighter::spellCheck(const QString &text)
133
if (spellCheckActive) {
134
// split text into words
135
QString str = text.simplified();
136
if (!str.isEmpty()) {
138
// Will Kraft 11/27/12: this is Jan's old regex for Checkliste: ([^\\w,^\\\\]|(?=\\\\))+
139
// I had to change the regexp because it was flagging contractions (didn't, couldn't, etc.)
140
QStringList Checkliste = str.split(QRegExp("([^\\w,^\\\\,']|(?=\\\\))+"),
141
QString::SkipEmptyParts);
144
for (int i=0; i<Checkliste.size(); ++i) {
145
str = Checkliste.at(i);
146
if (str.length()>1 &&!str.startsWith('\\'))
148
if (!checkWord(str)) {
149
number = text.count(QRegExp("\\b" + str + "\\b"));
151
// underline all incorrect occurences of misspelled word
152
for (int j=0;j < number; ++j)
154
l = text.indexOf(QRegExp("\\b" + str + "\\b"),l+1);
156
setFormat(l, str.length(), spellCheckFormat);
161
} // if spell chek error
162
} // if str.length > 1
168
bool Highlighter::checkWord(QString word)
171
/*switch(check=mWords.value(word,-1)){
174
QByteArray encodedString;
175
encodedString = codec->fromUnicode(word);
176
check = pChecker->spell(encodedString.data());
184
QByteArray encodedString;
185
encodedString = codec->fromUnicode(word);
186
check = pChecker->spell(encodedString.data());
190
bool Highlighter::setDict(const QString SpellDic)
195
spell_dic=SpellDic.left(SpellDic.length()-4);
197
pChecker = new Hunspell(Buffer::current_dictionary_aff.toLatin1(),Buffer::current_dictionary.toLatin1());
199
spell_encoding=QString(pChecker->get_dic_encoding());
200
codec = QTextCodec::codecForName(spell_encoding.toLatin1());
202
QFileInfo fi(SpellDic);
204
if (fi.exists() && fi.isReadable()) spell=true;
207
// get user config dictionary
209
QString filePath=Buffer::current_dictionary;
211
//std::cout << qPrintable(filePath) << std::endl;
212
fi=QFileInfo(filePath);
213
if (fi.exists() && fi.isReadable()){
214
pChecker->add_dic(filePath.toLatin1());
218
spellCheckFormat.setForeground(Qt::red);//faster Cursoroperation ...
219
//spellCheckFormat.setUnderlineColor(QColor(Qt::red));
220
//spellCheckFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
224
spellCheckActive=spellCheckActive && spell;
229
void Highlighter::slot_addWord(QString word)
231
//std::cout << qPrintable(word) << std::endl;
232
QByteArray encodedString;
233
QString spell_encoding=QString(pChecker->get_dic_encoding());
234
QTextCodec *codec = QTextCodec::codecForName(spell_encoding.toLatin1());
235
encodedString = codec->fromUnicode(word);
236
pChecker->add(encodedString.data());