From b50c56cbeafa1ea15d71829ef2f633cb212f8fcc Mon Sep 17 00:00:00 2001 From: Vlad Voicu Date: Tue, 29 Nov 2011 04:41:07 +0200 Subject: Added Factory for creeating SpellCheckers diff --git a/SwifTools/HunspellChecker.cpp b/SwifTools/HunspellChecker.cpp new file mode 100644 index 0000000..2a36930 --- /dev/null +++ b/SwifTools/HunspellChecker.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include + +#include +#include +#include + + +namespace Swift { + +HunspellChecker::HunspellChecker(const char* affix_path, const char* dictionary_path) { + speller_ = new Hunspell(affix_path, dictionary_path); +} + +HunspellChecker::~HunspellChecker() { + delete speller_; +} + +bool HunspellChecker::isCorrect(const std::string& word) { + return speller_->spell(word.c_str()); +} + +void HunspellChecker::getSuggestions(const std::string& word, std::vector& list) { + char **suggestList; + int words_returned; + if (!word.empty()) { + words_returned = speller_->suggest(&suggestList, word.c_str()); + } + for (int i = 0; i < words_returned; ++i) { + list.push_back(suggestList[i]); + free(suggestList[i]); + } +} + +} diff --git a/SwifTools/HunspellChecker.h b/SwifTools/HunspellChecker.h new file mode 100644 index 0000000..7117ccd --- /dev/null +++ b/SwifTools/HunspellChecker.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include +#include +#include + +#pragma once + +class Hunspell; + +namespace Swift { + class HunspellChecker : public SpellChecker { + public: + HunspellChecker(const char* affix_path, const char* dict_path); + virtual ~HunspellChecker(); + virtual bool isCorrect(const std::string& word); + virtual void getSuggestions(const std::string& word, std::vector& list); + private: + Hunspell* speller_; + }; +} diff --git a/SwifTools/SConscript b/SwifTools/SConscript index ac2dc6b..9a50b38 100644 --- a/SwifTools/SConscript +++ b/SwifTools/SConscript @@ -28,7 +28,8 @@ if env["SCONS_STAGE"] == "build" : "Linkify.cpp", "TabComplete.cpp", "LastLineTracker.cpp", - "SpellChecker.cpp", + "SpellCheckerFactory.cpp", + "HunspellChecker.cpp", ] if swiftools_env.get("HAVE_SPARKLE", 0) : diff --git a/SwifTools/SpellChecker.h b/SwifTools/SpellChecker.h index 1c3148b..64a7c95 100644 --- a/SwifTools/SpellChecker.h +++ b/SwifTools/SpellChecker.h @@ -4,21 +4,16 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#include #include +#include #pragma once -class Hunspell; - namespace Swift { class SpellChecker { public: - SpellChecker(); - ~SpellChecker(); - bool isCorrect(const std::string& word); - void getSuggestions(const std::string& word, std::vector& list); - private: - Hunspell* speller_; + virtual ~SpellChecker() { }; + virtual bool isCorrect(const std::string& word) = 0; + virtual void getSuggestions(const std::string& word, std::vector& list) = 0; }; } diff --git a/SwifTools/SpellCheckerFactory.cpp b/SwifTools/SpellCheckerFactory.cpp new file mode 100644 index 0000000..46c8f65 --- /dev/null +++ b/SwifTools/SpellCheckerFactory.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include +#include +#include + +#ifdef HAVE_HUNSPELL +#include +#else +#define HAVE_NO_SPELL_CHECK +#endif + +namespace Swift { + +SpellCheckerFactory::SpellCheckerFactory() { +} + +SpellChecker* SpellCheckerFactory::createSpellChecker() { +#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"; + return new HunspellChecker(affixPath, dictPath); +#endif +} + +} diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h new file mode 100644 index 0000000..72ed911 --- /dev/null +++ b/SwifTools/SpellCheckerFactory.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2011 Voicu Vlad + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#define HAVE_HUNSPELL + +namespace Swift { + class SpellChecker; + class SpellCheckerFactory { + public: + SpellCheckerFactory(); + SpellChecker* createSpellChecker(); + }; +} diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index e1fdd26..0d0f31c 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -4,6 +4,7 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ +#include #include #include @@ -16,10 +17,12 @@ namespace Swift { -QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent){ +QtTextEdit::QtTextEdit(QWidget* parent) : QTextEdit(parent) { connect(this, SIGNAL(textChanged()), this, SLOT(handleTextChanged())); handleTextChanged(); - checker_ = new SpellChecker(); + SpellCheckerFactory *checkerFactory = new SpellCheckerFactory(); + checker_ = checkerFactory->createSpellChecker(); + delete checkerFactory; }; QtTextEdit::~QtTextEdit() { -- cgit v0.10.2-6-g49f6