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"
54 using support::bformat;
55 using support::contains;
57 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
58 : GuiDialog(lv, "spellchecker"), exitEarly_(false),
59 oldval_(0), newvalue_(0), count_(0), speller_(0)
62 setViewTitle(_("Spellchecker"));
64 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
65 connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
66 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
67 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
68 connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
70 connect(replaceCO, SIGNAL(highlighted(QString)),
71 this, SLOT(replaceChanged(QString)));
72 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
73 this, SLOT(replace()));
74 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
75 this, SLOT(suggestionChanged(QListWidgetItem*)));
77 wordED->setReadOnly(true);
79 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
80 bc().setCancel(closePB);
84 GuiSpellchecker::~GuiSpellchecker()
90 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
92 if (replaceCO->count() != 0)
93 replaceCO->setItemText(0, item->text());
95 replaceCO->addItem(item->text());
97 replaceCO->setCurrentIndex(0);
101 void GuiSpellchecker::replaceChanged(const QString & str)
103 if (suggestionsLW->currentItem()->text() == str)
106 for (int i = 0; i != suggestionsLW->count(); ++i) {
107 if (suggestionsLW->item(i)->text() == str) {
108 suggestionsLW->setCurrentRow(i);
115 void GuiSpellchecker::closeEvent(QCloseEvent * e)
118 GuiDialog::closeEvent(e);
122 void GuiSpellchecker::reject()
129 void GuiSpellchecker::updateContents()
131 // The clauses below are needed because the spellchecker
132 // has many flaws (see bugs 1950, 2218).
133 // Basically, we have to distinguish the case where a
134 // spellcheck has already been performed for the whole
135 // document (exitEarly() == true, isVisible() == false)
136 // from the rest (exitEarly() == false, isVisible() == true).
137 // FIXME: rewrite the whole beast!
138 static bool check_after_early_exit;
140 // a spellcheck has already been performed,
142 check_after_early_exit = true;
144 else if (isVisible()) {
145 // the above check triggers a second update,
146 // and isVisible() is true then. Prevent a
147 // second check that skips the first word
148 if (check_after_early_exit)
149 // don't check, but reset the bool.
150 // business as usual after this.
151 check_after_early_exit = false;
153 // perform spellcheck (default case)
159 void GuiSpellchecker::accept()
165 void GuiSpellchecker::add()
171 void GuiSpellchecker::ignore()
177 void GuiSpellchecker::replace()
179 replace(qstring_to_ucs4(replaceCO->currentText()));
183 void GuiSpellchecker::partialUpdate(int state)
186 case SPELL_PROGRESSED:
187 spellcheckPR->setValue(getProgress());
190 case SPELL_FOUND_WORD: {
191 wordED->setText(toqstr(getWord()));
192 suggestionsLW->clear();
195 while (!(w = getSuggestion()).empty())
196 suggestionsLW->addItem(toqstr(w));
198 if (suggestionsLW->count() == 0)
199 suggestionChanged(new QListWidgetItem(wordED->text()));
201 suggestionChanged(suggestionsLW->item(0));
203 suggestionsLW->setCurrentRow(0);
210 static SpellBase * getSpeller(BufferParams const & bp)
212 string lang = (lyxrc.isp_use_alt_lang)
214 : bp.language->code();
216 #if defined(USE_ASPELL)
217 if (lyxrc.use_spell_lib)
218 return new ASpell(bp, lang);
219 #elif defined(USE_PSPELL)
220 if (lyxrc.use_spell_lib)
221 return new PSpell(bp, lang);
224 #if defined(USE_ISPELL)
225 lang = lyxrc.isp_use_alt_lang ?
226 lyxrc.isp_alt_lang : bp.language->lang();
228 return new ISpell(bp, lang);
230 return new SpellBase;
235 bool GuiSpellchecker::initialiseParams(std::string const &)
237 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
239 speller_ = getSpeller(buffer().params());
243 // reset values to initial
248 bool const success = speller_->error().empty();
251 Alert::error(_("Spellchecker error"),
252 _("The spellchecker could not be started\n")
253 + speller_->error());
262 void GuiSpellchecker::clearParams()
264 LYXERR(Debug::GUI, "Spellchecker::clearParams");
270 static bool isLetter(DocIterator const & dit)
272 return dit.inTexted()
273 && dit.inset().allowSpellCheck()
274 && dit.pos() != dit.lastpos()
275 && (dit.paragraph().isLetter(dit.pos())
276 // We want to pass the ' and escape chars to ispell
277 || contains(from_utf8(lyxrc.isp_esc_chars + '\''),
278 dit.paragraph().getChar(dit.pos())))
279 && !dit.paragraph().isDeleted(dit.pos());
283 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
285 BufferParams const & bp = cur.bv().buffer().params();
287 bool ignoreword = false;
292 while (cur.depth()) {
299 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
301 // Insets like optional hyphens and ligature
302 // break are part of a word.
303 if (!cur.paragraph().isInset(cur.pos())) {
304 char_type const c = cur.paragraph().getChar(cur.pos());
309 } else { // !isLetter(cur)
311 if (!word.empty() && !ignoreword) {
313 return WordLangTuple(word, lang_code);
322 return WordLangTuple(docstring(), string());
326 void GuiSpellchecker::check()
328 LYXERR(Debug::GUI, "Check the spelling of a word");
330 SpellBase::Result res = SpellBase::OK;
332 Cursor cur = bufferview()->cursor();
333 while (cur && cur.pos() && isLetter(cur))
338 DocIterator it = DocIterator(buffer().inset());
339 for (start = 0; it != cur; it.forwardPos())
342 for (total = start; it; it.forwardPos())
347 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
348 word_ = nextWord(cur, start);
351 if (getWord().empty()) {
359 // Update slider if and only if value has changed
360 float progress = total ? float(start)/total : 1;
361 newvalue_ = int(100.0 * progress);
362 if (newvalue_!= oldval_) {
363 LYXERR(Debug::GUI, "Updating spell progress.");
366 partialUpdate(SPELL_PROGRESSED);
369 // speller might be dead ...
373 res = speller_->check(word_);
375 // ... or it might just be reporting an error
380 LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
382 int const size = cur.selEnd().pos() - cur.selBegin().pos();
384 bufferview()->putSelectionAt(cur, size, false);
385 // FIXME: if we used a lfun like in find/replace, dispatch would do
387 // FIXME: this Controller is very badly designed...
388 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
391 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
392 LYXERR(Debug::GUI, "Found a word needing checking.");
393 partialUpdate(SPELL_FOUND_WORD);
398 bool GuiSpellchecker::checkAlive()
400 if (speller_->alive() && speller_->error().empty())
404 if (speller_->error().empty())
405 message = _("The spellchecker has died for some reason.\n"
406 "Maybe it has been killed.");
408 message = _("The spellchecker has failed.\n") + speller_->error();
412 Alert::error(_("The spellchecker has failed"), message);
417 void GuiSpellchecker::showSummary()
419 if (!checkAlive() || count_ == 0) {
426 message = bformat(_("%1$d words checked."), count_);
428 message = _("One word checked.");
431 Alert::information(_("Spelling check completed"), message);
435 void GuiSpellchecker::replace(docstring const & replacement)
437 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
438 << to_utf8(replacement) << ")");
439 cap::replaceSelectionWithString(bufferview()->cursor(), replacement, true);
440 buffer().markDirty();
441 // If we used an LFUN, we would not need that
442 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
449 void GuiSpellchecker::replaceAll(docstring const & replacement)
452 replace(replacement);
456 void GuiSpellchecker::insert()
458 speller_->insert(word_);
463 docstring GuiSpellchecker::getSuggestion() const
465 return speller_->nextMiss();
469 docstring GuiSpellchecker::getWord() const
475 void GuiSpellchecker::ignoreAll()
477 speller_->accept(word_);
482 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
484 } // namespace frontend
487 #include "GuiSpellchecker_moc.cpp"