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