19 #include "QtSpell.hpp"
20 #include "Codetable.hpp"
22 #include <enchant++.h>
23 #include <QApplication>
24 #include <QLibraryInfo>
27 #include <QTranslator>
30 static void dict_describe_cb(
const char*
const lang_tag,
36 QList<QString>* languages =
static_cast<QList<QString>*
>(user_data);
37 languages->append(lang_tag);
40 static enchant::Broker* get_enchant_broker() {
41 #ifdef QTSPELL_ENCHANT2
42 static enchant::Broker broker;
45 return enchant::Broker::instance();
50 class TranslationsInit {
53 QString translationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
55 QDir packageDir = QDir(QString(
"%1/../").arg(QApplication::applicationDirPath()));
56 translationsPath = packageDir.absolutePath() + translationsPath.mid(QLibraryInfo::location(QLibraryInfo::PrefixPath).length());
58 spellTranslator.load(
"QtSpell_" + QLocale::system().name(), translationsPath);
59 QApplication::instance()->installTranslator(&spellTranslator);
62 QTranslator spellTranslator;
70 return get_enchant_broker()->dict_exists(lang.toStdString());
76 static TranslationsInit tsInit;
80 setLanguageInternal(
"");
90 bool success = setLanguageInternal(lang);
97 bool Checker::setLanguageInternal(
const QString &lang)
104 if(m_lang.isEmpty()){
105 m_lang = QLocale::system().name();
106 if(m_lang.toLower() ==
"c" || m_lang.isEmpty()){
107 qWarning() <<
"Cannot use system locale " << m_lang;
115 m_speller = get_enchant_broker()->request_dict(m_lang.toStdString());
116 }
catch(enchant::Exception& e) {
117 qWarning() <<
"Failed to load dictionary: " << e.what();
128 m_speller->add(word.toUtf8().data());
134 if(!m_speller || !m_spellingEnabled){
138 if(word.length() < 2){
142 return m_speller->check(word.toUtf8().data());
143 }
catch(
const enchant::Exception&){
150 m_speller->add_to_session(word.toUtf8().data());
157 std::vector<std::string> suggestions;
158 m_speller->suggest(word.toUtf8().data(), suggestions);
159 for(std::size_t i = 0, n = suggestions.size(); i < n; ++i){
160 list.append(QString::fromUtf8(suggestions[i].c_str()));
168 enchant::Broker* broker = get_enchant_broker();
169 QList<QString> languages;
170 broker->list_dicts(dict_describe_cb, &languages);
171 std::sort(languages.begin(), languages.end());
177 QString language, country, extra;
179 if(!country.isEmpty()){
180 QString decoded = QString(
"%1 (%2)").arg(language, country);
181 if(!extra.isEmpty()) {
182 decoded += QString(
" [%1]").arg(extra);
190 void Checker::showContextMenu(QMenu* menu,
const QPoint& pos,
int wordPos)
192 QAction* insertPos = menu->actions().first();
193 if(m_speller && m_spellingEnabled){
194 QString word =
getWord(wordPos);
198 if(!suggestions.isEmpty()){
199 for(
int i = 0, n = qMin(10, suggestions.length()); i < n; ++i){
200 QAction* action =
new QAction(suggestions[i], menu);
201 action->setProperty(
"wordPos", wordPos);
202 action->setProperty(
"suggestion", suggestions[i]);
203 connect(action, &QAction::triggered,
this, &Checker::slotReplaceWord);
204 menu->insertAction(insertPos, action);
206 if(suggestions.length() > 10) {
207 QMenu* moreMenu =
new QMenu();
208 for(
int i = 10, n = suggestions.length(); i < n; ++i){
209 QAction* action =
new QAction(suggestions[i], moreMenu);
210 action->setProperty(
"wordPos", wordPos);
211 action->setProperty(
"suggestion", suggestions[i]);
212 connect(action, &QAction::triggered,
this, &Checker::slotReplaceWord);
213 moreMenu->addAction(action);
215 QAction* action =
new QAction(tr(
"More..."), menu);
216 menu->insertAction(insertPos, action);
217 action->setMenu(moreMenu);
219 menu->insertSeparator(insertPos);
222 QAction* addAction =
new QAction(tr(
"Add \"%1\" to dictionary").arg(word), menu);
223 addAction->setData(wordPos);
224 connect(addAction, &QAction::triggered,
this, &Checker::slotAddWord);
225 menu->insertAction(insertPos, addAction);
227 QAction* ignoreAction =
new QAction(tr(
"Ignore \"%1\"").arg(word), menu);
228 ignoreAction->setData(wordPos);
229 connect(ignoreAction, &QAction::triggered,
this, &Checker::slotIgnoreWord);
230 menu->insertAction(insertPos, ignoreAction);
231 menu->insertSeparator(insertPos);
234 if(m_spellingCheckbox){
235 QAction* action =
new QAction(tr(
"Check spelling"), menu);
236 action->setCheckable(
true);
237 action->setChecked(m_spellingEnabled);
239 menu->insertAction(insertPos, action);
241 if(m_speller && m_spellingEnabled){
242 QMenu* languagesMenu =
new QMenu();
243 QActionGroup* actionGroup =
new QActionGroup(languagesMenu);
246 QAction* action =
new QAction(text, languagesMenu);
247 action->setData(lang);
248 action->setCheckable(
true);
250 connect(action, &QAction::triggered,
this, &Checker::slotSetLanguage);
251 languagesMenu->addAction(action);
252 actionGroup->addAction(action);
254 QAction* langsAction =
new QAction(tr(
"Languages"), menu);
255 langsAction->setMenu(languagesMenu);
256 menu->insertAction(insertPos, langsAction);
257 menu->insertSeparator(insertPos);
264 void Checker::slotAddWord()
266 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
272 void Checker::slotIgnoreWord()
274 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
280 void Checker::slotReplaceWord()
282 QAction* action = qobject_cast<QAction*>(QObject::sender());
283 int wordPos = action->property(
"wordPos").toInt();
285 getWord(wordPos, &start, &end);
286 insertWord(start, end, action->property(
"suggestion").toString());
289 void Checker::slotSetLanguage(
bool checked)
292 QAction* action = qobject_cast<QAction*>(QObject::sender());
293 QString lang = action->data().toString();
295 action->setChecked(
false);
Checker(QObject *parent=0)
QtSpell::Checker object constructor.
bool setLanguage(const QString &lang)
Set the spell checking language.
virtual bool isAttached() const =0
Returns whether a widget is attached to the checker.
QList< QString > getSpellingSuggestions(const QString &word) const
Retreive a list of spelling suggestions for the misspelled word.
static QList< QString > getLanguageList()
Requests the list of languages available for spell checking.
virtual ~Checker()
QtSpell::Checker object destructor.
bool getDecodeLanguageCodes() const
Return whether langauge codes are decoded in the UI.
void setSpellingEnabled(bool enabled)
Set whether spell checking should be performed.
bool checkWord(const QString &word) const
Check the specified word.
void languageChanged(const QString &newLang)
This signal is emitted when the user selects a new language from the spellchecker UI.
virtual void checkSpelling(int start=0, int end=-1)=0
Check the spelling.
virtual QString getWord(int pos, int *start=0, int *end=0) const =0
Get the word at the specified cursor position.
virtual void insertWord(int start, int end, const QString &word)=0
Replaces the specified range with the specified word.
static QString decodeLanguageCode(const QString &lang)
Translates a language code to a human readable format (i.e. "en_US" -> "English (United States)").
void addWordToDictionary(const QString &word)
Add the specified word to the user dictionary.
void ignoreWord(const QString &word) const
Ignore a word for the current session.
const QString & getLanguage() const
Retreive the current spelling language.
static Codetable * instance()
Get codetable instance.
void lookup(const QString &language_code, QString &language_name, QString &country_name, QString &extra) const
Looks up the language and country name for the specified language code. If no matching entries are fo...
bool checkLanguageInstalled(const QString &lang)
Check whether the dictionary for a language is installed.