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"
31 #include "Paragraph.h"
32 #include "WordLangTuple.h"
34 #include "support/debug.h"
35 #include "support/docstring.h"
36 #include "support/docstring_list.h"
37 #include "support/ExceptionMessage.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40 #include "support/textutils.h"
42 #include <QListWidgetItem>
44 #include "SpellChecker.h"
46 #include "frontends/alert.h"
49 using namespace lyx::support;
55 struct GuiSpellchecker::Private
57 Private() : progress_(0), count_(0) {}
58 Ui::SpellcheckerUi ui;
59 /// current word being checked and lang code
61 /// values for progress
69 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
70 : DockView(lv, "spellchecker", qt_("Spellchecker"),
71 Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
75 connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
76 this, SLOT(on_replacePB_clicked()));
78 d->ui.wordED->setReadOnly(true);
82 GuiSpellchecker::~GuiSpellchecker()
88 void GuiSpellchecker::on_closePB_clicked()
94 void GuiSpellchecker::on_suggestionsLW_itemChanged(QListWidgetItem * item)
96 if (d->ui.replaceCO->count() != 0)
97 d->ui.replaceCO->setItemText(0, item->text());
99 d->ui.replaceCO->addItem(item->text());
101 d->ui.replaceCO->setCurrentIndex(0);
105 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
107 QListWidget * lw = d->ui.suggestionsLW;
108 if (lw->currentItem() && lw->currentItem()->text() == str)
111 for (int i = 0; i != lw->count(); ++i) {
112 if (lw->item(i)->text() == str) {
113 lw->setCurrentRow(i);
120 void GuiSpellchecker::updateView()
127 void GuiSpellchecker::on_ignoreAllPB_clicked()
129 /// replace all occurances of word
130 theSpellChecker()->accept(d->word_);
135 void GuiSpellchecker::on_addPB_clicked()
137 /// insert word in personal dictionary
138 theSpellChecker()->insert(d->word_);
143 void GuiSpellchecker::on_ignorePB_clicked()
145 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
150 void GuiSpellchecker::on_replacePB_clicked()
152 docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
154 LYXERR(Debug::GUI, "Replace (" << replacement << ")");
155 BufferView * bv = const_cast<BufferView *>(bufferview());
156 cap::replaceSelectionWithString(bv->cursor(), replacement, true);
157 bv->buffer().markDirty();
158 // If we used an LFUN, we would not need that
159 bv->processUpdateFlags(Update::Force | Update::FitCursor);
166 void GuiSpellchecker::updateSuggestions(docstring_list & words)
168 QString const suggestion = toqstr(d->word_.word());
169 d->ui.wordED->setText(suggestion);
170 QListWidget * lw = d->ui.suggestionsLW;
174 on_suggestionsLW_itemChanged(new QListWidgetItem(suggestion));
177 for (size_t i = 0; i != words.size(); ++i)
178 lw->addItem(toqstr(words[i]));
180 on_suggestionsLW_itemChanged(lw->item(0));
181 lw->setCurrentRow(0);
185 bool GuiSpellchecker::initialiseParams(string const &)
187 LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
189 if (!theSpellChecker())
192 DocIterator const begin = doc_iterator_begin(&buffer());
193 Cursor const & cur = bufferview()->cursor();
194 d->progress_ = countWords(begin, cur);
195 d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
201 void GuiSpellchecker::check()
203 LYXERR(Debug::GUI, "Check the spelling of a word");
205 DocIterator from = bufferview()->cursor();
207 WordLangTuple word_lang;
208 docstring_list suggestions;
212 progress = buffer().spellCheck(from, to, word_lang, suggestions);
213 } catch (ExceptionMessage const & message) {
214 if (message.type_ == WarningException) {
215 Alert::warning(message.title_, message.details_);
221 LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
222 d->count_ += progress;
223 d->progress_ += progress;
226 if (from == doc_iterator_end(&buffer())) {
233 d->word_ = word_lang;
235 int const progress_bar = d->total_
236 ? int(100.0 * float(d->progress_)/d->total_) : 100;
237 LYXERR(Debug::GUI, "Updating spell progress.");
239 d->ui.spellcheckPR->setValue(progress_bar);
241 updateSuggestions(suggestions);
243 // FIXME: if we used a lfun like in find/replace, dispatch would do
245 int const size = to.pos() - from.pos();
246 BufferView * bv = const_cast<BufferView *>(bufferview());
247 bv->putSelectionAt(from, size, false);
251 void GuiSpellchecker::showSummary()
253 if (d->count_ == 0) {
260 message = bformat(_("%1$d words checked."), d->count_);
262 message = _("One word checked.");
265 Alert::information(_("Spelling check completed"), message);
269 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
271 } // namespace frontend
274 #include "moc_GuiSpellchecker.cpp"