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"
16 #include "GuiApplication.h"
18 #include "qt_helpers.h"
20 #include "ui_SpellcheckerUi.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "buffer_funcs.h"
28 #include "CutAndPaste.h"
29 #include "FuncRequest.h"
35 #include "Paragraph.h"
36 #include "WordLangTuple.h"
38 #include "support/debug.h"
39 #include "support/docstring.h"
40 #include "support/docstring_list.h"
41 #include "support/ExceptionMessage.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/textutils.h"
47 #include <QListWidgetItem>
48 #include <QMessageBox>
50 #include "SpellChecker.h"
52 #include "frontends/alert.h"
55 using namespace lyx::support;
61 struct SpellcheckerWidget::Private
63 Private(SpellcheckerWidget * parent, DockView * dv, GuiView * gv)
64 : p(parent), dv_(dv), gv_(gv), incheck_(false), wrap_around_(false) {}
65 /// update from controller
66 void updateSuggestions(docstring_list & words);
67 /// move to next position after current word
69 /// check text until next misspelled/unknown word
71 /// close the spell checker dialog
73 /// make/restore a selection between from and to
74 void setSelection(DocIterator const & from, DocIterator const & to) const;
75 /// if no selection was checked:
76 /// ask the user if the check should start over
77 bool continueFromBeginning();
78 /// set the given language in language chooser
79 void setLanguage(Language const * lang);
80 /// test and set guard flag
87 void canCheck() { incheck_ = false; }
88 /// check for wrap around
89 void wrapAround(bool flag) {
95 /// test for existing association with a document buffer
96 /// and test for already active check
98 return gv_->documentBufferView() == 0 || inCheck();
100 /// the cursor position of the buffer view
101 DocIterator const cursor() const;
103 bool isCurrentBuffer(DocIterator const & cursor) const;
104 bool isWrapAround(DocIterator const & cursor) const;
105 bool isWrapAround() const { return wrap_around_; }
106 bool atLastPos(DocIterator const & cursor) const;
107 /// validate the cached doc iterators
108 /// The spell checker dialog is not modal.
109 /// The user may change the buffer being checked and break the iterators.
110 void fixPositionsIfBroken();
112 Ui::SpellcheckerUi ui;
114 SpellcheckerWidget * p;
119 /// current word being checked and lang code
121 /// cursor position where spell checking starts
123 /// range to spell check
124 /// for selection both are non-empty
125 /// after wrap around the start position becomes the end
135 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
136 : QTabWidget(parent), d(new Private(this, dv, gv))
140 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
141 this, SLOT(on_replacePB_clicked()));
144 QAbstractItemModel * language_model = guiApp->languageModel();
145 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
146 language_model->sort(0);
147 d->ui.languageCO->setModel(language_model);
148 d->ui.languageCO->setModelColumn(1);
150 d->ui.wordED->setReadOnly(true);
152 d->ui.suggestionsLW->installEventFilter(this);
156 SpellcheckerWidget::~SpellcheckerWidget()
162 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
164 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
165 QKeyEvent *e = static_cast<QKeyEvent *> (event);
166 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
167 if (d->ui.suggestionsLW->currentItem()) {
168 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
169 on_replacePB_clicked();
172 } else if (e->key() == Qt::Key_Right) {
173 if (d->ui.suggestionsLW->currentItem())
174 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
178 // standard event processing
179 return QWidget::eventFilter(obj, event);
183 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
185 if (d->ui.replaceCO->count() != 0)
186 d->ui.replaceCO->setItemText(0, item->text());
188 d->ui.replaceCO->addItem(item->text());
190 d->ui.replaceCO->setCurrentIndex(0);
194 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
196 QListWidget * lw = d->ui.suggestionsLW;
197 if (lw->currentItem() && lw->currentItem()->text() == str)
200 for (int i = 0; i != lw->count(); ++i) {
201 if (lw->item(i)->text() == str) {
202 lw->setCurrentRow(i);
209 void SpellcheckerWidget::updateView()
211 BufferView * bv = d->gv_->documentBufferView();
212 // we need a buffer view and the buffer has to be writable
213 bool const enabled = bv != 0 && !bv->buffer().isReadonly();
215 if (enabled && hasFocus()) {
216 Cursor const & cursor = bv->cursor();
217 if (d->start_.empty() || !d->isCurrentBuffer(cursor)) {
218 if (cursor.selection()) {
219 d->begin_ = cursor.selectionBegin();
220 d->end_ = cursor.selectionEnd();
221 d->start_ = d->begin_;
222 bv->cursor().setCursor(d->start_);
224 d->begin_ = DocIterator();
225 d->end_ = DocIterator();
228 d->wrapAround(false);
234 DocIterator const SpellcheckerWidget::Private::cursor() const
236 BufferView * bv = gv_->documentBufferView();
237 return bv ? bv->cursor() : DocIterator();
240 bool SpellcheckerWidget::Private::continueFromBeginning()
242 DocIterator const current_ = cursor();
243 if (isCurrentBuffer(current_) && !begin_.empty()) {
244 // selection was checked
245 // start over from beginning makes no sense
246 fixPositionsIfBroken();
248 if (current_ == start_) {
249 // no errors found... tell the user the good news
250 // so there is some feedback
251 QMessageBox::information(p,
252 qt_("Spell Checker"),
253 qt_("Spell check of the selection done, "
254 "did not find any errors."));
258 QMessageBox::StandardButton const answer = QMessageBox::question(p,
259 qt_("Spell Checker"),
260 qt_("We reached the end of the document, would you like to "
261 "continue from the beginning?"),
262 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
263 if (answer == QMessageBox::No) {
264 fixPositionsIfBroken();
268 // there is no selection, start over from the beginning now
270 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
274 bool SpellcheckerWidget::Private::isCurrentBuffer(DocIterator const & cursor) const
276 return start_.buffer() == cursor.buffer();
279 bool SpellcheckerWidget::Private::atLastPos(DocIterator const & cursor) const
281 bool const valid_end = !end_.empty();
282 return cursor.depth() <= 1 && (
284 (valid_end && isCurrentBuffer(cursor) && cursor >= end_));
287 bool SpellcheckerWidget::Private::isWrapAround(DocIterator const & cursor) const
289 return wrap_around_ && isCurrentBuffer(cursor) && start_ < cursor;
292 void SpellcheckerWidget::Private::fixPositionsIfBroken()
294 DocIterator const current_ = cursor();
295 if (!isCurrentBuffer(current_)) {
296 LYXERR(Debug::GUI, "wrong document of current cursor position " << start_);
298 begin_ = DocIterator();
299 end_ = DocIterator();
301 if (start_.fixIfBroken())
302 LYXERR(Debug::GUI, "broken start position fixed " << start_);
303 if (begin_.fixIfBroken()) {
304 LYXERR(Debug::GUI, "broken selection begin position fixed " << begin_);
305 begin_ = DocIterator();
306 end_ = DocIterator();
308 if (end_.fixIfBroken())
309 LYXERR(Debug::GUI, "broken selection end position fixed " << end_);
312 void SpellcheckerWidget::Private::hide() const
314 BufferView * bv = gv_->documentBufferView();
315 Cursor & bvcur = bv->cursor();
317 if (isCurrentBuffer(bvcur)) {
318 if (!begin_.empty() && !end_.empty()) {
319 // restore previous selection
320 setSelection(begin_, end_);
322 // restore cursor position
323 bvcur.setCursor(start_);
324 bvcur.clearSelection();
325 bv->processUpdateFlags(Update::Force | Update::FitCursor);
330 void SpellcheckerWidget::Private::setSelection(
331 DocIterator const & from, DocIterator const & to) const
333 BufferView * bv = gv_->documentBufferView();
334 DocIterator end = to;
336 if (from.pit() != end.pit()) {
337 // there are multiple paragraphs in selection
338 Cursor & bvcur = bv->cursor();
339 bvcur.setCursor(from);
340 bvcur.clearSelection();
341 bvcur.selection(true);
342 bvcur.setCursor(end);
343 bvcur.selection(true);
346 // If we used a LFUN, dispatch would do all of this for us
347 int const size = end.pos() - from.pos();
348 bv->putSelectionAt(from, size, false);
350 bv->processUpdateFlags(Update::Force | Update::FitCursor);
353 void SpellcheckerWidget::Private::forward()
355 DocIterator const from = cursor();
357 dispatch(FuncRequest(LFUN_ESCAPE));
358 fixPositionsIfBroken();
359 if (!atLastPos(cursor())) {
360 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
362 if (atLastPos(cursor())) {
365 if (from == cursor()) {
366 //FIXME we must be at the end of a cell
367 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
369 if (isWrapAround(cursor())) {
375 void SpellcheckerWidget::on_languageCO_activated(int index)
378 fromqstr(d->ui.languageCO->itemData(index).toString());
379 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
382 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
387 bool SpellcheckerWidget::initialiseParams(std::string const &)
389 BufferView * bv = d->gv_->documentBufferView();
392 std::set<Language const *> langs =
393 bv->buffer().masterBuffer()->getLanguages();
395 d->setLanguage(*langs.begin());
396 d->start_ = DocIterator();
397 d->wrapAround(false);
403 void SpellcheckerWidget::on_ignoreAllPB_clicked()
405 /// ignore all occurrences of word
408 LYXERR(Debug::GUI, "Spellchecker: ignore all button");
409 if (d->word_.lang() && !d->word_.word().empty())
410 theSpellChecker()->accept(d->word_);
417 void SpellcheckerWidget::on_addPB_clicked()
419 /// insert word in personal dictionary
422 LYXERR(Debug::GUI, "Spellchecker: add word button");
423 theSpellChecker()->insert(d->word_);
430 void SpellcheckerWidget::on_ignorePB_clicked()
432 /// ignore this occurrence of word
435 LYXERR(Debug::GUI, "Spellchecker: ignore button");
442 void SpellcheckerWidget::on_findNextPB_clicked()
446 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
447 docstring const datastring = find2string(textfield,
449 LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
450 dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
455 void SpellcheckerWidget::on_replacePB_clicked()
459 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
460 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
461 docstring const datastring =
462 replace2string(replacement, textfield,
463 true, // case sensitive
469 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
470 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
477 void SpellcheckerWidget::on_replaceAllPB_clicked()
481 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
482 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
483 docstring const datastring =
484 replace2string(replacement, textfield,
485 true, // case sensitive
491 LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
492 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
494 // replace all wraps around
496 d->check(); // continue spellchecking
501 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
503 QString const suggestion = toqstr(word_.word());
504 ui.wordED->setText(suggestion);
505 QListWidget * lw = ui.suggestionsLW;
509 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
512 for (size_t i = 0; i != words.size(); ++i)
513 lw->addItem(toqstr(words[i]));
515 p->on_suggestionsLW_itemClicked(lw->item(0));
516 lw->setCurrentRow(0);
520 void SpellcheckerWidget::Private::setLanguage(Language const * lang)
522 int const pos = ui.languageCO->findData(toqstr(lang->lang()));
524 ui.languageCO->setCurrentIndex(pos);
528 void SpellcheckerWidget::Private::check()
530 BufferView * bv = gv_->documentBufferView();
531 if (!bv || bv->buffer().text().empty())
534 fixPositionsIfBroken();
536 SpellChecker * speller = theSpellChecker();
537 if (speller && !speller->hasDictionary(bv->buffer().language())) {
538 int dsize = speller->numDictionaries();
541 QMessageBox::information(p,
542 qt_("Spell Checker"),
543 qt_("Spell checker has no dictionaries."));
548 DocIterator from = bv->cursor();
549 DocIterator to = isCurrentBuffer(from) ? end_ : doc_iterator_end(&bv->buffer());
550 WordLangTuple word_lang;
551 docstring_list suggestions;
553 LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
555 bv->buffer().spellCheck(from, to, word_lang, suggestions);
556 } catch (ExceptionMessage const & message) {
557 if (message.type_ == WarningException) {
558 Alert::warning(message.title_, message.details_);
564 // end of document or selection?
565 if (atLastPos(from)) {
566 if (isWrapAround()) {
570 if (continueFromBeginning())
575 if (isWrapAround(from)) {
583 updateSuggestions(suggestions);
585 if (!word_lang.lang())
587 setLanguage(word_lang.lang());
588 // mark misspelled word
589 setSelection(from, to);
593 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
594 Qt::DockWidgetArea area, Qt::WindowFlags flags)
595 : DockView(parent, "spellchecker", qt_("Spellchecker"),
598 widget_ = new SpellcheckerWidget(&parent, this);
600 setFocusProxy(widget_);
604 GuiSpellchecker::~GuiSpellchecker()
611 void GuiSpellchecker::updateView()
613 widget_->updateView();
617 Dialog * createGuiSpellchecker(GuiView & lv)
619 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
621 gui->setFloating(true);
627 } // namespace frontend
630 #include "moc_GuiSpellchecker.cpp"