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"
21 #include "buffer_funcs.h"
23 #include "CutAndPaste.h"
27 #include "Paragraph.h"
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstring_list.h"
32 #include "support/ExceptionMessage.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/textutils.h"
37 #include <QListWidgetItem>
39 #include "SpellChecker.h"
41 #include "frontends/alert.h"
44 using namespace lyx::support;
50 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
51 : GuiDialog(lv, "spellchecker", qt_("Spellchecker")),
52 progress_(0), count_(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 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
78 if (replaceCO->count() != 0)
79 replaceCO->setItemText(0, item->text());
81 replaceCO->addItem(item->text());
83 replaceCO->setCurrentIndex(0);
87 void GuiSpellchecker::replaceChanged(const QString & str)
89 if (suggestionsLW->currentItem()
90 && suggestionsLW->currentItem()->text() == str)
93 for (int i = 0; i != suggestionsLW->count(); ++i) {
94 if (suggestionsLW->item(i)->text() == str) {
95 suggestionsLW->setCurrentRow(i);
102 void GuiSpellchecker::reject()
109 void GuiSpellchecker::updateContents()
116 void GuiSpellchecker::accept()
118 theSpellChecker()->accept(word_);
123 void GuiSpellchecker::add()
125 theSpellChecker()->insert(word_);
130 void GuiSpellchecker::ignore()
136 void GuiSpellchecker::replace()
138 replace(qstring_to_ucs4(replaceCO->currentText()));
142 void GuiSpellchecker::updateSuggestions(docstring_list & words)
144 wordED->setText(toqstr(word_.word()));
145 suggestionsLW->clear();
148 suggestionChanged(new QListWidgetItem(wordED->text()));
151 for (size_t i = 0; i != words.size(); ++i)
152 suggestionsLW->addItem(toqstr(words[i]));
154 suggestionChanged(suggestionsLW->item(0));
155 suggestionsLW->setCurrentRow(0);
159 bool GuiSpellchecker::initialiseParams(string const &)
161 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
163 if (!theSpellChecker())
166 DocIterator const begin = doc_iterator_begin(&buffer());
167 Cursor const & cur = bufferview()->cursor();
168 progress_ = countWords(begin, cur);
169 total_ = progress_ + countWords(cur, doc_iterator_end(&buffer()));
175 void GuiSpellchecker::check()
177 LYXERR(Debug::GUI, "Check the spelling of a word");
179 DocIterator from = bufferview()->cursor();
181 WordLangTuple word_lang;
182 docstring_list suggestions;
186 progress = buffer().spellCheck(from, to, word_lang, suggestions);
187 } catch (ExceptionMessage const & message) {
188 if (message.type_ == WarningException) {
189 Alert::warning(message.title_, message.details_);
195 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
197 progress_ += progress;
209 int const progress_bar = total_
210 ? int(100.0 * float(progress_)/total_) : 100;
211 LYXERR(Debug::GUI, "Updating spell progress.");
213 spellcheckPR->setValue(progress_bar);
215 updateSuggestions(suggestions);
217 // FIXME: if we used a lfun like in find/replace, dispatch would do
219 int const size = to.pos() - from.pos();
220 BufferView * bv = const_cast<BufferView *>(bufferview());
221 bv->putSelectionAt(from, size, false);
225 void GuiSpellchecker::showSummary()
234 message = bformat(_("%1$d words checked."), count_);
236 message = _("One word checked.");
239 Alert::information(_("Spelling check completed"), message);
243 void GuiSpellchecker::replace(docstring const & replacement)
245 LYXERR(Debug::GUI, "GuiSpellchecker::replace("
246 << to_utf8(replacement) << ")");
247 BufferView * bv = const_cast<BufferView *>(bufferview());
248 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
249 bv->buffer().markDirty();
250 // If we used an LFUN, we would not need that
251 bv->processUpdateFlags(Update::Force | Update::FitCursor);
258 void GuiSpellchecker::replaceAll(docstring const & replacement)
261 replace(replacement);
265 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
267 } // namespace frontend
270 #include "moc_GuiSpellchecker.cpp"