]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
* GuiSpellchecker.cpp:
[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) {}
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 };
70
71
72 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
73         : DockView(lv, "spellchecker", qt_("Spellchecker"),
74         Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
75 {
76         d->ui.setupUi(this);
77
78         connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
79                 this, SLOT(on_replacePB_clicked()));
80
81         // language
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
87         d->ui.wordED->setReadOnly(true);
88
89         d->ui.suggestionsLW->installEventFilter(this);
90 }
91
92
93 GuiSpellchecker::~GuiSpellchecker()
94 {
95         delete d;
96 }
97
98
99 void GuiSpellchecker::on_closePB_clicked()
100 {
101         close();
102 }
103
104
105 bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
106 {
107         if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
108                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
109                 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
110                         on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
111                         on_replacePB_clicked();
112                         return true;
113                 } else if (e->key() == Qt::Key_Right) {
114                         on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
115                         return true;
116                 }
117         }
118         // standard event processing
119         return QWidget::eventFilter(obj, event);
120 }
121
122
123 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
124 {
125         if (d->ui.replaceCO->count() != 0)
126                 d->ui.replaceCO->setItemText(0, item->text());
127         else
128                 d->ui.replaceCO->addItem(item->text());
129
130         d->ui.replaceCO->setCurrentIndex(0);
131 }
132
133
134 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
135 {
136         QListWidget * lw = d->ui.suggestionsLW;
137         if (lw->currentItem() && lw->currentItem()->text() == str)
138                 return;
139
140         for (int i = 0; i != lw->count(); ++i) {
141                 if (lw->item(i)->text() == str) {
142                         lw->setCurrentRow(i);
143                         break;
144                 }
145         }
146 }
147
148
149 void GuiSpellchecker::updateView()
150 {
151         if (hasFocus())
152                 check();
153 }
154
155
156 void GuiSpellchecker::on_languageCO_activated(int index)
157 {
158         string const lang =
159                 fromqstr(d->ui.languageCO->itemData(index).toString());
160         if (d->word_.lang()->lang() == lang)
161                 // nothing changed
162                 return;
163         dispatch(FuncRequest(LFUN_LANGUAGE, lang));
164         check();
165 }
166
167
168 void GuiSpellchecker::on_ignoreAllPB_clicked()
169 {
170         /// replace all occurances of word
171         theSpellChecker()->accept(d->word_);
172         check();
173 }
174
175
176 void GuiSpellchecker::on_addPB_clicked()
177 {
178         /// insert word in personal dictionary
179         theSpellChecker()->insert(d->word_);
180         check();
181 }
182
183
184 void GuiSpellchecker::on_ignorePB_clicked()
185 {
186         dispatch(FuncRequest(LFUN_ESCAPE));
187         dispatch(FuncRequest(LFUN_CHAR_FORWARD));
188         check();
189 }
190
191
192 void GuiSpellchecker::on_findNextPB_clicked()
193 {
194         docstring const data = find2string(
195                                 qstring_to_ucs4(d->ui.wordED->text()),
196                                 true, true, true);
197         dispatch(FuncRequest(LFUN_WORD_FIND, data));
198 }
199
200
201 void GuiSpellchecker::on_replacePB_clicked()
202 {
203         docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
204
205         LYXERR(Debug::GUI, "Replace (" << replacement << ")");
206         BufferView * bv = const_cast<BufferView *>(bufferview());
207         if (!bv->cursor().inTexted())
208                 return;
209         cap::replaceSelectionWithString(bv->cursor(), replacement, true);
210         bv->buffer().markDirty();
211         // If we used an LFUN, we would not need that
212         bv->processUpdateFlags(Update::Force | Update::FitCursor);
213         // fix up the count
214         --d->count_;
215         check();
216 }
217
218
219 void GuiSpellchecker::on_replaceAllPB_clicked()
220 {
221         docstring const data = replace2string(
222                 qstring_to_ucs4(d->ui.replaceCO->currentText()),
223                 qstring_to_ucs4(d->ui.wordED->text()),
224                 true, true, true, true);
225         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
226         check(); // continue spellchecking
227 }
228
229
230 void GuiSpellchecker::updateSuggestions(docstring_list & words)
231 {
232         QString const suggestion = toqstr(d->word_.word());
233         d->ui.wordED->setText(suggestion);
234         QListWidget * lw = d->ui.suggestionsLW;
235         lw->clear();
236
237         if (words.empty()) {
238                 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
239                 return;
240         }
241         for (size_t i = 0; i != words.size(); ++i)
242                 lw->addItem(toqstr(words[i]));
243
244         on_suggestionsLW_itemClicked(lw->item(0));
245         lw->setCurrentRow(0);
246 }
247
248
249 bool GuiSpellchecker::initialiseParams(string const &)
250 {
251         LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
252
253         if (!theSpellChecker())
254                 return false;
255
256         DocIterator const begin = doc_iterator_begin(&buffer());
257         Cursor const & cur = bufferview()->cursor();
258         d->progress_ = countWords(begin, cur);
259         d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
260         d->count_ = 0;
261         return true;
262 }
263
264
265 void GuiSpellchecker::check()
266 {
267         LYXERR(Debug::GUI, "Check the spelling of a word");
268
269         DocIterator from = bufferview()->cursor();
270         DocIterator to;
271         WordLangTuple word_lang;
272         docstring_list suggestions;
273
274         int progress;
275         try {
276                 progress = buffer().spellCheck(from, to, word_lang, suggestions);
277         } catch (ExceptionMessage const & message) {
278                 if (message.type_ == WarningException) {
279                         Alert::warning(message.title_, message.details_);
280                         close();
281                         return;
282                 }
283                 throw message;
284         }
285         LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
286         d->count_ += progress;
287         d->progress_ += progress;
288
289         // end of document
290         if (from == doc_iterator_end(&buffer())) {
291                 showSummary();
292                 return;
293         }
294         if (!isVisible())
295                 show();
296
297         d->word_ = word_lang;
298
299         int const progress_bar = d->total_
300                 ? int(100.0 * float(d->progress_)/d->total_) : 100;
301         LYXERR(Debug::GUI, "Updating spell progress.");
302         // set progress bar
303         d->ui.spellcheckPR->setValue(progress_bar);
304         // set suggestions
305         updateSuggestions(suggestions);
306         // set language
307         int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
308         if (pos != -1)
309                 d->ui.languageCO->setCurrentIndex(pos);
310
311         // FIXME: if we used a lfun like in find/replace, dispatch would do
312         // that for us
313         int const size = to.pos() - from.pos();
314         BufferView * bv = const_cast<BufferView *>(bufferview());
315         bv->putSelectionAt(from, size, false);
316 }
317
318
319 void GuiSpellchecker::showSummary()
320 {
321         if (d->count_ == 0) {
322                 close();
323                 return;
324         }
325
326         docstring message;
327         if (d->count_ != 1)
328                 message = bformat(_("%1$d words checked."), d->count_);
329         else
330                 message = _("One word checked.");
331
332         close();
333         Alert::information(_("Spelling check completed"), message);
334 }
335
336
337 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
338
339 } // namespace frontend
340 } // namespace lyx
341
342 #include "moc_GuiSpellchecker.cpp"