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