From 3049bf68ede3e1ce152561f6e8a054cca8dcad56 Mon Sep 17 00:00:00 2001 From: Vlad Voicu Date: Mon, 5 Mar 2012 20:28:47 +0200 Subject: Improved settings for Spell Checker diff --git a/SwifTools/SpellCheckerFactory.cpp b/SwifTools/SpellCheckerFactory.cpp index 9ec1151..9ac33cd 100644 --- a/SwifTools/SpellCheckerFactory.cpp +++ b/SwifTools/SpellCheckerFactory.cpp @@ -10,8 +10,6 @@ #ifdef HAVE_HUNSPELL #include -#else -#define HAVE_NO_SPELL_CHECK #endif namespace Swift { @@ -19,14 +17,12 @@ namespace Swift { SpellCheckerFactory::SpellCheckerFactory() { } -SpellChecker* SpellCheckerFactory::createSpellChecker() { +SpellChecker* SpellCheckerFactory::createSpellChecker(const char* affixPath, const char* dictPath) { #ifdef HAVE_HUNSPELL - //TODO(vladv): Do something smart about finding available dictionaries, - // maybe a UI button to select language from available dictionaries? - const char* dictPath = "/usr/share/hunspell/en_US.dic"; - const char* affixPath = "/usr/share/hunspell/en_US.aff"; + std::cout << affixPath << std::endl << dictPath << std::endl; return new HunspellChecker(affixPath, dictPath); #endif + return NULL; } } diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h index 23f89b8..85aa8b6 100644 --- a/SwifTools/SpellCheckerFactory.h +++ b/SwifTools/SpellCheckerFactory.h @@ -13,6 +13,6 @@ namespace Swift { class SpellCheckerFactory { public: SpellCheckerFactory(); - SpellChecker* createSpellChecker(); + SpellChecker* createSpellChecker(const char* affixPath, const char* dictPath); }; } diff --git a/Swift/Controllers/SettingConstants.cpp b/Swift/Controllers/SettingConstants.cpp index 6f147a7..678babc 100644 --- a/Swift/Controllers/SettingConstants.cpp +++ b/Swift/Controllers/SettingConstants.cpp @@ -21,6 +21,6 @@ const SettingsProvider::Setting SettingConstants::EXPANDED_ROSTER_G const SettingsProvider::Setting SettingConstants::PLAY_SOUNDS("playSounds", true); const SettingsProvider::Setting SettingConstants::SPELL_CHECKER("spellChecker", false); const SettingsProvider::Setting SettingConstants::DICT_PATH("dictPath", "/usr/share/myspell/dicts/"); -const SettingsProvider::Setting SettingConstants::PERSONAL_DICT_PATH("personaldictPath", "/home"); +const SettingsProvider::Setting SettingConstants::PERSONAL_DICT_PATH("personaldictPath", "/home/"); const SettingsProvider::Setting SettingConstants::DICT_FILE("dictFile", "en_US.dic"); } diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp index dd33e28..79fe95f 100644 --- a/Swift/QtUI/QtChatWindow.cpp +++ b/Swift/QtUI/QtChatWindow.cpp @@ -132,7 +132,7 @@ QtChatWindow::QtChatWindow(const QString &contact, QtChatTheme* theme, UIEventSt QHBoxLayout* inputBarLayout = new QHBoxLayout(); inputBarLayout->setContentsMargins(0,0,0,0); inputBarLayout->setSpacing(2); - input_ = new QtTextEdit(this); + input_ = new QtTextEdit(settings_, this); input_->setAcceptRichText(false); inputBarLayout->addWidget(input_); correctingLabel_ = new QLabel(tr("Correcting"), this); diff --git a/Swift/QtUI/QtSpellCheckerWindow.cpp b/Swift/QtUI/QtSpellCheckerWindow.cpp index c6fcc2c..ef1985e 100644 --- a/Swift/QtUI/QtSpellCheckerWindow.cpp +++ b/Swift/QtUI/QtSpellCheckerWindow.cpp @@ -27,13 +27,19 @@ QtSpellCheckerWindow::QtSpellCheckerWindow(SettingsProvider* settings, QWidget* connect(ui_.pathButton, SIGNAL(clicked()), this, SLOT(handlePathButton())); connect(ui_.personalPathButton, SIGNAL(clicked()), this, SLOT(handlePersonalPathButton())); setFromSettings(); - setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER)); } void QtSpellCheckerWindow::setFromSettings() { ui_.spellChecker->setChecked(settings_->getSetting(SettingConstants::SPELL_CHECKER)); ui_.personalPathContent->insert(P2QSTRING(settings_->getSetting(SettingConstants::PERSONAL_DICT_PATH))); ui_.pathContent->insert(P2QSTRING(settings_->getSetting(SettingConstants::DICT_PATH))); + ui_.currentLanguageValue->setText(P2QSTRING(settings_->getSetting(SettingConstants::DICT_FILE))); + std::string currentPath = settings_->getSetting(SettingConstants::DICT_PATH); + QString filename = "*.dic"; + QDir dictDirectory = QDir(P2QSTRING(currentPath)); + QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files); + showFiles(files); + setEnabled(settings_->getSetting(SettingConstants::SPELL_CHECKER)); } void QtSpellCheckerWindow::handleChecker(bool state) { @@ -48,6 +54,8 @@ void QtSpellCheckerWindow::setEnabled(bool 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); } @@ -56,6 +64,8 @@ void QtSpellCheckerWindow::handleApply() { QList selectedLanguage = ui_.languageView->selectedItems(); if (!selectedLanguage.empty()) { settings_->storeSetting(SettingConstants::DICT_FILE, Q2PSTRING(selectedLanguage[0]->text())); + } else { + settings_->storeSetting(SettingConstants::SPELL_CHECKER, false); } this->done(0); } @@ -67,10 +77,13 @@ 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 != NULL) { - QDir dictDirectory = QDir(dirpath); + if (!dirpath.isEmpty()) { + if (!dirpath.endsWith("/")) { + dirpath.append("/"); + } settings_->storeSetting(SettingConstants::DICT_PATH, Q2PSTRING(dirpath)); - ui_.personalPathContent->insert(dirpath); + QDir dictDirectory = QDir(dirpath); + ui_.pathContent->insert(dirpath); QString filename = "*.dic"; QStringList files = dictDirectory.entryList(QStringList(filename), QDir::Files); showFiles(files); diff --git a/Swift/QtUI/QtSpellCheckerWindow.ui b/Swift/QtUI/QtSpellCheckerWindow.ui index 619ed88..63f3ed4 100644 --- a/Swift/QtUI/QtSpellCheckerWindow.ui +++ b/Swift/QtUI/QtSpellCheckerWindow.ui @@ -6,8 +6,8 @@ 0 0 - 431 - 214 + 502 + 303 @@ -31,7 +31,7 @@ - + Change @@ -41,14 +41,14 @@ - Personal Dictionary + Personal Dictionary: - + Change @@ -56,13 +56,30 @@ + + + Current Language: + + + + + + + + + + + - Language + Language: - + + + + @@ -93,9 +110,6 @@ - - - diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index 60f0939..e523dca 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -5,12 +5,14 @@ */ #include +#include #include #include #include #include +#include #include #include @@ -20,12 +22,11 @@ namespace Swift { -QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent) { +QtTextEdit::QtTextEdit(SettingsProvider* settings, QWidget* parent) : QTextEdit(parent) { connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged())); + settings_ = settings; + setUpSpellChecker(); handleTextChanged(); - SpellCheckerFactory *checkerFactory = new SpellCheckerFactory(); - checker_ = checkerFactory->createSpellChecker(); - delete checkerFactory; }; QtTextEdit::~QtTextEdit() { @@ -57,11 +58,21 @@ void QtTextEdit::keyPressEvent(QKeyEvent* event) { } else { QTextEdit::keyPressEvent(event); - underlineMisspells(); + if(settings_->getSetting(SettingConstants::SPELL_CHECKER)) { + underlineMisspells(); + } } } void QtTextEdit::underlineMisspells() { + if (checker_ == NULL) { + // Try to setUp the checker again in case that + // settings changed. + setUpSpellChecker(); + if (checker_ == NULL) { + return; + } + } misspelledPositions_.clear(); QTextCharFormat spellingErrorFormat; QTextCharFormat normalFormat; @@ -160,4 +171,26 @@ void QtTextEdit::contextMenuEvent(QContextMenuEvent* event) { delete menu; } +void QtTextEdit::setUpSpellChecker() +{ + SpellCheckerFactory *checkerFactory = new SpellCheckerFactory(); + std::string dictFile = settings_->getSetting(SettingConstants::DICT_FILE); + if (dictFile.empty()) { + // Disable the dictionary to force the user to select a dictionary + settings_->storeSetting(SettingConstants::SPELL_CHECKER, false); + } + if (settings_->getSetting(SettingConstants::SPELL_CHECKER)) { + std::string dictPath = settings_->getSetting(SettingConstants::DICT_PATH); + std::string affixFile(dictFile); + boost::replace_all(affixFile, ".dic", ".aff"); + if (checker_ != NULL) { + delete checker_; + } + checker_ = checkerFactory->createSpellChecker((dictPath + affixFile).c_str(), (dictPath + dictFile).c_str()); + delete checkerFactory; + } else { + checker_ = NULL; + } +} + } diff --git a/Swift/QtUI/QtTextEdit.h b/Swift/QtUI/QtTextEdit.h index 297312d..38d5d0a 100644 --- a/Swift/QtUI/QtTextEdit.h +++ b/Swift/QtUI/QtTextEdit.h @@ -6,15 +6,19 @@ #pragma once -#include #include +#include +#include + +#include + namespace Swift { class SpellChecker; class QtTextEdit : public QTextEdit { Q_OBJECT public: - QtTextEdit(QWidget* parent = 0); + QtTextEdit(SettingsProvider* settings, QWidget* parent = 0); virtual ~QtTextEdit(); virtual QSize sizeHint() const; signals: @@ -31,6 +35,8 @@ namespace Swift { private: SpellChecker *checker_; PositionPairList misspelledPositions_; + SettingsProvider *settings_; + void setUpSpellChecker(); void underlineMisspells(); boost::tuple getWordFromCursor(int cursorPosition); }; -- cgit v0.10.2-6-g49f6