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