2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-08-31 05:53:55 +00:00
|
|
|
* \file GuiSpellchecker.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
2007-10-06 19:51:03 +00:00
|
|
|
* \author Edwin Leuven
|
2009-04-19 08:57:43 +00:00
|
|
|
* \author Abdelrazak Younes
|
2006-03-05 17:24:44 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiSpellchecker.h"
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
#include "ui_SpellcheckerUi.h"
|
|
|
|
|
2007-10-06 19:51:03 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "BufferView.h"
|
2009-04-04 16:40:47 +00:00
|
|
|
#include "buffer_funcs.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
#include "Cursor.h"
|
|
|
|
#include "CutAndPaste.h"
|
2009-06-22 17:40:09 +00:00
|
|
|
#include "FuncRequest.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
#include "Language.h"
|
2009-03-29 21:18:16 +00:00
|
|
|
#include "LyX.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
#include "LyXRC.h"
|
2009-07-04 17:15:27 +00:00
|
|
|
#include "lyxfind.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
#include "Paragraph.h"
|
2009-04-19 08:57:43 +00:00
|
|
|
#include "WordLangTuple.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
#include "support/docstring.h"
|
2009-04-04 16:40:47 +00:00
|
|
|
#include "support/docstring_list.h"
|
|
|
|
#include "support/ExceptionMessage.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/gettext.h"
|
2007-11-13 23:21:29 +00:00
|
|
|
#include "support/lstrings.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/textutils.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2006-05-03 19:51:15 +00:00
|
|
|
#include <QListWidgetItem>
|
2007-04-24 15:32:14 +00:00
|
|
|
|
2009-03-29 19:49:52 +00:00
|
|
|
#include "SpellChecker.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
struct GuiSpellchecker::Private
|
|
|
|
{
|
|
|
|
Private() : progress_(0), count_(0) {}
|
|
|
|
Ui::SpellcheckerUi ui;
|
|
|
|
/// current word being checked and lang code
|
|
|
|
WordLangTuple word_;
|
|
|
|
/// values for progress
|
|
|
|
int total_;
|
|
|
|
int progress_;
|
|
|
|
/// word count
|
|
|
|
int count_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-23 09:44:02 +00:00
|
|
|
GuiSpellchecker::GuiSpellchecker(GuiView & lv)
|
2009-04-19 08:57:43 +00:00
|
|
|
: DockView(lv, "spellchecker", qt_("Spellchecker"),
|
|
|
|
Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
|
2007-04-24 15:32:14 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
d->ui.setupUi(this);
|
|
|
|
|
|
|
|
connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
|
|
|
|
this, SLOT(on_replacePB_clicked()));
|
2007-04-24 15:32:14 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
d->ui.wordED->setReadOnly(true);
|
|
|
|
}
|
2007-04-24 15:32:14 +00:00
|
|
|
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
GuiSpellchecker::~GuiSpellchecker()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
|
|
|
|
void GuiSpellchecker::on_closePB_clicked()
|
|
|
|
{
|
|
|
|
close();
|
2007-09-05 20:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 21:06:21 +00:00
|
|
|
void GuiSpellchecker::on_suggestionsLW_itemChanged(QListWidgetItem * item)
|
2007-04-24 15:32:14 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
if (d->ui.replaceCO->count() != 0)
|
|
|
|
d->ui.replaceCO->setItemText(0, item->text());
|
2007-04-24 15:32:14 +00:00
|
|
|
else
|
2009-04-19 08:57:43 +00:00
|
|
|
d->ui.replaceCO->addItem(item->text());
|
2007-04-24 15:32:14 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
d->ui.replaceCO->setCurrentIndex(0);
|
2007-04-24 15:32:14 +00:00
|
|
|
}
|
|
|
|
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2009-04-21 21:06:21 +00:00
|
|
|
void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
|
2007-04-24 15:32:14 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
QListWidget * lw = d->ui.suggestionsLW;
|
|
|
|
if (lw->currentItem() && lw->currentItem()->text() == str)
|
2007-04-24 15:32:14 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
for (int i = 0; i != lw->count(); ++i) {
|
|
|
|
if (lw->item(i)->text() == str) {
|
|
|
|
lw->setCurrentRow(i);
|
2007-04-24 15:32:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
void GuiSpellchecker::updateView()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-04-04 17:25:14 +00:00
|
|
|
if (hasFocus())
|
2007-10-06 19:51:03 +00:00
|
|
|
check();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 21:06:21 +00:00
|
|
|
void GuiSpellchecker::on_ignoreAllPB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
/// replace all occurances of word
|
|
|
|
theSpellChecker()->accept(d->word_);
|
2009-04-04 16:40:47 +00:00
|
|
|
check();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
void GuiSpellchecker::on_addPB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
/// insert word in personal dictionary
|
|
|
|
theSpellChecker()->insert(d->word_);
|
2009-04-04 16:40:47 +00:00
|
|
|
check();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
void GuiSpellchecker::on_ignorePB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-06-22 17:40:09 +00:00
|
|
|
dispatch(FuncRequest(LFUN_CHAR_FORWARD));
|
2007-10-06 19:51:03 +00:00
|
|
|
check();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-04 17:15:27 +00:00
|
|
|
void GuiSpellchecker::on_findNextPB_clicked()
|
|
|
|
{
|
|
|
|
docstring const data = find2string(
|
|
|
|
qstring_to_ucs4(d->ui.wordED->text()),
|
|
|
|
true, true, true);
|
|
|
|
dispatch(FuncRequest(LFUN_WORD_FIND, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
void GuiSpellchecker::on_replacePB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
|
|
|
|
|
|
|
|
LYXERR(Debug::GUI, "Replace (" << replacement << ")");
|
|
|
|
BufferView * bv = const_cast<BufferView *>(bufferview());
|
|
|
|
cap::replaceSelectionWithString(bv->cursor(), replacement, true);
|
|
|
|
bv->buffer().markDirty();
|
|
|
|
// If we used an LFUN, we would not need that
|
|
|
|
bv->processUpdateFlags(Update::Force | Update::FitCursor);
|
|
|
|
// fix up the count
|
|
|
|
--d->count_;
|
|
|
|
check();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-04 17:15:27 +00:00
|
|
|
void GuiSpellchecker::on_replaceAllPB_clicked()
|
|
|
|
{
|
|
|
|
docstring const data = replace2string(
|
|
|
|
qstring_to_ucs4(d->ui.replaceCO->currentText()),
|
2009-07-04 23:02:27 +00:00
|
|
|
qstring_to_ucs4(d->ui.wordED->text()),
|
2009-07-04 17:15:27 +00:00
|
|
|
true, true, true, true);
|
|
|
|
dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-04 16:40:47 +00:00
|
|
|
void GuiSpellchecker::updateSuggestions(docstring_list & words)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
QString const suggestion = toqstr(d->word_.word());
|
|
|
|
d->ui.wordED->setText(suggestion);
|
|
|
|
QListWidget * lw = d->ui.suggestionsLW;
|
|
|
|
lw->clear();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-04-04 16:55:13 +00:00
|
|
|
if (words.empty()) {
|
2009-04-21 21:06:21 +00:00
|
|
|
on_suggestionsLW_itemChanged(new QListWidgetItem(suggestion));
|
2009-04-04 16:40:47 +00:00
|
|
|
return;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2009-04-04 16:40:47 +00:00
|
|
|
for (size_t i = 0; i != words.size(); ++i)
|
2009-04-19 08:57:43 +00:00
|
|
|
lw->addItem(toqstr(words[i]));
|
2009-04-04 16:40:47 +00:00
|
|
|
|
2009-04-21 21:06:21 +00:00
|
|
|
on_suggestionsLW_itemChanged(lw->item(0));
|
2009-04-19 08:57:43 +00:00
|
|
|
lw->setCurrentRow(0);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
bool GuiSpellchecker::initialiseParams(string const &)
|
2007-10-06 19:51:03 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2009-04-04 16:40:47 +00:00
|
|
|
if (!theSpellChecker())
|
2007-10-06 19:51:03 +00:00
|
|
|
return false;
|
|
|
|
|
2009-04-04 16:40:47 +00:00
|
|
|
DocIterator const begin = doc_iterator_begin(&buffer());
|
|
|
|
Cursor const & cur = bufferview()->cursor();
|
2009-04-19 08:57:43 +00:00
|
|
|
d->progress_ = countWords(begin, cur);
|
|
|
|
d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
|
|
|
|
d->count_ = 0;
|
2009-04-04 16:40:47 +00:00
|
|
|
return true;
|
2007-10-06 19:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiSpellchecker::check()
|
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "Check the spelling of a word");
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2009-04-04 16:40:47 +00:00
|
|
|
DocIterator from = bufferview()->cursor();
|
|
|
|
DocIterator to;
|
|
|
|
WordLangTuple word_lang;
|
|
|
|
docstring_list suggestions;
|
|
|
|
|
|
|
|
int progress;
|
|
|
|
try {
|
|
|
|
progress = buffer().spellCheck(from, to, word_lang, suggestions);
|
|
|
|
} catch (ExceptionMessage const & message) {
|
|
|
|
if (message.type_ == WarningException) {
|
|
|
|
Alert::warning(message.title_, message.details_);
|
2009-04-19 08:57:43 +00:00
|
|
|
close();
|
2007-10-06 19:51:03 +00:00
|
|
|
return;
|
2009-04-04 16:40:47 +00:00
|
|
|
}
|
|
|
|
throw message;
|
|
|
|
}
|
|
|
|
LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
|
2009-04-19 08:57:43 +00:00
|
|
|
d->count_ += progress;
|
|
|
|
d->progress_ += progress;
|
2009-04-04 16:40:47 +00:00
|
|
|
|
|
|
|
// end of document
|
2009-06-22 21:49:48 +00:00
|
|
|
if (from == doc_iterator_end(&buffer())) {
|
2009-04-04 16:40:47 +00:00
|
|
|
showSummary();
|
|
|
|
return;
|
2007-10-06 19:51:03 +00:00
|
|
|
}
|
2009-04-04 17:25:14 +00:00
|
|
|
if (!isVisible())
|
|
|
|
show();
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
d->word_ = word_lang;
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
int const progress_bar = d->total_
|
|
|
|
? int(100.0 * float(d->progress_)/d->total_) : 100;
|
2009-04-04 16:40:47 +00:00
|
|
|
LYXERR(Debug::GUI, "Updating spell progress.");
|
|
|
|
// set progress bar
|
2009-04-19 08:57:43 +00:00
|
|
|
d->ui.spellcheckPR->setValue(progress_bar);
|
2009-04-04 16:40:47 +00:00
|
|
|
// set suggestions
|
|
|
|
updateSuggestions(suggestions);
|
2007-10-06 19:51:03 +00:00
|
|
|
|
|
|
|
// FIXME: if we used a lfun like in find/replace, dispatch would do
|
|
|
|
// that for us
|
2009-04-04 16:40:47 +00:00
|
|
|
int const size = to.pos() - from.pos();
|
|
|
|
BufferView * bv = const_cast<BufferView *>(bufferview());
|
|
|
|
bv->putSelectionAt(from, size, false);
|
2007-10-06 19:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiSpellchecker::showSummary()
|
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
if (d->count_ == 0) {
|
|
|
|
close();
|
2007-10-06 19:51:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
docstring message;
|
2009-04-19 08:57:43 +00:00
|
|
|
if (d->count_ != 1)
|
|
|
|
message = bformat(_("%1$d words checked."), d->count_);
|
2007-10-06 19:51:03 +00:00
|
|
|
else
|
|
|
|
message = _("One word checked.");
|
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
close();
|
2007-10-06 19:51:03 +00:00
|
|
|
Alert::information(_("Spelling check completed"), message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-23 09:44:02 +00:00
|
|
|
Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
|
2007-10-06 19:51:03 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2007-04-24 15:32:14 +00:00
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_GuiSpellchecker.cpp"
|