diff options
Diffstat (limited to 'Swift')
-rw-r--r-- | Swift/QtUI/QtTextEdit.cpp | 57 | ||||
-rw-r--r-- | Swift/QtUI/QtTextEdit.h | 5 |
2 files changed, 39 insertions, 23 deletions
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index 0d0f31c..2dabd7e 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -4,6 +4,8 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ +#include <boost/tuple/tuple.hpp> + #include <SwifTools/SpellCheckerFactory.h> #include <SwifTools/SpellChecker.h> @@ -59,25 +61,24 @@ void QtTextEdit::keyPressEvent(QKeyEvent* event) { } void QtTextEdit::underlineMisspells() { + misspelledPositions_.clear(); QTextCharFormat spellingErrorFormat; QTextCharFormat normalFormat; spellingErrorFormat.setUnderlineColor(QColor(Qt::red)); spellingErrorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline); QTextCursor cursor = textCursor(); - QString word; - cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 1); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1); - word = cursor.selectedText(); - // Qt handles punctuation as words. Workaround that. - while (!word.isEmpty() && !word.at(0).isLetter()) { - cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 2); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1); - word = cursor.selectedText(); - } - if ((!word.isEmpty()) && !checker_->isCorrect(word.toStdString())) { - cursor.mergeCharFormat(spellingErrorFormat); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor, 1); - cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor, 1); + cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1); + cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor, 1); + cursor.setCharFormat(normalFormat); + std::string fragment = Q2PSTRING(cursor.selectedText()); + checker_->checkFragment(fragment, misspelledPositions_); + for (PositionPairVector::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) { + if (textCursor().position() > boost::get<1>(*it)) { + cursor.setPosition(boost::get<0>(*it), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(*it), QTextCursor::KeepAnchor); + cursor.setCharFormat(spellingErrorFormat); + cursor.clearSelection(); + } cursor.setCharFormat(normalFormat); } } @@ -90,14 +91,23 @@ void QtTextEdit::handleTextChanged() { } } -void QtTextEdit::handleReplaceMisspellWord(const QString& word, const QPoint& position) { - QTextCursor cursor = cursorForPosition(position); - cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 1); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1); +void QtTextEdit::handleReplaceMisspellWord(const QString& word, const boost::tuple<int, int>& wordPosition) { + QTextCursor cursor = textCursor(); + cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); QTextCharFormat normalFormat; cursor.insertText(word, normalFormat); } +boost::tuple<int, int> QtTextEdit::getWordFromCursor(int cursorPosition) { + for (PositionPairVector::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) { + if (cursorPosition >= boost::get<0>(*it) && cursorPosition <= boost::get<1>(*it)) { + return *it; + } + } + return boost::make_tuple(-1,-1); +} + QSize QtTextEdit::sizeHint() const { QFontMetrics inputMetrics(currentFont()); QRect horizontalBounds = contentsRect().adjusted(0,0,0,9999); @@ -113,11 +123,14 @@ QSize QtTextEdit::sizeHint() const { void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { QTextCursor cursor = cursorForPosition(event->pos()); - cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor, 1); - cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor, 1); + boost::tuple<int, int> wordPosition = getWordFromCursor(cursor.position()); + if (boost::get<0>(wordPosition) < 0) { + return; + } + cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); std::vector<std::string> wordList; checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList); - std::vector<QAction*> replaceWordActions; QMenu *menu = createStandardContextMenu(); for (std::vector<std::string>::iterator it = wordList.begin(); it != wordList.end(); ++it) { @@ -127,7 +140,7 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { QAction* result = menu->exec(event->globalPos()); for (std::vector<QAction*>::iterator it = replaceWordActions.begin(); it != replaceWordActions.end(); ++it) { if (*it == result) { - handleReplaceMisspellWord((*it)->text(), event->pos()); + handleReplaceMisspellWord((*it)->text(), wordPosition); } } delete menu; diff --git a/Swift/QtUI/QtTextEdit.h b/Swift/QtUI/QtTextEdit.h index a940879..76087c9 100644 --- a/Swift/QtUI/QtTextEdit.h +++ b/Swift/QtUI/QtTextEdit.h @@ -7,6 +7,7 @@ #pragma once #include <QTextEdit> +#include <SwifTools/SpellParser.h> namespace Swift { class SpellChecker; @@ -21,7 +22,7 @@ namespace Swift { void returnPressed(); void unhandledKeyPressEvent(QKeyEvent* event); public slots: - void handleReplaceMisspellWord(const QString& word, const QPoint& position); + void handleReplaceMisspellWord(const QString& word, const boost::tuple<int,int>& wordPosition); protected: virtual void keyPressEvent(QKeyEvent* event); virtual void contextMenuEvent(QContextMenuEvent* event); @@ -29,6 +30,8 @@ namespace Swift { void handleTextChanged(); private: SpellChecker *checker_; + PositionPairVector misspelledPositions_; void underlineMisspells(); + boost::tuple<int,int> getWordFromCursor(int cursorPosition); }; } |