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"
|
2010-02-09 13:01:37 +00:00
|
|
|
#include "GuiApplication.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"
|
2011-03-24 11:49:34 +00:00
|
|
|
#include "Text.h"
|
2007-10-06 19:51:03 +00:00
|
|
|
#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
|
|
|
|
2010-01-17 12:27:44 +00:00
|
|
|
#include <QKeyEvent>
|
2011-03-23 23:10:12 +00:00
|
|
|
#include <QListWidgetItem>
|
|
|
|
#include <QMessageBox>
|
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
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
struct SpellcheckerWidget::Private
|
2009-04-19 08:57:43 +00:00
|
|
|
{
|
2012-01-12 06:12:28 +00:00
|
|
|
Private(SpellcheckerWidget * parent, DockView * dv, GuiView * gv)
|
|
|
|
: p(parent), dv_(dv), gv_(gv), incheck_(false), wrap_around_(false) {}
|
2011-03-23 23:10:12 +00:00
|
|
|
/// 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();
|
2012-01-12 06:12:28 +00:00
|
|
|
/// close the spell checker dialog
|
2011-10-06 10:28:34 +00:00
|
|
|
void hide() const;
|
2012-01-12 06:12:28 +00:00
|
|
|
/// make/restore a selection between from and to
|
2011-10-06 10:28:34 +00:00
|
|
|
void setSelection(DocIterator const & from, DocIterator const & to) const;
|
2012-01-12 06:12:28 +00:00
|
|
|
/// if no selection was checked:
|
|
|
|
/// ask the user if the check should start over
|
2011-03-23 23:10:12 +00:00
|
|
|
bool continueFromBeginning();
|
2012-01-12 06:12:28 +00:00
|
|
|
/// set the given language in language chooser
|
2011-04-06 18:26:50 +00:00
|
|
|
void setLanguage(Language const * lang);
|
2011-04-08 13:26:45 +00:00
|
|
|
/// test and set guard flag
|
|
|
|
bool inCheck() {
|
|
|
|
if (incheck_)
|
|
|
|
return true;
|
|
|
|
incheck_ = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void canCheck() { incheck_ = false; }
|
2011-10-06 10:28:34 +00:00
|
|
|
/// check for wrap around
|
|
|
|
void wrapAround(bool flag) {
|
|
|
|
wrap_around_ = flag;
|
|
|
|
if (flag) {
|
|
|
|
end_ = start_;
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 06:12:28 +00:00
|
|
|
/// 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
|
2011-12-24 12:47:46 +00:00
|
|
|
bool isCurrentBuffer(DocIterator const & cursor) const;
|
|
|
|
bool isWrapAround(DocIterator const & cursor) const;
|
2011-10-06 10:28:34 +00:00
|
|
|
bool isWrapAround() const { return wrap_around_; }
|
2011-12-24 12:47:46 +00:00
|
|
|
bool atLastPos(DocIterator const & cursor) const;
|
2012-01-12 06:12:28 +00:00
|
|
|
/// 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();
|
2011-04-06 18:26:50 +00:00
|
|
|
///
|
2009-04-19 08:57:43 +00:00
|
|
|
Ui::SpellcheckerUi ui;
|
2011-03-23 23:10:12 +00:00
|
|
|
///
|
|
|
|
SpellcheckerWidget * p;
|
|
|
|
///
|
2011-04-08 13:26:45 +00:00
|
|
|
DockView * dv_;
|
2017-07-03 17:53:14 +00:00
|
|
|
///
|
2012-01-12 06:12:28 +00:00
|
|
|
GuiView * gv_;
|
2009-04-19 08:57:43 +00:00
|
|
|
/// current word being checked and lang code
|
|
|
|
WordLangTuple word_;
|
2012-01-12 06:12:28 +00:00
|
|
|
/// cursor position where spell checking starts
|
2011-04-13 11:38:07 +00:00
|
|
|
DocIterator start_;
|
2011-10-06 10:28:34 +00:00
|
|
|
/// range to spell check
|
|
|
|
/// for selection both are non-empty
|
2012-01-12 06:12:28 +00:00
|
|
|
/// after wrap around the start position becomes the end
|
2011-10-06 10:28:34 +00:00
|
|
|
DocIterator begin_;
|
|
|
|
DocIterator end_;
|
2011-04-08 13:26:45 +00:00
|
|
|
///
|
|
|
|
bool incheck_;
|
2011-04-13 11:38:07 +00:00
|
|
|
///
|
|
|
|
bool wrap_around_;
|
2009-04-19 08:57:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-08 13:26:45 +00:00
|
|
|
SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
|
2012-01-12 06:12:28 +00:00
|
|
|
: QTabWidget(parent), d(new Private(this, dv, gv))
|
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
|
|
|
|
2010-02-09 13:01:37 +00:00
|
|
|
// 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);
|
2010-02-10 08:10:31 +00:00
|
|
|
d->ui.languageCO->setModelColumn(1);
|
2010-02-09 13:01:37 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
d->ui.wordED->setReadOnly(true);
|
2010-01-17 12:27:44 +00:00
|
|
|
|
|
|
|
d->ui.suggestionsLW->installEventFilter(this);
|
2009-04-19 08:57:43 +00:00
|
|
|
}
|
2007-04-24 15:32:14 +00:00
|
|
|
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
SpellcheckerWidget::~SpellcheckerWidget()
|
2009-04-19 08:57:43 +00:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
2007-09-05 20:33:29 +00:00
|
|
|
|
2009-04-19 08:57:43 +00:00
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
|
2010-01-17 12:27:44 +00:00
|
|
|
{
|
|
|
|
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) {
|
2010-03-15 10:03:02 +00:00
|
|
|
if (d->ui.suggestionsLW->currentItem()) {
|
|
|
|
on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
|
|
|
|
on_replacePB_clicked();
|
|
|
|
}
|
2010-01-17 12:27:44 +00:00
|
|
|
return true;
|
|
|
|
} else if (e->key() == Qt::Key_Right) {
|
2010-03-15 10:03:02 +00:00
|
|
|
if (d->ui.suggestionsLW->currentItem())
|
|
|
|
on_suggestionsLW_itemClicked(d->ui.suggestionsLW->currentItem());
|
2010-01-17 12:27:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// standard event processing
|
|
|
|
return QWidget::eventFilter(obj, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_suggestionsLW_itemClicked(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
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::updateView()
|
|
|
|
{
|
|
|
|
BufferView * bv = d->gv_->documentBufferView();
|
2011-10-06 10:28:34 +00:00
|
|
|
// 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();
|
2011-12-24 12:47:46 +00:00
|
|
|
if (d->start_.empty() || !d->isCurrentBuffer(cursor)) {
|
2011-10-06 10:28:34 +00:00
|
|
|
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();
|
|
|
|
}
|
2011-04-06 18:26:50 +00:00
|
|
|
}
|
2011-03-23 23:10:12 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 06:12:28 +00:00
|
|
|
DocIterator const SpellcheckerWidget::Private::cursor() const
|
|
|
|
{
|
|
|
|
BufferView * bv = gv_->documentBufferView();
|
|
|
|
return bv ? bv->cursor() : DocIterator();
|
|
|
|
}
|
2011-03-23 23:10:12 +00:00
|
|
|
|
|
|
|
bool SpellcheckerWidget::Private::continueFromBeginning()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2012-01-12 06:12:28 +00:00
|
|
|
DocIterator const current_ = cursor();
|
2011-12-24 12:47:46 +00:00
|
|
|
if (isCurrentBuffer(current_) && !begin_.empty()) {
|
2011-10-06 10:28:34 +00:00
|
|
|
// selection was checked
|
|
|
|
// start over from beginning makes no sense
|
2012-01-12 06:12:28 +00:00
|
|
|
fixPositionsIfBroken();
|
2011-10-06 10:28:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-04-13 11:38:07 +00:00
|
|
|
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) {
|
2012-01-12 06:12:28 +00:00
|
|
|
fixPositionsIfBroken();
|
2011-10-06 10:28:34 +00:00
|
|
|
hide();
|
2011-04-13 11:38:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-10-06 10:28:34 +00:00
|
|
|
// there is no selection, start over from the beginning now
|
|
|
|
wrapAround(true);
|
2011-04-13 11:38:07 +00:00
|
|
|
dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
|
|
|
|
return true;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 12:47:46 +00:00
|
|
|
bool SpellcheckerWidget::Private::isCurrentBuffer(DocIterator const & cursor) const
|
|
|
|
{
|
|
|
|
return start_.buffer() == cursor.buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpellcheckerWidget::Private::atLastPos(DocIterator const & cursor) const
|
2011-10-06 10:28:34 +00:00
|
|
|
{
|
|
|
|
bool const valid_end = !end_.empty();
|
|
|
|
return cursor.depth() <= 1 && (
|
|
|
|
cursor.atEnd() ||
|
2011-12-24 12:47:46 +00:00
|
|
|
(valid_end && isCurrentBuffer(cursor) && cursor >= end_));
|
2011-10-06 10:28:34 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 12:47:46 +00:00
|
|
|
bool SpellcheckerWidget::Private::isWrapAround(DocIterator const & cursor) const
|
2011-07-02 10:41:36 +00:00
|
|
|
{
|
2011-12-24 12:47:46 +00:00
|
|
|
return wrap_around_ && isCurrentBuffer(cursor) && start_ < cursor;
|
2011-07-02 10:41:36 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 06:12:28 +00:00
|
|
|
void SpellcheckerWidget::Private::fixPositionsIfBroken()
|
|
|
|
{
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
2011-10-06 10:28:34 +00:00
|
|
|
void SpellcheckerWidget::Private::hide() const
|
|
|
|
{
|
2011-12-24 12:47:46 +00:00
|
|
|
BufferView * bv = gv_->documentBufferView();
|
|
|
|
Cursor & bvcur = bv->cursor();
|
2011-10-06 10:28:34 +00:00
|
|
|
dv_->hide();
|
2011-12-24 12:47:46 +00:00
|
|
|
if (isCurrentBuffer(bvcur)) {
|
|
|
|
if (!begin_.empty() && !end_.empty()) {
|
|
|
|
// restore previous selection
|
|
|
|
setSelection(begin_, end_);
|
|
|
|
} else {
|
|
|
|
// restore cursor position
|
|
|
|
bvcur.setCursor(start_);
|
|
|
|
bvcur.clearSelection();
|
2017-07-03 17:53:14 +00:00
|
|
|
bv->processUpdateFlags(Update::Force | Update::FitCursor);
|
2011-12-24 12:47:46 +00:00
|
|
|
}
|
2011-10-06 10:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpellcheckerWidget::Private::setSelection(
|
|
|
|
DocIterator const & from, DocIterator const & to) const
|
|
|
|
{
|
|
|
|
BufferView * bv = gv_->documentBufferView();
|
|
|
|
DocIterator end = to;
|
|
|
|
|
|
|
|
if (from.pit() != end.pit()) {
|
2017-07-03 17:53:14 +00:00
|
|
|
// there are multiple paragraphs in selection
|
2011-10-06 10:28:34 +00:00
|
|
|
Cursor & bvcur = bv->cursor();
|
|
|
|
bvcur.setCursor(from);
|
|
|
|
bvcur.clearSelection();
|
2016-02-28 16:36:29 +00:00
|
|
|
bvcur.selection(true);
|
2011-10-06 10:28:34 +00:00
|
|
|
bvcur.setCursor(end);
|
2016-02-28 16:36:29 +00:00
|
|
|
bvcur.selection(true);
|
2011-10-06 10:28:34 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2016-09-14 08:23:39 +00:00
|
|
|
bv->processUpdateFlags(Update::Force | Update::FitCursor);
|
2011-10-06 10:28:34 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::Private::forward()
|
2010-09-28 15:09:06 +00:00
|
|
|
{
|
2012-01-12 06:12:28 +00:00
|
|
|
DocIterator const from = cursor();
|
2010-12-05 06:51:32 +00:00
|
|
|
|
2010-09-28 15:09:06 +00:00
|
|
|
dispatch(FuncRequest(LFUN_ESCAPE));
|
2012-01-12 06:12:28 +00:00
|
|
|
fixPositionsIfBroken();
|
|
|
|
if (!atLastPos(cursor())) {
|
2011-10-06 10:28:34 +00:00
|
|
|
dispatch(FuncRequest(LFUN_CHAR_FORWARD));
|
|
|
|
}
|
2012-01-12 06:12:28 +00:00
|
|
|
if (atLastPos(cursor())) {
|
2011-03-23 23:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-01-12 06:12:28 +00:00
|
|
|
if (from == cursor()) {
|
2011-03-23 23:10:12 +00:00
|
|
|
//FIXME we must be at the end of a cell
|
|
|
|
dispatch(FuncRequest(LFUN_CHAR_FORWARD));
|
|
|
|
}
|
2012-01-12 06:12:28 +00:00
|
|
|
if (isWrapAround(cursor())) {
|
2011-10-06 10:28:34 +00:00
|
|
|
hide();
|
2011-04-13 11:38:07 +00:00
|
|
|
}
|
2010-09-28 15:09:06 +00:00
|
|
|
}
|
2011-04-06 18:26:50 +00:00
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_languageCO_activated(int index)
|
2010-02-09 13:01:37 +00:00
|
|
|
{
|
|
|
|
string const lang =
|
|
|
|
fromqstr(d->ui.languageCO->itemData(index).toString());
|
2010-02-26 13:34:36 +00:00
|
|
|
if (!d->word_.lang() || d->word_.lang()->lang() == lang)
|
2010-02-09 13:01:37 +00:00
|
|
|
// nothing changed
|
|
|
|
return;
|
|
|
|
dispatch(FuncRequest(LFUN_LANGUAGE, lang));
|
2011-03-23 23:10:12 +00:00
|
|
|
d->check();
|
2010-02-09 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-08 13:26:45 +00:00
|
|
|
bool SpellcheckerWidget::initialiseParams(std::string const &)
|
|
|
|
{
|
2011-04-13 11:38:07 +00:00
|
|
|
BufferView * bv = d->gv_->documentBufferView();
|
|
|
|
if (bv == 0)
|
|
|
|
return false;
|
2017-12-16 05:48:34 +00:00
|
|
|
std::set<Language const *> langs =
|
2011-10-06 10:28:34 +00:00
|
|
|
bv->buffer().masterBuffer()->getLanguages();
|
2017-12-16 05:48:34 +00:00
|
|
|
if (!langs.empty())
|
|
|
|
d->setLanguage(*langs.begin());
|
2011-04-13 11:38:07 +00:00
|
|
|
d->start_ = DocIterator();
|
2011-10-06 10:28:34 +00:00
|
|
|
d->wrapAround(false);
|
|
|
|
d->canCheck();
|
2011-04-08 13:26:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_ignoreAllPB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2011-04-08 13:26:45 +00:00
|
|
|
/// ignore all occurrences of word
|
2012-01-12 06:12:28 +00:00
|
|
|
if (d->disabled())
|
2011-04-08 13:26:45 +00:00
|
|
|
return;
|
|
|
|
LYXERR(Debug::GUI, "Spellchecker: ignore all button");
|
2010-04-30 09:52:00 +00:00
|
|
|
if (d->word_.lang() && !d->word_.word().empty())
|
|
|
|
theSpellChecker()->accept(d->word_);
|
2011-03-23 23:10:12 +00:00
|
|
|
d->forward();
|
|
|
|
d->check();
|
2011-04-08 13:26:45 +00:00
|
|
|
d->canCheck();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_addPB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2009-04-19 08:57:43 +00:00
|
|
|
/// insert word in personal dictionary
|
2012-01-12 06:12:28 +00:00
|
|
|
if (d->disabled())
|
2011-04-08 13:26:45 +00:00
|
|
|
return;
|
|
|
|
LYXERR(Debug::GUI, "Spellchecker: add word button");
|
2009-04-19 08:57:43 +00:00
|
|
|
theSpellChecker()->insert(d->word_);
|
2011-03-23 23:10:12 +00:00
|
|
|
d->forward();
|
|
|
|
d->check();
|
2011-04-08 13:26:45 +00:00
|
|
|
d->canCheck();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_ignorePB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2011-04-08 13:26:45 +00:00
|
|
|
/// ignore this occurrence of word
|
2012-01-12 06:12:28 +00:00
|
|
|
if (d->disabled())
|
2011-04-08 13:26:45 +00:00
|
|
|
return;
|
|
|
|
LYXERR(Debug::GUI, "Spellchecker: ignore button");
|
2011-03-23 23:10:12 +00:00
|
|
|
d->forward();
|
|
|
|
d->check();
|
2011-04-08 13:26:45 +00:00
|
|
|
d->canCheck();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_findNextPB_clicked()
|
2009-07-04 17:15:27 +00:00
|
|
|
{
|
2012-01-12 06:12:28 +00:00
|
|
|
if (d->disabled())
|
2011-04-08 13:26:45 +00:00
|
|
|
return;
|
|
|
|
docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
|
|
|
|
docstring const datastring = find2string(textfield,
|
2009-07-04 17:15:27 +00:00
|
|
|
true, true, true);
|
2011-04-08 13:26:45 +00:00
|
|
|
LYXERR(Debug::GUI, "Spellchecker: find next (" << textfield << ")");
|
|
|
|
dispatch(FuncRequest(LFUN_WORD_FIND, datastring));
|
|
|
|
d->canCheck();
|
2009-07-04 17:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_replacePB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2012-01-12 06:12:28 +00:00
|
|
|
if (d->disabled())
|
2011-04-08 13:26:45 +00:00
|
|
|
return;
|
|
|
|
docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
|
2009-04-19 08:57:43 +00:00
|
|
|
docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
|
2017-07-03 17:53:14 +00:00
|
|
|
docstring const datastring =
|
2012-07-26 22:34:17 +00:00
|
|
|
replace2string(replacement, textfield,
|
|
|
|
true, // case sensitive
|
|
|
|
true, // match word
|
|
|
|
false, // all words
|
|
|
|
true, // forward
|
|
|
|
false); // find next
|
2009-04-19 08:57:43 +00:00
|
|
|
|
|
|
|
LYXERR(Debug::GUI, "Replace (" << replacement << ")");
|
2011-04-08 13:26:45 +00:00
|
|
|
dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
|
2011-03-23 23:10:12 +00:00
|
|
|
d->forward();
|
|
|
|
d->check();
|
2011-04-08 13:26:45 +00:00
|
|
|
d->canCheck();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::on_replaceAllPB_clicked()
|
2009-07-04 17:15:27 +00:00
|
|
|
{
|
2012-01-12 06:12:28 +00:00
|
|
|
if (d->disabled())
|
2011-04-08 13:26:45 +00:00
|
|
|
return;
|
|
|
|
docstring const textfield = qstring_to_ucs4(d->ui.wordED->text());
|
|
|
|
docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
|
2012-07-26 22:34:17 +00:00
|
|
|
docstring const datastring =
|
|
|
|
replace2string(replacement, textfield,
|
|
|
|
true, // case sensitive
|
|
|
|
true, // match word
|
|
|
|
true, // all words
|
|
|
|
true, // forward
|
|
|
|
false); // find next
|
2011-04-08 13:26:45 +00:00
|
|
|
|
|
|
|
LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
|
|
|
|
dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
|
2011-03-23 23:10:12 +00:00
|
|
|
d->forward();
|
2016-09-14 08:23:39 +00:00
|
|
|
// replace all wraps around
|
|
|
|
d->wrapAround(true);
|
2011-03-23 23:10:12 +00:00
|
|
|
d->check(); // continue spellchecking
|
2011-04-08 13:26:45 +00:00
|
|
|
d->canCheck();
|
2009-07-04 17:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2011-03-23 23:10:12 +00:00
|
|
|
QString const suggestion = toqstr(word_.word());
|
|
|
|
ui.wordED->setText(suggestion);
|
|
|
|
QListWidget * lw = ui.suggestionsLW;
|
2009-04-19 08:57:43 +00:00
|
|
|
lw->clear();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2009-04-04 16:55:13 +00:00
|
|
|
if (words.empty()) {
|
2011-03-23 23:10:12 +00:00
|
|
|
p->on_suggestionsLW_itemClicked(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
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
p->on_suggestionsLW_itemClicked(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
|
|
|
|
2011-04-06 18:26:50 +00:00
|
|
|
void SpellcheckerWidget::Private::setLanguage(Language const * lang)
|
|
|
|
{
|
|
|
|
int const pos = ui.languageCO->findData(toqstr(lang->lang()));
|
|
|
|
if (pos != -1)
|
|
|
|
ui.languageCO->setCurrentIndex(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
void SpellcheckerWidget::Private::check()
|
2007-10-06 19:51:03 +00:00
|
|
|
{
|
2011-03-23 23:10:12 +00:00
|
|
|
BufferView * bv = gv_->documentBufferView();
|
2011-03-24 11:49:34 +00:00
|
|
|
if (!bv || bv->buffer().text().empty())
|
2010-12-05 06:51:32 +00:00
|
|
|
return;
|
2012-01-12 06:12:28 +00:00
|
|
|
|
|
|
|
fixPositionsIfBroken();
|
2017-07-03 17:53:14 +00:00
|
|
|
|
2012-01-04 22:02:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2011-03-23 23:10:12 +00:00
|
|
|
|
|
|
|
DocIterator from = bv->cursor();
|
2011-12-24 12:47:46 +00:00
|
|
|
DocIterator to = isCurrentBuffer(from) ? end_ : doc_iterator_end(&bv->buffer());
|
2009-04-04 16:40:47 +00:00
|
|
|
WordLangTuple word_lang;
|
|
|
|
docstring_list suggestions;
|
|
|
|
|
2011-04-08 13:26:45 +00:00
|
|
|
LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
|
2009-04-04 16:40:47 +00:00
|
|
|
try {
|
2011-09-23 17:51:39 +00:00
|
|
|
bv->buffer().spellCheck(from, to, word_lang, suggestions);
|
2009-04-04 16:40:47 +00:00
|
|
|
} catch (ExceptionMessage const & message) {
|
|
|
|
if (message.type_ == WarningException) {
|
|
|
|
Alert::warning(message.title_, message.details_);
|
2007-10-06 19:51:03 +00:00
|
|
|
return;
|
2009-04-04 16:40:47 +00:00
|
|
|
}
|
|
|
|
throw message;
|
|
|
|
}
|
|
|
|
|
2011-10-06 10:28:34 +00:00
|
|
|
// end of document or selection?
|
|
|
|
if (atLastPos(from)) {
|
|
|
|
if (isWrapAround()) {
|
|
|
|
hide();
|
2011-04-13 11:38:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-03-23 23:10:12 +00:00
|
|
|
if (continueFromBeginning())
|
|
|
|
check();
|
2009-04-04 16:40:47 +00:00
|
|
|
return;
|
2007-10-06 19:51:03 +00:00
|
|
|
}
|
2010-12-05 06:51:32 +00:00
|
|
|
|
2011-07-02 10:41:36 +00:00
|
|
|
if (isWrapAround(from)) {
|
2011-10-06 10:28:34 +00:00
|
|
|
hide();
|
2011-04-13 11:38:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
word_ = word_lang;
|
2010-12-05 06:51:32 +00:00
|
|
|
|
2009-04-04 16:40:47 +00:00
|
|
|
// set suggestions
|
|
|
|
updateSuggestions(suggestions);
|
2010-02-09 13:01:37 +00:00
|
|
|
// set language
|
2011-10-06 10:28:34 +00:00
|
|
|
if (!word_lang.lang())
|
|
|
|
return;
|
2011-04-06 18:26:50 +00:00
|
|
|
setLanguage(word_lang.lang());
|
2011-10-06 10:28:34 +00:00
|
|
|
// mark misspelled word
|
|
|
|
setSelection(from, to);
|
2007-10-06 19:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
GuiSpellchecker::GuiSpellchecker(GuiView & parent,
|
|
|
|
Qt::DockWidgetArea area, Qt::WindowFlags flags)
|
|
|
|
: DockView(parent, "spellchecker", qt_("Spellchecker"),
|
|
|
|
area, flags)
|
2007-10-06 19:51:03 +00:00
|
|
|
{
|
2011-04-08 13:26:45 +00:00
|
|
|
widget_ = new SpellcheckerWidget(&parent, this);
|
2011-03-23 23:10:12 +00:00
|
|
|
setWidget(widget_);
|
|
|
|
setFocusProxy(widget_);
|
|
|
|
}
|
2007-10-06 19:51:03 +00:00
|
|
|
|
|
|
|
|
2011-03-23 23:10:12 +00:00
|
|
|
GuiSpellchecker::~GuiSpellchecker()
|
|
|
|
{
|
|
|
|
setFocusProxy(0);
|
|
|
|
delete widget_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GuiSpellchecker::updateView()
|
|
|
|
{
|
|
|
|
widget_->updateView();
|
2007-10-06 19:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
Dialog * createGuiSpellchecker(GuiView & lv)
|
|
|
|
{
|
2011-03-23 23:10:12 +00:00
|
|
|
GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
|
2014-08-25 18:08:59 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2010-03-11 19:33:08 +00:00
|
|
|
gui->setFloating(true);
|
|
|
|
#endif
|
|
|
|
return gui;
|
|
|
|
}
|
|
|
|
|
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"
|