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; }
82 /// check for wrap around of current position
83 bool isWrapAround(DocIterator cursor) const;
85 Ui::SpellcheckerUi ui;
87 SpellcheckerWidget * p;
92 /// current word being checked and lang code
103 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
104 : QTabWidget(parent), d(new Private(this, dv))
109 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
110 this, SLOT(on_replacePB_clicked()));
113 QAbstractItemModel * language_model = guiApp->languageModel();
114 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
115 language_model->sort(0);
116 d->ui.languageCO->setModel(language_model);
117 d->ui.languageCO->setModelColumn(1);
119 d->ui.wordED->setReadOnly(true);
121 d->ui.suggestionsLW->installEventFilter(this);
125 SpellcheckerWidget::~SpellcheckerWidget()
131 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
133 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
134 QKeyEvent *e = static_cast<QKeyEvent *> (event);
135 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
136 if (d->ui.suggestionsLW->currentItem()) {
137 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
138 on_replacePB_clicked();
141 } else if (e->key() == Qt::Key_Right) {
142 if (d->ui.suggestionsLW->currentItem())
143 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
147 // standard event processing
148 return QWidget::eventFilter(obj, event);
152 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
154 if (d->ui.replaceCO->count() != 0)
155 d->ui.replaceCO->setItemText(0, item->text());
157 d->ui.replaceCO->addItem(item->text());
159 d->ui.replaceCO->setCurrentIndex(0);
163 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
165 QListWidget * lw = d->ui.suggestionsLW;
166 if (lw->currentItem() && lw->currentItem()->text() == str)
169 for (int i = 0; i != lw->count(); ++i) {
170 if (lw->item(i)->text() == str) {
171 lw->setCurrentRow(i);
178 void SpellcheckerWidget::updateView()
180 BufferView * bv = d->gv_->documentBufferView();
182 if (bv && hasFocus() && d->start_.empty()) {
183 d->start_ = bv->cursor();
189 bool SpellcheckerWidget::Private::continueFromBeginning()
191 QMessageBox::StandardButton const answer = QMessageBox::question(p,
192 qt_("Spell Checker"),
193 qt_("We reached the end of the document, would you like to "
194 "continue from the beginning?"),
195 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
196 if (answer == QMessageBox::No) {
200 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
205 bool SpellcheckerWidget::Private::isWrapAround(DocIterator cursor) const
207 return wrap_around_ && start_.buffer() == cursor.buffer() && start_ < cursor;
211 void SpellcheckerWidget::Private::forward()
213 BufferView * bv = gv_->documentBufferView();
214 DocIterator from = bv->cursor();
216 dispatch(FuncRequest(LFUN_ESCAPE));
217 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
218 if (bv->cursor().depth() <= 1 && bv->cursor().atLastPos()) {
219 continueFromBeginning();
222 if (from == bv->cursor()) {
223 //FIXME we must be at the end of a cell
224 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
226 if (isWrapAround(bv->cursor())) {
232 void SpellcheckerWidget::on_languageCO_activated(int index)
235 fromqstr(d->ui.languageCO->itemData(index).toString());
236 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
239 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
244 bool SpellcheckerWidget::initialiseParams(std::string const &)
246 BufferView * bv = d->gv_->documentBufferView();
249 std::set<Language const *> languages =
250 bv->buffer().masterBuffer()->getLanguages();
251 if (!languages.empty())
252 d->setLanguage(*languages.begin());
253 d->start_ = DocIterator();
254 d->wrap_around_ = false;
260 void SpellcheckerWidget::on_ignoreAllPB_clicked()
262 /// ignore all occurrences of word
265 LYXERR(Debug::GUI, "Spellchecker: ignore all button");
266 if (d->word_.lang() && !d->word_.word().empty())
267 theSpellChecker()->accept(d->word_);
274 void SpellcheckerWidget::on_addPB_clicked()
276 /// insert word in personal dictionary
279 LYXERR(Debug::GUI, "Spellchecker: add word button");
280 theSpellChecker()->insert(d->word_);
287 void SpellcheckerWidget::on_ignorePB_clicked()
289 /// ignore this occurrence of word
292 LYXERR(Debug::GUI, "Spellchecker: ignore button");
299 void SpellcheckerWidget::on_findNextPB_clicked()
303 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
304 docstring const datastring = find2string(textfield,
306 LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
307 dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
312 void SpellcheckerWidget::on_replacePB_clicked()
316 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
317 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
318 docstring const datastring = replace2string(replacement, textfield,
319 true, true, false, false);
321 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
322 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
329 void SpellcheckerWidget::on_replaceAllPB_clicked()
333 docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
334 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
335 docstring const datastring = replace2string(replacement, textfield,
336 true, true, true, true);
338 LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
339 dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
341 d->check(); // continue spellchecking
346 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
348 QString const suggestion = toqstr(word_.word());
349 ui.wordED->setText(suggestion);
350 QListWidget * lw = ui.suggestionsLW;
354 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
357 for (size_t i = 0; i != words.size(); ++i)
358 lw->addItem(toqstr(words[i]));
360 p->on_suggestionsLW_itemClicked(lw->item(0));
361 lw->setCurrentRow(0);
365 void SpellcheckerWidget::Private::setLanguage(Language const * lang)
367 int const pos = ui.languageCO->findData(toqstr(lang->lang()));
369 ui.languageCO->setCurrentIndex(pos);
373 void SpellcheckerWidget::Private::check()
375 BufferView * bv = gv_->documentBufferView();
376 if (!bv || bv->buffer().text().empty())
379 DocIterator from = bv->cursor();
381 WordLangTuple word_lang;
382 docstring_list suggestions;
384 LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
387 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
388 } catch (ExceptionMessage const & message) {
389 if (message.type_ == WarningException) {
390 Alert::warning(message.title_, message.details_);
397 if (from == doc_iterator_end(&bv->buffer())) {
398 if (wrap_around_ || start_ == doc_iterator_begin(&bv->buffer())) {
402 if (continueFromBeginning())
407 if (isWrapAround(from)) {
415 updateSuggestions(suggestions);
417 setLanguage(word_lang.lang());
420 // If we used a LFUN, dispatch would do all of this for us
421 int const size = to.pos() - from.pos();
422 bv->putSelectionAt(from, size, false);
423 bv->processUpdateFlags(Update::Force | Update::FitCursor);
427 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
428 Qt::DockWidgetArea area, Qt::WindowFlags flags)
429 : DockView(parent, "spellchecker", qt_("Spellchecker"),
432 widget_ = new SpellcheckerWidget(&parent, this);
434 setFocusProxy(widget_);
438 GuiSpellchecker::~GuiSpellchecker()
445 void GuiSpellchecker::updateView()
447 widget_->updateView();
451 Dialog * createGuiSpellchecker(GuiView & lv)
453 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
455 gui->setFloating(true);
461 } // namespace frontend
464 #include "moc_GuiSpellchecker.cpp"