Make spellchecker work in read-only document

Mistakes are visible but word replacement is disabled. Disable relevant
widgets depending on the situation.

Move updateView code to SpellcheckerWidget::Private.
This commit is contained in:
Jean-Marc Lasgouttes 2020-12-07 17:53:40 +01:00
parent ccbfd22f56
commit 1dcb08104c
2 changed files with 42 additions and 21 deletions

View File

@ -62,6 +62,8 @@ struct SpellcheckerWidget::Private
{ {
Private(SpellcheckerWidget * parent, DockView * dv, GuiView * gv) Private(SpellcheckerWidget * parent, DockView * dv, GuiView * gv)
: p(parent), dv_(dv), gv_(gv), incheck_(false), wrap_around_(false) {} : p(parent), dv_(dv), gv_(gv), incheck_(false), wrap_around_(false) {}
///
void updateView();
/// update from controller /// update from controller
void updateSuggestions(docstring_list & words); void updateSuggestions(docstring_list & words);
/// move to next position after current word /// move to next position after current word
@ -146,9 +148,6 @@ SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * pa
language_model->sort(0); language_model->sort(0);
d->ui.languageCO->setModel(language_model); d->ui.languageCO->setModel(language_model);
d->ui.languageCO->setModelColumn(1); d->ui.languageCO->setModelColumn(1);
d->ui.wordED->setReadOnly(true);
d->ui.suggestionsLW->installEventFilter(this); d->ui.suggestionsLW->installEventFilter(this);
} }
@ -208,27 +207,48 @@ void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
void SpellcheckerWidget::updateView() void SpellcheckerWidget::updateView()
{ {
BufferView * bv = d->gv_->documentBufferView(); d->updateView();
// 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()) { void SpellcheckerWidget::Private::updateView()
Cursor const & cursor = bv->cursor(); {
if (d->start_.empty() || !d->isCurrentBuffer(cursor)) { BufferView * bv = gv_->documentBufferView();
if (cursor.selection()) { bool const enabled = bv != nullptr;
d->begin_ = cursor.selectionBegin(); // Check cursor position
d->end_ = cursor.selectionEnd(); if (enabled && p->hasFocus()) {
d->start_ = d->begin_; Cursor const & cur = bv->cursor();
bv->cursor().setCursor(d->start_); if (start_.empty() || !isCurrentBuffer(cur)) {
if (cur.selection()) {
begin_ = cur.selectionBegin();
end_ = cur.selectionEnd();
start_ = begin_;
bv->cursor().setCursor(start_);
} else { } else {
d->begin_ = DocIterator(); begin_ = DocIterator();
d->end_ = DocIterator(); end_ = DocIterator();
d->start_ = cursor; start_ = cur;
} }
d->wrapAround(false); wrapAround(false);
d->check(); check();
} }
} }
// Enable widgets as needed.
bool const has_word = enabled && !ui.wordED->text().isEmpty();
bool const can_replace = has_word && !bv->buffer().isReadonly();
ui.findNextPB->setEnabled(enabled);
ui.TextLabel3->setEnabled(enabled);
ui.wordED->setEnabled(enabled);
ui.ignorePB->setEnabled(has_word);
ui.ignoreAllPB->setEnabled(has_word);
ui.addPB->setEnabled(has_word);
ui.TextLabel1->setEnabled(can_replace);
ui.replaceCO->setEnabled(can_replace);
ui.TextLabel2->setEnabled(has_word);
ui.suggestionsLW->setEnabled(has_word);
ui.replacePB->setEnabled(can_replace);
ui.replaceAllPB->setEnabled(can_replace);
} }
DocIterator const SpellcheckerWidget::Private::cursor() const DocIterator const SpellcheckerWidget::Private::cursor() const
@ -587,6 +607,8 @@ void SpellcheckerWidget::Private::check()
setLanguage(word_lang.lang()); setLanguage(word_lang.lang());
// mark misspelled word // mark misspelled word
setSelection(from, to); setSelection(from, to);
// enable relevant widgets
updateView();
} }

View File

@ -2193,7 +2193,6 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
enable = FileName(doc_buffer->logName()).isReadableFile(); enable = FileName(doc_buffer->logName()).isReadableFile();
else if (name == "spellchecker") else if (name == "spellchecker")
enable = theSpellChecker() enable = theSpellChecker()
&& !doc_buffer->isReadonly()
&& !doc_buffer->text().empty(); && !doc_buffer->text().empty();
else if (name == "vclog") else if (name == "vclog")
enable = doc_buffer->lyxvc().inUse(); enable = doc_buffer->lyxvc().inUse();