*
* \author John Levon
* \author Edwin Leuven
+ * \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#include "qt_helpers.h"
+#include "ui_SpellcheckerUi.h"
+
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "buffer_funcs.h"
#include "Cursor.h"
#include "CutAndPaste.h"
+#include "FuncRequest.h"
#include "Language.h"
#include "LyX.h"
#include "LyXRC.h"
+#include "lyxfind.h"
#include "Paragraph.h"
+#include "WordLangTuple.h"
#include "support/debug.h"
#include "support/docstring.h"
#include "support/textutils.h"
#include <QListWidgetItem>
+#include <QKeyEvent>
#include "SpellChecker.h"
namespace frontend {
+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_;
+};
+
+
GuiSpellchecker::GuiSpellchecker(GuiView & lv)
- : GuiDialog(lv, "spellchecker", qt_("Spellchecker")), exitEarly_(false),
- progress_(0), count_(0)
+ : DockView(lv, "spellchecker", qt_("Spellchecker"),
+ Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
{
- setupUi(this);
+ d->ui.setupUi(this);
+
+ connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
+ this, SLOT(on_replacePB_clicked()));
+
+ d->ui.wordED->setReadOnly(true);
+
+ d->ui.suggestionsLW->installEventFilter(this);
+}
- connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
- connect(replacePB, SIGNAL(clicked()), this, SLOT(replace()));
- connect(ignorePB, SIGNAL(clicked()), this, SLOT(ignore()));
- connect(replacePB_3, SIGNAL(clicked()), this, SLOT(accept()));
- connect(addPB, SIGNAL(clicked()), this, SLOT(add()));
- connect(replaceCO, SIGNAL(highlighted(QString)),
- this, SLOT(replaceChanged(QString)));
- connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
- this, SLOT(replace()));
- connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
- this, SLOT(suggestionChanged(QListWidgetItem*)));
+GuiSpellchecker::~GuiSpellchecker()
+{
+ delete d;
+}
- wordED->setReadOnly(true);
- bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
- bc().setCancel(closePB);
+void GuiSpellchecker::on_closePB_clicked()
+{
+ close();
}
-void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
+bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
{
- if (replaceCO->count() != 0)
- replaceCO->setItemText(0, item->text());
+ if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
+ QKeyEvent *e = static_cast<QKeyEvent *> (event);
+ if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
+ on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
+ on_replacePB_clicked();
+ return true;
+ } else if (e->key() == Qt::Key_Right) {
+ on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
+ return true;
+ }
+ }
+ // standard event processing
+ return QWidget::eventFilter(obj, event);
+}
+
+
+void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
+{
+ if (d->ui.replaceCO->count() != 0)
+ d->ui.replaceCO->setItemText(0, item->text());
else
- replaceCO->addItem(item->text());
+ d->ui.replaceCO->addItem(item->text());
- replaceCO->setCurrentIndex(0);
+ d->ui.replaceCO->setCurrentIndex(0);
}
-void GuiSpellchecker::replaceChanged(const QString & str)
+void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
{
- if (suggestionsLW->currentItem()
- && suggestionsLW->currentItem()->text() == str)
+ QListWidget * lw = d->ui.suggestionsLW;
+ if (lw->currentItem() && lw->currentItem()->text() == str)
return;
- for (int i = 0; i != suggestionsLW->count(); ++i) {
- if (suggestionsLW->item(i)->text() == str) {
- suggestionsLW->setCurrentRow(i);
+ for (int i = 0; i != lw->count(); ++i) {
+ if (lw->item(i)->text() == str) {
+ lw->setCurrentRow(i);
break;
}
}
}
-void GuiSpellchecker::reject()
+void GuiSpellchecker::updateView()
{
- slotClose();
- QDialog::reject();
+ if (hasFocus())
+ check();
}
-void GuiSpellchecker::updateContents()
+void GuiSpellchecker::on_ignoreAllPB_clicked()
{
- // The clauses below are needed because the spellchecker
- // has many flaws (see bugs 1950, 2218).
- // Basically, we have to distinguish the case where a
- // spellcheck has already been performed for the whole
- // document (exitEarly() == true, isVisible() == false)
- // from the rest (exitEarly() == false, isVisible() == true).
- // FIXME: rewrite the whole beast!
- static bool check_after_early_exit;
- if (exitEarly()) {
- // a spellcheck has already been performed,
- check();
- check_after_early_exit = true;
- }
- else if (isVisible()) {
- // the above check triggers a second update,
- // and isVisible() is true then. Prevent a
- // second check that skips the first word
- if (check_after_early_exit)
- // don't check, but reset the bool.
- // business as usual after this.
- check_after_early_exit = false;
- else
- // perform spellcheck (default case)
- check();
- }
+ /// replace all occurances of word
+ theSpellChecker()->accept(d->word_);
+ check();
}
-void GuiSpellchecker::accept()
+void GuiSpellchecker::on_addPB_clicked()
{
- theSpellChecker()->accept(word_);
+ /// insert word in personal dictionary
+ theSpellChecker()->insert(d->word_);
check();
}
-void GuiSpellchecker::add()
+void GuiSpellchecker::on_ignorePB_clicked()
{
- theSpellChecker()->insert(word_);
+ dispatch(FuncRequest(LFUN_CHAR_FORWARD));
check();
}
-void GuiSpellchecker::ignore()
+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));
+}
+
+
+void GuiSpellchecker::on_replacePB_clicked()
{
+ docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
+
+ LYXERR(Debug::GUI, "Replace (" << replacement << ")");
+ BufferView * bv = const_cast<BufferView *>(bufferview());
+ if (!bv->cursor().inTexted())
+ return;
+ 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();
}
-void GuiSpellchecker::replace()
+void GuiSpellchecker::on_replaceAllPB_clicked()
{
- replace(qstring_to_ucs4(replaceCO->currentText()));
+ docstring const data = replace2string(
+ qstring_to_ucs4(d->ui.replaceCO->currentText()),
+ qstring_to_ucs4(d->ui.wordED->text()),
+ true, true, true, true);
+ dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
+ check(); // continue spellchecking
}
void GuiSpellchecker::updateSuggestions(docstring_list & words)
{
- wordED->setText(toqstr(word_.word()));
- suggestionsLW->clear();
+ QString const suggestion = toqstr(d->word_.word());
+ d->ui.wordED->setText(suggestion);
+ QListWidget * lw = d->ui.suggestionsLW;
+ lw->clear();
- if (words.empty() == 0) {
- suggestionChanged(new QListWidgetItem(wordED->text()));
+ if (words.empty()) {
+ on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
return;
}
for (size_t i = 0; i != words.size(); ++i)
- suggestionsLW->addItem(toqstr(words[i]));
+ lw->addItem(toqstr(words[i]));
- suggestionChanged(suggestionsLW->item(0));
- suggestionsLW->setCurrentRow(0);
+ on_suggestionsLW_itemClicked(lw->item(0));
+ lw->setCurrentRow(0);
}
DocIterator const begin = doc_iterator_begin(&buffer());
Cursor const & cur = bufferview()->cursor();
- progress_ = countWords(begin, cur);
- total_ = progress_ + countWords(cur, doc_iterator_end(&buffer()));
- count_ = 0;
+ d->progress_ = countWords(begin, cur);
+ d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
+ d->count_ = 0;
return true;
}
-void GuiSpellchecker::clearParams()
-{
- LYXERR(Debug::GUI, "Spellchecker::clearParams");
-}
-
-
void GuiSpellchecker::check()
{
LYXERR(Debug::GUI, "Check the spelling of a word");
DocIterator from = bufferview()->cursor();
- while (from && from.pos() && isLetter(from))
- from.backwardPos();
DocIterator to;
- exitEarly_ = false;
WordLangTuple word_lang;
docstring_list suggestions;
} catch (ExceptionMessage const & message) {
if (message.type_ == WarningException) {
Alert::warning(message.title_, message.details_);
- slotClose();
+ close();
return;
}
throw message;
}
LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"");
- count_ += progress;
- progress_ += progress;
+ d->count_ += progress;
+ d->progress_ += progress;
// end of document
- if (from == to) {
+ if (from == doc_iterator_end(&buffer())) {
showSummary();
- exitEarly_ = true;
return;
}
- word_ = word_lang;
+ if (!isVisible())
+ show();
- int const progress_bar = total_
- ? int(100.0 * float(progress_)/total_) : 100;
+ d->word_ = word_lang;
+
+ int const progress_bar = d->total_
+ ? int(100.0 * float(d->progress_)/d->total_) : 100;
LYXERR(Debug::GUI, "Updating spell progress.");
// set progress bar
- spellcheckPR->setValue(progress_bar);
+ d->ui.spellcheckPR->setValue(progress_bar);
// set suggestions
updateSuggestions(suggestions);
void GuiSpellchecker::showSummary()
{
- if (count_ == 0) {
- slotClose();
+ if (d->count_ == 0) {
+ close();
return;
}
docstring message;
- if (count_ != 1)
- message = bformat(_("%1$d words checked."), count_);
+ if (d->count_ != 1)
+ message = bformat(_("%1$d words checked."), d->count_);
else
message = _("One word checked.");
- slotClose();
+ close();
Alert::information(_("Spelling check completed"), message);
}
-void GuiSpellchecker::replace(docstring const & replacement)
-{
- LYXERR(Debug::GUI, "GuiSpellchecker::replace("
- << to_utf8(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
- --count_;
- check();
-}
-
-
-void GuiSpellchecker::replaceAll(docstring const & replacement)
-{
- // TODO: add to list
- replace(replacement);
-}
-
-
Dialog * createGuiSpellchecker(GuiView & lv) { return new GuiSpellchecker(lv); }
} // namespace frontend