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 #if QT_VERSION >= 0x040600
85 : QTabWidget(parent), d(new Private(this))
87 : QWidget(parent), d(new Private(this))
93 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
94 this, SLOT(on_replacePB_clicked()));
97 QAbstractItemModel * language_model = guiApp->languageModel();
98 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
99 language_model->sort(0);
100 d->ui.languageCO->setModel(language_model);
101 d->ui.languageCO->setModelColumn(1);
103 d->ui.wordED->setReadOnly(true);
105 d->ui.suggestionsLW->installEventFilter(this);
109 SpellcheckerWidget::~SpellcheckerWidget()
115 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
117 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
118 QKeyEvent *e = static_cast<QKeyEvent *> (event);
119 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
120 if (d->ui.suggestionsLW->currentItem()) {
121 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
122 on_replacePB_clicked();
125 } else if (e->key() == Qt::Key_Right) {
126 if (d->ui.suggestionsLW->currentItem())
127 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
131 // standard event processing
132 return QWidget::eventFilter(obj, event);
136 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
138 if (d->ui.replaceCO->count() != 0)
139 d->ui.replaceCO->setItemText(0, item->text());
141 d->ui.replaceCO->addItem(item->text());
143 d->ui.replaceCO->setCurrentIndex(0);
147 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
149 QListWidget * lw = d->ui.suggestionsLW;
150 if (lw->currentItem() && lw->currentItem()->text() == str)
153 for (int i = 0; i != lw->count(); ++i) {
154 if (lw->item(i)->text() == str) {
155 lw->setCurrentRow(i);
162 void SpellcheckerWidget::updateView()
164 BufferView * bv = d->gv_->documentBufferView();
166 if (bv && hasFocus())
171 bool SpellcheckerWidget::Private::continueFromBeginning()
173 QMessageBox::StandardButton const answer = QMessageBox::question(p,
174 qt_("Spell Checker"),
175 qt_("We reached the end of the document, would you like to "
176 "continue from the beginning?"),
177 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
178 if (answer == QMessageBox::No)
180 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
185 void SpellcheckerWidget::Private::forward()
187 BufferView * bv = gv_->documentBufferView();
188 DocIterator from = bv->cursor();
190 dispatch(FuncRequest(LFUN_ESCAPE));
191 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
192 if (bv->cursor().atLastPos()) {
193 continueFromBeginning();
196 if (from == bv->cursor()) {
197 //FIXME we must be at the end of a cell
198 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
203 void SpellcheckerWidget::on_languageCO_activated(int index)
206 fromqstr(d->ui.languageCO->itemData(index).toString());
207 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
210 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
215 void SpellcheckerWidget::on_ignoreAllPB_clicked()
217 /// replace all occurrences of word
218 if (d->word_.lang() && !d->word_.word().empty())
219 theSpellChecker()->accept(d->word_);
225 void SpellcheckerWidget::on_addPB_clicked()
227 /// insert word in personal dictionary
228 theSpellChecker()->insert(d->word_);
234 void SpellcheckerWidget::on_ignorePB_clicked()
241 void SpellcheckerWidget::on_findNextPB_clicked()
243 docstring const data = find2string(
244 qstring_to_ucs4(d->ui.wordED->text()),
246 dispatch(FuncRequest(LFUN_WORD_FIND, data));
250 void SpellcheckerWidget::on_replacePB_clicked()
252 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
253 docstring const data = replace2string(
254 replacement, qstring_to_ucs4(d->ui.wordED->text()),
255 true, true, false, false);
257 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
258 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
264 void SpellcheckerWidget::on_replaceAllPB_clicked()
266 docstring const data = replace2string(
267 qstring_to_ucs4(d->ui.replaceCO->currentText()),
268 qstring_to_ucs4(d->ui.wordED->text()),
269 true, true, true, true);
270 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
272 d->check(); // continue spellchecking
276 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
278 QString const suggestion = toqstr(word_.word());
279 ui.wordED->setText(suggestion);
280 QListWidget * lw = ui.suggestionsLW;
284 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
287 for (size_t i = 0; i != words.size(); ++i)
288 lw->addItem(toqstr(words[i]));
290 p->on_suggestionsLW_itemClicked(lw->item(0));
291 lw->setCurrentRow(0);
295 void SpellcheckerWidget::Private::check()
297 BufferView * bv = gv_->documentBufferView();
298 if (!bv || bv->buffer().text().empty())
301 DocIterator from = bv->cursor();
303 WordLangTuple word_lang;
304 docstring_list suggestions;
308 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
309 } catch (ExceptionMessage const & message) {
310 if (message.type_ == WarningException) {
311 Alert::warning(message.title_, message.details_);
318 if (from == doc_iterator_end(&bv->buffer())) {
319 if (continueFromBeginning())
327 updateSuggestions(suggestions);
329 int const pos = ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
331 ui.languageCO->setCurrentIndex(pos);
334 // If we used a LFUN, dispatch would do all of this for us
335 int const size = to.pos() - from.pos();
336 bv->putSelectionAt(from, size, false);
337 bv->processUpdateFlags(Update::Force | Update::FitCursor);
341 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
342 Qt::DockWidgetArea area, Qt::WindowFlags flags)
343 : DockView(parent, "spellchecker", qt_("Spellchecker"),
346 widget_ = new SpellcheckerWidget(&parent);
348 setFocusProxy(widget_);
352 GuiSpellchecker::~GuiSpellchecker()
359 void GuiSpellchecker::updateView()
361 widget_->updateView();
365 Dialog * createGuiSpellchecker(GuiView & lv)
367 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
369 gui->setFloating(true);
375 } // namespace frontend
378 #include "moc_GuiSpellchecker.cpp"