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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* This file is part of the KDE project
* Copyright (C) 2007, 2008 Fredy Yanardi <fyanardi@gmail.com>
* Copyright (C) 2007,2009,2010 Thomas Zander <zander@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* 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 "SpellCheck.h"
#include "BgSpellCheck.h"
#include <KoCharacterStyle.h>
#include <KoResourceManager.h>
#include <KLocale>
#include <KDebug>
#include <KAction>
#include <sonnet/configdialog.h>
#include <QTextBlock>
#include <QThread>
#include <QTimer>
#include <QApplication>
#include <QTextCharFormat>
SpellCheck::SpellCheck()
: m_bgSpellCheck(0),
m_enableSpellCheck(true),
m_allowSignals(true),
m_documentIsLoading(false),
m_isChecking(false)
{
/* setup actions for this plugin */
KAction *configureAction = new KAction(i18n("Configure &Spell Checking..."), this);
connect(configureAction, SIGNAL(triggered()), this, SLOT(configureSpellCheck()));
addAction("tool_configure_spellcheck", configureAction);
KConfigGroup spellConfig = KGlobal::config()->group("Spelling");
m_speller = Sonnet::Speller(spellConfig.readEntry("defaultLanguage", "en_US"));
m_bgSpellCheck = new BgSpellCheck(m_speller, this);
m_defaultMisspelledFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
m_defaultMisspelledFormat.setUnderlineColor(QColor(Qt::red)); // TODO make use kde-config
connect(m_bgSpellCheck, SIGNAL(misspelledWord(const QString &,int,bool)),
this, SLOT(highlightMisspelled(const QString &,int,bool)));
connect(m_bgSpellCheck, SIGNAL(done()), this, SLOT(finishedRun()));
}
void SpellCheck::finishedWord(QTextDocument *document, int cursorPosition)
{
if (!m_enableSpellCheck)
return;
QTextBlock block = document->findBlock(cursorPosition);
if (!block.isValid())
return;
checkSection(document, block.position(), block.position() + block.length() - 1);
}
void SpellCheck::finishedParagraph(QTextDocument *document, int cursorPosition)
{
Q_UNUSED(document);
Q_UNUSED(cursorPosition);
//QTextBlock block = document->findBlock(cursorPosition);
//checkSection(document, block.position(), block.position() + block.length());
}
void SpellCheck::checkSection(QTextDocument *document, int startPosition, int endPosition)
{
if (!m_enableSpellCheck)
return;
if (startPosition >= endPosition) // no work
return;
foreach (const SpellSections &ss, m_documentsQueue) {
if (ss.from <= startPosition && ss.to >= endPosition)
return;
// TODO also check if we should replace an existing queued item with a longer span
}
disconnect (document, SIGNAL(contentsChange(int,int,int)), this, SLOT(documentChanged(int,int,int)));
connect (document, SIGNAL(contentsChange(int,int,int)), this, SLOT(documentChanged(int,int,int)));
SpellSections ss(document, startPosition, endPosition);
m_documentsQueue.enqueue(ss);
runQueue();
}
QStringList SpellCheck::availableBackends() const
{
return m_speller.availableBackends();
}
QStringList SpellCheck::availableLanguages() const
{
return m_speller.availableLanguages();
}
void SpellCheck::setDefaultLanguage(const QString &language)
{
m_speller.setDefaultLanguage(language);
m_bgSpellCheck->setDefaultLanguage(language);
}
void SpellCheck::setBackgroundSpellChecking(bool on)
{
if (m_enableSpellCheck == on)
return;
m_enableSpellCheck = on;
if (!m_enableSpellCheck) {
// TODO remove all misspellings.
}
}
void SpellCheck::setSkipAllUppercaseWords(bool on)
{
m_speller.setAttribute(Speller::CheckUppercase, !on);
}
void SpellCheck::setSkipRunTogetherWords(bool on)
{
m_speller.setAttribute(Speller::SkipRunTogether, on);
}
QString SpellCheck::defaultLanguage() const
{
return m_speller.defaultLanguage();
}
bool SpellCheck::backgroundSpellChecking()
{
return m_enableSpellCheck;
}
bool SpellCheck::skipAllUppercaseWords()
{
return m_speller.testAttribute(Speller::CheckUppercase);
}
bool SpellCheck::skipRunTogetherWords()
{
return m_speller.testAttribute(Speller::SkipRunTogether);
}
void SpellCheck::highlightMisspelled(const QString &word, int startPosition, bool misspelled)
{
if (!misspelled)
return;
#if 0
// DEBUG
class MyThread : public QThread { public: static void mySleep(unsigned long msecs) { msleep(msecs); }};
static_cast<MyThread*>(QThread::currentThread())->mySleep(400);
#endif
int blockIndex = 0;
while (blockIndex < m_misspellings.count()) {
BlockLayout bl = m_misspellings[blockIndex];
if (bl.start <= startPosition && bl.start + bl.length > startPosition) {
break;
}
++blockIndex;
}
if (blockIndex >= m_misspellings.count()) // not found, doc went out of sync
return;
BlockLayout block = m_misspellings.at(blockIndex);
QTextLayout::FormatRange range;
range.format = m_defaultMisspelledFormat;
range.start = startPosition - block.start;
range.length = word.trimmed().length();
block.ranges << range;
m_misspellings[blockIndex] = block;
}
void SpellCheck::documentChanged(int from, int min, int plus)
{
if (min == plus)
return;
if (m_isChecking)
return;
QTextDocument *document = qobject_cast<QTextDocument*>(sender());
if (document == 0)
return;
QTextBlock block = document->findBlock(from);
if (!block.isValid())
return;
QList<QTextLayout::FormatRange> ranges = block.layout()->additionalFormats();
bool changed = false;
for (int i=0; i < ranges.count(); ++i) {
const QTextLayout::FormatRange &range = ranges.at(i);
if (range.start > from && range.format == m_defaultMisspelledFormat) {
QTextLayout::FormatRange newRange = range;
newRange.start += plus - min;
ranges[i] = newRange;
changed = true;
} else if ((range.start > from || range.start + range.length > from)
&& range.format == m_defaultMisspelledFormat) {
ranges.removeAt(i);
--i;
changed = true;
}
}
if (changed) {
block.layout()->setAdditionalFormats(ranges);
}
}
void SpellCheck::runQueue()
{
Q_ASSERT(QThread::currentThread() == QApplication::instance()->thread());
if (m_isChecking)
return;
while (!m_documentsQueue.isEmpty()) {
SpellSections section = m_documentsQueue.dequeue();
if (section.document.isNull())
continue;
QTextBlock block = section.document->findBlock(section.from);
if (!block.isValid())
continue;
m_isChecking = true;
m_document = section.document;
m_misspellings.clear();
do {
BlockLayout bl;
bl.start = block.position();
bl.length = block.length();
bl.checkStart = qMax(bl.start, section.from);
m_misspellings << bl;
block = block.next();
} while(block.isValid() && block.position() < section.to);
m_bgSpellCheck->startRun(section.document, section.from, section.to);
break;
}
}
void SpellCheck::configureSpellCheck()
{
Sonnet::ConfigDialog *dialog = new Sonnet::ConfigDialog(KGlobal::config().data(), 0);
connect (dialog, SIGNAL(languageChanged(const QString&)), this, SLOT(setDefaultLanguage(const QString&)));
dialog->exec();
delete dialog;
}
void SpellCheck::finishedRun()
{
Q_ASSERT(QThread::currentThread() == QApplication::instance()->thread());
m_isChecking = false;
m_allowSignals = false;
foreach (const BlockLayout &bl, m_misspellings) {
QTextBlock block = m_document->findBlock(bl.start);
if (!block.isValid())
continue;
if (bl.start != block.position() || bl.length != block.length())
continue;
QList<QTextLayout::FormatRange> ranges = block.layout()->additionalFormats();
bool changed = false;
int numMisspellings = 0;
foreach (const QTextLayout::FormatRange &range, ranges) {
if (range.format != m_defaultMisspelledFormat)
continue;
if (range.start + range.length < bl.checkStart)
continue;
++numMisspellings;
bool found = false;
foreach (const QTextLayout::FormatRange &newRange, bl.ranges) {
if (range.start == newRange.start && range.length == newRange.length) {
found = true;
break;
}
}
if (!found) {
changed = true;
break;
}
}
if (changed || numMisspellings != bl.ranges.count()) {
QList<QTextLayout::FormatRange> newRanges;
newRanges = bl.ranges;
foreach (const QTextLayout::FormatRange &range, ranges) {
if (range.format != m_defaultMisspelledFormat
|| range.start + range.length < bl.checkStart)
newRanges << range;
}
if (newRanges.isEmpty())
block.layout()->clearAdditionalFormats();
else
block.layout()->setAdditionalFormats(newRanges);
m_document->markContentsDirty(bl.start, bl.start + bl.length);
}
}
m_allowSignals = true;
QTimer::singleShot(0, this, SLOT(runQueue()));
}
#include <SpellCheck.moc>
|