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"
50 using namespace lyx::support;
56 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
57 : GuiDialog(lv, "spellchecker", qt_("Spellchecker")), exitEarly_(false),
58 oldval_(0), newvalue_(0), count_(0), speller_(0)
62 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
63 connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
64 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
65 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
66 connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
68 connect(replaceCO, SIGNAL(highlighted(QString)),
69 this, SLOT(replaceChanged(QString)));
70 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
71 this, SLOT(replace()));
72 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
73 this, SLOT(suggestionChanged(QListWidgetItem*)));
75 wordED->setReadOnly(true);
77 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
78 bc().setCancel(closePB);
82 GuiSpellchecker::~GuiSpellchecker()
88 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
90 if (replaceCO->count() != 0)
91 replaceCO->setItemText(0, item->text());
93 replaceCO->addItem(item->text());
95 replaceCO->setCurrentIndex(0);
99 void GuiSpellchecker::replaceChanged(const QString & str)
101 if (suggestionsLW->currentItem()->text() == str)
104 for (int i = 0; i != suggestionsLW->count(); ++i) {
105 if (suggestionsLW->item(i)->text() == str) {
106 suggestionsLW->setCurrentRow(i);
113 void GuiSpellchecker::closeEvent(QCloseEvent * e)
116 GuiDialog::closeEvent(e);
120 void GuiSpellchecker::reject()
127 void GuiSpellchecker::updateContents()
129 // The clauses below are needed because the spellchecker
130 // has many flaws (see bugs 1950, 2218).
131 // Basically, we have to distinguish the case where a
132 // spellcheck has already been performed for the whole
133 // document (exitEarly() == true, isVisible() == false)
134 // from the rest (exitEarly() == false, isVisible() == true).
135 // FIXME: rewrite the whole beast!
136 static bool check_after_early_exit;
138 // a spellcheck has already been performed,
140 check_after_early_exit = true;
142 else if (isVisible()) {
143 // the above check triggers a second update,
144 // and isVisible() is true then. Prevent a
145 // second check that skips the first word
146 if (check_after_early_exit)
147 // don't check, but reset the bool.
148 // business as usual after this.
149 check_after_early_exit = false;
151 // perform spellcheck (default case)
157 void GuiSpellchecker::accept()
163 void GuiSpellchecker::add()
169 void GuiSpellchecker::ignore()
175 void GuiSpellchecker::replace()
177 replace(qstring_to_ucs4(replaceCO->currentText()));
181 void GuiSpellchecker::partialUpdate(int state)
184 case SPELL_PROGRESSED:
185 spellcheckPR->setValue(getProgress());
188 case SPELL_FOUND_WORD: {
189 wordED->setText(toqstr(getWord()));
190 suggestionsLW->clear();
193 while (!(w = getSuggestion()).empty())
194 suggestionsLW->addItem(toqstr(w));
196 if (suggestionsLW->count() == 0)
197 suggestionChanged(new QListWidgetItem(wordED->text()));
199 suggestionChanged(suggestionsLW->item(0));
201 suggestionsLW->setCurrentRow(0);
208 static SpellBase * getSpeller(BufferParams const & bp)
210 string lang = (lyxrc.isp_use_alt_lang)
212 : bp.language->code();
214 #if defined(USE_ASPELL)
215 if (lyxrc.use_spell_lib)
216 return new ASpell(bp, lang);
217 #elif defined(USE_PSPELL)
218 if (lyxrc.use_spell_lib)
219 return new PSpell(bp, lang);
222 #if defined(USE_ISPELL)
223 lang = lyxrc.isp_use_alt_lang ?
224 lyxrc.isp_alt_lang : bp.language->lang();
226 return new ISpell(bp, lang);
228 return new SpellBase;
233 bool GuiSpellchecker::initialiseParams(string const &)
235 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
237 speller_ = getSpeller(buffer().params());
241 // reset values to initial
246 bool const success = speller_->error().empty();
249 Alert::error(_("Spellchecker error"),
250 _("The spellchecker could not be started\n")
251 + speller_->error());
260 void GuiSpellchecker::clearParams()
262 LYXERR(Debug::GUI, "Spellchecker::clearParams");
268 static bool isLetter(DocIterator const & dit)
270 return dit.inTexted()
271 && dit.inset().allowSpellCheck()
272 && dit.pos() != dit.lastpos()
273 && (dit.paragraph().isLetter(dit.pos())
274 // We want to pass the ' and escape chars to ispell
275 || contains(from_utf8(lyxrc.isp_esc_chars + '\''),
276 dit.paragraph().getChar(dit.pos())))
277 && !dit.paragraph().isDeleted(dit.pos());
281 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
283 BufferParams const & bp = cur.bv().buffer().params();
285 bool ignoreword = false;
290 while (cur.depth()) {
297 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
299 // Insets like optional hyphens and ligature
300 // break are part of a word.
301 if (!cur.paragraph().isInset(cur.pos())) {
302 char_type const c = cur.paragraph().getChar(cur.pos());
307 } else { // !isLetter(cur)
309 if (!word.empty() && !ignoreword) {
311 return WordLangTuple(word, lang_code);
320 return WordLangTuple(docstring(), string());
324 void GuiSpellchecker::check()
326 LYXERR(Debug::GUI, "Check the spelling of a word");
328 SpellBase::Result res = SpellBase::OK;
330 Cursor cur = bufferview()->cursor();
331 while (cur && cur.pos() && isLetter(cur))
336 DocIterator it = doc_iterator_begin(buffer().inset());
337 for (start = 1; it != cur; it.forwardPos())
340 for (total = start; it; it.forwardPos())
345 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
346 word_ = nextWord(cur, start);
349 if (getWord().empty()) {
357 // Update slider if and only if value has changed
358 float progress = total ? float(start)/total : 1;
359 newvalue_ = int(100.0 * progress);
360 if (newvalue_!= oldval_) {
361 LYXERR(Debug::GUI, "Updating spell progress.");
364 partialUpdate(SPELL_PROGRESSED);
367 // speller might be dead ...
371 res = speller_->check(word_);
373 // ... or it might just be reporting an error
378 LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
380 int const size = cur.selEnd().pos() - cur.selBegin().pos();
382 bufferview()->putSelectionAt(cur, size, false);
383 // FIXME: if we used a lfun like in find/replace, dispatch would do
385 // FIXME: this Controller is very badly designed...
386 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
389 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
390 LYXERR(Debug::GUI, "Found a word needing checking.");
391 partialUpdate(SPELL_FOUND_WORD);
396 bool GuiSpellchecker::checkAlive()
398 if (speller_->alive() && speller_->error().empty())
402 if (speller_->error().empty())
403 message = _("The spellchecker has died for some reason.\n"
404 "Maybe it has been killed.");
406 message = _("The spellchecker has failed.\n") + speller_->error();
410 Alert::error(_("The spellchecker has failed"), message);
415 void GuiSpellchecker::showSummary()
417 if (!checkAlive() || count_ == 0) {
424 message = bformat(_("%1$d words checked."), count_);
426 message = _("One word checked.");
429 Alert::information(_("Spelling check completed"), message);
433 void GuiSpellchecker::replace(docstring const & replacement)
435 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
436 << to_utf8(replacement) << ")");
437 cap::replaceSelectionWithString(bufferview()->cursor(), replacement, true);
438 buffer().markDirty();
439 // If we used an LFUN, we would not need that
440 bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
447 void GuiSpellchecker::replaceAll(docstring const & replacement)
450 replace(replacement);
454 void GuiSpellchecker::insert()
456 speller_->insert(word_);
461 docstring GuiSpellchecker::getSuggestion() const
463 return speller_->nextMiss();
467 docstring GuiSpellchecker::getWord() const
473 void GuiSpellchecker::ignoreAll()
475 speller_->accept(word_);
480 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
482 } // namespace frontend
485 #include "GuiSpellchecker_moc.cpp"