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"
27 #include "Paragraph.h"
29 #include "support/textutils.h"
30 #include "support/convert.h"
31 #include "support/docstring.h"
33 #include <QProgressBar>
35 #include <QPushButton>
36 #include <QListWidget>
37 #include <QListWidgetItem>
38 #include <QCloseEvent>
39 #include <QSyntaxHighlighter>
40 #include <QTextCharFormat>
41 #include <QTextDocument>
43 #if defined(USE_ASPELL)
44 # include "ASpell_local.h"
45 #elif defined(USE_PSPELL)
49 #if defined(USE_ISPELL)
52 # include "SpellBase.h"
55 #include "frontends/alert.h"
56 // FIXME: those two headers are needed because of the
57 // WorkArea::redraw() call below.
58 #include "frontends/LyXView.h"
59 #include "frontends/WorkArea.h"
70 using support::bformat;
71 using support::contains;
73 GuiSpellchecker::GuiSpellchecker(LyXView & lv)
74 : GuiDialog(lv, "spellchecker"), exitEarly_(false),
75 oldval_(0), newvalue_(0), count_(0), speller_(0)
78 setViewTitle(_("Spellchecker"));
80 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
81 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
82 connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignoreClicked()));
83 connect(replacePB_3, SIGNAL(clicked()), this, SLOT(acceptClicked()));
84 connect(addPB, SIGNAL(clicked()), this, SLOT(addClicked()));
86 connect(replaceCO, SIGNAL(highlighted(QString)),
87 this, SLOT(replaceChanged(QString)));
88 connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
89 this, SLOT(replaceClicked()));
90 connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
91 this, SLOT(suggestionChanged(QListWidgetItem*)));
93 wordED->setReadOnly(true);
95 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
96 bc().setCancel(closePB);
100 GuiSpellchecker::~GuiSpellchecker()
106 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
108 if (replaceCO->count() != 0)
109 replaceCO->setItemText(0, item->text());
111 replaceCO->addItem(item->text());
113 replaceCO->setCurrentIndex(0);
117 void GuiSpellchecker::replaceChanged(const QString & str)
119 if (suggestionsLW->currentItem()->text() == str)
122 for (int i = 0; i != suggestionsLW->count(); ++i) {
123 if (suggestionsLW->item(i)->text() == str) {
124 suggestionsLW->setCurrentRow(i);
131 void GuiSpellchecker::closeEvent(QCloseEvent * e)
134 GuiDialog::closeEvent(e);
138 void GuiSpellchecker::reject()
145 void GuiSpellchecker::updateContents()
147 if (isVisibleView() || exitEarly())
152 void GuiSpellchecker::accept()
158 void GuiSpellchecker::add()
164 void GuiSpellchecker::ignore()
170 void GuiSpellchecker::replace()
172 replace(qstring_to_ucs4(replaceCO->currentText()));
176 void GuiSpellchecker::partialUpdate(int state)
179 case SPELL_PROGRESSED:
180 spellcheckPR->setValue(getProgress());
183 case SPELL_FOUND_WORD: {
184 wordED->setText(toqstr(getWord()));
185 suggestionsLW->clear();
188 while (!(w = getSuggestion()).empty())
189 suggestionsLW->addItem(toqstr(w));
191 if (suggestionsLW->count() == 0)
192 suggestionChanged(new QListWidgetItem(wordED->text()));
194 suggestionChanged(suggestionsLW->item(0));
196 suggestionsLW->setCurrentRow(0);
203 static SpellBase * getSpeller(BufferParams const & bp)
205 string lang = (lyxrc.isp_use_alt_lang)
207 : bp.language->code();
209 #if defined(USE_ASPELL)
210 if (lyxrc.use_spell_lib)
211 return new ASpell(bp, lang);
212 #elif defined(USE_PSPELL)
213 if (lyxrc.use_spell_lib)
214 return new PSpell(bp, lang);
217 #if defined(USE_ISPELL)
218 lang = lyxrc.isp_use_alt_lang ?
219 lyxrc.isp_alt_lang : bp.language->lang();
221 return new ISpell(bp, lang);
223 return new SpellBase;
228 bool GuiSpellchecker::initialiseParams(std::string const &)
230 LYXERR(Debug::GUI) << "Spellchecker::initialiseParams" << endl;
232 speller_ = getSpeller(buffer().params());
236 // reset values to initial
241 bool const success = speller_->error().empty();
244 Alert::error(_("Spellchecker error"),
245 _("The spellchecker could not be started\n")
246 + speller_->error());
255 void GuiSpellchecker::clearParams()
257 LYXERR(Debug::GUI) << "Spellchecker::clearParams" << endl;
263 static bool isLetter(DocIterator const & dit)
265 return dit.inTexted()
266 && dit.inset().allowSpellCheck()
267 && dit.pos() != dit.lastpos()
268 && (dit.paragraph().isLetter(dit.pos())
269 // We want to pass the ' and escape chars to ispell
270 || contains(from_utf8(lyxrc.isp_esc_chars + '\''),
271 dit.paragraph().getChar(dit.pos())))
272 && !dit.paragraph().isDeleted(dit.pos());
276 static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
278 BufferParams const & bp = cur.bv().buffer().params();
280 bool ignoreword = false;
285 while (cur.depth()) {
292 lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
294 // Insets like optional hyphens and ligature
295 // break are part of a word.
296 if (!cur.paragraph().isInset(cur.pos())) {
297 Paragraph::value_type const c =
298 cur.paragraph().getChar(cur.pos());
303 } else { // !isLetter(cur)
305 if (!word.empty() && !ignoreword) {
307 return WordLangTuple(word, lang_code);
316 return WordLangTuple(docstring(), string());
320 void GuiSpellchecker::check()
322 LYXERR(Debug::GUI) << "Check the spelling of a word" << endl;
324 SpellBase::Result res = SpellBase::OK;
326 Cursor cur = bufferview()->cursor();
327 while (cur && cur.pos() && isLetter(cur))
332 DocIterator it = DocIterator(buffer().inset());
333 for (start = 0; it != cur; it.forwardPos())
336 for (total = start; it; it.forwardPos())
341 while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
342 word_ = nextWord(cur, start);
345 if (getWord().empty()) {
353 // Update slider if and only if value has changed
354 float progress = total ? float(start)/total : 1;
355 newvalue_ = int(100.0 * progress);
356 if (newvalue_!= oldval_) {
357 LYXERR(Debug::GUI) << "Updating spell progress." << endl;
360 dialog().partialUpdateView(SPELL_PROGRESSED);
363 // speller might be dead ...
367 res = speller_->check(word_);
369 // ... or it might just be reporting an error
374 LYXERR(Debug::GUI) << "Found word \"" << to_utf8(getWord()) << "\"" << endl;
376 int const size = cur.selEnd().pos() - cur.selBegin().pos();
378 bufferview()->putSelectionAt(cur, size, false);
379 // FIXME: if we used a lfun like in find/replace, dispatch would do
381 bufferview()->update();
382 // FIXME: this Controller is very badly designed...
383 lyxview().currentWorkArea()->redraw();
386 if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
387 LYXERR(Debug::GUI) << "Found a word needing checking." << endl;
388 dialog().partialUpdateView(SPELL_FOUND_WORD);
393 bool GuiSpellchecker::checkAlive()
395 if (speller_->alive() && speller_->error().empty())
399 if (speller_->error().empty())
400 message = _("The spellchecker has died for some reason.\n"
401 "Maybe it has been killed.");
403 message = _("The spellchecker has failed.\n") + speller_->error();
405 dialog().slotClose();
407 Alert::error(_("The spellchecker has failed"), message);
412 void GuiSpellchecker::showSummary()
414 if (!checkAlive() || count_ == 0) {
415 dialog().slotClose();
421 message = bformat(_("%1$d words checked."), count_);
423 message = _("One word checked.");
425 dialog().slotClose();
426 Alert::information(_("Spelling check completed"), message);
430 void GuiSpellchecker::replace(docstring const & replacement)
432 LYXERR(Debug::GUI) << "GuiSpellchecker::replace("
433 << to_utf8(replacement) << ")" << std::endl;
434 cap::replaceSelectionWithString(bufferview()->cursor(), replacement, true);
435 buffer().markDirty();
436 // If we used an LFUN, we would not need that
437 bufferview()->update();
444 void GuiSpellchecker::replaceAll(docstring const & replacement)
447 replace(replacement);
451 void GuiSpellchecker::insert()
453 speller_->insert(word_);
458 docstring GuiSpellchecker::getSuggestion() const
460 return speller_->nextMiss();
464 docstring GuiSpellchecker::getWord() const
470 void GuiSpellchecker::ignoreAll()
472 speller_->accept(word_);
477 Dialog * createGuiSpellchecker(LyXView & lv) { return new GuiSpellchecker(lv); }
479 } // namespace frontend
482 #include "GuiSpellchecker_moc.cpp"