]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
#6917 move forward after adding a word to personal dictionary or the list of ignored...
[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                         if (d->ui.suggestionsLW->currentItem()) {
112                                 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
113                                 on_replacePB_clicked();
114                         }
115                         return true;
116                 } else if (e->key() == Qt::Key_Right) {
117                         if (d->ui.suggestionsLW->currentItem())
118                                 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
119                         return true;
120                 }
121         }
122         // standard event processing
123         return QWidget::eventFilter(obj, event);
124 }
125
126
127 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
128 {
129         if (d->ui.replaceCO->count() != 0)
130                 d->ui.replaceCO->setItemText(0, item->text());
131         else
132                 d->ui.replaceCO->addItem(item->text());
133
134         d->ui.replaceCO->setCurrentIndex(0);
135 }
136
137
138 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
139 {
140         QListWidget * lw = d->ui.suggestionsLW;
141         if (lw->currentItem() && lw->currentItem()->text() == str)
142                 return;
143
144         for (int i = 0; i != lw->count(); ++i) {
145                 if (lw->item(i)->text() == str) {
146                         lw->setCurrentRow(i);
147                         break;
148                 }
149         }
150 }
151
152
153 void GuiSpellchecker::updateView()
154 {
155         if (hasFocus())
156                 check();
157 }
158
159
160 void GuiSpellchecker::forward()
161 {
162         dispatch(FuncRequest(LFUN_ESCAPE));
163         dispatch(FuncRequest(LFUN_CHAR_FORWARD));
164 }
165         
166         
167 void GuiSpellchecker::on_languageCO_activated(int index)
168 {
169         string const lang =
170                 fromqstr(d->ui.languageCO->itemData(index).toString());
171         if (!d->word_.lang() || d->word_.lang()->lang() == lang)
172                 // nothing changed
173                 return;
174         dispatch(FuncRequest(LFUN_LANGUAGE, lang));
175         check();
176 }
177
178
179 void GuiSpellchecker::on_ignoreAllPB_clicked()
180 {
181         /// replace all occurrences of word
182         if (d->word_.lang() && !d->word_.word().empty())
183                 theSpellChecker()->accept(d->word_);
184         forward();
185         check();
186 }
187
188
189 void GuiSpellchecker::on_addPB_clicked()
190 {
191         /// insert word in personal dictionary
192         theSpellChecker()->insert(d->word_);
193         forward();
194         check();
195 }
196
197
198 void GuiSpellchecker::on_ignorePB_clicked()
199 {
200         forward();
201         check();
202 }
203
204
205 void GuiSpellchecker::on_findNextPB_clicked()
206 {
207         docstring const data = find2string(
208                                 qstring_to_ucs4(d->ui.wordED->text()),
209                                 true, true, true);
210         dispatch(FuncRequest(LFUN_WORD_FIND, data));
211 }
212
213
214 void GuiSpellchecker::on_replacePB_clicked()
215 {
216         docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
217
218         LYXERR(Debug::GUI, "Replace (" << replacement << ")");
219         /*
220           Slight hack ahead: we want to use the dispatch machinery
221           (see bug #6217), but self-insert honors the ``auto region
222           delete'' setting, which is not wanted here. Creating a new
223           ad-hoc LFUN seems overkill, but it could be an option (JMarc).
224         */
225         bool const ard = lyxrc.auto_region_delete;
226         lyxrc.auto_region_delete = true;
227         dispatch(FuncRequest(LFUN_SELF_INSERT, replacement));
228         lyxrc.auto_region_delete = ard;
229         // fix up the count
230         --d->count_;
231         check();
232 }
233
234
235 void GuiSpellchecker::on_replaceAllPB_clicked()
236 {
237         docstring const data = replace2string(
238                 qstring_to_ucs4(d->ui.replaceCO->currentText()),
239                 qstring_to_ucs4(d->ui.wordED->text()),
240                 true, true, true, true);
241         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
242         check(); // continue spellchecking
243 }
244
245
246 void GuiSpellchecker::updateSuggestions(docstring_list & words)
247 {
248         QString const suggestion = toqstr(d->word_.word());
249         d->ui.wordED->setText(suggestion);
250         QListWidget * lw = d->ui.suggestionsLW;
251         lw->clear();
252
253         if (words.empty()) {
254                 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
255                 return;
256         }
257         for (size_t i = 0; i != words.size(); ++i)
258                 lw->addItem(toqstr(words[i]));
259
260         on_suggestionsLW_itemClicked(lw->item(0));
261         lw->setCurrentRow(0);
262 }
263
264
265 bool GuiSpellchecker::initialiseParams(string const &)
266 {
267         LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
268
269         if (!theSpellChecker())
270                 return false;
271
272         DocIterator const begin = doc_iterator_begin(&buffer());
273         Cursor const & cur = bufferview()->cursor();
274         d->progress_ = countWords(begin, cur);
275         d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
276         d->count_ = 0;
277         return true;
278 }
279
280
281 void GuiSpellchecker::check()
282 {
283         LYXERR(Debug::GUI, "Check the spelling of a word");
284
285         DocIterator from = bufferview()->cursor();
286         DocIterator to;
287         WordLangTuple word_lang;
288         docstring_list suggestions;
289
290         int progress;
291         try {
292                 progress = buffer().spellCheck(from, to, word_lang, suggestions);
293         } catch (ExceptionMessage const & message) {
294                 if (message.type_ == WarningException) {
295                         Alert::warning(message.title_, message.details_);
296                         close();
297                         return;
298                 }
299                 throw message;
300         }
301         LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
302         d->count_ += progress;
303         d->progress_ += progress;
304
305         // end of document
306         if (from == doc_iterator_end(&buffer())) {
307                 showSummary();
308                 return;
309         }
310         if (!isVisible())
311                 show();
312
313         d->word_ = word_lang;
314
315         int const progress_bar = d->total_
316                 ? int(100.0 * float(d->progress_)/d->total_) : 100;
317         LYXERR(Debug::GUI, "Updating spell progress.");
318         // set progress bar
319         d->ui.spellcheckPR->setValue(progress_bar);
320         // set suggestions
321         updateSuggestions(suggestions);
322         // set language
323         int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
324         if (pos != -1)
325                 d->ui.languageCO->setCurrentIndex(pos);
326
327         // FIXME: if we used a lfun like in find/replace, dispatch would do
328         // that for us
329         int const size = to.pos() - from.pos();
330         BufferView * bv = const_cast<BufferView *>(bufferview());
331         bv->putSelectionAt(from, size, false);
332 }
333
334
335 void GuiSpellchecker::showSummary()
336 {
337         if (d->count_ == 0) {
338                 close();
339                 return;
340         }
341
342         docstring message;
343         if (d->count_ != 1)
344                 message = bformat(_("%1$d words checked."), d->count_);
345         else
346                 message = _("One word checked.");
347
348         close();
349         Alert::information(_("Spelling check completed"), message);
350 }
351
352
353 Dialog * createGuiSpellchecker(GuiView & lv) 
354
355         GuiSpellchecker * gui = new GuiSpellchecker(lv);
356 #ifdef Q_WS_MACX
357         gui->setFloating(true);
358 #endif
359         return gui;
360 }
361
362
363 } // namespace frontend
364 } // namespace lyx
365
366 #include "moc_GuiSpellchecker.cpp"