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"
30 #include "Paragraph.h"
31 #include "WordLangTuple.h"
33 #include "support/debug.h"
34 #include "support/docstring.h"
35 #include "support/docstring_list.h"
36 #include "support/ExceptionMessage.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/textutils.h"
41 #include <QListWidgetItem>
43 #include "SpellChecker.h"
45 #include "frontends/alert.h"
48 using namespace lyx::support;
54 struct GuiSpellchecker::Private
56 Private() : progress_(0), count_(0) {}
57 Ui::SpellcheckerUi ui;
58 /// current word being checked and lang code
60 /// values for progress
68 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
69 : DockView(lv, "spellchecker", qt_("Spellchecker"),
70 Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
74 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
75 this, SLOT(on_replacePB_clicked()));
77 d->ui.wordED->setReadOnly(true);
81 GuiSpellchecker::~GuiSpellchecker()
87 void GuiSpellchecker::on_closePB_clicked()
93 void GuiSpellchecker::on_suggestionsLW_changed(QListWidgetItem * item)
95 if (d->ui.replaceCO->count() != 0)
96 d->ui.replaceCO->setItemText(0, item->text());
98 d->ui.replaceCO->addItem(item->text());
100 d->ui.replaceCO->setCurrentIndex(0);
104 void GuiSpellchecker::on_replaceC0_highlighted(const QString & str)
106 QListWidget * lw = d->ui.suggestionsLW;
107 if (lw->currentItem() && lw->currentItem()->text() == str)
110 for (int i = 0; i != lw->count(); ++i) {
111 if (lw->item(i)->text() == str) {
112 lw->setCurrentRow(i);
119 void GuiSpellchecker::updateView()
126 void GuiSpellchecker::on_replaceAllPB_clicked()
128 /// replace all occurances of word
129 theSpellChecker()->accept(d->word_);
134 void GuiSpellchecker::on_addPB_clicked()
136 /// insert word in personal dictionary
137 theSpellChecker()->insert(d->word_);
142 void GuiSpellchecker::on_ignorePB_clicked()
148 void GuiSpellchecker::on_replacePB_clicked()
150 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
152 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
153 BufferView * bv = const_cast<BufferView *>(bufferview());
154 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
155 bv->buffer().markDirty();
156 // If we used an LFUN, we would not need that
157 bv->processUpdateFlags(Update::Force | Update::FitCursor);
164 void GuiSpellchecker::updateSuggestions(docstring_list & words)
166 QString const suggestion = toqstr(d->word_.word());
167 d->ui.wordED->setText(suggestion);
168 QListWidget * lw = d->ui.suggestionsLW;
172 on_suggestionsLW_changed(new QListWidgetItem(suggestion));
175 for (size_t i = 0; i != words.size(); ++i)
176 lw->addItem(toqstr(words[i]));
178 on_suggestionsLW_changed(lw->item(0));
179 lw->setCurrentRow(0);
183 bool GuiSpellchecker::initialiseParams(string const &)
185 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
187 if (!theSpellChecker())
190 DocIterator const begin = doc_iterator_begin(&buffer());
191 Cursor const & cur = bufferview()->cursor();
192 d->progress_ = countWords(begin, cur);
193 d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
199 void GuiSpellchecker::check()
201 LYXERR(Debug::GUI, "Check the spelling of a word");
203 DocIterator from = bufferview()->cursor();
205 WordLangTuple word_lang;
206 docstring_list suggestions;
210 progress = buffer().spellCheck(from, to, word_lang, suggestions);
211 } catch (ExceptionMessage const & message) {
212 if (message.type_ == WarningException) {
213 Alert::warning(message.title_, message.details_);
219 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
220 d->count_ += progress;
221 d->progress_ += progress;
231 d->word_ = word_lang;
233 int const progress_bar = d->total_
234 ? int(100.0 * float(d->progress_)/d->total_) : 100;
235 LYXERR(Debug::GUI, "Updating spell progress.");
237 d->ui.spellcheckPR->setValue(progress_bar);
239 updateSuggestions(suggestions);
241 // FIXME: if we used a lfun like in find/replace, dispatch would do
243 int const size = to.pos() - from.pos();
244 BufferView * bv = const_cast<BufferView *>(bufferview());
245 bv->putSelectionAt(from, size, false);
249 void GuiSpellchecker::showSummary()
251 if (d->count_ == 0) {
258 message = bformat(_("%1$d words checked."), d->count_);
260 message = _("One word checked.");
263 Alert::information(_("Spelling check completed"), message);
267 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
269 } // namespace frontend
272 #include "moc_GuiSpellchecker.cpp"