struct SpellcheckerWidget::Private
{
- Private(SpellcheckerWidget * parent)
- : p(parent) {}
+ Private(SpellcheckerWidget * parent, DockView * dv)
+ : p(parent), dv_(dv), incheck_(false), wrap_around_(false) {}
/// update from controller
void updateSuggestions(docstring_list & words);
/// move to next position after current word
/// check text until next misspelled/unknown word
void check();
///
+ void hide() const;
+ ///
+ void setSelection(DocIterator const & from, DocIterator const & to) const;
+ ///
bool continueFromBeginning();
///
+ 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_;
+ }
+ }
+ bool isWrapAround(DocIterator cursor) const;
+ bool isWrapAround() const { return wrap_around_; }
+ ///
+ bool atLastPos(DocIterator cursor) const;
+ ///
Ui::SpellcheckerUi ui;
///
SpellcheckerWidget * p;
///
GuiView * gv_;
+ ///
+ DockView * dv_;
/// current word being checked and lang code
WordLangTuple word_;
+ /// cursor position when spell checking starts
+ DocIterator start_;
+ /// range to spell check
+ /// for selection both are non-empty
+ /// after wrap around the start becomes the end
+ DocIterator begin_;
+ DocIterator end_;
+ ///
+ bool incheck_;
+ ///
+ bool wrap_around_;
};
-SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, QWidget * parent)
- : QWidget(parent), d(new Private(this))
+SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
+ : QTabWidget(parent), d(new Private(this, dv))
{
d->ui.setupUi(this);
d->gv_ = gv;
void SpellcheckerWidget::updateView()
{
BufferView * bv = d->gv_->documentBufferView();
- setEnabled(bv != 0);
- if (bv && hasFocus())
- d->check();
+ // 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->start_.buffer() != cursor.buffer()) {
+ 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();
+ }
+ }
}
bool SpellcheckerWidget::Private::continueFromBeginning()
{
- 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)
- return false;
- dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
- return true;
+ BufferView * bv = gv_->documentBufferView();
+ if (!begin_.empty()) {
+ // selection was checked
+ // start over from beginning makes no sense
+ DocIterator current_ = bv->cursor();
+ 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;
+ }
+ 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) {
+ hide();
+ return false;
+ }
+ // there is no selection, start over from the beginning now
+ wrapAround(true);
+ dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
+ return true;
+}
+
+bool SpellcheckerWidget::Private::atLastPos(DocIterator cursor) const
+{
+ bool const valid_end = !end_.empty();
+ return cursor.depth() <= 1 && (
+ cursor.atEnd() ||
+ (valid_end && cursor >= end_));
}
+bool SpellcheckerWidget::Private::isWrapAround(DocIterator cursor) const
+{
+ return wrap_around_ && start_ < cursor;
+}
+
+void SpellcheckerWidget::Private::hide() const
+{
+ dv_->hide();
+ if (!begin_.empty() && !end_.empty()) {
+ // restore previous selection
+ setSelection(begin_, end_);
+ } else {
+ // restore cursor position
+ BufferView * bv = gv_->documentBufferView();
+ Cursor & bvcur = bv->cursor();
+ bvcur.setCursor(start_);
+ bvcur.clearSelection();
+ bv->processUpdateFlags(Update::Force | Update::FitCursor);
+ }
+}
+
+void SpellcheckerWidget::Private::setSelection(
+ DocIterator const & from, DocIterator const & to) const
+{
+ BufferView * bv = gv_->documentBufferView();
+ DocIterator end = to;
+
+ // spell checker corrections may have invalidated the end
+ end.fixIfBroken();
+
+ 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);
+}
void SpellcheckerWidget::Private::forward()
{
DocIterator from = bv->cursor();
dispatch(FuncRequest(LFUN_ESCAPE));
- dispatch(FuncRequest(LFUN_CHAR_FORWARD));
- if (bv->cursor().atLastPos()) {
- continueFromBeginning();
+ if (!atLastPos(bv->cursor())) {
+ dispatch(FuncRequest(LFUN_CHAR_FORWARD));
+ }
+ if (atLastPos(bv->cursor())) {
return;
}
if (from == bv->cursor()) {
//FIXME we must be at the end of a cell
dispatch(FuncRequest(LFUN_CHAR_FORWARD));
}
+ if (isWrapAround(bv->cursor())) {
+ hide();
+ }
}
-
-
+
+
void SpellcheckerWidget::on_languageCO_activated(int index)
{
string const lang =
}
+bool SpellcheckerWidget::initialiseParams(std::string const &)
+{
+ 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;
+}
+
+
void SpellcheckerWidget::on_ignoreAllPB_clicked()
{
- /// replace all occurrences of word
+ /// ignore all occurrences of word
+ if (d->inCheck())
+ 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 SpellcheckerWidget::on_addPB_clicked()
{
/// insert word in personal dictionary
+ if (d->inCheck())
+ return;
+ LYXERR(Debug::GUI, "Spellchecker: add word button");
theSpellChecker()->insert(d->word_);
d->forward();
d->check();
+ d->canCheck();
}
void SpellcheckerWidget::on_ignorePB_clicked()
{
+ /// ignore this occurrence of word
+ if (d->inCheck())
+ return;
+ LYXERR(Debug::GUI, "Spellchecker: ignore button");
d->forward();
d->check();
+ d->canCheck();
}
void SpellcheckerWidget::on_findNextPB_clicked()
{
- docstring const data = find2string(
- qstring_to_ucs4(d->ui.wordED->text()),
+ if (d->inCheck())
+ return;
+ docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
+ docstring const datastring = find2string(textfield,
true, true, true);
- dispatch(FuncRequest(LFUN_WORD_FIND, data));
+ LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
+ dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
+ d->canCheck();
}
void SpellcheckerWidget::on_replacePB_clicked()
{
+ if (d->inCheck())
+ return;
+ docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
- docstring const data = replace2string(
- replacement, qstring_to_ucs4(d->ui.wordED->text()),
+ docstring const datastring = replace2string(replacement, textfield,
true, true, false, false);
LYXERR(Debug::GUI, "Replace (" << replacement << ")");
- dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
+ dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
d->forward();
d->check();
+ d->canCheck();
}
void SpellcheckerWidget::on_replaceAllPB_clicked()
{
- docstring const data = replace2string(
- qstring_to_ucs4(d->ui.replaceCO->currentText()),
- qstring_to_ucs4(d->ui.wordED->text()),
+ if (d->inCheck())
+ 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);
- dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
+
+ LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
+ dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
d->forward();
d->check(); // continue spellchecking
+ d->canCheck();
}
}
+void SpellcheckerWidget::Private::setLanguage(Language const * lang)
+{
+ int const pos = ui.languageCO->findData(toqstr(lang->lang()));
+ if (pos != -1)
+ ui.languageCO->setCurrentIndex(pos);
+}
+
+
void SpellcheckerWidget::Private::check()
{
BufferView * bv = gv_->documentBufferView();
return;
DocIterator from = bv->cursor();
- DocIterator to;
+ DocIterator to = end_;
WordLangTuple word_lang;
docstring_list suggestions;
- int progress;
+ LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
try {
- progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
+ bv->buffer().spellCheck(from, to, word_lang, suggestions);
} catch (ExceptionMessage const & message) {
if (message.type_ == WarningException) {
Alert::warning(message.title_, message.details_);
throw message;
}
- // end of document
- if (from == doc_iterator_end(&bv->buffer())) {
+ // end of document or selection?
+ if (atLastPos(from)) {
+ if (isWrapAround()) {
+ hide();
+ return;
+ }
if (continueFromBeginning())
check();
return;
}
+ if (isWrapAround(from)) {
+ hide();
+ return;
+ }
+
word_ = word_lang;
// set suggestions
updateSuggestions(suggestions);
// set language
- int const pos = ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
- if (pos != -1)
- ui.languageCO->setCurrentIndex(pos);
-
- // FIXME LFUN
- // If we used a LFUN, dispatch would do all of this for us
- int const size = to.pos() - from.pos();
- bv->putSelectionAt(from, size, false);
- bv->processUpdateFlags(Update::Force | Update::FitCursor);
+ if (!word_lang.lang())
+ return;
+ setLanguage(word_lang.lang());
+ // mark misspelled word
+ setSelection(from, to);
}
: DockView(parent, "spellchecker", qt_("Spellchecker"),
area, flags)
{
- widget_ = new SpellcheckerWidget(&parent);
+ widget_ = new SpellcheckerWidget(&parent, this);
setWidget(widget_);
setFocusProxy(widget_);
}