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"
27 #include "CutAndPaste.h"
28 #include "FuncRequest.h"
33 #include "Paragraph.h"
34 #include "WordLangTuple.h"
36 #include "support/debug.h"
37 #include "support/docstring.h"
38 #include "support/docstring_list.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42 #include "support/textutils.h"
45 #include <QListWidgetItem>
46 #include <QMessageBox>
48 #include "SpellChecker.h"
50 #include "frontends/alert.h"
53 using namespace lyx::support;
59 struct SpellcheckerWidget::Private
61 Private(SpellcheckerWidget * parent)
63 /// update from controller
64 void updateSuggestions(docstring_list & words);
65 /// move to next position after current word
67 /// check text until next misspelled/unknown word
70 bool continueFromBeginning();
72 Ui::SpellcheckerUi ui;
74 SpellcheckerWidget * p;
77 /// current word being checked and lang code
82 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, QWidget * parent)
83 : QWidget(parent), d(new Private(this))
88 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
89 this, SLOT(on_replacePB_clicked()));
92 QAbstractItemModel * language_model = guiApp->languageModel();
93 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
94 language_model->sort(0);
95 d->ui.languageCO->setModel(language_model);
96 d->ui.languageCO->setModelColumn(1);
98 d->ui.wordED->setReadOnly(true);
100 d->ui.suggestionsLW->installEventFilter(this);
104 SpellcheckerWidget::~SpellcheckerWidget()
110 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
112 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
113 QKeyEvent *e = static_cast<QKeyEvent *> (event);
114 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
115 if (d->ui.suggestionsLW->currentItem()) {
116 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
117 on_replacePB_clicked();
120 } else if (e->key() == Qt::Key_Right) {
121 if (d->ui.suggestionsLW->currentItem())
122 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
126 // standard event processing
127 return QWidget::eventFilter(obj, event);
131 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
133 if (d->ui.replaceCO->count() != 0)
134 d->ui.replaceCO->setItemText(0, item->text());
136 d->ui.replaceCO->addItem(item->text());
138 d->ui.replaceCO->setCurrentIndex(0);
142 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
144 QListWidget * lw = d->ui.suggestionsLW;
145 if (lw->currentItem() && lw->currentItem()->text() == str)
148 for (int i = 0; i != lw->count(); ++i) {
149 if (lw->item(i)->text() == str) {
150 lw->setCurrentRow(i);
157 void SpellcheckerWidget::updateView()
159 BufferView * bv = d->gv_->documentBufferView();
161 if (bv && hasFocus())
166 bool SpellcheckerWidget::Private::continueFromBeginning()
168 QMessageBox::StandardButton const answer = QMessageBox::question(p,
169 qt_("Spell Checker"),
170 qt_("We reached the end of the document, would you like to "
171 "continue from the beginning?"),
172 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
173 if (answer == QMessageBox::No)
175 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
180 void SpellcheckerWidget::Private::forward()
182 BufferView * bv = gv_->documentBufferView();
183 DocIterator from = bv->cursor();
185 dispatch(FuncRequest(LFUN_ESCAPE));
186 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
187 if (bv->cursor().atLastPos()) {
188 continueFromBeginning();
191 if (from == bv->cursor()) {
192 //FIXME we must be at the end of a cell
193 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
198 void SpellcheckerWidget::on_languageCO_activated(int index)
201 fromqstr(d->ui.languageCO->itemData(index).toString());
202 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
205 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
210 void SpellcheckerWidget::on_ignoreAllPB_clicked()
212 /// replace all occurrences of word
213 if (d->word_.lang() && !d->word_.word().empty())
214 theSpellChecker()->accept(d->word_);
220 void SpellcheckerWidget::on_addPB_clicked()
222 /// insert word in personal dictionary
223 theSpellChecker()->insert(d->word_);
229 void SpellcheckerWidget::on_ignorePB_clicked()
236 void SpellcheckerWidget::on_findNextPB_clicked()
238 docstring const data = find2string(
239 qstring_to_ucs4(d->ui.wordED->text()),
241 dispatch(FuncRequest(LFUN_WORD_FIND, data));
245 void SpellcheckerWidget::on_replacePB_clicked()
247 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
248 docstring const data = replace2string(
249 replacement, qstring_to_ucs4(d->ui.wordED->text()),
250 true, true, false, false);
252 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
253 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
259 void SpellcheckerWidget::on_replaceAllPB_clicked()
261 docstring const data = replace2string(
262 qstring_to_ucs4(d->ui.replaceCO->currentText()),
263 qstring_to_ucs4(d->ui.wordED->text()),
264 true, true, true, true);
265 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
267 d->check(); // continue spellchecking
271 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
273 QString const suggestion = toqstr(word_.word());
274 ui.wordED->setText(suggestion);
275 QListWidget * lw = ui.suggestionsLW;
279 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
282 for (size_t i = 0; i != words.size(); ++i)
283 lw->addItem(toqstr(words[i]));
285 p->on_suggestionsLW_itemClicked(lw->item(0));
286 lw->setCurrentRow(0);
290 void SpellcheckerWidget::Private::check()
292 BufferView * bv = gv_->documentBufferView();
296 DocIterator from = bv->cursor();
298 WordLangTuple word_lang;
299 docstring_list suggestions;
303 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
304 } catch (ExceptionMessage const & message) {
305 if (message.type_ == WarningException) {
306 Alert::warning(message.title_, message.details_);
313 if (from == doc_iterator_end(&bv->buffer())) {
314 if (continueFromBeginning())
322 updateSuggestions(suggestions);
324 int const pos = ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
326 ui.languageCO->setCurrentIndex(pos);
329 // If we used a LFUN, dispatch would do all of this for us
330 int const size = to.pos() - from.pos();
331 bv->putSelectionAt(from, size, false);
332 bv->processUpdateFlags(Update::Force | Update::FitCursor);
336 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
337 Qt::DockWidgetArea area, Qt::WindowFlags flags)
338 : DockView(parent, "spellchecker", qt_("Spellchecker"),
341 widget_ = new SpellcheckerWidget(&parent);
343 setFocusProxy(widget_);
347 GuiSpellchecker::~GuiSpellchecker()
354 void GuiSpellchecker::updateView()
356 widget_->updateView();
360 Dialog * createGuiSpellchecker(GuiView & lv)
362 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
364 gui->setFloating(true);
370 } // namespace frontend
373 #include "moc_GuiSpellchecker.cpp"