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"
25 #include "Paragraph.h"
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/textutils.h"
33 #include <QListWidgetItem>
35 #if defined(USE_ASPELL)
36 # include "ASpell_local.h"
39 #include "SpellBase.h"
41 #include "frontends/alert.h"
44 using namespace lyx::support;
50 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
51 : GuiDialog(lv, "spellchecker", qt_("Spellchecker")), exitEarly_(false),
52 oldval_(0), newvalue_(0), count_(0), speller_(0)
56 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
57 connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
58 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
59 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
60 connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
62 connect(replaceCO, SIGNAL(highlighted(QString)),
63 this, SLOT(replaceChanged(QString)));
64 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
65 this, SLOT(replace()));
66 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
67 this, SLOT(suggestionChanged(QListWidgetItem*)));
69 wordED->setReadOnly(true);
71 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
72 bc().setCancel(closePB);
76 GuiSpellchecker::~GuiSpellchecker()
82 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
84 if (replaceCO->count() != 0)
85 replaceCO->setItemText(0, item->text());
87 replaceCO->addItem(item->text());
89 replaceCO->setCurrentIndex(0);
93 void GuiSpellchecker::replaceChanged(const QString & str)
95 if (suggestionsLW->currentItem()
96 && suggestionsLW->currentItem()->text() == str)
99 for (int i = 0; i != suggestionsLW->count(); ++i) {
100 if (suggestionsLW->item(i)->text() == str) {
101 suggestionsLW->setCurrentRow(i);
108 void GuiSpellchecker::reject()
115 void GuiSpellchecker::updateContents()
117 // The clauses below are needed because the spellchecker
118 // has many flaws (see bugs 1950, 2218).
119 // Basically, we have to distinguish the case where a
120 // spellcheck has already been performed for the whole
121 // document (exitEarly() == true, isVisible() == false)
122 // from the rest (exitEarly() == false, isVisible() == true).
123 // FIXME: rewrite the whole beast!
124 static bool check_after_early_exit;
126 // a spellcheck has already been performed,
128 check_after_early_exit = true;
130 else if (isVisible()) {
131 // the above check triggers a second update,
132 // and isVisible() is true then. Prevent a
133 // second check that skips the first word
134 if (check_after_early_exit)
135 // don't check, but reset the bool.
136 // business as usual after this.
137 check_after_early_exit = false;
139 // perform spellcheck (default case)
145 void GuiSpellchecker::accept()
151 void GuiSpellchecker::add()
157 void GuiSpellchecker::ignore()
163 void GuiSpellchecker::replace()
165 replace(qstring_to_ucs4(replaceCO->currentText()));
169 void GuiSpellchecker::partialUpdate(int state)
172 case SPELL_PROGRESSED:
173 spellcheckPR->setValue(getProgress());
176 case SPELL_FOUND_WORD: {
177 wordED->setText(toqstr(getWord()));
178 suggestionsLW->clear();
181 while (!(w = getSuggestion()).empty())
182 suggestionsLW->addItem(toqstr(w));
184 if (suggestionsLW->count() == 0)
185 suggestionChanged(new QListWidgetItem(wordED->text()));
187 suggestionChanged(suggestionsLW->item(0));
189 suggestionsLW->setCurrentRow(0);
196 static SpellBase * createSpeller(BufferParams const & bp)
198 string lang = lyxrc.spellchecker_use_alt_lang
199 ? lyxrc.spellchecker_alt_lang
200 : bp.language->code();
202 #if defined(USE_ASPELL)
203 return new ASpell(bp, lang);
205 return new SpellBase;
209 bool GuiSpellchecker::initialiseParams(string const &)
211 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
213 speller_ = createSpeller(buffer().params());
217 // reset values to initial
222 bool const success = speller_->error().empty();
225 Alert::error(_("Spellchecker error"),
226 _("The spellchecker could not be started\n")
227 + speller_->error());
236 void GuiSpellchecker::clearParams()
238 LYXERR(Debug::GUI, "Spellchecker::clearParams");
244 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
246 Buffer const & buf = cur.bv().buffer();
249 DocIterator from = cur;
251 if (!buf.nextWord(from, to, word))
252 return WordLangTuple(docstring(), string());
258 string lang_code = from.paragraph().getFontSettings(buf.params(), cur.pos()).language()->code();
260 return WordLangTuple(word, lang_code);
264 void GuiSpellchecker::check()
266 LYXERR(Debug::GUI, "Check the spelling of a word");
268 SpellBase::Result res = SpellBase::OK;
270 Cursor cur = bufferview()->cursor();
271 while (cur && cur.pos() && isLetter(cur))
276 DocIterator it = doc_iterator_begin(&buffer());
277 for (start = 1; it != cur; it.forwardPos())
280 for (total = start; it; it.forwardPos())
285 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
286 word_ = nextWord(cur, start);
289 if (getWord().empty()) {
297 // Update slider if and only if value has changed
298 float progress = total ? float(start)/total : 1;
299 newvalue_ = int(100.0 * progress);
300 if (newvalue_!= oldval_) {
301 LYXERR(Debug::GUI, "Updating spell progress.");
304 partialUpdate(SPELL_PROGRESSED);
307 // speller might be dead ...
311 res = speller_->check(word_);
313 // ... or it might just be reporting an error
318 LYXERR(Debug::GUI, "Found word \"" << to_utf8(getWord()) << "\"");
320 int const size = cur.selEnd().pos() - cur.selBegin().pos();
322 BufferView * bv = const_cast<BufferView *>(bufferview());
323 bv->putSelectionAt(cur, size, false);
324 // FIXME: if we used a lfun like in find/replace, dispatch would do
326 // FIXME: this Controller is very badly designed...
327 bv->processUpdateFlags(Update::Force | Update::FitCursor);
330 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
331 LYXERR(Debug::GUI, "Found a word needing checking.");
332 partialUpdate(SPELL_FOUND_WORD);
337 bool GuiSpellchecker::checkAlive()
339 if (speller_->alive() && speller_->error().empty())
343 if (speller_->error().empty())
344 message = _("The spellchecker has died for some reason.\n"
345 "Maybe it has been killed.");
347 message = _("The spellchecker has failed.\n") + speller_->error();
351 Alert::error(_("The spellchecker has failed"), message);
356 void GuiSpellchecker::showSummary()
358 if (!checkAlive() || count_ == 0) {
365 message = bformat(_("%1$d words checked."), count_);
367 message = _("One word checked.");
370 Alert::information(_("Spelling check completed"), message);
374 void GuiSpellchecker::replace(docstring const & replacement)
376 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
377 << to_utf8(replacement) << ")");
378 BufferView * bv = const_cast<BufferView *>(bufferview());
379 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
380 bv->buffer().markDirty();
381 // If we used an LFUN, we would not need that
382 bv->processUpdateFlags(Update::Force | Update::FitCursor);
389 void GuiSpellchecker::replaceAll(docstring const & replacement)
392 replace(replacement);
396 void GuiSpellchecker::insert()
398 speller_->insert(word_);
403 docstring GuiSpellchecker::getSuggestion() const
405 return speller_->nextMiss();
409 docstring GuiSpellchecker::getWord() const
415 void GuiSpellchecker::ignoreAll()
417 speller_->accept(word_);
422 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
424 } // namespace frontend
427 #include "moc_GuiSpellchecker.cpp"