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"
34 #include "Paragraph.h"
35 #include "WordLangTuple.h"
37 #include "support/debug.h"
38 #include "support/docstring.h"
39 #include "support/docstring_list.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43 #include "support/textutils.h"
46 #include <QListWidgetItem>
47 #include <QMessageBox>
49 #include "SpellChecker.h"
51 #include "frontends/alert.h"
54 using namespace lyx::support;
60 struct SpellcheckerWidget::Private
62 Private(SpellcheckerWidget * parent, DockView * dv)
63 : p(parent), dv_(dv), incheck_(false), wrap_around_(false) {}
64 /// update from controller
65 void updateSuggestions(docstring_list & words);
66 /// move to next position after current word
68 /// check text until next misspelled/unknown word
71 bool continueFromBeginning();
73 void setLanguage(Language const * lang);
74 /// test and set guard flag
81 void canCheck() { incheck_ = false; }
83 Ui::SpellcheckerUi ui;
85 SpellcheckerWidget * p;
90 /// current word being checked and lang code
101 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
102 : QTabWidget(parent), d(new Private(this, dv))
107 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
108 this, SLOT(on_replacePB_clicked()));
111 QAbstractItemModel * language_model = guiApp->languageModel();
112 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
113 language_model->sort(0);
114 d->ui.languageCO->setModel(language_model);
115 d->ui.languageCO->setModelColumn(1);
117 d->ui.wordED->setReadOnly(true);
119 d->ui.suggestionsLW->installEventFilter(this);
123 SpellcheckerWidget::~SpellcheckerWidget()
129 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
131 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
132 QKeyEvent *e = static_cast<QKeyEvent *> (event);
133 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
134 if (d->ui.suggestionsLW->currentItem()) {
135 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
136 on_replacePB_clicked();
139 } else if (e->key() == Qt::Key_Right) {
140 if (d->ui.suggestionsLW->currentItem())
141 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
145 // standard event processing
146 return QWidget::eventFilter(obj, event);
150 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
152 if (d->ui.replaceCO->count() != 0)
153 d->ui.replaceCO->setItemText(0, item->text());
155 d->ui.replaceCO->addItem(item->text());
157 d->ui.replaceCO->setCurrentIndex(0);
161 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
163 QListWidget * lw = d->ui.suggestionsLW;
164 if (lw->currentItem() && lw->currentItem()->text() == str)
167 for (int i = 0; i != lw->count(); ++i) {
168 if (lw->item(i)->text() == str) {
169 lw->setCurrentRow(i);
176 void SpellcheckerWidget::updateView()
178 BufferView * bv = d->gv_->documentBufferView();
180 if (bv && hasFocus() && d->start_.empty()) {
181 d->start_ = bv->cursor();
187 bool SpellcheckerWidget::Private::continueFromBeginning()
189 QMessageBox::StandardButton const answer = QMessageBox::question(p,
190 qt_("Spell Checker"),
191 qt_("We reached the end of the document, would you like to "
192 "continue from the beginning?"),
193 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
194 if (answer == QMessageBox::No) {
198 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
204 void SpellcheckerWidget::Private::forward()
206 BufferView * bv = gv_->documentBufferView();
207 DocIterator from = bv->cursor();
209 dispatch(FuncRequest(LFUN_ESCAPE));
210 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
211 if (bv->cursor().depth() <= 1 && bv->cursor().atLastPos()) {
212 continueFromBeginning();
215 if (from == bv->cursor()) {
216 //FIXME we must be at the end of a cell
217 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
219 if (wrap_around_ && start_ < bv->cursor()) {
225 void SpellcheckerWidget::on_languageCO_activated(int index)
228 fromqstr(d->ui.languageCO->itemData(index).toString());
229 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
232 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
237 bool SpellcheckerWidget::initialiseParams(std::string const &)
239 BufferView * bv = d->gv_->documentBufferView();
242 std::set<Language const *> languages =
243 bv->buffer().masterBuffer()->getLanguages();
244 if (!languages.empty())
245 d->setLanguage(*languages.begin());
246 d->start_ = DocIterator();
247 d->wrap_around_ = false;
253 void SpellcheckerWidget::on_ignoreAllPB_clicked()
255 /// ignore all occurrences of word
258 LYXERR(Debug::GUI, "Spellchecker: ignore all button");
259 if (d->word_.lang() && !d->word_.word().empty())
260 theSpellChecker()->accept(d->word_);
267 void SpellcheckerWidget::on_addPB_clicked()
269 /// insert word in personal dictionary
272 LYXERR(Debug::GUI, "Spellchecker: add word button");
273 theSpellChecker()->insert(d->word_);
280 void SpellcheckerWidget::on_ignorePB_clicked()
282 /// ignore this occurrence of word
285 LYXERR(Debug::GUI, "Spellchecker: ignore button");
292 void SpellcheckerWidget::on_findNextPB_clicked()
296 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
297 docstring const datastring = find2string(textfield,
299 LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
300 dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
305 void SpellcheckerWidget::on_replacePB_clicked()
309 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
310 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
311 docstring const datastring = replace2string(replacement, textfield,
312 true, true, false, false);
314 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
315 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
322 void SpellcheckerWidget::on_replaceAllPB_clicked()
326 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
327 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
328 docstring const datastring = replace2string(replacement, textfield,
329 true, true, true, true);
331 LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
332 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
334 d->check(); // continue spellchecking
339 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
341 QString const suggestion = toqstr(word_.word());
342 ui.wordED->setText(suggestion);
343 QListWidget * lw = ui.suggestionsLW;
347 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
350 for (size_t i = 0; i != words.size(); ++i)
351 lw->addItem(toqstr(words[i]));
353 p->on_suggestionsLW_itemClicked(lw->item(0));
354 lw->setCurrentRow(0);
358 void SpellcheckerWidget::Private::setLanguage(Language const * lang)
360 int const pos = ui.languageCO->findData(toqstr(lang->lang()));
362 ui.languageCO->setCurrentIndex(pos);
366 void SpellcheckerWidget::Private::check()
368 BufferView * bv = gv_->documentBufferView();
369 if (!bv || bv->buffer().text().empty())
372 DocIterator from = bv->cursor();
374 WordLangTuple word_lang;
375 docstring_list suggestions;
377 LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
380 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
381 } catch (ExceptionMessage const & message) {
382 if (message.type_ == WarningException) {
383 Alert::warning(message.title_, message.details_);
390 if (from == doc_iterator_end(&bv->buffer())) {
391 if (wrap_around_ || start_ == doc_iterator_begin(&bv->buffer())) {
395 if (continueFromBeginning())
400 if (wrap_around_ && start_ < from) {
408 updateSuggestions(suggestions);
410 setLanguage(word_lang.lang());
413 // If we used a LFUN, dispatch would do all of this for us
414 int const size = to.pos() - from.pos();
415 bv->putSelectionAt(from, size, false);
416 bv->processUpdateFlags(Update::Force | Update::FitCursor);
420 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
421 Qt::DockWidgetArea area, Qt::WindowFlags flags)
422 : DockView(parent, "spellchecker", qt_("Spellchecker"),
425 widget_ = new SpellcheckerWidget(&parent, this);
427 setFocusProxy(widget_);
431 GuiSpellchecker::~GuiSpellchecker()
438 void GuiSpellchecker::updateView()
440 widget_->updateView();
444 Dialog * createGuiSpellchecker(GuiView & lv)
446 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
448 gui->setFloating(true);
454 } // namespace frontend
457 #include "moc_GuiSpellchecker.cpp"