*
* \author John Levon
* \author Edwin Leuven
+ * \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "GuiSpellchecker.h"
+#include "GuiApplication.h"
#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 "Text.h"
#include "CutAndPaste.h"
-#include "debug.h"
-#include "gettext.h"
+#include "FuncRequest.h"
#include "Language.h"
+#include "LyX.h"
#include "LyXRC.h"
+#include "lyxfind.h"
#include "Paragraph.h"
+#include "WordLangTuple.h"
-#include "support/textutils.h"
-#include "support/convert.h"
+#include "support/debug.h"
#include "support/docstring.h"
+#include "support/docstring_list.h"
+#include "support/ExceptionMessage.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
+#include "support/textutils.h"
-#include <QProgressBar>
-#include <QLineEdit>
-#include <QPushButton>
-#include <QListWidget>
+#include <QKeyEvent>
#include <QListWidgetItem>
-#include <QCloseEvent>
-#include <QSyntaxHighlighter>
-#include <QTextCharFormat>
-#include <QTextDocument>
-
-#if defined(USE_ASPELL)
-# include "ASpell_local.h"
-#elif defined(USE_PSPELL)
-# include "PSpell.h"
-#endif
+#include <QMessageBox>
-#if defined(USE_ISPELL)
-# include "ISpell.h"
-#else
-# include "SpellBase.h"
-#endif
+#include "SpellChecker.h"
#include "frontends/alert.h"
-// FIXME: those two headers are needed because of the
-// WorkArea::redraw() call below.
-#include "frontends/LyXView.h"
-#include "frontends/WorkArea.h"
-
-using std::advance;
-using std::distance;
-using std::endl;
-using std::string;
+using namespace std;
+using namespace lyx::support;
namespace lyx {
namespace frontend {
-using support::bformat;
-using support::contains;
-GuiSpellchecker::GuiSpellchecker(LyXView & lv)
- : GuiDialog(lv, "spellchecker"), exitEarly_(false),
- oldval_(0), newvalue_(0), count_(0), speller_(0)
+struct SpellcheckerWidget::Private
+{
+ Private(SpellcheckerWidget * parent, DockView * dv, GuiView * gv)
+ : p(parent), dv_(dv), gv_(gv), incheck_(false), wrap_around_(false) {}
+ /// update from controller
+ void updateSuggestions(docstring_list & words);
+ /// move to next position after current word
+ void forward();
+ /// check text until next misspelled/unknown word
+ void check();
+ /// close the spell checker dialog
+ void hide() const;
+ /// make/restore a selection between from and to
+ void setSelection(DocIterator const & from, DocIterator const & to) const;
+ /// if no selection was checked:
+ /// ask the user if the check should start over
+ bool continueFromBeginning();
+ /// set the given language in language chooser
+ void setLanguage(Language const * lang);
+ /// test and set guard flag
+ bool inCheck() {
+ if (incheck_)
+ return true;
+ incheck_ = true;
+ return false;
+ }
+ void canCheck() { incheck_ = false; }
+ /// check for wrap around
+ void wrapAround(bool flag) {
+ wrap_around_ = flag;
+ if (flag) {
+ end_ = start_;
+ }
+ }
+ /// test for existing association with a document buffer
+ /// and test for already active check
+ bool disabled() {
+ return gv_->documentBufferView() == 0 || inCheck();
+ }
+ /// the cursor position of the buffer view
+ DocIterator const cursor() const;
+ /// status checks
+ bool isCurrentBuffer(DocIterator const & cursor) const;
+ bool isWrapAround(DocIterator const & cursor) const;
+ bool isWrapAround() const { return wrap_around_; }
+ bool atLastPos(DocIterator const & cursor) const;
+ /// validate the cached doc iterators
+ /// The spell checker dialog is not modal.
+ /// The user may change the buffer being checked and break the iterators.
+ void fixPositionsIfBroken();
+ ///
+ Ui::SpellcheckerUi ui;
+ ///
+ SpellcheckerWidget * p;
+ ///
+ DockView * dv_;
+ ///
+ GuiView * gv_;
+ /// current word being checked and lang code
+ WordLangTuple word_;
+ /// cursor position where spell checking starts
+ DocIterator start_;
+ /// range to spell check
+ /// for selection both are non-empty
+ /// after wrap around the start position becomes the end
+ DocIterator begin_;
+ DocIterator end_;
+ ///
+ bool incheck_;
+ ///
+ bool wrap_around_;
+};
+
+
+SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
+ : QTabWidget(parent), d(new Private(this, dv, gv))
+{
+ d->ui.setupUi(this);
+
+ connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
+ this, SLOT(on_replacePB_clicked()));
+
+ // language
+ QAbstractItemModel * language_model = guiApp->languageModel();
+ // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
+ language_model->sort(0);
+ d->ui.languageCO->setModel(language_model);
+ d->ui.languageCO->setModelColumn(1);
+
+ d->ui.wordED->setReadOnly(true);
+
+ d->ui.suggestionsLW->installEventFilter(this);
+}
+
+
+SpellcheckerWidget::~SpellcheckerWidget()
{
- setupUi(this);
- setViewTitle(_("Spellchecker"));
-
- 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*)));
-
- wordED->setReadOnly(true);
-
- bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
- bc().setCancel(closePB);
+ delete d;
}
-GuiSpellchecker::~GuiSpellchecker()
+bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
{
- delete speller_;
+ 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) {
+ if (d->ui.suggestionsLW->currentItem()) {
+ on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
+ on_replacePB_clicked();
+ }
+ return true;
+ } else if (e->key() == Qt::Key_Right) {
+ if (d->ui.suggestionsLW->currentItem())
+ on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
+ return true;
+ }
+ }
+ // standard event processing
+ return QWidget::eventFilter(obj, event);
}
-void GuiSpellchecker::suggestionChanged(QListWidgetItem * item)
+void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
{
- if (replaceCO->count() != 0)
- replaceCO->setItemText(0, item->text());
+ 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 SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
{
- if (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::closeEvent(QCloseEvent * e)
+void SpellcheckerWidget::updateView()
{
- slotClose();
- GuiDialog::closeEvent(e);
+ BufferView * bv = d->gv_->documentBufferView();
+ // we need a buffer view and the buffer has to be writable
+ bool const enabled = bv != 0 && !bv->buffer().isReadonly();
+ setEnabled(enabled);
+ if (enabled && hasFocus()) {
+ Cursor const & cursor = bv->cursor();
+ if (d->start_.empty() || !d->isCurrentBuffer(cursor)) {
+ if (cursor.selection()) {
+ d->begin_ = cursor.selectionBegin();
+ d->end_ = cursor.selectionEnd();
+ d->start_ = d->begin_;
+ bv->cursor().setCursor(d->start_);
+ } else {
+ d->begin_ = DocIterator();
+ d->end_ = DocIterator();
+ d->start_ = cursor;
+ }
+ d->wrapAround(false);
+ d->check();
+ }
+ }
}
-
-void GuiSpellchecker::reject()
+DocIterator const SpellcheckerWidget::Private::cursor() const
{
- slotClose();
- QDialog::reject();
+ BufferView * bv = gv_->documentBufferView();
+ return bv ? bv->cursor() : DocIterator();
}
-
-void GuiSpellchecker::updateContents()
+bool SpellcheckerWidget::Private::continueFromBeginning()
{
- // 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;
+ DocIterator const current_ = cursor();
+ if (isCurrentBuffer(current_) && !begin_.empty()) {
+ // selection was checked
+ // start over from beginning makes no sense
+ fixPositionsIfBroken();
+ hide();
+ if (current_ == start_) {
+ // no errors found... tell the user the good news
+ // so there is some feedback
+ QMessageBox::information(p,
+ qt_("Spell Checker"),
+ qt_("Spell check of the selection done, "
+ "did not find any errors."));
+ }
+ return false;
}
- 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();
+ QMessageBox::StandardButton const answer = QMessageBox::question(p,
+ qt_("Spell Checker"),
+ qt_("We reached the end of the document, would you like to "
+ "continue from the beginning?"),
+ QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
+ if (answer == QMessageBox::No) {
+ fixPositionsIfBroken();
+ hide();
+ return false;
}
+ // there is no selection, start over from the beginning now
+ wrapAround(true);
+ dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
+ return true;
}
-
-void GuiSpellchecker::accept()
+bool SpellcheckerWidget::Private::isCurrentBuffer(DocIterator const & cursor) const
{
- ignoreAll();
+ return start_.buffer() == cursor.buffer();
}
-
-void GuiSpellchecker::add()
+bool SpellcheckerWidget::Private::atLastPos(DocIterator const & cursor) const
{
- insert();
+ bool const valid_end = !end_.empty();
+ return cursor.depth() <= 1 && (
+ cursor.atEnd() ||
+ (valid_end && isCurrentBuffer(cursor) && cursor >= end_));
}
-
-void GuiSpellchecker::ignore()
+bool SpellcheckerWidget::Private::isWrapAround(DocIterator const & cursor) const
{
- check();
+ return wrap_around_ && isCurrentBuffer(cursor) && start_ < cursor;
}
-
-void GuiSpellchecker::replace()
+void SpellcheckerWidget::Private::fixPositionsIfBroken()
{
- replace(qstring_to_ucs4(replaceCO->currentText()));
+ DocIterator const current_ = cursor();
+ if (!isCurrentBuffer(current_)) {
+ LYXERR(Debug::GUI, "wrong document of current cursor position " << start_);
+ start_ = current_;
+ begin_ = DocIterator();
+ end_ = DocIterator();
+ }
+ if (start_.fixIfBroken())
+ LYXERR(Debug::GUI, "broken start position fixed " << start_);
+ if (begin_.fixIfBroken()) {
+ LYXERR(Debug::GUI, "broken selection begin position fixed " << begin_);
+ begin_ = DocIterator();
+ end_ = DocIterator();
+ }
+ if (end_.fixIfBroken())
+ LYXERR(Debug::GUI, "broken selection end position fixed " << end_);
}
-
-void GuiSpellchecker::partialUpdate(int state)
+void SpellcheckerWidget::Private::hide() const
{
- switch (state) {
- case SPELL_PROGRESSED:
- spellcheckPR->setValue(getProgress());
- break;
-
- case SPELL_FOUND_WORD: {
- wordED->setText(toqstr(getWord()));
- suggestionsLW->clear();
-
- docstring w;
- while (!(w = getSuggestion()).empty())
- suggestionsLW->addItem(toqstr(w));
-
- if (suggestionsLW->count() == 0)
- suggestionChanged(new QListWidgetItem(wordED->text()));
- else
- suggestionChanged(suggestionsLW->item(0));
-
- suggestionsLW->setCurrentRow(0);
- break;
+ BufferView * bv = gv_->documentBufferView();
+ Cursor & bvcur = bv->cursor();
+ dv_->hide();
+ if (isCurrentBuffer(bvcur)) {
+ if (!begin_.empty() && !end_.empty()) {
+ // restore previous selection
+ setSelection(begin_, end_);
+ } else {
+ // restore cursor position
+ bvcur.setCursor(start_);
+ bvcur.clearSelection();
+ bv->processUpdateFlags(Update::Force | Update::FitCursor);
}
}
}
-
-static SpellBase * getSpeller(BufferParams const & bp)
+void SpellcheckerWidget::Private::setSelection(
+ DocIterator const & from, DocIterator const & to) const
{
- string lang = (lyxrc.isp_use_alt_lang)
- ? lyxrc.isp_alt_lang
- : bp.language->code();
-
-#if defined(USE_ASPELL)
- if (lyxrc.use_spell_lib)
- return new ASpell(bp, lang);
-#elif defined(USE_PSPELL)
- if (lyxrc.use_spell_lib)
- return new PSpell(bp, lang);
-#endif
-
-#if defined(USE_ISPELL)
- lang = lyxrc.isp_use_alt_lang ?
- lyxrc.isp_alt_lang : bp.language->lang();
-
- return new ISpell(bp, lang);
-#else
- return new SpellBase;
-#endif
+ BufferView * bv = gv_->documentBufferView();
+ DocIterator end = to;
+
+ if (from.pit() != end.pit()) {
+ // there are multiple paragraphs in selection
+ Cursor & bvcur = bv->cursor();
+ bvcur.setCursor(from);
+ bvcur.clearSelection();
+ bvcur.setSelection(true);
+ bvcur.setCursor(end);
+ bvcur.setSelection(true);
+ } else {
+ // FIXME LFUN
+ // If we used a LFUN, dispatch would do all of this for us
+ int const size = end.pos() - from.pos();
+ bv->putSelectionAt(from, size, false);
+ }
+ bv->processUpdateFlags(Update::Force | Update::FitCursor);
}
-
-bool GuiSpellchecker::initialiseParams(std::string const &)
+void SpellcheckerWidget::Private::forward()
{
- LYXERR(Debug::GUI) << "Spellchecker::initialiseParams" << endl;
-
- speller_ = getSpeller(buffer().params());
- if (!speller_)
- return false;
-
- // reset values to initial
- oldval_ = 0;
- newvalue_ = 0;
- count_ = 0;
+ DocIterator const from = cursor();
- bool const success = speller_->error().empty();
-
- if (!success) {
- Alert::error(_("Spellchecker error"),
- _("The spellchecker could not be started\n")
- + speller_->error());
- delete speller_;
- speller_ = 0;
+ dispatch(FuncRequest(LFUN_ESCAPE));
+ fixPositionsIfBroken();
+ if (!atLastPos(cursor())) {
+ dispatch(FuncRequest(LFUN_CHAR_FORWARD));
+ }
+ if (atLastPos(cursor())) {
+ return;
+ }
+ if (from == cursor()) {
+ //FIXME we must be at the end of a cell
+ dispatch(FuncRequest(LFUN_CHAR_FORWARD));
+ }
+ if (isWrapAround(cursor())) {
+ hide();
}
-
- return success;
}
-void GuiSpellchecker::clearParams()
+void SpellcheckerWidget::on_languageCO_activated(int index)
{
- LYXERR(Debug::GUI) << "Spellchecker::clearParams" << endl;
- delete speller_;
- speller_ = 0;
+ string const lang =
+ fromqstr(d->ui.languageCO->itemData(index).toString());
+ if (!d->word_.lang() || d->word_.lang()->lang() == lang)
+ // nothing changed
+ return;
+ dispatch(FuncRequest(LFUN_LANGUAGE, lang));
+ d->check();
}
-static bool isLetter(DocIterator const & dit)
+bool SpellcheckerWidget::initialiseParams(std::string const &)
{
- return dit.inTexted()
- && dit.inset().allowSpellCheck()
- && dit.pos() != dit.lastpos()
- && (dit.paragraph().isLetter(dit.pos())
- // We want to pass the ' and escape chars to ispell
- || contains(from_utf8(lyxrc.isp_esc_chars + '\''),
- dit.paragraph().getChar(dit.pos())))
- && !dit.paragraph().isDeleted(dit.pos());
+ BufferView * bv = d->gv_->documentBufferView();
+ if (bv == 0)
+ return false;
+ std::set<Language const *> languages =
+ bv->buffer().masterBuffer()->getLanguages();
+ if (!languages.empty())
+ d->setLanguage(*languages.begin());
+ d->start_ = DocIterator();
+ d->wrapAround(false);
+ d->canCheck();
+ return true;
}
-static WordLangTuple nextWord(Cursor & cur, ptrdiff_t & progress)
+void SpellcheckerWidget::on_ignoreAllPB_clicked()
{
- BufferParams const & bp = cur.bv().buffer().params();
- bool inword = false;
- bool ignoreword = false;
- cur.resetAnchor();
- docstring word;
- string lang_code;
-
- while (cur.depth()) {
- if (isLetter(cur)) {
- if (!inword) {
- inword = true;
- ignoreword = false;
- cur.resetAnchor();
- word.clear();
- lang_code = cur.paragraph().getFontSettings(bp, cur.pos()).language()->code();
- }
- // Insets like optional hyphens and ligature
- // break are part of a word.
- if (!cur.paragraph().isInset(cur.pos())) {
- char_type const c = cur.paragraph().getChar(cur.pos());
- word += c;
- if (isDigit(c))
- ignoreword = true;
- }
- } else { // !isLetter(cur)
- if (inword)
- if (!word.empty() && !ignoreword) {
- cur.setSelection();
- return WordLangTuple(word, lang_code);
- }
- inword = false;
- }
-
- cur.forwardPos();
- ++progress;
- }
-
- return WordLangTuple(docstring(), string());
+ /// ignore all occurrences of word
+ if (d->disabled())
+ return;
+ LYXERR(Debug::GUI, "Spellchecker: ignore all button");
+ if (d->word_.lang() && !d->word_.word().empty())
+ theSpellChecker()->accept(d->word_);
+ d->forward();
+ d->check();
+ d->canCheck();
}
-void GuiSpellchecker::check()
+void SpellcheckerWidget::on_addPB_clicked()
{
- LYXERR(Debug::GUI) << "Check the spelling of a word" << endl;
-
- SpellBase::Result res = SpellBase::OK;
+ /// insert word in personal dictionary
+ if (d->disabled())
+ return;
+ LYXERR(Debug::GUI, "Spellchecker: add word button");
+ theSpellChecker()->insert(d->word_);
+ d->forward();
+ d->check();
+ d->canCheck();
+}
- Cursor cur = bufferview()->cursor();
- while (cur && cur.pos() && isLetter(cur))
- cur.backwardPos();
- ptrdiff_t start = 0;
- ptrdiff_t total = 0;
- DocIterator it = DocIterator(buffer().inset());
- for (start = 0; it != cur; it.forwardPos())
- ++start;
+void SpellcheckerWidget::on_ignorePB_clicked()
+{
+ /// ignore this occurrence of word
+ if (d->disabled())
+ return;
+ LYXERR(Debug::GUI, "Spellchecker: ignore button");
+ d->forward();
+ d->check();
+ d->canCheck();
+}
- for (total = start; it; it.forwardPos())
- ++total;
- exitEarly_ = false;
+void SpellcheckerWidget::on_findNextPB_clicked()
+{
+ if (d->disabled())
+ return;
+ docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
+ docstring const datastring = find2string(textfield,
+ true, true, true);
+ LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
+ dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
+ d->canCheck();
+}
- while (res == SpellBase::OK || res == SpellBase::IGNORED_WORD) {
- word_ = nextWord(cur, start);
- // end of document
- if (getWord().empty()) {
- showSummary();
- exitEarly_ = true;
- return;
- }
+void SpellcheckerWidget::on_replacePB_clicked()
+{
+ if (d->disabled())
+ return;
+ docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
+ docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
+ docstring const datastring = replace2string(replacement, textfield,
+ true, true, false, false);
+
+ LYXERR(Debug::GUI, "Replace (" << replacement << ")");
+ dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
+ d->forward();
+ d->check();
+ d->canCheck();
+}
- ++count_;
- // Update slider if and only if value has changed
- float progress = total ? float(start)/total : 1;
- newvalue_ = int(100.0 * progress);
- if (newvalue_!= oldval_) {
- LYXERR(Debug::GUI) << "Updating spell progress." << endl;
- oldval_ = newvalue_;
- // set progress bar
- partialUpdate(SPELL_PROGRESSED);
- }
+void SpellcheckerWidget::on_replaceAllPB_clicked()
+{
+ if (d->disabled())
+ return;
+ docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
+ docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
+ docstring const datastring = replace2string(replacement, textfield,
+ true, true, true, true);
+
+ LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
+ dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
+ d->forward();
+ d->check(); // continue spellchecking
+ d->canCheck();
+}
- // speller might be dead ...
- if (!checkAlive())
- return;
- res = speller_->check(word_);
+void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
+{
+ QString const suggestion = toqstr(word_.word());
+ ui.wordED->setText(suggestion);
+ QListWidget * lw = ui.suggestionsLW;
+ lw->clear();
- // ... or it might just be reporting an error
- if (!checkAlive())
- return;
+ if (words.empty()) {
+ p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
+ return;
}
+ for (size_t i = 0; i != words.size(); ++i)
+ lw->addItem(toqstr(words[i]));
- LYXERR(Debug::GUI) << "Found word \"" << to_utf8(getWord()) << "\"" << endl;
-
- int const size = cur.selEnd().pos() - cur.selBegin().pos();
- cur.pos() -= size;
- bufferview()->putSelectionAt(cur, size, false);
- // FIXME: if we used a lfun like in find/replace, dispatch would do
- // that for us
- // FIXME: this Controller is very badly designed...
- bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
-
- // set suggestions
- if (res != SpellBase::OK && res != SpellBase::IGNORED_WORD) {
- LYXERR(Debug::GUI) << "Found a word needing checking." << endl;
- partialUpdate(SPELL_FOUND_WORD);
- }
+ p->on_suggestionsLW_itemClicked(lw->item(0));
+ lw->setCurrentRow(0);
}
-bool GuiSpellchecker::checkAlive()
+void SpellcheckerWidget::Private::setLanguage(Language const * lang)
{
- if (speller_->alive() && speller_->error().empty())
- return true;
-
- docstring message;
- if (speller_->error().empty())
- message = _("The spellchecker has died for some reason.\n"
- "Maybe it has been killed.");
- else
- message = _("The spellchecker has failed.\n") + speller_->error();
-
- slotClose();
-
- Alert::error(_("The spellchecker has failed"), message);
- return false;
+ int const pos = ui.languageCO->findData(toqstr(lang->lang()));
+ if (pos != -1)
+ ui.languageCO->setCurrentIndex(pos);
}
-void GuiSpellchecker::showSummary()
+void SpellcheckerWidget::Private::check()
{
- if (!checkAlive() || count_ == 0) {
- slotClose();
+ BufferView * bv = gv_->documentBufferView();
+ if (!bv || bv->buffer().text().empty())
return;
- }
- docstring message;
- if (count_ != 1)
- message = bformat(_("%1$d words checked."), count_);
- else
- message = _("One word checked.");
+ fixPositionsIfBroken();
+
+ SpellChecker * speller = theSpellChecker();
+ if (speller && !speller->hasDictionary(bv->buffer().language())) {
+ int dsize = speller->numDictionaries();
+ if (0 == dsize) {
+ hide();
+ QMessageBox::information(p,
+ qt_("Spell Checker"),
+ qt_("Spell checker has no dictionaries."));
+ return;
+ }
+ }
- slotClose();
- Alert::information(_("Spelling check completed"), message);
-}
+ DocIterator from = bv->cursor();
+ DocIterator to = isCurrentBuffer(from) ? end_ : doc_iterator_end(&bv->buffer());
+ WordLangTuple word_lang;
+ docstring_list suggestions;
+
+ LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
+ try {
+ bv->buffer().spellCheck(from, to, word_lang, suggestions);
+ } catch (ExceptionMessage const & message) {
+ if (message.type_ == WarningException) {
+ Alert::warning(message.title_, message.details_);
+ return;
+ }
+ throw message;
+ }
+ // end of document or selection?
+ if (atLastPos(from)) {
+ if (isWrapAround()) {
+ hide();
+ return;
+ }
+ if (continueFromBeginning())
+ check();
+ return;
+ }
-void GuiSpellchecker::replace(docstring const & replacement)
-{
- LYXERR(Debug::GUI) << "GuiSpellchecker::replace("
- << to_utf8(replacement) << ")" << std::endl;
- cap::replaceSelectionWithString(bufferview()->cursor(), replacement, true);
- buffer().markDirty();
- // If we used an LFUN, we would not need that
- bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
- // fix up the count
- --count_;
- check();
-}
+ if (isWrapAround(from)) {
+ hide();
+ return;
+ }
+ word_ = word_lang;
-void GuiSpellchecker::replaceAll(docstring const & replacement)
-{
- // TODO: add to list
- replace(replacement);
+ // set suggestions
+ updateSuggestions(suggestions);
+ // set language
+ if (!word_lang.lang())
+ return;
+ setLanguage(word_lang.lang());
+ // mark misspelled word
+ setSelection(from, to);
}
-void GuiSpellchecker::insert()
+GuiSpellchecker::GuiSpellchecker(GuiView & parent,
+ Qt::DockWidgetArea area, Qt::WindowFlags flags)
+ : DockView(parent, "spellchecker", qt_("Spellchecker"),
+ area, flags)
{
- speller_->insert(word_);
- check();
+ widget_ = new SpellcheckerWidget(&parent, this);
+ setWidget(widget_);
+ setFocusProxy(widget_);
}
-docstring GuiSpellchecker::getSuggestion() const
+GuiSpellchecker::~GuiSpellchecker()
{
- return speller_->nextMiss();
+ setFocusProxy(0);
+ delete widget_;
}
-docstring GuiSpellchecker::getWord() const
+void GuiSpellchecker::updateView()
{
- return word_.word();
+ widget_->updateView();
}
-void GuiSpellchecker::ignoreAll()
-{
- speller_->accept(word_);
- check();
+Dialog * createGuiSpellchecker(GuiView & lv)
+{
+ GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
+#ifdef Q_WS_MACX
+ gui->setFloating(true);
+#endif
+ return gui;
}
-Dialog * createGuiSpellchecker(LyXView & lv) { return new GuiSpellchecker(lv); }
-
} // namespace frontend
} // namespace lyx
-#include "GuiSpellchecker_moc.cpp"
+#include "moc_GuiSpellchecker.cpp"