]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
Spellchecker: More simplifications.
[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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiSpellchecker.h"
15
16 #include "qt_helpers.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "buffer_funcs.h"
22 #include "Cursor.h"
23 #include "CutAndPaste.h"
24 #include "Language.h"
25 #include "LyX.h"
26 #include "LyXRC.h"
27 #include "Paragraph.h"
28
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstring_list.h"
32 #include "support/ExceptionMessage.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/textutils.h"
36
37 #include <QListWidgetItem>
38
39 #include "SpellChecker.h"
40
41 #include "frontends/alert.h"
42
43 using namespace std;
44 using namespace lyx::support;
45
46 namespace lyx {
47 namespace frontend {
48
49
50 GuiSpellchecker::GuiSpellchecker(GuiView & lv)
51         : GuiDialog(lv, "spellchecker", qt_("Spellchecker")),
52           progress_(0), count_(0)
53 {
54         setupUi(this);
55
56         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
57         connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
58         connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
59         connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
60         connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
61
62         connect(replaceCO, SIGNAL(highlighted(QString)),
63                 this, SLOT(replaceChanged(QString)));
64         connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
65                 this, SLOT(replace()));
66         connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
67                 this, SLOT(suggestionChanged(QListWidgetItem*)));
68
69         wordED->setReadOnly(true);
70
71         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
72         bc().setCancel(closePB);
73 }
74
75
76 void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
77 {
78         if (replaceCO->count() != 0)
79                 replaceCO->setItemText(0, item->text());
80         else
81                 replaceCO->addItem(item->text());
82
83         replaceCO->setCurrentIndex(0);
84 }
85
86
87 void GuiSpellchecker::replaceChanged(const QString & str)
88 {
89         if (suggestionsLW->currentItem()
90             && suggestionsLW->currentItem()->text() == str)
91                 return;
92
93         for (int i = 0; i != suggestionsLW->count(); ++i) {
94                 if (suggestionsLW->item(i)->text() == str) {
95                         suggestionsLW->setCurrentRow(i);
96                         break;
97                 }
98         }
99 }
100
101
102 void GuiSpellchecker::reject()
103 {
104         slotClose();
105         QDialog::reject();
106 }
107
108
109 void GuiSpellchecker::updateContents()
110 {
111         if (hasFocus())
112                 check();
113 }
114
115
116 void GuiSpellchecker::accept()
117 {
118         theSpellChecker()->accept(word_);
119         check();
120 }
121
122
123 void GuiSpellchecker::add()
124 {
125         theSpellChecker()->insert(word_);
126         check();
127 }
128
129
130 void GuiSpellchecker::ignore()
131 {
132         check();
133 }
134
135
136 void GuiSpellchecker::replace()
137 {
138         replace(qstring_to_ucs4(replaceCO->currentText()));
139 }
140
141
142 void GuiSpellchecker::updateSuggestions(docstring_list & words)
143 {
144         wordED->setText(toqstr(word_.word()));
145         suggestionsLW->clear();
146
147         if (words.empty()) {
148                 suggestionChanged(new QListWidgetItem(wordED->text()));
149                 return;
150         }
151         for (size_t i = 0; i != words.size(); ++i)
152                 suggestionsLW->addItem(toqstr(words[i]));
153
154         suggestionChanged(suggestionsLW->item(0));
155         suggestionsLW->setCurrentRow(0);
156 }
157
158
159 bool GuiSpellchecker::initialiseParams(string const &)
160 {
161         LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
162
163         if (!theSpellChecker())
164                 return false;
165
166         DocIterator const begin = doc_iterator_begin(&buffer());
167         Cursor const & cur = bufferview()->cursor();
168         progress_ = countWords(begin, cur);
169         total_ = progress_ + countWords(cur, doc_iterator_end(&buffer()));
170         count_ = 0;
171         return true;
172 }
173
174
175 void GuiSpellchecker::check()
176 {
177         LYXERR(Debug::GUI, "Check the spelling of a word");
178
179         DocIterator from = bufferview()->cursor();
180         DocIterator to;
181         WordLangTuple word_lang;
182         docstring_list suggestions;
183
184         int progress;
185         try {
186                 progress = buffer().spellCheck(from, to, word_lang, suggestions);
187         } catch (ExceptionMessage const & message) {
188                 if (message.type_ == WarningException) {
189                         Alert::warning(message.title_, message.details_);
190                         slotClose();
191                         return;
192                 }
193                 throw message;
194         }
195         LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
196         count_ += progress;
197         progress_ += progress;
198
199         // end of document
200         if (from == to) {
201                 showSummary();
202                 return;
203         }
204         if (!isVisible())
205                 show();
206
207         word_ = word_lang;
208
209         int const progress_bar = total_
210                 ? int(100.0 * float(progress_)/total_) : 100;
211         LYXERR(Debug::GUI, "Updating spell progress.");
212         // set progress bar
213         spellcheckPR->setValue(progress_bar);
214         // set suggestions
215         updateSuggestions(suggestions);
216
217         // FIXME: if we used a lfun like in find/replace, dispatch would do
218         // that for us
219         int const size = to.pos() - from.pos();
220         BufferView * bv = const_cast<BufferView *>(bufferview());
221         bv->putSelectionAt(from, size, false);
222 }
223
224
225 void GuiSpellchecker::showSummary()
226 {
227         if (count_ == 0) {
228                 slotClose();
229                 return;
230         }
231
232         docstring message;
233         if (count_ != 1)
234                 message = bformat(_("%1$d words checked."), count_);
235         else
236                 message = _("One word checked.");
237
238         slotClose();
239         Alert::information(_("Spelling check completed"), message);
240 }
241
242
243 void GuiSpellchecker::replace(docstring const & replacement)
244 {
245         LYXERR(Debug::GUI, "GuiSpellchecker::replace("
246                            << to_utf8(replacement) << ")");
247         BufferView * bv = const_cast<BufferView *>(bufferview());
248         cap::replaceSelectionWithString(bv->cursor(), replacement, true);
249         bv->buffer().markDirty();
250         // If we used an LFUN, we would not need that
251         bv->processUpdateFlags(Update::Force | Update::FitCursor);
252         // fix up the count
253         --count_;
254         check();
255 }
256
257
258 void GuiSpellchecker::replaceAll(docstring const & replacement)
259 {
260         // TODO: add to list
261         replace(replacement);
262 }
263
264
265 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
266
267 } // namespace frontend
268 } // namespace lyx
269
270 #include "moc_GuiSpellchecker.cpp"