diff options
Diffstat (limited to 'Swift/QtUI/QtTextEdit.cpp')
-rw-r--r-- | Swift/QtUI/QtTextEdit.cpp | 86 |
1 files changed, 58 insertions, 28 deletions
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index e523dca..f115d1c 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -65,23 +65,18 @@ void QtTextEdit::keyPressEvent(QKeyEvent* event) { } void QtTextEdit::underlineMisspells() { - if (checker_ == NULL) { - // Try to setUp the checker again in case that - // settings changed. - setUpSpellChecker(); - if (checker_ == NULL) { - return; - } - } + QTextCursor cursor = textCursor(); misspelledPositions_.clear(); - QTextCharFormat spellingErrorFormat; QTextCharFormat normalFormat; - spellingErrorFormat.setUnderlineColor(QColor(Qt::red)); - spellingErrorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline); - QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1); cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor, 1); cursor.setCharFormat(normalFormat); + if (checker_ == NULL) { + return; + } + QTextCharFormat spellingErrorFormat; + spellingErrorFormat.setUnderlineColor(QColor(Qt::red)); + spellingErrorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline); std::string fragment = Q2PSTRING(cursor.selectedText()); checker_->checkFragment(fragment, misspelledPositions_); for (PositionPairList::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) { @@ -103,8 +98,9 @@ void QtTextEdit::handleTextChanged() { } } -void QtTextEdit::handleReplaceMisspellWord(const QString& word, const boost::tuple<int, int>& wordPosition) { +void QtTextEdit::replaceMisspelledWord(const QString& word, int cursorPosition) { QTextCursor cursor = textCursor(); + boost::tuple<int, int> wordPosition = getWordFromCursor(cursorPosition); cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); QTextCharFormat normalFormat; @@ -136,41 +132,55 @@ QSize QtTextEdit::sizeHint() const { void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { QMenu *menu = createStandardContextMenu(); QTextCursor cursor = cursorForPosition(event->pos()); + QAction *insertPoint = menu->actions().first(); + + QAction *settingsAction = new QAction(QApplication::translate("QtTextEdit", "Spell Checker Options", 0, QApplication::UnicodeUTF8), menu); + menu->insertAction(insertPoint, settingsAction); + menu->insertAction(insertPoint, menu->addSeparator()); + addSuggestions(menu, event); + QAction* result = menu->exec(event->globalPos()); + if (result == settingsAction) { + spellCheckerSettingsWindow(); + } + for (std::vector<QAction*>::iterator it = replaceWordActions_.begin(); it != replaceWordActions_.end(); ++it) { + if (*it == result) { + replaceMisspelledWord((*it)->text(), cursor.position()); + } + } + delete menu; +} + +void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event) +{ + replaceWordActions_.clear(); + QAction *insertPoint = menu->actions().first(); + QTextCursor cursor = cursorForPosition(event->pos()); boost::tuple<int, int> wordPosition = getWordFromCursor(cursor.position()); if (boost::get<0>(wordPosition) < 0) { // The click was executed outside a spellable word so no - // suggestion menu is necessary - menu->exec(event->globalPos()); - delete menu; + // suggestions are necessary 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; - QAction *insertPoint = menu->actions().first(); if (wordList.size() == 0) { QAction *noSuggestions = new QAction(QApplication::translate("QtTextEdit", "No Suggestions", 0, QApplication::UnicodeUTF8), menu); noSuggestions->setDisabled(true); menu->insertAction(insertPoint, noSuggestions); - } else { + } + else { for (std::vector<std::string>::iterator it = wordList.begin(); it != wordList.end(); ++it) { QAction *wordAction = new QAction(it->c_str(), menu); menu->insertAction(insertPoint, wordAction); - replaceWordActions.push_back(wordAction); + replaceWordActions_.push_back(wordAction); } } menu->insertAction(insertPoint, menu->addSeparator()); - QAction* result = menu->exec(event->globalPos()); - for (std::vector<QAction*>::iterator it = replaceWordActions.begin(); it != replaceWordActions.end(); ++it) { - if (*it == result) { - handleReplaceMisspellWord((*it)->text(), wordPosition); - } - } - delete menu; } + void QtTextEdit::setUpSpellChecker() { SpellCheckerFactory *checkerFactory = new SpellCheckerFactory(); @@ -188,9 +198,29 @@ void QtTextEdit::setUpSpellChecker() } checker_ = checkerFactory->createSpellChecker((dictPath + affixFile).c_str(), (dictPath + dictFile).c_str()); delete checkerFactory; - } else { + } + else { checker_ = NULL; } } +void QtTextEdit::spellCheckerSettingsWindow() { + if (!spellCheckerWindow_) { + spellCheckerWindow_ = new QtSpellCheckerWindow(settings_); + connect(spellCheckerWindow_, SIGNAL(settingsChanged()), this, SLOT(handleModifiedSettings())); + spellCheckerWindow_->show(); + } + else { + spellCheckerWindow_->show(); + spellCheckerWindow_->raise(); + spellCheckerWindow_->activateWindow(); + } +} + +void QtTextEdit::handleModifiedSettings() { + delete checker_; + setUpSpellChecker(); + underlineMisspells(); +} + } |