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());
77 m_spellingCheckbox(false),
78 m_spellingEnabled(true)
80 static TranslationsInit tsInit;
84 setLanguageInternal(
"");
94 bool success = setLanguageInternal(lang);
101 bool Checker::setLanguageInternal(
const QString &lang)
108 if(m_lang.isEmpty()){
109 m_lang = QLocale::system().name();
110 if(m_lang.toLower() ==
"c" || m_lang.isEmpty()){
111 qWarning() <<
"Cannot use system locale " << m_lang;
112 m_lang = QString::null;
119 m_speller = get_enchant_broker()->request_dict(m_lang.toStdString());
120 }
catch(enchant::Exception& e) {
121 qWarning() <<
"Failed to load dictionary: " << e.what();
122 m_lang = QString::null;
132 m_speller->add(word.toUtf8().data());
138 if(!m_speller || !m_spellingEnabled){
142 if(word.length() < 2){
146 return m_speller->check(word.toUtf8().data());
147 }
catch(
const enchant::Exception&){
154 m_speller->add_to_session(word.toUtf8().data());
161 std::vector<std::string> suggestions;
162 m_speller->suggest(word.toUtf8().data(), suggestions);
163 for(std::size_t i = 0, n = suggestions.size(); i < n; ++i){
164 list.append(QString::fromUtf8(suggestions[i].c_str()));
172 enchant::Broker* broker = get_enchant_broker();
173 QList<QString> languages;
174 broker->list_dicts(dict_describe_cb, &languages);
181 QString language, country, extra;
183 if(!country.isEmpty()){
184 QString decoded = QString(
"%1 (%2)").arg(language, country);
185 if(!extra.isEmpty()) {
186 decoded += QString(
" [%1]").arg(extra);
194 void Checker::showContextMenu(QMenu* menu,
const QPoint& pos,
int wordPos)
196 QAction* insertPos = menu->actions().first();
197 if(m_speller && m_spellingEnabled){
198 QString word =
getWord(wordPos);
202 if(!suggestions.isEmpty()){
203 for(
int i = 0, n = qMin(10, suggestions.length()); i < n; ++i){
204 QAction* action =
new QAction(suggestions[i], menu);
205 action->setProperty(
"wordPos", wordPos);
206 action->setProperty(
"suggestion", suggestions[i]);
207 connect(action, SIGNAL(triggered()),
this, SLOT(slotReplaceWord()));
208 menu->insertAction(insertPos, action);
210 if(suggestions.length() > 10) {
211 QMenu* moreMenu =
new QMenu();
212 for(
int i = 10, n = suggestions.length(); i < n; ++i){
213 QAction* action =
new QAction(suggestions[i], moreMenu);
214 action->setProperty(
"wordPos", wordPos);
215 action->setProperty(
"suggestion", suggestions[i]);
216 connect(action, SIGNAL(triggered()),
this, SLOT(slotReplaceWord()));
217 moreMenu->addAction(action);
219 QAction* action =
new QAction(tr(
"More..."), menu);
220 menu->insertAction(insertPos, action);
221 action->setMenu(moreMenu);
223 menu->insertSeparator(insertPos);
226 QAction* addAction =
new QAction(tr(
"Add \"%1\" to dictionary").arg(word), menu);
227 addAction->setData(wordPos);
228 connect(addAction, SIGNAL(triggered()),
this, SLOT(slotAddWord()));
229 menu->insertAction(insertPos, addAction);
231 QAction* ignoreAction =
new QAction(tr(
"Ignore \"%1\"").arg(word), menu);
232 ignoreAction->setData(wordPos);
233 connect(ignoreAction, SIGNAL(triggered()),
this, SLOT(slotIgnoreWord()));
234 menu->insertAction(insertPos, ignoreAction);
235 menu->insertSeparator(insertPos);
238 if(m_spellingCheckbox){
239 QAction* action =
new QAction(tr(
"Check spelling"), menu);
240 action->setCheckable(
true);
241 action->setChecked(m_spellingEnabled);
243 menu->insertAction(insertPos, action);
245 if(m_speller && m_spellingEnabled){
246 QMenu* languagesMenu =
new QMenu();
247 QActionGroup* actionGroup =
new QActionGroup(languagesMenu);
250 QAction* action =
new QAction(text, languagesMenu);
251 action->setData(lang);
252 action->setCheckable(
true);
254 connect(action, SIGNAL(triggered(
bool)),
this, SLOT(slotSetLanguage(
bool)));
255 languagesMenu->addAction(action);
256 actionGroup->addAction(action);
258 QAction* langsAction =
new QAction(tr(
"Languages"), menu);
259 langsAction->setMenu(languagesMenu);
260 menu->insertAction(insertPos, langsAction);
261 menu->insertSeparator(insertPos);
268 void Checker::slotAddWord()
270 int wordPos = qobject_cast<QAction*>(QObject::sender())->property(
"wordPos").toInt();
276 void Checker::slotIgnoreWord()
278 int wordPos = qobject_cast<QAction*>(QObject::sender())->property(
"wordPos").toInt();
284 void Checker::slotReplaceWord()
286 QAction* action = qobject_cast<QAction*>(QObject::sender());
287 int wordPos = action->property(
"wordPos").toInt();
289 getWord(wordPos, &start, &end);
290 insertWord(start, end, action->property(
"suggestion").toString());
293 void Checker::slotSetLanguage(
bool checked)
296 QAction* action = qobject_cast<QAction*>(QObject::sender());
297 QString lang = action->data().toString();
299 action->setChecked(
false);
virtual ~Checker()
QtSpell::Checker object destructor.
static Codetable * instance()
Get codetable instance.
bool checkLanguageInstalled(const QString &lang)
Check whether the dictionary for a language is installed.
void addWordToDictionary(const QString &word)
Add the specified word to the user dictionary.
const QString & getLanguage() const
Retreive the current spelling language.
void ignoreWord(const QString &word) const
Ignore a word for the current session.
bool getDecodeLanguageCodes() const
Return whether langauge codes are decoded in the UI.
virtual void insertWord(int start, int end, const QString &word)=0
Replaces the specified range with the specified word.
virtual void checkSpelling(int start=0, int end=-1)=0
Check the spelling.
void languageChanged(const QString &newLang)
This signal is emitted when the user selects a new language from the spellchecker UI...
Checker(QObject *parent=0)
QtSpell::Checker object constructor.
void setSpellingEnabled(bool enabled)
Set whether spell checking should be performed.
static QString decodeLanguageCode(const QString &lang)
Translates a language code to a human readable format (i.e. "en_US" -> "English (United States)")...
virtual QString getWord(int pos, int *start=0, int *end=0) const =0
Get the word at the specified cursor position.
bool setLanguage(const QString &lang)
Set the spell checking language.
bool checkWord(const QString &word) const
Check the specified word.
static QList< QString > getLanguageList()
Requests the list of languages available for spell checking.
QList< QString > getSpellingSuggestions(const QString &word) const
Retreive a list of spelling suggestions for the misspelled word.
virtual bool isAttached() const =0
Returns whether a widget is attached to the checker.
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...