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)
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 Ui::SpellcheckerUi ui;
75 SpellcheckerWidget * p;
78 /// current word being checked and lang code
83 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, QWidget * parent)
84 : QTabWidget(parent), d(new Private(this))
89 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
90 this, SLOT(on_replacePB_clicked()));
93 QAbstractItemModel * language_model = guiApp->languageModel();
94 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
95 language_model->sort(0);
96 d->ui.languageCO->setModel(language_model);
97 d->ui.languageCO->setModelColumn(1);
99 d->ui.wordED->setReadOnly(true);
101 d->ui.suggestionsLW->installEventFilter(this);
105 SpellcheckerWidget::~SpellcheckerWidget()
111 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
113 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
114 QKeyEvent *e = static_cast<QKeyEvent *> (event);
115 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
116 if (d->ui.suggestionsLW->currentItem()) {
117 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
118 on_replacePB_clicked();
121 } else if (e->key() == Qt::Key_Right) {
122 if (d->ui.suggestionsLW->currentItem())
123 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
127 // standard event processing
128 return QWidget::eventFilter(obj, event);
132 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
134 if (d->ui.replaceCO->count() != 0)
135 d->ui.replaceCO->setItemText(0, item->text());
137 d->ui.replaceCO->addItem(item->text());
139 d->ui.replaceCO->setCurrentIndex(0);
143 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
145 QListWidget * lw = d->ui.suggestionsLW;
146 if (lw->currentItem() && lw->currentItem()->text() == str)
149 for (int i = 0; i != lw->count(); ++i) {
150 if (lw->item(i)->text() == str) {
151 lw->setCurrentRow(i);
158 void SpellcheckerWidget::updateView()
160 BufferView * bv = d->gv_->documentBufferView();
162 if (bv && hasFocus())
167 bool SpellcheckerWidget::Private::continueFromBeginning()
169 QMessageBox::StandardButton const answer = QMessageBox::question(p,
170 qt_("Spell Checker"),
171 qt_("We reached the end of the document, would you like to "
172 "continue from the beginning?"),
173 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
174 if (answer == QMessageBox::No)
176 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
181 void SpellcheckerWidget::Private::forward()
183 BufferView * bv = gv_->documentBufferView();
184 DocIterator from = bv->cursor();
186 dispatch(FuncRequest(LFUN_ESCAPE));
187 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
188 if (bv->cursor().atLastPos()) {
189 continueFromBeginning();
192 if (from == bv->cursor()) {
193 //FIXME we must be at the end of a cell
194 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
199 void SpellcheckerWidget::on_languageCO_activated(int index)
202 fromqstr(d->ui.languageCO->itemData(index).toString());
203 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
206 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
211 void SpellcheckerWidget::on_ignoreAllPB_clicked()
213 /// replace all occurrences of word
214 if (d->word_.lang() && !d->word_.word().empty())
215 theSpellChecker()->accept(d->word_);
221 void SpellcheckerWidget::on_addPB_clicked()
223 /// insert word in personal dictionary
224 theSpellChecker()->insert(d->word_);
230 void SpellcheckerWidget::on_ignorePB_clicked()
237 void SpellcheckerWidget::on_findNextPB_clicked()
239 docstring const data = find2string(
240 qstring_to_ucs4(d->ui.wordED->text()),
242 dispatch(FuncRequest(LFUN_WORD_FIND, data));
246 void SpellcheckerWidget::on_replacePB_clicked()
248 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
249 docstring const data = replace2string(
250 replacement, qstring_to_ucs4(d->ui.wordED->text()),
251 true, true, false, false);
253 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
254 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
260 void SpellcheckerWidget::on_replaceAllPB_clicked()
262 docstring const data = replace2string(
263 qstring_to_ucs4(d->ui.replaceCO->currentText()),
264 qstring_to_ucs4(d->ui.wordED->text()),
265 true, true, true, true);
266 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
268 d->check(); // continue spellchecking
272 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
274 QString const suggestion = toqstr(word_.word());
275 ui.wordED->setText(suggestion);
276 QListWidget * lw = ui.suggestionsLW;
280 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
283 for (size_t i = 0; i != words.size(); ++i)
284 lw->addItem(toqstr(words[i]));
286 p->on_suggestionsLW_itemClicked(lw->item(0));
287 lw->setCurrentRow(0);
291 void SpellcheckerWidget::Private::check()
293 BufferView * bv = gv_->documentBufferView();
294 if (!bv || bv->buffer().text().empty())
297 DocIterator from = bv->cursor();
299 WordLangTuple word_lang;
300 docstring_list suggestions;
304 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
305 } catch (ExceptionMessage const & message) {
306 if (message.type_ == WarningException) {
307 Alert::warning(message.title_, message.details_);
314 if (from == doc_iterator_end(&bv->buffer())) {
315 if (continueFromBeginning())
323 updateSuggestions(suggestions);
325 int const pos = ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
327 ui.languageCO->setCurrentIndex(pos);
330 // If we used a LFUN, dispatch would do all of this for us
331 int const size = to.pos() - from.pos();
332 bv->putSelectionAt(from, size, false);
333 bv->processUpdateFlags(Update::Force | Update::FitCursor);
337 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
338 Qt::DockWidgetArea area, Qt::WindowFlags flags)
339 : DockView(parent, "spellchecker", qt_("Spellchecker"),
342 widget_ = new SpellcheckerWidget(&parent);
344 setFocusProxy(widget_);
348 GuiSpellchecker::~GuiSpellchecker()
355 void GuiSpellchecker::updateView()
357 widget_->updateView();
361 Dialog * createGuiSpellchecker(GuiView & lv)
363 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
365 gui->setFloating(true);
371 } // namespace frontend
374 #include "moc_GuiSpellchecker.cpp"