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);
86 d->ui.languageCO->setModelColumn(1);
88 d->ui.wordED->setReadOnly(true);
90 d->ui.suggestionsLW->installEventFilter(this);
94 GuiSpellchecker::~GuiSpellchecker()
100 void GuiSpellchecker::on_closePB_clicked()
106 bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
108 if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
109 QKeyEvent *e = static_cast<QKeyEvent *> (event);
110 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
111 if (d->ui.suggestionsLW->currentItem()) {
112 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
113 on_replacePB_clicked();
116 } else if (e->key() == Qt::Key_Right) {
117 if (d->ui.suggestionsLW->currentItem())
118 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
122 // standard event processing
123 return QWidget::eventFilter(obj, event);
127 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
129 if (d->ui.replaceCO->count() != 0)
130 d->ui.replaceCO->setItemText(0, item->text());
132 d->ui.replaceCO->addItem(item->text());
134 d->ui.replaceCO->setCurrentIndex(0);
138 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
140 QListWidget * lw = d->ui.suggestionsLW;
141 if (lw->currentItem() && lw->currentItem()->text() == str)
144 for (int i = 0; i != lw->count(); ++i) {
145 if (lw->item(i)->text() == str) {
146 lw->setCurrentRow(i);
153 void GuiSpellchecker::updateView()
160 void GuiSpellchecker::on_languageCO_activated(int index)
163 fromqstr(d->ui.languageCO->itemData(index).toString());
164 if (!d->word_.lang() || d->word_.lang()->lang() == lang)
167 dispatch(FuncRequest(LFUN_LANGUAGE, lang));
172 void GuiSpellchecker::on_ignoreAllPB_clicked()
174 /// replace all occurences of word
175 if (d->word_.lang() && !d->word_.word().empty())
176 theSpellChecker()->accept(d->word_);
181 void GuiSpellchecker::on_addPB_clicked()
183 /// insert word in personal dictionary
184 theSpellChecker()->insert(d->word_);
189 void GuiSpellchecker::on_ignorePB_clicked()
191 dispatch(FuncRequest(LFUN_ESCAPE));
192 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
197 void GuiSpellchecker::on_findNextPB_clicked()
199 docstring const data = find2string(
200 qstring_to_ucs4(d->ui.wordED->text()),
202 dispatch(FuncRequest(LFUN_WORD_FIND, data));
206 void GuiSpellchecker::on_replacePB_clicked()
208 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
210 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
212 Slight hack ahead: we want to use the dispatch machinery
213 (see bug #6217), but self-insert honors the ``auto region
214 delete'' setting, which is not wanted here. Creating a new
215 ad-hoc LFUN seems overkill, but it could be an option (JMarc).
217 bool const ard = lyxrc.auto_region_delete;
218 lyxrc.auto_region_delete = true;
219 dispatch(FuncRequest(LFUN_SELF_INSERT, replacement));
220 lyxrc.auto_region_delete = ard;
227 void GuiSpellchecker::on_replaceAllPB_clicked()
229 docstring const data = replace2string(
230 qstring_to_ucs4(d->ui.replaceCO->currentText()),
231 qstring_to_ucs4(d->ui.wordED->text()),
232 true, true, true, true);
233 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
234 check(); // continue spellchecking
238 void GuiSpellchecker::updateSuggestions(docstring_list & words)
240 QString const suggestion = toqstr(d->word_.word());
241 d->ui.wordED->setText(suggestion);
242 QListWidget * lw = d->ui.suggestionsLW;
246 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
249 for (size_t i = 0; i != words.size(); ++i)
250 lw->addItem(toqstr(words[i]));
252 on_suggestionsLW_itemClicked(lw->item(0));
253 lw->setCurrentRow(0);
257 bool GuiSpellchecker::initialiseParams(string const &)
259 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
261 if (!theSpellChecker())
264 DocIterator const begin = doc_iterator_begin(&buffer());
265 Cursor const & cur = bufferview()->cursor();
266 d->progress_ = countWords(begin, cur);
267 d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
273 void GuiSpellchecker::check()
275 LYXERR(Debug::GUI, "Check the spelling of a word");
277 DocIterator from = bufferview()->cursor();
279 WordLangTuple word_lang;
280 docstring_list suggestions;
284 progress = buffer().spellCheck(from, to, word_lang, suggestions);
285 } catch (ExceptionMessage const & message) {
286 if (message.type_ == WarningException) {
287 Alert::warning(message.title_, message.details_);
293 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
294 d->count_ += progress;
295 d->progress_ += progress;
298 if (from == doc_iterator_end(&buffer())) {
305 d->word_ = word_lang;
307 int const progress_bar = d->total_
308 ? int(100.0 * float(d->progress_)/d->total_) : 100;
309 LYXERR(Debug::GUI, "Updating spell progress.");
311 d->ui.spellcheckPR->setValue(progress_bar);
313 updateSuggestions(suggestions);
315 int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
317 d->ui.languageCO->setCurrentIndex(pos);
319 // FIXME: if we used a lfun like in find/replace, dispatch would do
321 int const size = to.pos() - from.pos();
322 BufferView * bv = const_cast<BufferView *>(bufferview());
323 bv->putSelectionAt(from, size, false);
327 void GuiSpellchecker::showSummary()
329 if (d->count_ == 0) {
336 message = bformat(_("%1$d words checked."), d->count_);
338 message = _("One word checked.");
341 Alert::information(_("Spelling check completed"), message);
345 Dialog * createGuiSpellchecker(GuiView & lv)
347 GuiSpellchecker * gui = new GuiSpellchecker(lv);
349 gui->setFloating(true);
355 } // namespace frontend
358 #include "moc_GuiSpellchecker.cpp"