Add Remove Word button to the spellchecker dialog. Updates #1184.

This commit is contained in:
Stjepan Henc 2012-12-18 17:54:53 +01:00 committed by Thomas Goyne
parent 36012c3302
commit fe630e052b
5 changed files with 72 additions and 16 deletions

View File

@ -28,6 +28,10 @@ public:
/// @param word Word to add /// @param word Word to add
virtual void AddWord(std::string const& word)=0; virtual void AddWord(std::string const& word)=0;
/// Remove word from the dictionary
/// @param word Word to remove
virtual void RemoveWord(std::string const& word)=0;
/// Can the word be added to the current dictionary? /// Can the word be added to the current dictionary?
/// @param word Word to check /// @param word Word to check
/// @return Whether or not word can be added /// @return Whether or not word can be added

View File

@ -143,6 +143,9 @@ DialogSpellChecker::DialogSpellChecker(agi::Context *context)
actions_sizer->Add(add_button = new wxButton(this, -1, _("Add to &dictionary")), button_flags); actions_sizer->Add(add_button = new wxButton(this, -1, _("Add to &dictionary")), button_flags);
add_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogSpellChecker::OnAdd, this); add_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogSpellChecker::OnAdd, this);
actions_sizer->Add(remove_button = new wxButton(this, -1, _("Remove fro&m dictionary")), button_flags);
remove_button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogSpellChecker::OnRemove, this);
actions_sizer->Add(new HelpButton(this, "Spell Checker"), button_flags); actions_sizer->Add(new HelpButton(this, "Spell Checker"), button_flags);
actions_sizer->Add(new wxButton(this, wxID_CANCEL), button_flags.Border(0)); actions_sizer->Add(new wxButton(this, wxID_CANCEL), button_flags.Border(0));
@ -180,6 +183,13 @@ void DialogSpellChecker::OnAdd(wxCommandEvent&) {
FindNext(); FindNext();
} }
void DialogSpellChecker::OnRemove(wxCommandEvent&) {
// TODO pop-up dialog
spellchecker->RemoveWord(from_wx(replace_word->GetValue()));
FindNext();
}
void DialogSpellChecker::OnChangeLanguage(wxCommandEvent&) { void DialogSpellChecker::OnChangeLanguage(wxCommandEvent&) {
wxString code = dictionary_lang_codes[language->GetSelection()]; wxString code = dictionary_lang_codes[language->GetSelection()];
OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(code)); OPT_SET("Tool/Spell Checker/Language")->SetString(STD_STR(code));

View File

@ -58,6 +58,7 @@ class DialogSpellChecker : public wxDialog {
wxComboBox *language; ///< The list of available languages wxComboBox *language; ///< The list of available languages
wxButton *add_button; ///< Add word to currently active dictionary wxButton *add_button; ///< Add word to currently active dictionary
wxButton *remove_button; ///< Remove word from currently active dictionary
wxCheckBox *skip_comments; ///< Skip over commented lines wxCheckBox *skip_comments; ///< Skip over commented lines
AssDialogue *start_line; ///< The first line checked AssDialogue *start_line; ///< The first line checked
@ -87,6 +88,7 @@ class DialogSpellChecker : public wxDialog {
void OnReplaceAll(wxCommandEvent&); void OnReplaceAll(wxCommandEvent&);
void OnIgnoreAll(wxCommandEvent&); void OnIgnoreAll(wxCommandEvent&);
void OnAdd(wxCommandEvent&); void OnAdd(wxCommandEvent&);
void OnRemove(wxCommandEvent&);
public: public:
DialogSpellChecker(agi::Context *context); DialogSpellChecker(agi::Context *context);

View File

@ -71,6 +71,45 @@ void HunspellSpellChecker::AddWord(std::string const& word) {
std::set<std::string> words; std::set<std::string> words;
try {
ReadUserDictionary(words);
}
catch (agi::FileNotFoundError&) {
LOG_I("dictionary/hunspell/add") << "User dictionary not found; creating it";
}
// Add the word
words.insert(word);
WriteUserDictionary(words);
}
void HunspellSpellChecker::RemoveWord(std::string const& word) {
if (!hunspell) return;
// Remove it from the in-memory dictionary
hunspell->remove(conv->Convert(word).c_str());
std::set<std::string> words;
try {
ReadUserDictionary(words);
}
catch (agi::FileNotFoundError&) {
LOG_I("dictionary/hunspell/remove") << "User dictionary not found; nothing to remove";
return;
}
auto word_iter = words.find(word);
if (word_iter != words.end()) {
words.erase(word_iter);
WriteUserDictionary(words);
}
}
void HunspellSpellChecker::ReadUserDictionary(std::set<std::string> &words)
{
// Ensure that the path exists // Ensure that the path exists
wxFileName fn(userDicPath); wxFileName fn(userDicPath);
if (!fn.DirExists()) { if (!fn.DirExists()) {
@ -78,7 +117,6 @@ void HunspellSpellChecker::AddWord(std::string const& word) {
} }
// Read the old contents of the user's dictionary // Read the old contents of the user's dictionary
else { else {
try {
agi::scoped_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath))); agi::scoped_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath)));
remove_copy_if( remove_copy_if(
++agi::line_iterator<std::string>(*stream), ++agi::line_iterator<std::string>(*stream),
@ -86,14 +124,10 @@ void HunspellSpellChecker::AddWord(std::string const& word) {
inserter(words, words.end()), inserter(words, words.end()),
[](std::string const& str) { return str.empty(); }); [](std::string const& str) { return str.empty(); });
} }
catch (agi::FileNotFoundError&) {
LOG_I("dictionary/hunspell/add") << "User dictionary not found; creating it";
}
} }
// Add the word void HunspellSpellChecker::WriteUserDictionary(std::set<std::string> const& words)
words.insert(word); {
// Write the new dictionary // Write the new dictionary
{ {
agi::io::Save writer(STD_STR(userDicPath)); agi::io::Save writer(STD_STR(userDicPath));
@ -101,8 +135,8 @@ void HunspellSpellChecker::AddWord(std::string const& word) {
copy(words.begin(), words.end(), std::ostream_iterator<std::string>(writer.Get(), "\n")); copy(words.begin(), words.end(), std::ostream_iterator<std::string>(writer.Get(), "\n"));
} }
// Announce a language change so that any other spellcheckers pick up the // Announce a language change so that any other spellcheckers reload the
// new word // current dictionary to get the addition/removal
lang_listener.Block(); lang_listener.Block();
OPT_SET("Tool/Spell Checker/Language")->SetString(OPT_GET("Tool/Spell Checker/Language")->GetString()); OPT_SET("Tool/Spell Checker/Language")->SetString(OPT_GET("Tool/Spell Checker/Language")->GetString());
lang_listener.Unblock(); lang_listener.Unblock();

View File

@ -56,11 +56,17 @@ class HunspellSpellChecker : public agi::SpellChecker {
/// Dictionary path change handler /// Dictionary path change handler
void OnPathChanged(); void OnPathChanged();
/// Load words from custom dictionary
void ReadUserDictionary(std::set<std::string> &words);
/// Save words to custom dictionary
void WriteUserDictionary(std::set<std::string> const& words);
public: public:
HunspellSpellChecker(); HunspellSpellChecker();
~HunspellSpellChecker(); ~HunspellSpellChecker();
void AddWord(std::string const& word); void AddWord(std::string const& word);
void RemoveWord(std::string const& word);
bool CanAddWord(std::string const& word); bool CanAddWord(std::string const& word);
bool CheckWord(std::string const& word); bool CheckWord(std::string const& word);
std::vector<std::string> GetSuggestions(std::string const& word); std::vector<std::string> GetSuggestions(std::string const& word);