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());
63 Checker::Checker(QObject* parent)
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::fromUtf8(suggestions[i].c_str()));
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->setProperty(
"wordPos", wordPos);
192 action->setProperty(
"suggestion", suggestions[i]);
193 connect(action, SIGNAL(triggered()),
this, SLOT(slotReplaceWord()));
194 menu->insertAction(insertPos, action);
196 if(suggestions.length() > 10) {
197 QMenu* moreMenu =
new QMenu();
198 for(
int i = 10, n = suggestions.length(); i < n; ++i){
199 QAction* action =
new QAction(suggestions[i], moreMenu);
200 action->setProperty(
"wordPos", wordPos);
201 action->setProperty(
"suggestion", suggestions[i]);
202 connect(action, SIGNAL(triggered()),
this, SLOT(slotReplaceWord()));
203 moreMenu->addAction(action);
205 QAction* action =
new QAction(tr(
"More..."), menu);
206 menu->insertAction(insertPos, action);
207 action->setMenu(moreMenu);
209 menu->insertSeparator(insertPos);
212 QAction* addAction =
new QAction(tr(
"Add \"%1\" to dictionary").arg(word), menu);
213 addAction->setData(wordPos);
214 connect(addAction, SIGNAL(triggered()),
this, SLOT(slotAddWord()));
215 menu->insertAction(insertPos, addAction);
217 QAction* ignoreAction =
new QAction(tr(
"Ignore \"%1\"").arg(word), menu);
218 ignoreAction->setData(wordPos);
219 connect(ignoreAction, SIGNAL(triggered()),
this, SLOT(slotIgnoreWord()));
220 menu->insertAction(insertPos, ignoreAction);
221 menu->insertSeparator(insertPos);
224 if(m_spellingCheckbox){
225 QAction* action =
new QAction(tr(
"Check spelling"), menu);
226 action->setCheckable(
true);
227 action->setChecked(m_spellingEnabled);
229 menu->insertAction(insertPos, action);
231 if(m_speller && m_spellingEnabled){
232 QMenu* languagesMenu =
new QMenu();
233 QActionGroup* actionGroup =
new QActionGroup(languagesMenu);
236 QAction* action =
new QAction(text, languagesMenu);
237 action->setData(lang);
238 action->setCheckable(
true);
240 connect(action, SIGNAL(triggered(
bool)),
this, SLOT(slotSetLanguage(
bool)));
241 languagesMenu->addAction(action);
242 actionGroup->addAction(action);
244 QAction* langsAction =
new QAction(tr(
"Languages"), menu);
245 langsAction->setMenu(languagesMenu);
246 menu->insertAction(insertPos, langsAction);
247 menu->insertSeparator(insertPos);
254 void Checker::slotAddWord()
256 int wordPos = qobject_cast<QAction*>(QObject::sender())->property(
"wordPos").toInt();
262 void Checker::slotIgnoreWord()
264 int wordPos = qobject_cast<QAction*>(QObject::sender())->property(
"wordPos").toInt();
270 void Checker::slotReplaceWord()
272 QAction* action = qobject_cast<QAction*>(QObject::sender());
273 int wordPos = action->property(
"wordPos").toInt();
275 getWord(wordPos, &start, &end);
276 insertWord(start, end, action->property(
"suggestion").toString());
279 void Checker::slotSetLanguage(
bool checked)
282 QAction* action = qobject_cast<QAction*>(QObject::sender());
283 QString lang = action->data().toString();
285 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...
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.