2 * \file GuiSpellchecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "GuiSpellchecker.h"
16 #include "qt_helpers.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
22 #include "CutAndPaste.h"
23 #include "support/debug.h"
24 #include "support/gettext.h"
27 #include "Paragraph.h"
29 #include "support/textutils.h"
30 #include "support/docstring.h"
31 #include "support/lstrings.h"
33 #include <QListWidgetItem>
35 #if defined(USE_ASPELL)
36 # include "ASpell_local.h"
37 #elif defined(USE_PSPELL)
41 #if defined(USE_ISPELL)
44 # include "SpellBase.h"
47 #include "frontends/alert.h"
55 using support::bformat;
56 using support::contains;
58 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
59 : GuiDialog(lv, "spellchecker"), exitEarly_(false),
60 oldval_(0), newvalue_(0), count_(0), speller_(0)
63 setViewTitle(_("Spellchecker"));
65 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
66 connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
67 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
68 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
69 connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
71 connect(replaceCO, SIGNAL(highlighted(QString)),
72 this, SLOT(replaceChanged(QString)));
73 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
74 this, SLOT(replace()));
75 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
76 this, SLOT(suggestionChanged(QListWidgetItem*)));
78 wordED->setReadOnly(true);
80 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
81 bc().setCancel(closePB);
85 GuiSpellchecker::~GuiSpellchecker()
91 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
93 if (replaceCO->count() != 0)
94 replaceCO->setItemText(0, item->text());
96 replaceCO->addItem(item->text());
98 replaceCO->setCurrentIndex(0);
102 void GuiSpellchecker::replaceChanged(const QString & str)
104 if (suggestionsLW->currentItem()->text() == str)
107 for (int i = 0; i != suggestionsLW->count(); ++i) {
108 if (suggestionsLW->item(i)->text() == str) {
109 suggestionsLW->setCurrentRow(i);
116 void GuiSpellchecker::closeEvent(QCloseEvent * e)
119 GuiDialog::closeEvent(e);
123 void GuiSpellchecker::reject()
130 void GuiSpellchecker::updateContents()
132 // The clauses below are needed because the spellchecker
133 // has many flaws (see bugs 1950, 2218).
134 // Basically, we have to distinguish the case where a
135 // spellcheck has already been performed for the whole
136 // document (exitEarly() == true, isVisible() == false)
137 // from the rest (exitEarly() == false, isVisible() == true).
138 // FIXME: rewrite the whole beast!
139 static bool check_after_early_exit;
141 // a spellcheck has already been performed,
143 check_after_early_exit = true;
145 else if (isVisible()) {
146 // the above check triggers a second update,
147 // and isVisible() is true then. Prevent a
148 // second check that skips the first word
149 if (check_after_early_exit)
150 // don't check, but reset the bool.
151 // business as usual after this.
152 check_after_early_exit = false;
154 // perform spellcheck (default case)
160 void GuiSpellchecker::accept()
166 void GuiSpellchecker::add()
172 void GuiSpellchecker::ignore()
178 void GuiSpellchecker::replace()
180 replace(qstring_to_ucs4(replaceCO->currentText()));
184 void GuiSpellchecker::partialUpdate(int state)
187 case SPELL_PROGRESSED:
188 spellcheckPR->setValue(getProgress());
191 case SPELL_FOUND_WORD: {
192 wordED->setText(toqstr(getWord()));
193 suggestionsLW->clear();
196 while (!(w = getSuggestion()).empty())
197 suggestionsLW->addItem(toqstr(w));
199 if (suggestionsLW->count() == 0)
200 suggestionChanged(new QListWidgetItem(wordED->text()));
202 suggestionChanged(suggestionsLW->item(0));
204 suggestionsLW->setCurrentRow(0);
211 static SpellBase * getSpeller(BufferParams const & bp)
213 string lang = (lyxrc.isp_use_alt_lang)
215 : bp.language->code();
217 #if defined(USE_ASPELL)
218 if (lyxrc.use_spell_lib)
219 return new ASpell(bp, lang);
220 #elif defined(USE_PSPELL)
221 if (lyxrc.use_spell_lib)
222 return new PSpell(bp, lang);
225 #if defined(USE_ISPELL)
226 lang = lyxrc.isp_use_alt_lang ?
227 lyxrc.isp_alt_lang : bp.language->lang();
229 return new ISpell(bp, lang);
231 return new SpellBase;
236 bool GuiSpellchecker::initialiseParams(std::string const &)
238 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
240 speller_ = getSpeller(buffer().params());
244 // reset values to initial
249 bool const success = speller_->error().empty();
252 Alert::error(_("Spellchecker error"),
253 _("The spellchecker could not be started\n")
254 + speller_->error());
263 void GuiSpellchecker::clearParams()
265 LYXERR(Debug::GUI, "Spellchecker::clearParams");
271 static bool isLetter(DocIterator const & dit)
273 return dit.inTexted()
274 && dit.inset().allowSpellCheck()
275 && dit.pos() != dit.lastpos()
276 && (dit.paragraph().isLetter(dit.pos())
277 // We want to pass the ' and escape chars to ispell
278 || contains(from_utf8(lyxrc.isp_esc_chars + '\''),
279 dit.paragraph().getChar(dit.pos())))
280 && !dit.paragraph().isDeleted(dit.pos());
284 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
286 BufferParams const & bp = cur.bv().buffer().params();
288 bool ignoreword = false;
293 while (cur.depth()) {
300 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
302 // Insets like optional hyphens and ligature
303 // break are part of a word.
304 if (!cur.paragraph().isInset(cur.pos())) {
305 char_type const c = cur.paragraph().getChar(cur.pos());
310 } else { // !isLetter(cur)
312 if (!word.empty() && !ignoreword) {
314 return WordLangTuple(word, lang_code);
323 return WordLangTuple(docstring(), string());
327 void GuiSpellchecker::check()
329 LYXERR(Debug::GUI, "Check the spelling of a word");
331 SpellBase::Result res = SpellBase::OK;
333 Cursor cur = bufferview()->cursor();
334 while (cur && cur.pos() && isLetter(cur))
339 DocIterator it = DocIterator(buffer().inset());
340 for (start = 0; it != cur; it.forwardPos())
343 for (total = start; it; it.forwardPos())
348 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
349 word_ = nextWord(cur, start);
352 if (getWord().empty()) {
360 // Update slider if and only if value has changed
361 float progress = total ? float(start)/total : 1;
362 newvalue_ = int(100.0 * progress);
363 if (newvalue_!= oldval_) {
364 LYXERR(Debug::GUI, "Updating spell progress.");
367 partialUpdate(SPELL_PROGRESSED);
370 // speller might be dead ...
374 res = speller_->check(word_);
376 // ... or it might just be reporting an error
381 LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
383 int const size = cur.selEnd().pos() - cur.selBegin().pos();
385 bufferview()->putSelectionAt(cur, size, false);
386 // FIXME: if we used a lfun like in find/replace, dispatch would do
388 // FIXME: this Controller is very badly designed...
389 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
392 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
393 LYXERR(Debug::GUI, "Found a word needing checking.");
394 partialUpdate(SPELL_FOUND_WORD);
399 bool GuiSpellchecker::checkAlive()
401 if (speller_->alive() && speller_->error().empty())
405 if (speller_->error().empty())
406 message = _("The spellchecker has died for some reason.\n"
407 "Maybe it has been killed.");
409 message = _("The spellchecker has failed.\n") + speller_->error();
413 Alert::error(_("The spellchecker has failed"), message);
418 void GuiSpellchecker::showSummary()
420 if (!checkAlive() || count_ == 0) {
427 message = bformat(_("%1$d words checked."), count_);
429 message = _("One word checked.");
432 Alert::information(_("Spelling check completed"), message);
436 void GuiSpellchecker::replace(docstring const & replacement)
438 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
439 << to_utf8(replacement) << ")");
440 cap::replaceSelectionWithString(bufferview()->cursor(), replacement, true);
441 buffer().markDirty();
442 // If we used an LFUN, we would not need that
443 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
450 void GuiSpellchecker::replaceAll(docstring const & replacement)
453 replace(replacement);
457 void GuiSpellchecker::insert()
459 speller_->insert(word_);
464 docstring GuiSpellchecker::getSuggestion() const
466 return speller_->nextMiss();
470 docstring GuiSpellchecker::getWord() const
476 void GuiSpellchecker::ignoreAll()
478 speller_->accept(word_);
483 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
485 } // namespace frontend
488 #include "GuiSpellchecker_moc.cpp"