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 void setLanguage(Language const * lang);
75 Ui::SpellcheckerUi ui;
77 SpellcheckerWidget * p;
80 /// current word being checked and lang code
85 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, QWidget * parent)
86 : QTabWidget(parent), d(new Private(this))
91 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
92 this, SLOT(on_replacePB_clicked()));
95 QAbstractItemModel * language_model = guiApp->languageModel();
96 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
97 language_model->sort(0);
98 d->ui.languageCO->setModel(language_model);
99 d->ui.languageCO->setModelColumn(1);
101 d->ui.wordED->setReadOnly(true);
103 d->ui.suggestionsLW->installEventFilter(this);
107 SpellcheckerWidget::~SpellcheckerWidget()
113 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
115 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
116 QKeyEvent *e = static_cast<QKeyEvent *> (event);
117 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
118 if (d->ui.suggestionsLW->currentItem()) {
119 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
120 on_replacePB_clicked();
123 } else if (e->key() == Qt::Key_Right) {
124 if (d->ui.suggestionsLW->currentItem())
125 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
129 // standard event processing
130 return QWidget::eventFilter(obj, event);
134 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
136 if (d->ui.replaceCO->count() != 0)
137 d->ui.replaceCO->setItemText(0, item->text());
139 d->ui.replaceCO->addItem(item->text());
141 d->ui.replaceCO->setCurrentIndex(0);
145 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
147 QListWidget * lw = d->ui.suggestionsLW;
148 if (lw->currentItem() && lw->currentItem()->text() == str)
151 for (int i = 0; i != lw->count(); ++i) {
152 if (lw->item(i)->text() == str) {
153 lw->setCurrentRow(i);
160 void SpellcheckerWidget::updateView()
162 BufferView * bv = d->gv_->documentBufferView();
164 if (bv && hasFocus()) {
166 BufferView * bv = d->gv_->documentBufferView();
167 std::set<Language const *> languages =
168 bv->buffer().masterBuffer()->getLanguages();
169 if (!languages.empty())
170 d->setLanguage(*languages.begin());
177 bool SpellcheckerWidget::Private::continueFromBeginning()
179 QMessageBox::StandardButton const answer = QMessageBox::question(p,
180 qt_("Spell Checker"),
181 qt_("We reached the end of the document, would you like to "
182 "continue from the beginning?"),
183 QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
184 if (answer == QMessageBox::No)
186 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
191 void SpellcheckerWidget::Private::forward()
193 BufferView * bv = gv_->documentBufferView();
194 DocIterator from = bv->cursor();
196 dispatch(FuncRequest(LFUN_ESCAPE));
197 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
198 if (bv->cursor().atLastPos()) {
199 continueFromBeginning();
202 if (from == bv->cursor()) {
203 //FIXME we must be at the end of a cell
204 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
209 void SpellcheckerWidget::on_languageCO_activated(int index)
212 fromqstr(d->ui.languageCO->itemData(index).toString());
213 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
216 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
221 void SpellcheckerWidget::on_ignoreAllPB_clicked()
223 /// replace all occurrences of word
224 if (d->word_.lang() && !d->word_.word().empty())
225 theSpellChecker()->accept(d->word_);
231 void SpellcheckerWidget::on_addPB_clicked()
233 /// insert word in personal dictionary
234 theSpellChecker()->insert(d->word_);
240 void SpellcheckerWidget::on_ignorePB_clicked()
247 void SpellcheckerWidget::on_findNextPB_clicked()
249 docstring const data = find2string(
250 qstring_to_ucs4(d->ui.wordED->text()),
252 dispatch(FuncRequest(LFUN_WORD_FIND, data));
256 void SpellcheckerWidget::on_replacePB_clicked()
258 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
259 docstring const data = replace2string(
260 replacement, qstring_to_ucs4(d->ui.wordED->text()),
261 true, true, false, false);
263 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
264 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
270 void SpellcheckerWidget::on_replaceAllPB_clicked()
272 docstring const data = replace2string(
273 qstring_to_ucs4(d->ui.replaceCO->currentText()),
274 qstring_to_ucs4(d->ui.wordED->text()),
275 true, true, true, true);
276 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
278 d->check(); // continue spellchecking
282 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
284 QString const suggestion = toqstr(word_.word());
285 ui.wordED->setText(suggestion);
286 QListWidget * lw = ui.suggestionsLW;
290 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
293 for (size_t i = 0; i != words.size(); ++i)
294 lw->addItem(toqstr(words[i]));
296 p->on_suggestionsLW_itemClicked(lw->item(0));
297 lw->setCurrentRow(0);
301 void SpellcheckerWidget::Private::setLanguage(Language const * lang)
303 int const pos = ui.languageCO->findData(toqstr(lang->lang()));
305 ui.languageCO->setCurrentIndex(pos);
309 void SpellcheckerWidget::Private::check()
311 BufferView * bv = gv_->documentBufferView();
312 if (!bv || bv->buffer().text().empty())
315 DocIterator from = bv->cursor();
317 WordLangTuple word_lang;
318 docstring_list suggestions;
322 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
323 } catch (ExceptionMessage const & message) {
324 if (message.type_ == WarningException) {
325 Alert::warning(message.title_, message.details_);
332 if (from == doc_iterator_end(&bv->buffer())) {
333 if (continueFromBeginning())
341 updateSuggestions(suggestions);
343 setLanguage(word_lang.lang());
346 // If we used a LFUN, dispatch would do all of this for us
347 int const size = to.pos() - from.pos();
348 bv->putSelectionAt(from, size, false);
349 bv->processUpdateFlags(Update::Force | Update::FitCursor);
353 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
354 Qt::DockWidgetArea area, Qt::WindowFlags flags)
355 : DockView(parent, "spellchecker", qt_("Spellchecker"),
358 widget_ = new SpellcheckerWidget(&parent);
360 setFocusProxy(widget_);
364 GuiSpellchecker::~GuiSpellchecker()
371 void GuiSpellchecker::updateView()
373 widget_->updateView();
377 Dialog * createGuiSpellchecker(GuiView & lv)
379 GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
381 gui->setFloating(true);
387 } // namespace frontend
390 #include "moc_GuiSpellchecker.cpp"