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