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), start_(true), incheck_(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
99 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
100 : QTabWidget(parent), d(new Private(this, dv))
105 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
106 this, SLOT(on_replacePB_clicked()));
109 QAbstractItemModel * language_model = guiApp->languageModel();
110 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
111 language_model->sort(0);
112 d->ui.languageCO->setModel(language_model);
113 d->ui.languageCO->setModelColumn(1);
115 d->ui.wordED->setReadOnly(true);
117 d->ui.suggestionsLW->installEventFilter(this);
121 SpellcheckerWidget::~SpellcheckerWidget()
127 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
129 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
130 QKeyEvent *e = static_cast<QKeyEvent *> (event);
131 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
132 if (d->ui.suggestionsLW->currentItem()) {
133 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
134 on_replacePB_clicked();
137 } else if (e->key() == Qt::Key_Right) {
138 if (d->ui.suggestionsLW->currentItem())
139 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
143 // standard event processing
144 return QWidget::eventFilter(obj, event);
148 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
150 if (d->ui.replaceCO->count() != 0)
151 d->ui.replaceCO->setItemText(0, item->text());
153 d->ui.replaceCO->addItem(item->text());
155 d->ui.replaceCO->setCurrentIndex(0);
159 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
161 QListWidget * lw = d->ui.suggestionsLW;
162 if (lw->currentItem() && lw->currentItem()->text() == str)
165 for (int i = 0; i != lw->count(); ++i) {
166 if (lw->item(i)->text() == str) {
167 lw->setCurrentRow(i);
174 void SpellcheckerWidget::updateView()
176 BufferView * bv = d->gv_->documentBufferView();
178 if (bv && hasFocus() && d->start_) {
181 BufferView * bv = d->gv_->documentBufferView();
182 std::set<Language const *> languages =
183 bv->buffer().masterBuffer()->getLanguages();
184 if (!languages.empty())
185 d->setLanguage(*languages.begin());
192 bool SpellcheckerWidget::Private::continueFromBeginning()
194 QMessageBox::StandardButton const answer = QMessageBox::question(p,
195 qt_("Spell Checker"),
196 qt_("We reached the end of the document, would you like to "
197 "continue from the beginning?"),
198 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
199 if (answer == QMessageBox::No) {
203 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
208 void SpellcheckerWidget::Private::forward()
210 BufferView * bv = gv_->documentBufferView();
211 DocIterator from = bv->cursor();
213 dispatch(FuncRequest(LFUN_ESCAPE));
214 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
215 if (bv->cursor().depth() <= 1 && bv->cursor().atLastPos()) {
216 continueFromBeginning();
219 if (from == bv->cursor()) {
220 //FIXME we must be at the end of a cell
221 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
226 void SpellcheckerWidget::on_languageCO_activated(int index)
229 fromqstr(d->ui.languageCO->itemData(index).toString());
230 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
233 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
238 bool SpellcheckerWidget::initialiseParams(std::string const &)
245 void SpellcheckerWidget::on_ignoreAllPB_clicked()
247 /// ignore all occurrences of word
250 LYXERR(Debug::GUI, "Spellchecker: ignore all button");
251 if (d->word_.lang() && !d->word_.word().empty())
252 theSpellChecker()->accept(d->word_);
259 void SpellcheckerWidget::on_addPB_clicked()
261 /// insert word in personal dictionary
264 LYXERR(Debug::GUI, "Spellchecker: add word button");
265 theSpellChecker()->insert(d->word_);
272 void SpellcheckerWidget::on_ignorePB_clicked()
274 /// ignore this occurrence of word
277 LYXERR(Debug::GUI, "Spellchecker: ignore button");
284 void SpellcheckerWidget::on_findNextPB_clicked()
288 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
289 docstring const datastring = find2string(textfield,
291 LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
292 dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
297 void SpellcheckerWidget::on_replacePB_clicked()
301 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
302 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
303 docstring const datastring = replace2string(replacement, textfield,
304 true, true, false, false);
306 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
307 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
314 void SpellcheckerWidget::on_replaceAllPB_clicked()
318 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
319 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
320 docstring const datastring = replace2string(replacement, textfield,
321 true, true, true, true);
323 LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
324 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
326 d->check(); // continue spellchecking
331 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
333 QString const suggestion = toqstr(word_.word());
334 ui.wordED->setText(suggestion);
335 QListWidget * lw = ui.suggestionsLW;
339 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
342 for (size_t i = 0; i != words.size(); ++i)
343 lw->addItem(toqstr(words[i]));
345 p->on_suggestionsLW_itemClicked(lw->item(0));
346 lw->setCurrentRow(0);
350 void SpellcheckerWidget::Private::setLanguage(Language const * lang)
352 int const pos = ui.languageCO->findData(toqstr(lang->lang()));
354 ui.languageCO->setCurrentIndex(pos);
358 void SpellcheckerWidget::Private::check()
360 BufferView * bv = gv_->documentBufferView();
361 if (!bv || bv->buffer().text().empty())
364 DocIterator from = bv->cursor();
366 WordLangTuple word_lang;
367 docstring_list suggestions;
369 LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
372 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
373 } catch (ExceptionMessage const & message) {
374 if (message.type_ == WarningException) {
375 Alert::warning(message.title_, message.details_);
382 if (from == doc_iterator_end(&bv->buffer())) {
383 if (continueFromBeginning())
391 updateSuggestions(suggestions);
393 setLanguage(word_lang.lang());
396 // If we used a LFUN, dispatch would do all of this for us
397 int const size = to.pos() - from.pos();
398 bv->putSelectionAt(from, size, false);
399 bv->processUpdateFlags(Update::Force | Update::FitCursor);
403 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
404 Qt::DockWidgetArea area, Qt::WindowFlags flags)
405 : DockView(parent, "spellchecker", qt_("Spellchecker"),
408 widget_ = new SpellcheckerWidget(&parent, this);
410 setFocusProxy(widget_);
414 GuiSpellchecker::~GuiSpellchecker()
421 void GuiSpellchecker::updateView()
423 widget_->updateView();
427 Dialog * createGuiSpellchecker(GuiView & lv)
429 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
431 gui->setFloating(true);
437 } // namespace frontend
440 #include "moc_GuiSpellchecker.cpp"