From 7635196b727fb0757122643af992a974b008b4ff Mon Sep 17 00:00:00 2001 From: Szymon Tomasz Stefanek Date: Sun, 13 Sep 2015 01:15:07 +0200 Subject: Improve spellchecker support. Better highlight spelling errors (fixes #1562) Add $spellchecker.suggestions(). Fix the KviInputEditor context popup clipboard label with Qt5. Improve KviInputEditor context popup readability. ... --- src/modules/spellchecker/libkvispellchecker.cpp | 68 +++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) (limited to 'src/modules') diff --git a/src/modules/spellchecker/libkvispellchecker.cpp b/src/modules/spellchecker/libkvispellchecker.cpp index caa64c817..669d4f283 100644 --- a/src/modules/spellchecker/libkvispellchecker.cpp +++ b/src/modules/spellchecker/libkvispellchecker.cpp @@ -45,12 +45,20 @@ static KviPointerList* g_pEnchantDicts = NULL; This function returns a hash. For every supported dictionary, key is the language code, value is name of provider for that language (e.g. Aspell). */ -static void spellchecker_enumerate_dicts(const char* szLang, const char* /*szName*/, - const char* szDesc, const char* /*szFile*/, void* pData) { +static void spellchecker_enumerate_dicts( + const char* szLang, + const char* /*szName*/, + const char* szDesc, + const char* /*szFile*/, + void* pData + ) +{ KviKvsHash* pHash = reinterpret_cast(pData); pHash->set(szLang, new KviKvsVariant(szDesc)); } -static bool spellchecker_kvs_available_dictionaries(KviKvsModuleFunctionCall* c) { + +static bool spellchecker_kvs_available_dictionaries(KviKvsModuleFunctionCall* c) +{ KVSM_PARAMETERS_BEGIN(c) KVSM_PARAMETERS_END(c) KviKvsHash* pHash = new KviKvsHash; @@ -88,6 +96,59 @@ static bool spellchecker_kvs_check(KviKvsModuleFunctionCall* c) return true; } + +/* + @doc: spellchecker.suggestions + @type: + function + @title: + $spellchecker.suggestions + @short: + Get spelling suggestions for a single word + @syntax: + $spellchecker.suggestions() + @description: + This function returns the suggestions for the specified word. + If the word seems to be spelled correctly or there are no dictionaries + selected then the functon returns an empty array. +*/ +static bool spellchecker_kvs_suggestions(KviKvsModuleFunctionCall* c) +{ + QString szWord; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("word", KVS_PT_STRING, 0, szWord) + KVSM_PARAMETERS_END(c) + + QHash hAllSuggestions; + + if(!g_pEnchantDicts->isEmpty()) + { + QByteArray utf8 = szWord.toUtf8(); + + KviPointerListIterator it(*g_pEnchantDicts); + for(bool b = it.moveFirst(); b; b = it.moveNext()) + { + size_t iCount = 0; + char ** suggestions = enchant_dict_suggest(*it,utf8.data(),utf8.size(),&iCount); + if(suggestions) + { + for(int i=0;i lSuggestions = hAllSuggestions.keys(); + Q_FOREACH(QString szSuggestion,lSuggestions) + pArray->append(new KviKvsVariant(szSuggestion)); + + c->returnValue()->setArray(pArray); + return true; +} + static void spellchecker_reload_dicts() { while (!g_pEnchantDicts->isEmpty()) @@ -135,6 +196,7 @@ static bool spellchecker_module_init(KviModule* m) KVSM_REGISTER_SIMPLE_COMMAND(m, "reloadDictionaries", spellchecker_kvs_reload_dictionaries); KVSM_REGISTER_FUNCTION(m, "availableDictionaries", spellchecker_kvs_available_dictionaries); KVSM_REGISTER_FUNCTION(m, "check", spellchecker_kvs_check); + KVSM_REGISTER_FUNCTION(m, "suggestions", spellchecker_kvs_suggestions); return true; } -- cgit v1.3.1-10-gc9f91