~ubuntu-branches/ubuntu/vivid/robojournal/vivid-proposed

« back to all changes in this revision

Viewing changes to ui/highlighter.cpp

  • Committer: Package Import Robot
  • Author(s): Ritesh Raj Sarraf
  • Date: 2014-10-14 15:50:21 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20141014155021-gjd34w00cquge8jf
Tags: 0.5-1
* [d7ce610] Imported Upstream version 0.5
* [ff3b609] Refine what is in the docs/
* [c1e0474] Add doc-base registration
* [68cd341] Install upstream provided xpm icon
* [a627af4] Add patch to override hardcoded doc location

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of RoboJournal.
3
 
    Copyright (c) 2013 by Will Kraft <pwizard@gmail.com>.
4
 
    MADE IN USA
5
 
 
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
8
 
    RoboJournal >= 0.4.
9
 
 
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.
14
 
 
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.
19
 
 
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/>.
22
 
  */
23
 
 
24
 
 
25
 
 
26
 
/****************************************************************************
27
 
 ** ORIGINAL HEADER:
28
 
 **
29
 
 ** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
30
 
 **
31
 
 ** This file is part of the example classes of the Qt Toolkit.
32
 
 **
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.
45
 
 **
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.
53
 
 **
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
58
 
 ** libraries.
59
 
 **
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
63
 
 ** granted herein.
64
 
 **
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.
67
 
 **
68
 
 ****************************************************************************/
69
 
 
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.
72
 
 
73
 
#include <QtGui>
74
 
#include <iostream>
75
 
#include "ui/highlighter.h"
76
 
#include "core/buffer.h"
77
 
 
78
 
Highlighter::Highlighter(QTextDocument *parent,QString SpellDic,bool spellCheckState) :
79
 
        QSyntaxHighlighter(parent) {
80
 
        HighlightingRule rule;
81
 
 
82
 
 
83
 
        spellCheckFormat.setUnderlineColor(QColor(Qt::red));
84
 
        spellCheckFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
85
 
 
86
 
        //Settings for online spellchecking
87
 
    if(!SpellDic.isEmpty()){
88
 
                //mWords.clear();
89
 
                spell_dic=SpellDic.left(SpellDic.length()-4);
90
 
 
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());
94
 
 
95
 
                QFileInfo fi(SpellDic);
96
 
                if (fi.exists() && fi.isReadable()) spellCheckActive=true;
97
 
                else spellCheckActive=false;
98
 
                // get user config dictionary
99
 
 
100
 
        QString filePath=Buffer::current_dictionary;
101
 
 
102
 
        //std::cout << qPrintable(filePath) << std::endl;
103
 
                fi=QFileInfo(filePath);
104
 
                if (fi.exists() && fi.isReadable()){
105
 
                        pChecker->add_dic(filePath.toLatin1());
106
 
                }
107
 
                else filePath="";
108
 
        }
109
 
        else spellCheckActive=false;
110
 
        spellerError=!spellCheckActive;
111
 
        spellCheckActive=spellCheckActive && spellCheckState;
112
 
}
113
 
 
114
 
Highlighter::~Highlighter() {
115
 
        delete pChecker;
116
 
}
117
 
 
118
 
 
119
 
void Highlighter::highlightBlock(const QString &text) {
120
 
 
121
 
         spellCheck(text);
122
 
}
123
 
 
124
 
void Highlighter::enableSpellChecking(const bool state)
125
 
{
126
 
        bool old=spellCheckActive;
127
 
        if(!spellerError) spellCheckActive=state;
128
 
        if(old!=spellCheckActive) rehighlight();
129
 
}
130
 
 
131
 
void Highlighter::spellCheck(const QString &text)
132
 
{
133
 
        if (spellCheckActive) {
134
 
                // split text into words
135
 
                QString str = text.simplified();
136
 
                if (!str.isEmpty()) {
137
 
 
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);
142
 
                        int l,number;
143
 
                        // check all words
144
 
                        for (int i=0; i<Checkliste.size(); ++i) {
145
 
                                str = Checkliste.at(i);
146
 
                                if (str.length()>1 &&!str.startsWith('\\'))
147
 
                                {
148
 
                                        if (!checkWord(str)) {
149
 
                                                number = text.count(QRegExp("\\b" + str + "\\b"));
150
 
                                                l=-1;
151
 
                                                // underline all incorrect occurences of misspelled word
152
 
                                                for (int j=0;j < number; ++j)
153
 
                                                {
154
 
                                                        l = text.indexOf(QRegExp("\\b" + str + "\\b"),l+1);
155
 
                                                        if (l>=0)
156
 
                                                                setFormat(l, str.length(), spellCheckFormat);
157
 
 
158
 
 
159
 
 
160
 
                                                } // for j
161
 
                                        } // if spell chek error
162
 
                                } // if str.length > 1
163
 
                        } // for
164
 
                } // if str.isEmpty
165
 
        } // initial check
166
 
}
167
 
 
168
 
bool Highlighter::checkWord(QString word)
169
 
{
170
 
        int check;
171
 
        /*switch(check=mWords.value(word,-1)){
172
 
        case -1:
173
 
    {
174
 
                QByteArray encodedString;
175
 
                encodedString = codec->fromUnicode(word);
176
 
                check = pChecker->spell(encodedString.data());
177
 
                mWords[word]=check;
178
 
                break;
179
 
    }
180
 
        default:
181
 
                break;
182
 
        }
183
 
        */
184
 
        QByteArray encodedString;
185
 
        encodedString = codec->fromUnicode(word);
186
 
        check = pChecker->spell(encodedString.data());
187
 
        return bool(check);
188
 
}
189
 
 
190
 
bool Highlighter::setDict(const QString SpellDic)
191
 
{
192
 
        bool spell;
193
 
        if(SpellDic!=""){
194
 
                //mWords.clear();
195
 
                spell_dic=SpellDic.left(SpellDic.length()-4);
196
 
                delete pChecker;
197
 
                pChecker = new Hunspell(Buffer::current_dictionary_aff.toLatin1(),Buffer::current_dictionary.toLatin1());
198
 
 
199
 
                spell_encoding=QString(pChecker->get_dic_encoding());
200
 
                codec = QTextCodec::codecForName(spell_encoding.toLatin1());
201
 
 
202
 
                QFileInfo fi(SpellDic);
203
 
 
204
 
                if (fi.exists() && fi.isReadable()) spell=true;
205
 
                else spell=false;
206
 
 
207
 
                // get user config dictionary
208
 
 
209
 
        QString filePath=Buffer::current_dictionary;
210
 
 
211
 
        //std::cout << qPrintable(filePath) << std::endl;
212
 
                fi=QFileInfo(filePath);
213
 
                if (fi.exists() && fi.isReadable()){
214
 
                        pChecker->add_dic(filePath.toLatin1());
215
 
                }
216
 
                else filePath="";
217
 
 
218
 
                spellCheckFormat.setForeground(Qt::red);//faster Cursoroperation ...
219
 
                //spellCheckFormat.setUnderlineColor(QColor(Qt::red));
220
 
                //spellCheckFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
221
 
        }
222
 
        else spell=false;
223
 
        spellerError=!spell;
224
 
        spellCheckActive=spellCheckActive && spell;
225
 
        rehighlight();
226
 
        return spell;
227
 
}
228
 
 
229
 
void Highlighter::slot_addWord(QString word)
230
 
{
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());
237
 
    rehighlight();
238
 
}