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"
17 #include "qt_helpers.h"
19 #include "ui_SpellcheckerUi.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "buffer_funcs.h"
26 #include "CutAndPaste.h"
27 #include "FuncRequest.h"
32 #include "Paragraph.h"
33 #include "WordLangTuple.h"
35 #include "support/debug.h"
36 #include "support/docstring.h"
37 #include "support/docstring_list.h"
38 #include "support/ExceptionMessage.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41 #include "support/textutils.h"
43 #include <QListWidgetItem>
45 #include "SpellChecker.h"
47 #include "frontends/alert.h"
50 using namespace lyx::support;
56 struct GuiSpellchecker::Private
58 Private() : progress_(0), count_(0) {}
59 Ui::SpellcheckerUi ui;
60 /// current word being checked and lang code
62 /// values for progress
70 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
71 : DockView(lv, "spellchecker", qt_("Spellchecker"),
72 Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
76 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
77 this, SLOT(on_replacePB_clicked()));
79 d->ui.wordED->setReadOnly(true);
83 GuiSpellchecker::~GuiSpellchecker()
89 void GuiSpellchecker::on_closePB_clicked()
95 void GuiSpellchecker::on_suggestionsLW_itemChanged(QListWidgetItem * item)
97 if (d->ui.replaceCO->count() != 0)
98 d->ui.replaceCO->setItemText(0, item->text());
100 d->ui.replaceCO->addItem(item->text());
102 d->ui.replaceCO->setCurrentIndex(0);
106 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
108 QListWidget * lw = d->ui.suggestionsLW;
109 if (lw->currentItem() && lw->currentItem()->text() == str)
112 for (int i = 0; i != lw->count(); ++i) {
113 if (lw->item(i)->text() == str) {
114 lw->setCurrentRow(i);
121 void GuiSpellchecker::updateView()
128 void GuiSpellchecker::on_ignoreAllPB_clicked()
130 /// replace all occurances of word
131 theSpellChecker()->accept(d->word_);
136 void GuiSpellchecker::on_addPB_clicked()
138 /// insert word in personal dictionary
139 theSpellChecker()->insert(d->word_);
144 void GuiSpellchecker::on_ignorePB_clicked()
146 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
151 void GuiSpellchecker::on_findNextPB_clicked()
153 docstring const data = find2string(
154 qstring_to_ucs4(d->ui.wordED->text()),
156 dispatch(FuncRequest(LFUN_WORD_FIND, data));
160 void GuiSpellchecker::on_replacePB_clicked()
162 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
164 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
165 BufferView * bv = const_cast<BufferView *>(bufferview());
166 if (!bv->cursor().inTexted())
168 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
169 bv->buffer().markDirty();
170 // If we used an LFUN, we would not need that
171 bv->processUpdateFlags(Update::Force | Update::FitCursor);
178 void GuiSpellchecker::on_replaceAllPB_clicked()
180 docstring const data = replace2string(
181 qstring_to_ucs4(d->ui.replaceCO->currentText()),
182 qstring_to_ucs4(d->ui.wordED->text()),
183 true, true, true, true);
184 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
188 void GuiSpellchecker::updateSuggestions(docstring_list & words)
190 QString const suggestion = toqstr(d->word_.word());
191 d->ui.wordED->setText(suggestion);
192 QListWidget * lw = d->ui.suggestionsLW;
196 on_suggestionsLW_itemChanged(new QListWidgetItem(suggestion));
199 for (size_t i = 0; i != words.size(); ++i)
200 lw->addItem(toqstr(words[i]));
202 on_suggestionsLW_itemChanged(lw->item(0));
203 lw->setCurrentRow(0);
207 bool GuiSpellchecker::initialiseParams(string const &)
209 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
211 if (!theSpellChecker())
214 DocIterator const begin = doc_iterator_begin(&buffer());
215 Cursor const & cur = bufferview()->cursor();
216 d->progress_ = countWords(begin, cur);
217 d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
223 void GuiSpellchecker::check()
225 LYXERR(Debug::GUI, "Check the spelling of a word");
227 DocIterator from = bufferview()->cursor();
229 WordLangTuple word_lang;
230 docstring_list suggestions;
234 progress = buffer().spellCheck(from, to, word_lang, suggestions);
235 } catch (ExceptionMessage const & message) {
236 if (message.type_ == WarningException) {
237 Alert::warning(message.title_, message.details_);
243 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
244 d->count_ += progress;
245 d->progress_ += progress;
248 if (from == doc_iterator_end(&buffer())) {
255 d->word_ = word_lang;
257 int const progress_bar = d->total_
258 ? int(100.0 * float(d->progress_)/d->total_) : 100;
259 LYXERR(Debug::GUI, "Updating spell progress.");
261 d->ui.spellcheckPR->setValue(progress_bar);
263 updateSuggestions(suggestions);
265 // FIXME: if we used a lfun like in find/replace, dispatch would do
267 int const size = to.pos() - from.pos();
268 BufferView * bv = const_cast<BufferView *>(bufferview());
269 bv->putSelectionAt(from, size, false);
273 void GuiSpellchecker::showSummary()
275 if (d->count_ == 0) {
282 message = bformat(_("%1$d words checked."), d->count_);
284 message = _("One word checked.");
287 Alert::information(_("Spelling check completed"), message);
291 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
293 } // namespace frontend
296 #include "moc_GuiSpellchecker.cpp"