diff options
Diffstat (limited to 'Swift/QtUI/QtTextEdit.cpp')
-rw-r--r-- | Swift/QtUI/QtTextEdit.cpp | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index 513e6fd..4807f6b 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -6,7 +6,9 @@ #include <boost/tuple/tuple.hpp> #include <boost/algorithm/string.hpp> -#include <boost/filesystem/operations.hpp> +#include <boost/bind.hpp> + +#include <Swiften/Base/foreach.h> #include <SwifTools/SpellCheckerFactory.h> #include <SwifTools/SpellChecker.h> @@ -14,6 +16,7 @@ #include <Swift/QtUI/QtTextEdit.h> #include <Swift/QtUI/QtSwiftUtil.h> #include <Swift/QtUI/QtSpellCheckerWindow.h> +#include <Swift/Controllers/SettingConstants.h> #include <QApplication> #include <QFontMetrics> @@ -85,9 +88,9 @@ void QtTextEdit::underlineMisspells() { spellingErrorFormat.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline); std::string fragment = Q2PSTRING(cursor.selectedText()); checker_->checkFragment(fragment, misspelledPositions_); - for (PositionPairList::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) { - cursor.setPosition(boost::get<0>(*it), QTextCursor::MoveAnchor); - cursor.setPosition(boost::get<1>(*it), QTextCursor::KeepAnchor); + foreach (PositionPair position, misspelledPositions_) { + cursor.setPosition(boost::get<0>(position), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(position), QTextCursor::KeepAnchor); cursor.setCharFormat(spellingErrorFormat); cursor.clearSelection(); cursor.setCharFormat(normalFormat); @@ -104,14 +107,14 @@ void QtTextEdit::handleTextChanged() { void QtTextEdit::replaceMisspelledWord(const QString& word, int cursorPosition) { QTextCursor cursor = textCursor(); - boost::tuple<int, int> wordPosition = getWordFromCursor(cursorPosition); + PositionPair wordPosition = getWordFromCursor(cursorPosition); 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) { +PositionPair QtTextEdit::getWordFromCursor(int cursorPosition) { for (PositionPairList::iterator it = misspelledPositions_.begin(); it != misspelledPositions_.end(); ++it) { if (cursorPosition >= boost::get<0>(*it) && cursorPosition <= boost::get<1>(*it)) { return *it; @@ -134,11 +137,11 @@ QSize QtTextEdit::sizeHint() const { } void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { - QMenu *menu = createStandardContextMenu(); + QMenu* menu = createStandardContextMenu(); QTextCursor cursor = cursorForPosition(event->pos()); #ifdef HAVE_SPELLCHECKER - QAction *insertPoint = menu->actions().first(); - QAction *settingsAction = new QAction(QApplication::translate("QtTextEdit", "Spell Checker Options", 0, QApplication::UnicodeUTF8), menu); + 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); @@ -160,9 +163,9 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event) { replaceWordActions_.clear(); - QAction *insertPoint = menu->actions().first(); + QAction* insertPoint = menu->actions().first(); QTextCursor cursor = cursorForPosition(event->pos()); - boost::tuple<int, int> wordPosition = getWordFromCursor(cursor.position()); + PositionPair wordPosition = getWordFromCursor(cursor.position()); if (boost::get<0>(wordPosition) < 0) { // The click was executed outside a spellable word so no // suggestions are necessary @@ -173,13 +176,13 @@ void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event) std::vector<std::string> wordList; checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList); if (wordList.size() == 0) { - QAction *noSuggestions = new QAction(QApplication::translate("QtTextEdit", "No Suggestions", 0, QApplication::UnicodeUTF8), menu); + QAction* noSuggestions = new QAction(QApplication::translate("QtTextEdit", "No Suggestions", 0, QApplication::UnicodeUTF8), menu); noSuggestions->setDisabled(true); menu->insertAction(insertPoint, noSuggestions); } else { for (std::vector<std::string>::iterator it = wordList.begin(); it != wordList.end(); ++it) { - QAction *wordAction = new QAction(it->c_str(), menu); + QAction* wordAction = new QAction(it->c_str(), menu); menu->insertAction(insertPoint, wordAction); replaceWordActions_.push_back(wordAction); } @@ -190,21 +193,12 @@ void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event) void QtTextEdit::setUpSpellChecker() { - SpellCheckerFactory *checkerFactory = new SpellCheckerFactory(); + SpellCheckerFactory* checkerFactory = new SpellCheckerFactory(); delete checker_; if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) { std::string dictPath = settings_->getSetting(SettingConstants::DICT_PATH); std::string dictFile = settings_->getSetting(SettingConstants::DICT_FILE); - std::string affixFile(dictFile); - boost::replace_all(affixFile, ".dic", ".aff"); - if ((boost::filesystem::exists(dictPath + dictFile)) && (boost::filesystem::exists(dictPath + affixFile))) { - std::cout << dictPath + dictFile << std::endl; - checker_ = checkerFactory->createSpellChecker((dictPath + affixFile).c_str(), (dictPath + dictFile).c_str()); - } - else { - // If dictionaries don't exist disable the checker - checker_ = NULL; - } + checker_ = checkerFactory->createSpellChecker(dictPath + dictFile); delete checkerFactory; } else { @@ -215,7 +209,7 @@ void QtTextEdit::setUpSpellChecker() void QtTextEdit::spellCheckerSettingsWindow() { if (!spellCheckerWindow_) { spellCheckerWindow_ = new QtSpellCheckerWindow(settings_); - connect(spellCheckerWindow_, SIGNAL(settingsChanged()), this, SLOT(handleModifiedSettings())); + settings_->onSettingChanged.connect(boost::bind(&QtTextEdit::handleSettingChanged, this, _1)); spellCheckerWindow_->show(); } else { @@ -225,9 +219,13 @@ void QtTextEdit::spellCheckerSettingsWindow() { } } -void QtTextEdit::handleModifiedSettings() { - setUpSpellChecker(); - underlineMisspells(); +void QtTextEdit::handleSettingChanged(const std::string& settings) { + if (settings == SettingConstants::SPELL_CHECKER.getKey() + || settings == SettingConstants::DICT_PATH.getKey() + || settings == SettingConstants::DICT_FILE.getKey()) { + setUpSpellChecker(); + underlineMisspells(); + } } } |