]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
#7357 correct the numbers by introducing two modes of word count
[lyx.git] / src / frontends / qt4 / GuiSpellchecker.cpp
1 /**
2  * \file GuiSpellchecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Edwin Leuven
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiSpellchecker.h"
16 #include "GuiApplication.h"
17
18 #include "qt_helpers.h"
19
20 #include "ui_SpellcheckerUi.h"
21
22 #include "Buffer.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
25 #include "buffer_funcs.h"
26 #include "Cursor.h"
27 #include "CutAndPaste.h"
28 #include "FuncRequest.h"
29 #include "Language.h"
30 #include "LyX.h"
31 #include "LyXRC.h"
32 #include "lyxfind.h"
33 #include "Paragraph.h"
34 #include "WordLangTuple.h"
35
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"
43
44 #include <QListWidgetItem>
45 #include <QKeyEvent>
46
47 #include "SpellChecker.h"
48
49 #include "frontends/alert.h"
50
51 using namespace std;
52 using namespace lyx::support;
53
54 namespace lyx {
55 namespace frontend {
56
57
58 struct GuiSpellchecker::Private
59 {
60         Private() : progress_(0), count_(0), stuck_(false) {}
61         Ui::SpellcheckerUi ui;
62         /// current word being checked and lang code
63         WordLangTuple word_;
64         /// values for progress
65         int total_;
66         int progress_;
67         /// word count
68         int count_;
69         /// flag for last move forward success
70         bool stuck_;
71 };
72
73
74 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
75         : DockView(lv, "spellchecker", qt_("Spellchecker"),
76         Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
77 {
78         d->ui.setupUi(this);
79
80         connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
81                 this, SLOT(on_replacePB_clicked()));
82
83         // language
84         QAbstractItemModel * language_model = guiApp->languageModel();
85         // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
86         language_model->sort(0);
87         d->ui.languageCO->setModel(language_model);
88         d->ui.languageCO->setModelColumn(1);
89
90         d->ui.wordED->setReadOnly(true);
91
92         d->ui.suggestionsLW->installEventFilter(this);
93 }
94
95
96 GuiSpellchecker::~GuiSpellchecker()
97 {
98         delete d;
99 }
100
101
102 void GuiSpellchecker::on_closePB_clicked()
103 {
104         close();
105 }
106
107
108 bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
109 {
110         if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
111                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
112                 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
113                         if (d->ui.suggestionsLW->currentItem()) {
114                                 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
115                                 on_replacePB_clicked();
116                         }
117                         return true;
118                 } else if (e->key() == Qt::Key_Right) {
119                         if (d->ui.suggestionsLW->currentItem())
120                                 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
121                         return true;
122                 }
123         }
124         // standard event processing
125         return QWidget::eventFilter(obj, event);
126 }
127
128
129 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
130 {
131         if (d->ui.replaceCO->count() != 0)
132                 d->ui.replaceCO->setItemText(0, item->text());
133         else
134                 d->ui.replaceCO->addItem(item->text());
135
136         d->ui.replaceCO->setCurrentIndex(0);
137 }
138
139
140 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
141 {
142         QListWidget * lw = d->ui.suggestionsLW;
143         if (lw->currentItem() && lw->currentItem()->text() == str)
144                 return;
145
146         for (int i = 0; i != lw->count(); ++i) {
147                 if (lw->item(i)->text() == str) {
148                         lw->setCurrentRow(i);
149                         break;
150                 }
151         }
152 }
153
154
155 void GuiSpellchecker::updateView()
156 {
157         if (hasFocus() && d->count_ == 0)
158                 check();
159 }
160
161
162 void GuiSpellchecker::forward()
163 {
164         DocIterator from = bufferview()->cursor();
165
166         dispatch(FuncRequest(LFUN_ESCAPE));
167         dispatch(FuncRequest(LFUN_CHAR_FORWARD));
168         d->stuck_ = from == bufferview()->cursor();
169 }
170         
171         
172 void GuiSpellchecker::on_languageCO_activated(int index)
173 {
174         string const lang =
175                 fromqstr(d->ui.languageCO->itemData(index).toString());
176         if (!d->word_.lang() || d->word_.lang()->lang() == lang)
177                 // nothing changed
178                 return;
179         dispatch(FuncRequest(LFUN_LANGUAGE, lang));
180         check();
181 }
182
183
184 void GuiSpellchecker::on_ignoreAllPB_clicked()
185 {
186         /// replace all occurrences of word
187         if (d->word_.lang() && !d->word_.word().empty())
188                 theSpellChecker()->accept(d->word_);
189         forward();
190         check();
191 }
192
193
194 void GuiSpellchecker::on_addPB_clicked()
195 {
196         /// insert word in personal dictionary
197         theSpellChecker()->insert(d->word_);
198         forward();
199         check();
200 }
201
202
203 void GuiSpellchecker::on_ignorePB_clicked()
204 {
205         forward();
206         check();
207 }
208
209
210 void GuiSpellchecker::on_findNextPB_clicked()
211 {
212         docstring const data = find2string(
213                                 qstring_to_ucs4(d->ui.wordED->text()),
214                                 true, true, true);
215         dispatch(FuncRequest(LFUN_WORD_FIND, data));
216 }
217
218
219 void GuiSpellchecker::on_replacePB_clicked()
220 {
221         docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
222         docstring const data = replace2string(
223                 replacement, qstring_to_ucs4(d->ui.wordED->text()),
224                 true, true, false, false);
225
226         LYXERR(Debug::GUI, "Replace (" << replacement << ")");
227         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
228         forward();
229         check();
230 }
231
232
233 void GuiSpellchecker::on_replaceAllPB_clicked()
234 {
235         docstring const data = replace2string(
236                 qstring_to_ucs4(d->ui.replaceCO->currentText()),
237                 qstring_to_ucs4(d->ui.wordED->text()),
238                 true, true, true, true);
239         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
240         forward();
241         check(); // continue spellchecking
242 }
243
244
245 void GuiSpellchecker::updateSuggestions(docstring_list & words)
246 {
247         QString const suggestion = toqstr(d->word_.word());
248         d->ui.wordED->setText(suggestion);
249         QListWidget * lw = d->ui.suggestionsLW;
250         lw->clear();
251
252         if (words.empty()) {
253                 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
254                 return;
255         }
256         for (size_t i = 0; i != words.size(); ++i)
257                 lw->addItem(toqstr(words[i]));
258
259         on_suggestionsLW_itemClicked(lw->item(0));
260         lw->setCurrentRow(0);
261 }
262
263
264 bool GuiSpellchecker::initialiseParams(string const &)
265 {
266         LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
267
268         if (!theSpellChecker())
269                 return false;
270
271         DocIterator const begin = doc_iterator_begin(&buffer());
272         Cursor const & cur = bufferview()->cursor();
273         d->progress_ = countWords(begin, cur, false);
274         d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()), false);
275         d->count_ = 0;
276         return true;
277 }
278
279
280 void GuiSpellchecker::check()
281 {
282         LYXERR(Debug::GUI, "Check the spelling of the words starting at " << d->progress_);
283
284         // last move forward failed
285         if (d->stuck_) {
286                 d->stuck_ = false;
287                 showSummary();
288                 return;
289         }
290         
291         DocIterator from = bufferview()->cursor();
292         DocIterator to;
293         WordLangTuple word_lang;
294         docstring_list suggestions;
295
296         int progress;
297         try {
298                 progress = buffer().spellCheck(from, to, word_lang, suggestions);
299         } catch (ExceptionMessage const & message) {
300                 if (message.type_ == WarningException) {
301                         Alert::warning(message.title_, message.details_);
302                         close();
303                         return;
304                 }
305                 throw message;
306         }
307         d->count_ += progress;
308         d->progress_ += progress;
309         LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"" <<
310                    " at position " << d->progress_);
311
312         // end of document
313         if (from == doc_iterator_end(&buffer())) {
314                 showSummary();
315                 return;
316         }
317
318         // current misspelled word has to be counted too.
319         ++d->count_;
320         ++d->progress_;
321
322         if (!isVisible())
323                 show();
324
325         d->word_ = word_lang;
326
327         int const progress_bar = d->total_
328                 ? int(100.0 * float(d->progress_)/d->total_) : 100;
329         LYXERR(Debug::GUI, "Updating spell progress." <<
330                    " Now we have " << progress_bar << " percent.");
331         // set progress bar
332         d->ui.spellcheckPR->setValue(progress_bar);
333         // set suggestions
334         updateSuggestions(suggestions);
335         // set language
336         int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
337         if (pos != -1)
338                 d->ui.languageCO->setCurrentIndex(pos);
339
340         // FIXME LFUN
341         // If we used a LFUN, dispatch would do all of this for us
342         int const size = to.pos() - from.pos();
343         BufferView * bv = const_cast<BufferView *>(bufferview());
344         bv->putSelectionAt(from, size, false);
345         bv->processUpdateFlags(Update::Force | Update::FitCursor);
346         
347 }
348
349
350 void GuiSpellchecker::showSummary()
351 {
352         if (d->count_ == 0) {
353                 close();
354                 return;
355         }
356
357         docstring message;
358         if (d->count_ != 1)
359                 message = bformat(_("%1$d words checked."), d->count_);
360         else
361                 message = _("One word checked.");
362
363         close();
364         Alert::information(_("Spelling check completed"), message);
365 }
366
367
368 Dialog * createGuiSpellchecker(GuiView & lv) 
369
370         GuiSpellchecker * gui = new GuiSpellchecker(lv);
371 #ifdef Q_WS_MACX
372         gui->setFloating(true);
373 #endif
374         return gui;
375 }
376
377
378 } // namespace frontend
379 } // namespace lyx
380
381 #include "moc_GuiSpellchecker.cpp"