diff --git a/libs/text/KoTextBlockData.cpp b/libs/text/KoTextBlockData.cpp index 29abbf6dfc1..8cb698ca6fe 100644 --- a/libs/text/KoTextBlockData.cpp +++ b/libs/text/KoTextBlockData.cpp @@ -1,302 +1,310 @@ /* This file is part of the KDE project * Copyright (C) 2006 Thomas Zander * Copyright (C) 2010 C. Boemann * Copyright (C) 2011 Boudewijn Rempt * * 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 "KoTextBlockData.h" #include "KoTextBlockBorderData.h" #include "KoTextBlockPaintStrategyBase.h" class Q_DECL_HIDDEN KoTextBlockData::Private : public QTextBlockUserData { public: Private() : counterWidth(-1.0) , counterSpacing(0) , counterIsImage(false) , counterIndex(1) , border(0) , paintStrategy(0) { layoutedMarkupRanges[KoTextBlockData::Misspell] = false; layoutedMarkupRanges[KoTextBlockData::Grammar] = false; } ~Private() { if (border && !border->deref()) delete border; delete paintStrategy; } qreal counterWidth; qreal counterSpacing; QString counterPrefix; QString counterPlainText; QString counterSuffix; QString partialCounterText; bool counterIsImage; int counterIndex; QPointF counterPos; QTextCharFormat labelFormat; KoTextBlockBorderData *border; KoTextBlockPaintStrategyBase *paintStrategy; QMap > markupRangesMap; QMap layoutedMarkupRanges; }; KoTextBlockData::KoTextBlockData(QTextBlock &block) : d(block.userData() ? dynamic_cast(block.userData()) : new Private()) { block.setUserData(d); } KoTextBlockData::KoTextBlockData(QTextBlockUserData *userData) : d(dynamic_cast(userData)) { Q_ASSERT(d); } KoTextBlockData::~KoTextBlockData() { // explicitly do not delete the d-pointer here } void KoTextBlockData::appendMarkup(MarkupType type, int firstChar, int lastChar) { - Q_ASSERT(d->markupRangesMap[type].isEmpty() || d->markupRangesMap[type].last().lastChar < firstChar); + if (type == KoTextBlockData::Grammar) { + Q_ASSERT(d->markupRangesMap[type].isEmpty() || d->markupRangesMap[type].last().lastChar < firstChar); + } else if (!d->markupRangesMap[type].isEmpty() && d->markupRangesMap[type].last().lastChar >= firstChar) { + // Character positions from spellchecker are not in sync with document. + // I have only seen this in connection with dropcaps, so could be caused by + // large amount to spellcheck combined with large changes to the document. + // Anyway, returning here just means markup will be incorrect/missing until next spellcheck is run. + return; + } MarkupRange range; range.firstChar = firstChar; range.lastChar = lastChar; d->layoutedMarkupRanges[type] = false; d->markupRangesMap[type].append(range); } void KoTextBlockData::clearMarkups(MarkupType type) { d->markupRangesMap[type].clear(); d->layoutedMarkupRanges[type] = false; } KoTextBlockData::MarkupRange KoTextBlockData::findMarkup(MarkupType type, int positionWithin) const { foreach (const MarkupRange &range, d->markupRangesMap[type]) { if (positionWithin <= range.lastChar) { // possible hit if (positionWithin >= range.firstChar) { return range; } else { return MarkupRange(); // we have passed it without finding } } } return MarkupRange(); // either no ranges or not in last either } void KoTextBlockData::rebaseMarkups(MarkupType type, int fromPosition, int delta) { QVector::Iterator markIt = markupsBegin(type); QVector::Iterator markEnd = markupsEnd(type); while (markIt != markEnd) { if (fromPosition <= markIt->lastChar) { // we need to modify the end of this markIt->lastChar += delta; } if (fromPosition < markIt->firstChar) { // we need to modify the end of this markIt->firstChar += delta; } ++markIt; } } void KoTextBlockData::setMarkupsLayoutValidity(MarkupType type, bool valid) { d->layoutedMarkupRanges[type] = valid; } bool KoTextBlockData::isMarkupsLayoutValid(MarkupType type) const { return d->layoutedMarkupRanges[type]; } QVector::Iterator KoTextBlockData::markupsBegin(MarkupType type) { return d->markupRangesMap[type].begin(); } QVector::Iterator KoTextBlockData::markupsEnd(MarkupType type) { return d->markupRangesMap[type].end(); } bool KoTextBlockData::hasCounterData() const { return d->counterWidth >= 0 && (!d->counterPlainText.isNull() || d->counterIsImage); } qreal KoTextBlockData::counterWidth() const { return qMax(qreal(0), d->counterWidth); } void KoTextBlockData::setBorder(KoTextBlockBorderData *border) { if (d->border && !d->border->deref()) delete d->border; d->border = border; if (d->border) d->border->ref(); } void KoTextBlockData::setCounterWidth(qreal width) { d->counterWidth = width; } qreal KoTextBlockData::counterSpacing() const { return d->counterSpacing; } void KoTextBlockData::setCounterSpacing(qreal spacing) { d->counterSpacing = spacing; } QString KoTextBlockData::counterText() const { return d->counterPrefix + d->counterPlainText + d->counterSuffix; } void KoTextBlockData::clearCounter() { d->partialCounterText.clear(); d->counterPlainText.clear(); d->counterPrefix.clear(); d->counterSuffix.clear(); d->counterSpacing = 0.0; d->counterWidth = 0.0; d->counterIsImage = false; } void KoTextBlockData::setPartialCounterText(const QString &text) { d->partialCounterText = text; } QString KoTextBlockData::partialCounterText() const { return d->partialCounterText; } void KoTextBlockData::setCounterPlainText(const QString &text) { d->counterPlainText = text; } QString KoTextBlockData::counterPlainText() const { return d->counterPlainText; } void KoTextBlockData::setCounterPrefix(const QString &text) { d->counterPrefix = text; } QString KoTextBlockData::counterPrefix() const { return d->counterPrefix; } void KoTextBlockData::setCounterSuffix(const QString &text) { d->counterSuffix = text; } QString KoTextBlockData::counterSuffix() const { return d->counterSuffix; } void KoTextBlockData::setCounterIsImage(bool isImage) { d->counterIsImage = isImage; } bool KoTextBlockData::counterIsImage() const { return d->counterIsImage; } void KoTextBlockData::setCounterIndex(int index) { d->counterIndex = index; } int KoTextBlockData::counterIndex() const { return d->counterIndex; } void KoTextBlockData::setCounterPosition(const QPointF &position) { d->counterPos = position; } QPointF KoTextBlockData::counterPosition() const { return d->counterPos; } void KoTextBlockData::setLabelFormat(const QTextCharFormat &format) { d->labelFormat = format; } QTextCharFormat KoTextBlockData::labelFormat() const { return d->labelFormat; } KoTextBlockBorderData *KoTextBlockData::border() const { return d->border; } void KoTextBlockData::setPaintStrategy(KoTextBlockPaintStrategyBase *paintStrategy) { delete d->paintStrategy; d->paintStrategy = paintStrategy; } KoTextBlockPaintStrategyBase *KoTextBlockData::paintStrategy() const { return d->paintStrategy; } bool KoTextBlockData::saveXmlID() const { // as suggested by boemann, http://lists.kde.org/?l=calligra-devel&m=132396354701553&w=2 return d->paintStrategy != 0; } diff --git a/plugins/textediting/spellcheck/SpellCheck.cpp b/plugins/textediting/spellcheck/SpellCheck.cpp index 6d7efa0b9ee..ca96d810425 100644 --- a/plugins/textediting/spellcheck/SpellCheck.cpp +++ b/plugins/textediting/spellcheck/SpellCheck.cpp @@ -1,359 +1,364 @@ /* This file is part of the KDE project * Copyright (C) 2007, 2008 Fredy Yanardi * Copyright (C) 2007,2009,2010 Thomas Zander * Copyright (C) 2010 Christoph Goerlich * Copyright (C) 2012 Shreya Pandit * * 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 "SpellCheckMenu.h" #include "SpellCheckDebug.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include SpellCheck::SpellCheck() : m_document(0) , m_bgSpellCheck(0) , m_enableSpellCheck(true) , m_documentIsLoading(false) , m_isChecking(false) , m_spellCheckMenu(0) , m_activeSection(0, 0, 0) , m_simpleEdit(false) + , m_cursorPosition(0) { /* setup actions for this plugin */ QAction *configureAction = new QAction(i18n("Configure &Spell Checking..."), this); connect(configureAction, SIGNAL(triggered()), this, SLOT(configureSpellCheck())); addAction("tool_configure_spellcheck", configureAction); KToggleAction *spellCheck = new KToggleAction(i18n("Auto Spell Check"), this); addAction("tool_auto_spellcheck", spellCheck); KConfigGroup spellConfig = KSharedConfig::openConfig()->group("Spelling"); m_enableSpellCheck = spellConfig.readEntry("autoSpellCheck", m_enableSpellCheck); spellCheck->setChecked(m_enableSpellCheck); m_speller = Sonnet::Speller(spellConfig.readEntry("defaultLanguage", "en_US")); m_bgSpellCheck = new BgSpellCheck(m_speller, this); m_spellCheckMenu = new SpellCheckMenu(m_speller, this); QPair pair = m_spellCheckMenu->menuAction(); addAction(pair.first, pair.second); connect(m_bgSpellCheck, SIGNAL(misspelledWord(QString,int,bool)), this, SLOT(highlightMisspelled(QString,int,bool))); connect(m_bgSpellCheck, SIGNAL(done()), this, SLOT(finishedRun())); connect(spellCheck, SIGNAL(toggled(bool)), this, SLOT(setBackgroundSpellChecking(bool))); } void SpellCheck::finishedWord(QTextDocument *document, int cursorPosition) { setDocument(document); if (!m_enableSpellCheck) return; QTextBlock block = document->findBlock(cursorPosition); if (!block.isValid()) return; KoTextBlockData blockData(block); blockData.setMarkupsLayoutValidity(KoTextBlockData::Misspell, false); checkSection(document, block.position(), block.position() + block.length() - 1); } void SpellCheck::finishedParagraph(QTextDocument *document, int cursorPosition) { setDocument(document); Q_UNUSED(document); Q_UNUSED(cursorPosition); } void SpellCheck::startingSimpleEdit(QTextDocument *document, int cursorPosition) { m_simpleEdit = true; setDocument(document); - Q_UNUSED(document); - Q_UNUSED(cursorPosition); + m_cursorPosition = cursorPosition; } void SpellCheck::checkSection(QTextDocument *document, int startPosition, int endPosition) { if (startPosition >= endPosition) { // no work return; } foreach (const SpellSections &ss, m_documentsQueue) { if (ss.from <= startPosition && ss.to >= endPosition) { runQueue(); m_spellCheckMenu->setVisible(true); return; } // TODO also check if we should replace an existing queued item with a longer span } SpellSections ss(document, startPosition, endPosition); m_documentsQueue.enqueue(ss); runQueue(); m_spellCheckMenu->setVisible(true); } void SpellCheck::setDocument(QTextDocument *document) { if (m_document == document) return; if (m_document) disconnect (document, SIGNAL(contentsChange(int,int,int)), this, SLOT(documentChanged(int,int,int))); m_document = document; connect (document, SIGNAL(contentsChange(int,int,int)), this, SLOT(documentChanged(int,int,int))); } 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); if (m_enableSpellCheck && m_document) { checkSection(m_document, 0, m_document->characterCount() - 1); } } void SpellCheck::setBackgroundSpellChecking(bool on) { if (m_enableSpellCheck == on) return; KConfigGroup spellConfig = KSharedConfig::openConfig()->group("Spelling"); m_enableSpellCheck = on; spellConfig.writeEntry("autoSpellCheck", m_enableSpellCheck); if (m_document) { if (!m_enableSpellCheck) { for (QTextBlock block = m_document->begin(); block != m_document->end(); block = block.next()) { KoTextBlockData blockData(block); blockData.clearMarkups(KoTextBlockData::Misspell); } m_spellCheckMenu->setEnabled(false); m_spellCheckMenu->setVisible(false); } else { //when re-enabling 'Auto Spell Check' we want spellchecking the whole document checkSection(m_document, 0, m_document->characterCount() - 1); m_spellCheckMenu->setVisible(true); } } } void SpellCheck::setSkipAllUppercaseWords(bool on) { m_speller.setAttribute(Speller::CheckUppercase, !on); } void SpellCheck::setSkipRunTogetherWords(bool on) { m_speller.setAttribute(Speller::SkipRunTogether, on); } bool SpellCheck::addWordToPersonal(const QString &word, int startPosition) { QTextBlock block = m_document->findBlock(startPosition); if (!block.isValid()) return false; KoTextBlockData blockData(block); blockData.setMarkupsLayoutValidity(KoTextBlockData::Misspell, false); checkSection(m_document, block.position(), block.position() + block.length() - 1); // TODO we should probably recheck the entire document so other occurrences are also removed, but then again we should recheck every document (footer,header etc) not sure how to do this return m_bgSpellCheck->addWordToPersonal(word); } 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(QThread::currentThread())->mySleep(400); #endif QTextBlock block = m_activeSection.document->findBlock(startPosition); KoTextBlockData blockData(block); blockData.appendMarkup(KoTextBlockData::Misspell, startPosition - block.position(), startPosition - block.position() + word.trimmed().length()); } -void SpellCheck::documentChanged(int from, int min, int plus) +void SpellCheck::documentChanged(int from, int charsRemoved, int charsAdded) { + Q_UNUSED(charsRemoved); QTextDocument *document = qobject_cast(sender()); if (document == 0) return; QTextBlock block = document->findBlock(from); if (!block.isValid()) return; do { KoTextBlockData blockData(block); if (m_enableSpellCheck) { blockData.setMarkupsLayoutValidity(KoTextBlockData::Misspell, false); - if (m_simpleEdit) { - // if it's a simple edit we will wait until finishedWord - blockData.rebaseMarkups(KoTextBlockData::Misspell, from, plus - min); + // We cannot safely handle combined remove/add in case it spans words. + // This should probably never be defined as a simple edit anyway, + // but atm lines with dropcaps trigger this behaviour. See KoTextLayoutArea. + if (m_simpleEdit && (charsRemoved == 0 || charsAdded == 0)) { + // If it's a simple edit we will wait until finishedWord before spellchecking + // but we need to adjust all markups behind the added/removed character(s) + blockData.rebaseMarkups(KoTextBlockData::Misspell, from - block.position(), charsAdded - charsRemoved); } else { checkSection(document, block.position(), block.position() + block.length() - 1); } } else { blockData.clearMarkups(KoTextBlockData::Misspell); } block = block.next(); - } while(block.isValid() && block.position() <= from + plus); + } while(block.isValid() && block.position() <= from + charsAdded); m_simpleEdit = false; } void SpellCheck::runQueue() { Q_ASSERT(QThread::currentThread() == QApplication::instance()->thread()); if (m_isChecking) return; while (!m_documentsQueue.isEmpty()) { m_activeSection = m_documentsQueue.dequeue(); if (m_activeSection.document.isNull()) continue; QTextBlock block = m_activeSection.document->findBlock(m_activeSection.from); if (!block.isValid()) continue; m_isChecking = true; do { KoTextBlockData blockData(block); blockData.clearMarkups(KoTextBlockData::Misspell); block = block.next(); } while(block.isValid() && block.position() < m_activeSection.to); m_bgSpellCheck->startRun(m_activeSection.document, m_activeSection.from, m_activeSection.to); break; } } void SpellCheck::configureSpellCheck() { Sonnet::ConfigDialog *dialog = new Sonnet::ConfigDialog(0); connect (dialog, SIGNAL(languageChanged(QString)), this, SLOT(setDefaultLanguage(QString))); dialog->exec(); delete dialog; } void SpellCheck::finishedRun() { Q_ASSERT(QThread::currentThread() == QApplication::instance()->thread()); m_isChecking = false; KoTextDocumentLayout *lay = qobject_cast(m_activeSection.document->documentLayout()); lay->provider()->updateAll(); QTimer::singleShot(0, this, SLOT(runQueue())); } void SpellCheck::setCurrentCursorPosition(QTextDocument *document, int cursorPosition) { setDocument(document); if (m_enableSpellCheck) { //check if word at cursor is misspelled QTextBlock block = m_document->findBlock(cursorPosition); if (block.isValid()) { KoTextBlockData blockData(block); KoTextBlockData::MarkupRange range = blockData.findMarkup(KoTextBlockData::Misspell, cursorPosition - block.position()); if (int length = range.lastChar - range.firstChar) { QString word = block.text().mid(range.firstChar, length); m_spellCheckMenu->setMisspelled(word, block.position() + range.firstChar, length); QString language = m_bgSpellCheck->currentLanguage(); if (!m_bgSpellCheck->currentLanguage().isEmpty() && !m_bgSpellCheck->currentCountry().isEmpty()) { language += '_'; } language += m_bgSpellCheck->currentCountry(); m_spellCheckMenu->setCurrentLanguage(language); m_spellCheckMenu->setVisible(true); m_spellCheckMenu->setEnabled(true); return; } m_spellCheckMenu->setEnabled(false); } else { m_spellCheckMenu->setEnabled(false); } } } void SpellCheck::replaceWordBySuggestion(const QString &word, int startPosition, int lengthOfWord) { if (!m_document) return; QTextBlock block = m_document->findBlock(startPosition); if (!block.isValid()) return; QTextCursor cursor(m_document); cursor.setPosition(startPosition); cursor.movePosition(QTextCursor::NextCharacter,QTextCursor::KeepAnchor, lengthOfWord); cursor.removeSelectedText(); cursor.insertText(word); } diff --git a/plugins/textediting/spellcheck/SpellCheck.h b/plugins/textediting/spellcheck/SpellCheck.h index 485fdbb07a2..93c789d7a19 100644 --- a/plugins/textediting/spellcheck/SpellCheck.h +++ b/plugins/textediting/spellcheck/SpellCheck.h @@ -1,117 +1,118 @@ /* This file is part of the KDE project * Copyright (C) 2007 Fredy Yanardi * Copyright (C) 2007,2010 Thomas Zander * Copyright (C) 2010 Christoph Goerlich * Copyright (C) 2012 Shreya Pandit * * 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. */ #ifndef SPELLCHECK_H #define SPELLCHECK_H #include #include #include #include #include #include #include #include class QTextDocument; class QTextStream; class BgSpellCheck; class SpellCheckMenu; class SpellCheck : public KoTextEditingPlugin { Q_OBJECT public: SpellCheck(); /// reimplemented from superclass void finishedWord(QTextDocument *document, int cursorPosition); /// reimplemented from superclass void finishedParagraph(QTextDocument *document, int cursorPosition); /// reimplemented from superclass void startingSimpleEdit(QTextDocument *document, int cursorPosition); /// reimplemented from superclass void checkSection(QTextDocument *document, int startPosition, int endPosition); ///reimplemented from superclass void setCurrentCursorPosition(QTextDocument *document, int cursorPosition); QStringList availableBackends() const; QStringList availableLanguages() const; void setSkipAllUppercaseWords(bool b); void setSkipRunTogetherWords(bool b); QString defaultLanguage() const; bool backgroundSpellChecking(); bool skipAllUppercaseWords(); bool skipRunTogetherWords(); bool addWordToPersonal(const QString &word, int startPosition); //reimplemented from Calligra2.0, we disconnect and re- connect the 'documentChanged' signal only when the document has replaced void setDocument(QTextDocument *document); void replaceWordBySuggestion(const QString &word, int startPosition,int lengthOfWord); public Q_SLOTS: void setDefaultLanguage(const QString &lang); private Q_SLOTS: void highlightMisspelled(const QString &word, int startPosition, bool misspelled = true); void finishedRun(); void configureSpellCheck(); void runQueue(); void setBackgroundSpellChecking(bool b); - void documentChanged(int from, int min, int plus); + void documentChanged(int from, int charsRemoved, int charsAdded); private: Sonnet::Speller m_speller; QPointer m_document; QString m_word; BgSpellCheck *m_bgSpellCheck; struct SpellSections { SpellSections(QTextDocument *doc, int start, int end) : document(doc) { from = start; to = end; } QPointer document; int from; int to; }; QQueue m_documentsQueue; bool m_enableSpellCheck; bool m_documentIsLoading; bool m_isChecking; QTextStream stream; SpellCheckMenu *m_spellCheckMenu; SpellSections m_activeSection; // the section we are currently doing a run on; - bool m_simpleEdit; //set when user is doing a simple edit, meaning we should ignore documentCanged + bool m_simpleEdit; //set when user is doing a simple edit, meaning we should not start spellchecking + int m_cursorPosition; // simple edit cursor position }; #endif