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"
44 #include <QListWidgetItem>
47 #include "SpellChecker.h"
49 #include "frontends/alert.h"
52 using namespace lyx::support;
58 struct GuiSpellchecker::Private
60 Private() : progress_(0), count_(0) {}
61 Ui::SpellcheckerUi ui;
62 /// current word being checked and lang code
64 /// values for progress
72 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
73 : DockView(lv, "spellchecker", qt_("Spellchecker"),
74 Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
78 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
79 this, SLOT(on_replacePB_clicked()));
82 QAbstractItemModel * language_model = guiApp->languageModel();
83 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
84 language_model->sort(0);
85 d->ui.languageCO->setModel(language_model);
87 d->ui.wordED->setReadOnly(true);
89 d->ui.suggestionsLW->installEventFilter(this);
93 GuiSpellchecker::~GuiSpellchecker()
99 void GuiSpellchecker::on_closePB_clicked()
105 bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
107 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
108 QKeyEvent *e = static_cast<QKeyEvent *> (event);
109 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
110 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
111 on_replacePB_clicked();
113 } else if (e->key() == Qt::Key_Right) {
114 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
118 // standard event processing
119 return QWidget::eventFilter(obj, event);
123 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
125 if (d->ui.replaceCO->count() != 0)
126 d->ui.replaceCO->setItemText(0, item->text());
128 d->ui.replaceCO->addItem(item->text());
130 d->ui.replaceCO->setCurrentIndex(0);
134 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
136 QListWidget * lw = d->ui.suggestionsLW;
137 if (lw->currentItem() && lw->currentItem()->text() == str)
140 for (int i = 0; i != lw->count(); ++i) {
141 if (lw->item(i)->text() == str) {
142 lw->setCurrentRow(i);
149 void GuiSpellchecker::updateView()
156 void GuiSpellchecker::on_languageCO_activated(int index)
159 fromqstr(d->ui.languageCO->itemData(index).toString());
160 if (d->word_.lang()->lang() == lang)
163 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
168 void GuiSpellchecker::on_ignoreAllPB_clicked()
170 /// replace all occurances of word
171 theSpellChecker()->accept(d->word_);
176 void GuiSpellchecker::on_addPB_clicked()
178 /// insert word in personal dictionary
179 theSpellChecker()->insert(d->word_);
184 void GuiSpellchecker::on_ignorePB_clicked()
186 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
191 void GuiSpellchecker::on_findNextPB_clicked()
193 docstring const data = find2string(
194 qstring_to_ucs4(d->ui.wordED->text()),
196 dispatch(FuncRequest(LFUN_WORD_FIND, data));
200 void GuiSpellchecker::on_replacePB_clicked()
202 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
204 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
205 BufferView * bv = const_cast<BufferView *>(bufferview());
206 if (!bv->cursor().inTexted())
208 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
209 bv->buffer().markDirty();
210 // If we used an LFUN, we would not need that
211 bv->processUpdateFlags(Update::Force | Update::FitCursor);
218 void GuiSpellchecker::on_replaceAllPB_clicked()
220 docstring const data = replace2string(
221 qstring_to_ucs4(d->ui.replaceCO->currentText()),
222 qstring_to_ucs4(d->ui.wordED->text()),
223 true, true, true, true);
224 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
225 check(); // continue spellchecking
229 void GuiSpellchecker::updateSuggestions(docstring_list & words)
231 QString const suggestion = toqstr(d->word_.word());
232 d->ui.wordED->setText(suggestion);
233 QListWidget * lw = d->ui.suggestionsLW;
237 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
240 for (size_t i = 0; i != words.size(); ++i)
241 lw->addItem(toqstr(words[i]));
243 on_suggestionsLW_itemClicked(lw->item(0));
244 lw->setCurrentRow(0);
248 bool GuiSpellchecker::initialiseParams(string const &)
250 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
252 if (!theSpellChecker())
255 DocIterator const begin = doc_iterator_begin(&buffer());
256 Cursor const & cur = bufferview()->cursor();
257 d->progress_ = countWords(begin, cur);
258 d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
264 void GuiSpellchecker::check()
266 LYXERR(Debug::GUI, "Check the spelling of a word");
268 DocIterator from = bufferview()->cursor();
270 WordLangTuple word_lang;
271 docstring_list suggestions;
275 progress = buffer().spellCheck(from, to, word_lang, suggestions);
276 } catch (ExceptionMessage const & message) {
277 if (message.type_ == WarningException) {
278 Alert::warning(message.title_, message.details_);
284 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
285 d->count_ += progress;
286 d->progress_ += progress;
289 if (from == doc_iterator_end(&buffer())) {
296 d->word_ = word_lang;
298 int const progress_bar = d->total_
299 ? int(100.0 * float(d->progress_)/d->total_) : 100;
300 LYXERR(Debug::GUI, "Updating spell progress.");
302 d->ui.spellcheckPR->setValue(progress_bar);
304 updateSuggestions(suggestions);
306 int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
308 d->ui.languageCO->setCurrentIndex(pos);
310 // FIXME: if we used a lfun like in find/replace, dispatch would do
312 int const size = to.pos() - from.pos();
313 BufferView * bv = const_cast<BufferView *>(bufferview());
314 bv->putSelectionAt(from, size, false);
318 void GuiSpellchecker::showSummary()
320 if (d->count_ == 0) {
327 message = bformat(_("%1$d words checked."), d->count_);
329 message = _("One word checked.");
332 Alert::information(_("Spelling check completed"), message);
336 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
338 } // namespace frontend
341 #include "moc_GuiSpellchecker.cpp"