]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
* fix spelling in comments to please John.
[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
17 #include "qt_helpers.h"
18
19 #include "ui_SpellcheckerUi.h"
20
21 #include "Buffer.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "buffer_funcs.h"
25 #include "Cursor.h"
26 #include "CutAndPaste.h"
27 #include "FuncRequest.h"
28 #include "Language.h"
29 #include "LyX.h"
30 #include "LyXRC.h"
31 #include "lyxfind.h"
32 #include "Paragraph.h"
33 #include "WordLangTuple.h"
34
35 #include "support/debug.h"
36 #include "support/docstring.h"
37 #include "support/docstring_list.h"
38 #include "support/ExceptionMessage.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41 #include "support/textutils.h"
42
43 #include <QListWidgetItem>
44 #include <QKeyEvent>
45
46 #include "SpellChecker.h"
47
48 #include "frontends/alert.h"
49
50 using namespace std;
51 using namespace lyx::support;
52
53 namespace lyx {
54 namespace frontend {
55
56
57 struct GuiSpellchecker::Private
58 {
59         Private() : progress_(0), count_(0) {}
60         Ui::SpellcheckerUi ui;
61         /// current word being checked and lang code
62         WordLangTuple word_;
63         /// values for progress
64         int total_;
65         int progress_;
66         /// word count
67         int count_;
68 };
69
70
71 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
72         : DockView(lv, "spellchecker", qt_("Spellchecker"),
73         Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
74 {
75         d->ui.setupUi(this);
76
77         connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
78                 this, SLOT(on_replacePB_clicked()));
79
80         d->ui.wordED->setReadOnly(true);
81
82         d->ui.suggestionsLW->installEventFilter(this);
83 }
84
85
86 GuiSpellchecker::~GuiSpellchecker()
87 {
88         delete d;
89 }
90
91
92 void GuiSpellchecker::on_closePB_clicked()
93 {
94         close();
95 }
96
97
98 bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
99 {
100         if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
101                 QKeyEvent *e = static_cast<QKeyEvent *> (event);
102                 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
103                         on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
104                         on_replacePB_clicked();
105                         return true;
106                 } else if (e->key() == Qt::Key_Right) {
107                         on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
108                         return true;
109                 }
110         }
111         // standard event processing
112         return QWidget::eventFilter(obj, event);
113 }
114
115
116 void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
117 {
118         if (d->ui.replaceCO->count() != 0)
119                 d->ui.replaceCO->setItemText(0, item->text());
120         else
121                 d->ui.replaceCO->addItem(item->text());
122
123         d->ui.replaceCO->setCurrentIndex(0);
124 }
125
126
127 void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
128 {
129         QListWidget * lw = d->ui.suggestionsLW;
130         if (lw->currentItem() && lw->currentItem()->text() == str)
131                 return;
132
133         for (int i = 0; i != lw->count(); ++i) {
134                 if (lw->item(i)->text() == str) {
135                         lw->setCurrentRow(i);
136                         break;
137                 }
138         }
139 }
140
141
142 void GuiSpellchecker::updateView()
143 {
144         if (hasFocus())
145                 check();
146 }
147
148
149 void GuiSpellchecker::on_ignoreAllPB_clicked()
150 {
151         /// replace all occurances of word
152         theSpellChecker()->accept(d->word_);
153         check();
154 }
155
156
157 void GuiSpellchecker::on_addPB_clicked()
158 {
159         /// insert word in personal dictionary
160         theSpellChecker()->insert(d->word_);
161         check();
162 }
163
164
165 void GuiSpellchecker::on_ignorePB_clicked()
166 {
167         dispatch(FuncRequest(LFUN_CHAR_FORWARD));
168         check();
169 }
170
171
172 void GuiSpellchecker::on_findNextPB_clicked()
173 {
174         docstring const data = find2string(
175                                 qstring_to_ucs4(d->ui.wordED->text()),
176                                 true, true, true);
177         dispatch(FuncRequest(LFUN_WORD_FIND, data));
178 }
179
180
181 void GuiSpellchecker::on_replacePB_clicked()
182 {
183         docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
184
185         LYXERR(Debug::GUI, "Replace (" << replacement << ")");
186         BufferView * bv = const_cast<BufferView *>(bufferview());
187         if (!bv->cursor().inTexted())
188                 return;
189         cap::replaceSelectionWithString(bv->cursor(), replacement, true);
190         bv->buffer().markDirty();
191         // If we used an LFUN, we would not need that
192         bv->processUpdateFlags(Update::Force | Update::FitCursor);
193         // fix up the count
194         --d->count_;
195         check();
196 }
197
198
199 void GuiSpellchecker::on_replaceAllPB_clicked()
200 {
201         docstring const data = replace2string(
202                 qstring_to_ucs4(d->ui.replaceCO->currentText()),
203                 qstring_to_ucs4(d->ui.wordED->text()),
204                 true, true, true, true);
205         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
206         check(); // continue spellchecking
207 }
208
209
210 void GuiSpellchecker::updateSuggestions(docstring_list & words)
211 {
212         QString const suggestion = toqstr(d->word_.word());
213         d->ui.wordED->setText(suggestion);
214         QListWidget * lw = d->ui.suggestionsLW;
215         lw->clear();
216
217         if (words.empty()) {
218                 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
219                 return;
220         }
221         for (size_t i = 0; i != words.size(); ++i)
222                 lw->addItem(toqstr(words[i]));
223
224         on_suggestionsLW_itemClicked(lw->item(0));
225         lw->setCurrentRow(0);
226 }
227
228
229 bool GuiSpellchecker::initialiseParams(string const &)
230 {
231         LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
232
233         if (!theSpellChecker())
234                 return false;
235
236         DocIterator const begin = doc_iterator_begin(&buffer());
237         Cursor const & cur = bufferview()->cursor();
238         d->progress_ = countWords(begin, cur);
239         d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
240         d->count_ = 0;
241         return true;
242 }
243
244
245 void GuiSpellchecker::check()
246 {
247         LYXERR(Debug::GUI, "Check the spelling of a word");
248
249         DocIterator from = bufferview()->cursor();
250         DocIterator to;
251         WordLangTuple word_lang;
252         docstring_list suggestions;
253
254         int progress;
255         try {
256                 progress = buffer().spellCheck(from, to, word_lang, suggestions);
257         } catch (ExceptionMessage const & message) {
258                 if (message.type_ == WarningException) {
259                         Alert::warning(message.title_, message.details_);
260                         close();
261                         return;
262                 }
263                 throw message;
264         }
265         LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
266         d->count_ += progress;
267         d->progress_ += progress;
268
269         // end of document
270         if (from == doc_iterator_end(&buffer())) {
271                 showSummary();
272                 return;
273         }
274         if (!isVisible())
275                 show();
276
277         d->word_ = word_lang;
278
279         int const progress_bar = d->total_
280                 ? int(100.0 * float(d->progress_)/d->total_) : 100;
281         LYXERR(Debug::GUI, "Updating spell progress.");
282         // set progress bar
283         d->ui.spellcheckPR->setValue(progress_bar);
284         // set suggestions
285         updateSuggestions(suggestions);
286
287         // FIXME: if we used a lfun like in find/replace, dispatch would do
288         // that for us
289         int const size = to.pos() - from.pos();
290         BufferView * bv = const_cast<BufferView *>(bufferview());
291         bv->putSelectionAt(from, size, false);
292 }
293
294
295 void GuiSpellchecker::showSummary()
296 {
297         if (d->count_ == 0) {
298                 close();
299                 return;
300         }
301
302         docstring message;
303         if (d->count_ != 1)
304                 message = bformat(_("%1$d words checked."), d->count_);
305         else
306                 message = _("One word checked.");
307
308         close();
309         Alert::information(_("Spelling check completed"), message);
310 }
311
312
313 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
314
315 } // namespace frontend
316 } // namespace lyx
317
318 #include "moc_GuiSpellchecker.cpp"