]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
#7430 add check for first document language for the spell checker and initialize...
[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 "Text.h"
28 #include "CutAndPaste.h"
29 #include "FuncRequest.h"
30 #include "Language.h"
31 #include "LyX.h"
32 #include "LyXRC.h"
33 #include "lyxfind.h"
34 #include "Paragraph.h"
35 #include "WordLangTuple.h"
36
37 #include "support/debug.h"
38 #include "support/docstring.h"
39 #include "support/docstring_list.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43 #include "support/textutils.h"
44
45 #include <QKeyEvent>
46 #include <QListWidgetItem>
47 #include <QMessageBox>
48
49 #include "SpellChecker.h"
50
51 #include "frontends/alert.h"
52
53 using namespace std;
54 using namespace lyx::support;
55
56 namespace lyx {
57 namespace frontend {
58
59
60 struct SpellcheckerWidget::Private
61 {
62         Private(SpellcheckerWidget * parent)
63                 : p(parent) {}
64         /// update from controller
65         void updateSuggestions(docstring_list & words);
66         /// move to next position after current word
67         void forward();
68         /// check text until next misspelled/unknown word
69         void check();
70         ///
71         bool continueFromBeginning();
72         ///
73         void setLanguage(Language const * lang);
74         ///
75         Ui::SpellcheckerUi ui;
76         ///
77         SpellcheckerWidget * p;
78         ///
79         GuiView * gv_;
80         /// current word being checked and lang code
81         WordLangTuple word_;
82 };
83
84
85 SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, QWidget * parent)
86         : QTabWidget(parent), d(new Private(this))
87 {
88         d->ui.setupUi(this);
89         d->gv_ = gv;
90
91         connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
92                 this, SLOT(on_replacePB_clicked()));
93
94         // language
95         QAbstractItemModel * language_model = guiApp->languageModel();
96         // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
97         language_model->sort(0);
98         d->ui.languageCO->setModel(language_model);
99         d->ui.languageCO->setModelColumn(1);
100
101         d->ui.wordED->setReadOnly(true);
102
103         d->ui.suggestionsLW->installEventFilter(this);
104 }
105
106
107 SpellcheckerWidget::~SpellcheckerWidget()
108 {
109         delete d;
110 }
111
112
113 bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
114 {
115         if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
116                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
117                 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
118                         if (d->ui.suggestionsLW->currentItem()) {
119                                 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
120                                 on_replacePB_clicked();
121                         }
122                         return true;
123                 } else if (e->key() == Qt::Key_Right) {
124                         if (d->ui.suggestionsLW->currentItem())
125                                 on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
126                         return true;
127                 }
128         }
129         // standard event processing
130         return QWidget::eventFilter(obj, event);
131 }
132
133
134 void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
135 {
136         if (d->ui.replaceCO->count() != 0)
137                 d->ui.replaceCO->setItemText(0, item->text());
138         else
139                 d->ui.replaceCO->addItem(item->text());
140
141         d->ui.replaceCO->setCurrentIndex(0);
142 }
143
144
145 void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
146 {
147         QListWidget * lw = d->ui.suggestionsLW;
148         if (lw->currentItem() && lw->currentItem()->text() == str)
149                 return;
150
151         for (int i = 0; i != lw->count(); ++i) {
152                 if (lw->item(i)->text() == str) {
153                         lw->setCurrentRow(i);
154                         break;
155                 }
156         }
157 }
158
159
160 void SpellcheckerWidget::updateView()
161 {
162         BufferView * bv = d->gv_->documentBufferView();
163         setEnabled(bv != 0);
164         if (bv && hasFocus()) {
165
166                 BufferView * bv = d->gv_->documentBufferView();
167                 std::set<Language const *> languages = 
168                 bv->buffer().masterBuffer()->getLanguages();
169                 if (!languages.empty())
170                         d->setLanguage(*languages.begin());
171                 
172                 d->check();
173         }
174 }
175
176
177 bool SpellcheckerWidget::Private::continueFromBeginning()
178 {
179                 QMessageBox::StandardButton const answer = QMessageBox::question(p,
180                         qt_("Spell Checker"),
181                         qt_("We reached the end of the document, would you like to "
182                                 "continue from the beginning?"),
183                         QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
184                 if (answer == QMessageBox::No)
185                         return false;
186                 dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
187                 return true;
188 }
189
190
191 void SpellcheckerWidget::Private::forward()
192 {
193         BufferView * bv = gv_->documentBufferView();
194         DocIterator from = bv->cursor();
195
196         dispatch(FuncRequest(LFUN_ESCAPE));
197         dispatch(FuncRequest(LFUN_CHAR_FORWARD));
198         if (bv->cursor().atLastPos()) {
199                 continueFromBeginning();
200                 return;
201         }
202         if (from == bv->cursor()) {
203                 //FIXME we must be at the end of a cell
204                 dispatch(FuncRequest(LFUN_CHAR_FORWARD));
205         }
206 }
207
208
209 void SpellcheckerWidget::on_languageCO_activated(int index)
210 {
211         string const lang =
212                 fromqstr(d->ui.languageCO->itemData(index).toString());
213         if (!d->word_.lang() || d->word_.lang()->lang() == lang)
214                 // nothing changed
215                 return;
216         dispatch(FuncRequest(LFUN_LANGUAGE, lang));
217         d->check();
218 }
219
220
221 void SpellcheckerWidget::on_ignoreAllPB_clicked()
222 {
223         /// replace all occurrences of word
224         if (d->word_.lang() && !d->word_.word().empty())
225                 theSpellChecker()->accept(d->word_);
226         d->forward();
227         d->check();
228 }
229
230
231 void SpellcheckerWidget::on_addPB_clicked()
232 {
233         /// insert word in personal dictionary
234         theSpellChecker()->insert(d->word_);
235         d->forward();
236         d->check();
237 }
238
239
240 void SpellcheckerWidget::on_ignorePB_clicked()
241 {
242         d->forward();
243         d->check();
244 }
245
246
247 void SpellcheckerWidget::on_findNextPB_clicked()
248 {
249         docstring const data = find2string(
250                                 qstring_to_ucs4(d->ui.wordED->text()),
251                                 true, true, true);
252         dispatch(FuncRequest(LFUN_WORD_FIND, data));
253 }
254
255
256 void SpellcheckerWidget::on_replacePB_clicked()
257 {
258         docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
259         docstring const data = replace2string(
260                 replacement, qstring_to_ucs4(d->ui.wordED->text()),
261                 true, true, false, false);
262
263         LYXERR(Debug::GUI, "Replace (" << replacement << ")");
264         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
265         d->forward();
266         d->check();
267 }
268
269
270 void SpellcheckerWidget::on_replaceAllPB_clicked()
271 {
272         docstring const data = replace2string(
273                 qstring_to_ucs4(d->ui.replaceCO->currentText()),
274                 qstring_to_ucs4(d->ui.wordED->text()),
275                 true, true, true, true);
276         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
277         d->forward();
278         d->check(); // continue spellchecking
279 }
280
281
282 void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
283 {
284         QString const suggestion = toqstr(word_.word());
285         ui.wordED->setText(suggestion);
286         QListWidget * lw = ui.suggestionsLW;
287         lw->clear();
288
289         if (words.empty()) {
290                 p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
291                 return;
292         }
293         for (size_t i = 0; i != words.size(); ++i)
294                 lw->addItem(toqstr(words[i]));
295
296         p->on_suggestionsLW_itemClicked(lw->item(0));
297         lw->setCurrentRow(0);
298 }
299
300
301 void SpellcheckerWidget::Private::setLanguage(Language const * lang)
302 {
303         int const pos = ui.languageCO->findData(toqstr(lang->lang()));
304         if (pos != -1)
305                 ui.languageCO->setCurrentIndex(pos);
306 }
307
308
309 void SpellcheckerWidget::Private::check()
310 {
311         BufferView * bv = gv_->documentBufferView();
312         if (!bv || bv->buffer().text().empty())
313                 return;
314
315         DocIterator from = bv->cursor();
316         DocIterator to;
317         WordLangTuple word_lang;
318         docstring_list suggestions;
319
320         int progress;
321         try {
322                 progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
323         } catch (ExceptionMessage const & message) {
324                 if (message.type_ == WarningException) {
325                         Alert::warning(message.title_, message.details_);
326                         return;
327                 }
328                 throw message;
329         }
330
331         // end of document
332         if (from == doc_iterator_end(&bv->buffer())) {
333                 if (continueFromBeginning())
334                         check();
335                 return;
336         }
337
338         word_ = word_lang;
339
340         // set suggestions
341         updateSuggestions(suggestions);
342         // set language
343         setLanguage(word_lang.lang());
344
345         // FIXME LFUN
346         // If we used a LFUN, dispatch would do all of this for us
347         int const size = to.pos() - from.pos();
348         bv->putSelectionAt(from, size, false);
349         bv->processUpdateFlags(Update::Force | Update::FitCursor);      
350 }
351
352
353 GuiSpellchecker::GuiSpellchecker(GuiView & parent,
354                 Qt::DockWidgetArea area, Qt::WindowFlags flags)
355         : DockView(parent, "spellchecker", qt_("Spellchecker"),
356                    area, flags)
357 {
358         widget_ = new SpellcheckerWidget(&parent);
359         setWidget(widget_);
360         setFocusProxy(widget_);
361 }
362
363
364 GuiSpellchecker::~GuiSpellchecker()
365 {
366         setFocusProxy(0);
367         delete widget_;
368 }
369
370
371 void GuiSpellchecker::updateView()
372 {
373         widget_->updateView();
374 }
375
376
377 Dialog * createGuiSpellchecker(GuiView & lv) 
378
379         GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
380 #ifdef Q_WS_MACX
381         gui->setFloating(true);
382 #endif
383         return gui;
384 }
385
386
387 } // namespace frontend
388 } // namespace lyx
389
390 #include "moc_GuiSpellchecker.cpp"