2 * \file GuiSpellchecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiSpellchecker.h"
17 #include "qt_helpers.h"
19 #include "ui_SpellcheckerUi.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "buffer_funcs.h"
26 #include "CutAndPaste.h"
27 #include "FuncRequest.h"
32 #include "Paragraph.h"
33 #include "WordLangTuple.h"
35 #include "support/debug.h"
36 #include "support/docstring.h"
37 #include "support/docstring_list.h"
38 #include "support/ExceptionMessage.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41 #include "support/textutils.h"
43 #include <QListWidgetItem>
46 #include "SpellChecker.h"
48 #include "frontends/alert.h"
51 using namespace lyx::support;
57 struct GuiSpellchecker::Private
59 Private() : progress_(0), count_(0) {}
60 Ui::SpellcheckerUi ui;
61 /// current word being checked and lang code
63 /// values for progress
71 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
72 : DockView(lv, "spellchecker", qt_("Spellchecker"),
73 Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
77 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
78 this, SLOT(on_replacePB_clicked()));
80 d->ui.wordED->setReadOnly(true);
82 d->ui.suggestionsLW->installEventFilter(this);
86 GuiSpellchecker::~GuiSpellchecker()
92 void GuiSpellchecker::on_closePB_clicked()
98 bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
100 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
101 QKeyEvent *e = static_cast<QKeyEvent *> (event);
102 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
103 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
104 on_replacePB_clicked();
106 } else if (e->key() == Qt::Key_Right) {
107 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
111 // standard event processing
112 return QWidget::eventFilter(obj, event);
116 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
118 if (d->ui.replaceCO->count() != 0)
119 d->ui.replaceCO->setItemText(0, item->text());
121 d->ui.replaceCO->addItem(item->text());
123 d->ui.replaceCO->setCurrentIndex(0);
127 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
129 QListWidget * lw = d->ui.suggestionsLW;
130 if (lw->currentItem() && lw->currentItem()->text() == str)
133 for (int i = 0; i != lw->count(); ++i) {
134 if (lw->item(i)->text() == str) {
135 lw->setCurrentRow(i);
142 void GuiSpellchecker::updateView()
149 void GuiSpellchecker::on_ignoreAllPB_clicked()
151 /// replace all occurances of word
152 theSpellChecker()->accept(d->word_);
157 void GuiSpellchecker::on_addPB_clicked()
159 /// insert word in personal dictionary
160 theSpellChecker()->insert(d->word_);
165 void GuiSpellchecker::on_ignorePB_clicked()
167 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
172 void GuiSpellchecker::on_findNextPB_clicked()
174 docstring const data = find2string(
175 qstring_to_ucs4(d->ui.wordED->text()),
177 dispatch(FuncRequest(LFUN_WORD_FIND, data));
181 void GuiSpellchecker::on_replacePB_clicked()
183 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
185 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
186 BufferView * bv = const_cast<BufferView *>(bufferview());
187 if (!bv->cursor().inTexted())
189 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
190 bv->buffer().markDirty();
191 // If we used an LFUN, we would not need that
192 bv->processUpdateFlags(Update::Force | Update::FitCursor);
199 void GuiSpellchecker::on_replaceAllPB_clicked()
201 docstring const data = replace2string(
202 qstring_to_ucs4(d->ui.replaceCO->currentText()),
203 qstring_to_ucs4(d->ui.wordED->text()),
204 true, true, true, true);
205 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
206 check(); // continue spellchecking
210 void GuiSpellchecker::updateSuggestions(docstring_list & words)
212 QString const suggestion = toqstr(d->word_.word());
213 d->ui.wordED->setText(suggestion);
214 QListWidget * lw = d->ui.suggestionsLW;
218 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
221 for (size_t i = 0; i != words.size(); ++i)
222 lw->addItem(toqstr(words[i]));
224 on_suggestionsLW_itemClicked(lw->item(0));
225 lw->setCurrentRow(0);
229 bool GuiSpellchecker::initialiseParams(string const &)
231 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
233 if (!theSpellChecker())
236 DocIterator const begin = doc_iterator_begin(&buffer());
237 Cursor const & cur = bufferview()->cursor();
238 d->progress_ = countWords(begin, cur);
239 d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
245 void GuiSpellchecker::check()
247 LYXERR(Debug::GUI, "Check the spelling of a word");
249 DocIterator from = bufferview()->cursor();
251 WordLangTuple word_lang;
252 docstring_list suggestions;
256 progress = buffer().spellCheck(from, to, word_lang, suggestions);
257 } catch (ExceptionMessage const & message) {
258 if (message.type_ == WarningException) {
259 Alert::warning(message.title_, message.details_);
265 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
266 d->count_ += progress;
267 d->progress_ += progress;
270 if (from == doc_iterator_end(&buffer())) {
277 d->word_ = word_lang;
279 int const progress_bar = d->total_
280 ? int(100.0 * float(d->progress_)/d->total_) : 100;
281 LYXERR(Debug::GUI, "Updating spell progress.");
283 d->ui.spellcheckPR->setValue(progress_bar);
285 updateSuggestions(suggestions);
287 // FIXME: if we used a lfun like in find/replace, dispatch would do
289 int const size = to.pos() - from.pos();
290 BufferView * bv = const_cast<BufferView *>(bufferview());
291 bv->putSelectionAt(from, size, false);
295 void GuiSpellchecker::showSummary()
297 if (d->count_ == 0) {
304 message = bformat(_("%1$d words checked."), d->count_);
306 message = _("One word checked.");
309 Alert::information(_("Spelling check completed"), message);
313 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
315 } // namespace frontend
318 #include "moc_GuiSpellchecker.cpp"