79CheckerPrivate::CheckerPrivate()
83CheckerPrivate::~CheckerPrivate()
88void CheckerPrivate::init()
90 static TranslationsInit tsInit;
93 setLanguageInternal(
"");
98 return get_enchant_broker()->dict_exists(lang.toStdString());
103 , d_ptr(new CheckerPrivate())
125 bool success = d->setLanguageInternal(lang);
138bool CheckerPrivate::setLanguageInternal(
const QString &newLang)
146 lang = QLocale::system().name();
147 if(lang.toLower() ==
"c" || lang.isEmpty()){
148 qWarning() <<
"Cannot use system locale " << lang;
156 speller = get_enchant_broker()->request_dict(lang.toStdString());
157 }
catch(enchant::Exception& e) {
158 qWarning() <<
"Failed to load dictionary: " << e.what();
169 d->decodeCodes = decode;
175 return d->decodeCodes;
181 d->spellingCheckbox = show;
187 return d->spellingCheckbox;
193 return d->spellingEnabled;
200 d->speller->add(word.toUtf8().data());
207 if(!d->speller || !d->spellingEnabled){
211 if(word.length() < 2){
215 return d->speller->check(word.toUtf8().data());
216 }
catch(
const enchant::Exception&){
224 d->speller->add_to_session(word.toUtf8().data());
232 std::vector<std::string> suggestions;
233 d->speller->suggest(word.toUtf8().data(), suggestions);
234 for(std::size_t i = 0, n = suggestions.size(); i < n; ++i){
235 list.append(QString::fromUtf8(suggestions[i].c_str()));
243 enchant::Broker* broker = get_enchant_broker();
244 QList<QString> languages;
245 broker->list_dicts(dict_describe_cb, &languages);
246 std::sort(languages.begin(), languages.end());
252 QString language, country, extra;
254 if(!country.isEmpty()){
255 QString decoded = QString(
"%1 (%2)").arg(language, country);
256 if(!extra.isEmpty()) {
257 decoded += QString(
" [%1]").arg(extra);
268 d->spellingEnabled = enabled;
272void Checker::showContextMenu(QMenu* menu,
const QPoint& pos,
int wordPos)
275 QAction* insertPos = menu->actions().first();
276 if(d->speller && d->spellingEnabled){
277 QString word =
getWord(wordPos);
281 if(!suggestions.isEmpty()){
282 for(
int i = 0, n = qMin(10, suggestions.length()); i < n; ++i){
283 QAction* action =
new QAction(suggestions[i], menu);
284 action->setProperty(
"wordPos", wordPos);
285 action->setProperty(
"suggestion", suggestions[i]);
286 connect(action, &QAction::triggered,
this, &Checker::slotReplaceWord);
287 menu->insertAction(insertPos, action);
289 if(suggestions.length() > 10) {
290 QMenu* moreMenu =
new QMenu();
291 for(
int i = 10, n = suggestions.length(); i < n; ++i){
292 QAction* action =
new QAction(suggestions[i], moreMenu);
293 action->setProperty(
"wordPos", wordPos);
294 action->setProperty(
"suggestion", suggestions[i]);
295 connect(action, &QAction::triggered,
this, &Checker::slotReplaceWord);
296 moreMenu->addAction(action);
298 QAction* action =
new QAction(tr(
"More..."), menu);
299 menu->insertAction(insertPos, action);
300 action->setMenu(moreMenu);
302 menu->insertSeparator(insertPos);
305 QAction* addAction =
new QAction(tr(
"Add \"%1\" to dictionary").arg(word), menu);
306 addAction->setData(wordPos);
307 connect(addAction, &QAction::triggered,
this, &Checker::slotAddWord);
308 menu->insertAction(insertPos, addAction);
310 QAction* ignoreAction =
new QAction(tr(
"Ignore \"%1\"").arg(word), menu);
311 ignoreAction->setData(wordPos);
312 connect(ignoreAction, &QAction::triggered,
this, &Checker::slotIgnoreWord);
313 menu->insertAction(insertPos, ignoreAction);
314 menu->insertSeparator(insertPos);
317 if(d->spellingCheckbox){
318 QAction* action =
new QAction(tr(
"Check spelling"), menu);
319 action->setCheckable(
true);
320 action->setChecked(d->spellingEnabled);
322 menu->insertAction(insertPos, action);
324 if(d->speller && d->spellingEnabled){
325 QMenu* languagesMenu =
new QMenu();
326 QActionGroup* actionGroup =
new QActionGroup(languagesMenu);
329 QAction* action =
new QAction(text, languagesMenu);
330 action->setData(lang);
331 action->setCheckable(
true);
333 connect(action, &QAction::triggered,
this, &Checker::slotSetLanguage);
334 languagesMenu->addAction(action);
335 actionGroup->addAction(action);
337 QAction* langsAction =
new QAction(tr(
"Languages"), menu);
338 langsAction->setMenu(languagesMenu);
339 menu->insertAction(insertPos, langsAction);
340 menu->insertSeparator(insertPos);
347void Checker::slotAddWord()
349 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
355void Checker::slotIgnoreWord()
357 int wordPos = qobject_cast<QAction*>(QObject::sender())->data().toInt();
363void Checker::slotReplaceWord()
365 QAction* action = qobject_cast<QAction*>(QObject::sender());
366 int wordPos = action->property(
"wordPos").toInt();
368 getWord(wordPos, &start, &end);
369 insertWord(start, end, action->property(
"suggestion").toString());
372void Checker::slotSetLanguage(
bool checked)
375 QAction* action = qobject_cast<QAction*>(QObject::sender());
376 QString lang = action->data().toString();
378 action->setChecked(
false);