]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[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_itemClicked(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         check(); // continue spellchecking
186 }
187
188
189 void GuiSpellchecker::updateSuggestions(docstring_list & words)
190 {
191         QString const suggestion = toqstr(d->word_.word());
192         d->ui.wordED->setText(suggestion);
193         QListWidget * lw = d->ui.suggestionsLW;
194         lw->clear();
195
196         if (words.empty()) {
197                 on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
198                 return;
199         }
200         for (size_t i = 0; i != words.size(); ++i)
201                 lw->addItem(toqstr(words[i]));
202
203         on_suggestionsLW_itemClicked(lw->item(0));
204         lw->setCurrentRow(0);
205 }
206
207
208 bool GuiSpellchecker::initialiseParams(string const &)
209 {
210         LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
211
212         if (!theSpellChecker())
213                 return false;
214
215         DocIterator const begin = doc_iterator_begin(&buffer());
216         Cursor const & cur = bufferview()->cursor();
217         d->progress_ = countWords(begin, cur);
218         d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
219         d->count_ = 0;
220         return true;
221 }
222
223
224 void GuiSpellchecker::check()
225 {
226         LYXERR(Debug::GUI, "Check the spelling of a word");
227
228         DocIterator from = bufferview()->cursor();
229         DocIterator to;
230         WordLangTuple word_lang;
231         docstring_list suggestions;
232
233         int progress;
234         try {
235                 progress = buffer().spellCheck(from, to, word_lang, suggestions);
236         } catch (ExceptionMessage const & message) {
237                 if (message.type_ == WarningException) {
238                         Alert::warning(message.title_, message.details_);
239                         close();
240                         return;
241                 }
242                 throw message;
243         }
244         LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
245         d->count_ += progress;
246         d->progress_ += progress;
247
248         // end of document
249         if (from == doc_iterator_end(&buffer())) {
250                 showSummary();
251                 return;
252         }
253         if (!isVisible())
254                 show();
255
256         d->word_ = word_lang;
257
258         int const progress_bar = d->total_
259                 ? int(100.0 * float(d->progress_)/d->total_) : 100;
260         LYXERR(Debug::GUI, "Updating spell progress.");
261         // set progress bar
262         d->ui.spellcheckPR->setValue(progress_bar);
263         // set suggestions
264         updateSuggestions(suggestions);
265
266         // FIXME: if we used a lfun like in find/replace, dispatch would do
267         // that for us
268         int const size = to.pos() - from.pos();
269         BufferView * bv = const_cast<BufferView *>(bufferview());
270         bv->putSelectionAt(from, size, false);
271 }
272
273
274 void GuiSpellchecker::showSummary()
275 {
276         if (d->count_ == 0) {
277                 close();
278                 return;
279         }
280
281         docstring message;
282         if (d->count_ != 1)
283                 message = bformat(_("%1$d words checked."), d->count_);
284         else
285                 message = _("One word checked.");
286
287         close();
288         Alert::information(_("Spelling check completed"), message);
289 }
290
291
292 Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
293
294 } // namespace frontend
295 } // namespace lyx
296
297 #include "moc_GuiSpellchecker.cpp"