From 5f745ebd56014a68695ac4b6fe7fb5dc96e34384 Mon Sep 17 00:00:00 2001 From: Vlad Voicu Date: Mon, 19 Mar 2012 20:36:07 +0200 Subject: Addressed Kev's code review diff --git a/BuildTools/SCons/SConstruct b/BuildTools/SCons/SConstruct index 79e2f6d..9964211 100644 --- a/BuildTools/SCons/SConstruct +++ b/BuildTools/SCons/SConstruct @@ -455,9 +455,9 @@ hunspell_env.MergeFlags(hunspell_flags) env["HAVE_HUNSPELL"] = 0; hunspell_conf = Configure(hunspell_env) -if hunspell_conf.CheckCXXHeader("hunspell/hunspell.hxx") and hunspell_conf.CheckLib("hunspell-1.3") : +if hunspell_conf.CheckCXXHeader("hunspell/hunspell.hxx") and hunspell_conf.CheckLib("hunspell") : env["HAVE_HUNSPELL"] = 1 - hunspell_flags["LIBS"] = ["hunspell-1.3"] + hunspell_flags["LIBS"] = ["hunspell"] env["HUNSPELL_FLAGS"] = hunspell_flags hunspell_conf.Finish() diff --git a/SwifTools/HunspellChecker.cpp b/SwifTools/HunspellChecker.cpp index fa5917a..f37b3e5 100644 --- a/SwifTools/HunspellChecker.cpp +++ b/SwifTools/HunspellChecker.cpp @@ -35,6 +35,7 @@ void HunspellChecker::getSuggestions(const std::string& word, std::vector + #include #include #include @@ -17,9 +19,14 @@ namespace Swift { SpellCheckerFactory::SpellCheckerFactory() { } -SpellChecker* SpellCheckerFactory::createSpellChecker(const char* affixPath, const char* dictPath) { +SpellChecker* SpellCheckerFactory::createSpellChecker(const std::string& dictFile) { #ifdef HAVE_HUNSPELL - return new HunspellChecker(affixPath, dictPath); + std::string affixFile(dictFile); + boost::replace_all(affixFile, ".dic", ".aff"); + if ((boost::filesystem::exists(dictFile)) && (boost::filesystem::exists(affixFile))) { + return new HunspellChecker(affixFile.c_str(), dictFile.c_str()); + } + // If dictionaries don't exist disable the checker #endif return NULL; } diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h index 5519db5..086ea66 100644 --- a/SwifTools/SpellCheckerFactory.h +++ b/SwifTools/SpellCheckerFactory.h @@ -15,6 +15,6 @@ namespace Swift { class SpellCheckerFactory { public: SpellCheckerFactory(); - SpellChecker* createSpellChecker(const char* affixPath, const char* dictPath); + SpellChecker* createSpellChecker(const std::string& dictFile); }; } diff --git a/SwifTools/SpellParser.h b/SwifTools/SpellParser.h index b18f4c2..3fe0613 100644 --- a/SwifTools/SpellParser.h +++ b/SwifTools/SpellParser.h @@ -21,6 +21,7 @@ namespace Swift { ID_CHAR = 4, }; typedef std::list > PositionPairList; + typedef boost::tuple PositionPair; class SpellParser{ public: diff --git a/Swift/QtUI/QtSpellCheckerWindow.cpp b/Swift/QtUI/QtSpellCheckerWindow.cpp index ff6ef39..e2c5b0d 100644 --- a/Swift/QtUI/QtSpellCheckerWindow.cpp +++ b/Swift/QtUI/QtSpellCheckerWindow.cpp @@ -25,13 +25,11 @@ QtSpellCheckerWindow::QtSpellCheckerWindow(SettingsProvider* settings, QWidget* connect(ui_.cancel, SIGNAL(clicked()), this, SLOT(handleCancel())); connect(ui_.apply, SIGNAL(clicked()), this, SLOT(handleApply())); connect(ui_.pathButton, SIGNAL(clicked()), this, SLOT(handlePathButton())); - connect(ui_.personalPathButton, SIGNAL(clicked()), this, SLOT(handlePersonalPathButton())); setFromSettings(); } void QtSpellCheckerWindow::setFromSettings() { ui_.spellChecker->setChecked(settings_->getSetting(SettingConstants::SPELL_CHECKER)); - ui_.personalPathContent->setText(P2QSTRING(settings_->getSetting(SettingConstants::PERSONAL_DICT_PATH))); ui_.pathContent->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_PATH))); ui_.currentLanguageValue->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_FILE))); std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH); @@ -48,12 +46,9 @@ void QtSpellCheckerWindow::handleChecker(bool state) { void QtSpellCheckerWindow::setEnabled(bool state) { ui_.pathContent->setEnabled(state); - ui_.personalPathContent->setEnabled(state); ui_.languageView->setEnabled(state); ui_.pathButton->setEnabled(state); - ui_.personalPathButton->setEnabled(state); ui_.pathLabel->setEnabled(state); - ui_.personalDictionaryLabel->setEnabled(state); ui_.currentLanguage->setEnabled(state); ui_.currentLanguageValue->setEnabled(state); ui_.language->setEnabled(state); @@ -65,7 +60,6 @@ void QtSpellCheckerWindow::handleApply() { if (!selectedLanguage.empty()) { settings_->storeSetting(SettingConstants::DICT_FILE, Q2PSTRING((selectedLanguage.first())->text())); } - emit settingsChanged(); this->done(0); } @@ -76,7 +70,7 @@ void QtSpellCheckerWindow::handleCancel() { void QtSpellCheckerWindow::handlePathButton() { std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH); QString dirpath = QFileDialog::getExistingDirectory(this, tr("Dictionary Path"), P2QSTRING(currentPath)); - if (dirpath.compare(P2QSTRING(currentPath))) { + if (dirpath != P2QSTRING(currentPath)) { ui_.languageView->clear(); settings_->storeSetting(SettingConstants::DICT_FILE, ""); ui_.currentLanguageValue->setText(" "); diff --git a/Swift/QtUI/QtSpellCheckerWindow.h b/Swift/QtUI/QtSpellCheckerWindow.h index 40ed882..ad94907 100644 --- a/Swift/QtUI/QtSpellCheckerWindow.h +++ b/Swift/QtUI/QtSpellCheckerWindow.h @@ -22,8 +22,6 @@ namespace Swift { void handlePathButton(); void handlePersonalPathButton(); void handleApply(); - signals: - void settingsChanged(); private: void setEnabled(bool state); diff --git a/Swift/QtUI/QtSpellCheckerWindow.ui b/Swift/QtUI/QtSpellCheckerWindow.ui index 63f3ed4..b98bb6d 100644 --- a/Swift/QtUI/QtSpellCheckerWindow.ui +++ b/Swift/QtUI/QtSpellCheckerWindow.ui @@ -6,110 +6,80 @@ 0 0 - 502 - 303 + 353 + 207 Dialog - + + + + + + Cancel + + + + + + + Apply + + + + + + Spell Checker Enabled - + Dictionary Path: - + - + Change - - - - Personal Dictionary: - - - - - - - - - - Change - - - - + Current Language: - + - + + + + Language: - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Cancel - - - - - - - Apply - - - - - 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 #include -#include +#include + +#include #include #include @@ -14,6 +16,7 @@ #include #include #include +#include #include #include @@ -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 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 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 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 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::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(); + } } } diff --git a/Swift/QtUI/QtTextEdit.h b/Swift/QtUI/QtTextEdit.h index b40c935..a8df4d3 100644 --- a/Swift/QtUI/QtTextEdit.h +++ b/Swift/QtUI/QtTextEdit.h @@ -28,7 +28,7 @@ namespace Swift { void returnPressed(); void unhandledKeyPressEvent(QKeyEvent* event); public slots: - void handleModifiedSettings(); + void handleSettingChanged(const std::string& settings); protected: virtual void keyPressEvent(QKeyEvent* event); virtual void contextMenuEvent(QContextMenuEvent* event); @@ -45,6 +45,6 @@ namespace Swift { void setUpSpellChecker(); void underlineMisspells(); void spellCheckerSettingsWindow(); - boost::tuple getWordFromCursor(int cursorPosition); + PositionPair getWordFromCursor(int cursorPosition); }; } -- cgit v0.10.2-6-g49f6