19 #include "QtSpell.hpp"
20 #include "Codetable.hpp"
22 #include <enchant++.h>
23 #include <QApplication>
24 #include <QLibraryInfo>
27 #include <QTranslator>
29 static void dict_describe_cb(
const char*
const lang_tag,
35 QList<QString>* languages =
static_cast<QList<QString>*
>(user_data);
36 languages->append(lang_tag);
40 class TranslationsInit {
43 QString translationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
45 QDir packageDir = QDir(QString(
"%1/../").arg(QApplication::applicationDirPath()));
46 translationsPath = packageDir.absolutePath() + translationsPath.mid(QLibraryInfo::location(QLibraryInfo::PrefixPath).length());
48 spellTranslator.load(
"QtSpell_" + QLocale::system().name(), translationsPath);
49 QApplication::instance()->installTranslator(&spellTranslator);
52 QTranslator spellTranslator;
60 return enchant::Broker::instance()->dict_exists(lang.toStdString());
67 m_spellingCheckbox(false),
68 m_spellingEnabled(true)
70 static TranslationsInit tsInit;
74 setLanguageInternal(
"");
84 bool success = setLanguageInternal(lang);
91 bool Checker::setLanguageInternal(
const QString &lang)
99 m_lang = QLocale::system().name();
100 if(m_lang.toLower() ==
"c" || m_lang.isEmpty()){
101 qWarning(
"Cannot use system locale %s", m_lang.toLatin1().data());
102 m_lang = QString::null;
109 m_speller = enchant::Broker::instance()->request_dict(m_lang.toStdString());
110 }
catch(enchant::Exception& e) {
111 qWarning(
"Failed to load dictionary: %s", e.what());
112 m_lang = QString::null;
122 m_speller->add(word.toUtf8().data());
128 if(!m_speller || !m_spellingEnabled){
132 if(word.length() < 2){
136 return m_speller->check(word.toUtf8().data());
137 }
catch(
const enchant::Exception&){
144 m_speller->add_to_session(word.toUtf8().data());
151 std::vector<std::string> suggestions;
152 m_speller->suggest(word.toUtf8().data(), suggestions);
153 for(std::size_t i = 0, n = suggestions.size(); i < n; ++i){
154 list.append(QString::fromStdString(suggestions[i]));
162 enchant::Broker* broker = enchant::Broker::instance();
163 QList<QString> languages;
164 broker->list_dicts(dict_describe_cb, &languages);
171 QString language, country;
173 if(!country.isEmpty()){
174 return QString(
"%1 (%2)").arg(language, country);
180 void Checker::showContextMenu(QMenu* menu,
const QPoint& pos,
int wordPos)
182 QAction* insertPos = menu->actions().first();
183 if(m_speller && m_spellingEnabled){
184 QString word =
getWord(wordPos);
188 if(!suggestions.isEmpty()){
189 for(
int i = 0, n = qMin(10, suggestions.length()); i < n; ++i){
190 QAction* action =
new QAction(suggestions[i], menu);
191 action->setData(wordPos);
192 connect(action, SIGNAL(triggered()),
this, SLOT(slotReplaceWord()));
193 menu->insertAction(insertPos, action);
195 if(suggestions.length() > 10) {
196 QMenu* moreMenu =
new QMenu();
197 for(
int i = 10, n = suggestions.length(); i < n; ++i){
198 QAction* action =
new QAction(suggestions[i], moreMenu);
199 action->setData(wordPos);
200 connect(action, SIGNAL(triggered()),
this, SLOT(slotReplaceWord()));
201 moreMenu->addAction(action);
203 QAction* action =
new QAction(tr(
"More..."), menu);
204 menu->insertAction(insertPos, action);
205 action->setMenu(moreMenu);
207 menu->insertSeparator(insertPos);
210 QAction* addAction =
new QAction(tr(
"Add \"%1\" to dictionary").arg(word), menu);
211 addAction->setData(wordPos);
212 connect(addAction, SIGNAL(triggered()),
this, SLOT(slotAddWord()));
213 menu->insertAction(insertPos, addAction);
215 QAction* ignoreAction =
new QAction(tr(
"Ignore \"%1\"").arg(word), menu);
216 ignoreAction->setData(wordPos);
217 connect(ignoreAction, SIGNAL(triggered()),
this, SLOT(slotIgnoreWord()));
218 menu->insertAction(insertPos, ignoreAction);
219 menu->insertSeparator(insertPos);
222 if(m_spellingCheckbox){
223 QAction* action =
new QAction(tr(
"Check spelling"), menu);
224 action->setCheckable(
true);
225 action->setChecked(m_spellingEnabled);
227 menu->insertAction(insertPos, action);
229 if(m_speller && m_spellingEnabled){
230 QMenu* languagesMenu =
new QMenu();
231 QActionGroup* actionGroup =
new QActionGroup(languagesMenu);
234 QAction* action =
new QAction(text, languagesMenu);
235 action->setData(lang);
236 action->setCheckable(
true);
238 connect(action, SIGNAL(triggered(
bool)),
this, SLOT(slotSetLanguage(
bool)));
239 languagesMenu->addAction(action);
240 actionGroup->addAction(action);
242 QAction* langsAction =
new QAction(tr(
"Languages"), menu);
243 langsAction->setMenu(languagesMenu);
244 menu->insertAction(insertPos, langsAction);
245 menu->insertSeparator(insertPos);
252 void Checker::slotAddWord()
254 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
260 void Checker::slotIgnoreWord()
262 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
268 void Checker::slotReplaceWord()
270 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
272 getWord(wordPos, &start, &end);
273 insertWord(start, end, qobject_cast<QAction*>(QObject::sender())->text());
276 void Checker::slotSetLanguage(
bool checked)
279 QAction* action = qobject_cast<QAction*>(QObject::sender());
280 QString lang = action->data().toString();
282 action->setChecked(
false);
virtual ~Checker()
QtSpell::Checker object destructor.
static Codetable * instance()
Get codetable instance.
void lookup(const QString &language_code, QString &language_name, QString &country_name) 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.
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.