]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSpellchecker.cpp
Don't allow newline characters in preference (#5840).
[lyx.git] / src / frontends / qt4 / GuiSpellchecker.cpp
index 9e9da468dc0fbcc3380abd12761acf465db5cb4b..5e7305e75a9897ffe7409753e6d20b9d33e4290a 100644 (file)
@@ -24,6 +24,7 @@
 #include "BufferView.h"
 #include "buffer_funcs.h"
 #include "Cursor.h"
+#include "Text.h"
 #include "CutAndPaste.h"
 #include "FuncRequest.h"
 #include "Language.h"
@@ -41,8 +42,9 @@
 #include "support/lstrings.h"
 #include "support/textutils.h"
 
-#include <QListWidgetItem>
 #include <QKeyEvent>
+#include <QListWidgetItem>
+#include <QMessageBox>
 
 #include "SpellChecker.h"
 
@@ -55,27 +57,54 @@ namespace lyx {
 namespace frontend {
 
 
-struct GuiSpellchecker::Private
+struct SpellcheckerWidget::Private
 {
-       Private() : progress_(0), count_(0), stuck_(false) {}
+       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
+       void forward();
+       /// check text until next misspelled/unknown word
+       void check();
+       ///
+       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 of current position
+       bool isWrapAround(DocIterator cursor) const;
+       ///
        Ui::SpellcheckerUi ui;
+       ///
+       SpellcheckerWidget * p;
+       ///
+       GuiView * gv_;
+       ///
+       DockView * dv_;
        /// current word being checked and lang code
        WordLangTuple word_;
-       /// values for progress
-       int total_;
-       int progress_;
-       /// word count
-       int count_;
-       /// flag for last move forward success
-       bool stuck_;
+       ///
+       DocIterator start_;
+       ///
+       bool incheck_;
+       ///
+       bool wrap_around_;
 };
 
 
-GuiSpellchecker::GuiSpellchecker(GuiView & lv)
-       : DockView(lv, "spellchecker", qt_("Spellchecker"),
-       Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
+SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, DockView * dv, QWidget * parent)
+       : QTabWidget(parent), d(new Private(this, dv))
 {
        d->ui.setupUi(this);
+       d->gv_ = gv;
 
        connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
                this, SLOT(on_replacePB_clicked()));
@@ -93,19 +122,13 @@ GuiSpellchecker::GuiSpellchecker(GuiView & lv)
 }
 
 
-GuiSpellchecker::~GuiSpellchecker()
+SpellcheckerWidget::~SpellcheckerWidget()
 {
        delete d;
 }
 
 
-void GuiSpellchecker::on_closePB_clicked()
-{
-       close();
-}
-
-
-bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
+bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
 {
        if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
                QKeyEvent *e = static_cast<QKeyEvent *> (event);
@@ -126,7 +149,7 @@ bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
 }
 
 
-void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
+void SpellcheckerWidget::on_suggestionsLW_itemClicked(QListWidgetItem * item)
 {
        if (d->ui.replaceCO->count() != 0)
                d->ui.replaceCO->setItemText(0, item->text());
@@ -137,7 +160,7 @@ void GuiSpellchecker::on_suggestionsLW_itemClicked(QListWidgetItem * item)
 }
 
 
-void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
+void SpellcheckerWidget::on_replaceCO_highlighted(const QString & str)
 {
        QListWidget * lw = d->ui.suggestionsLW;
        if (lw->currentItem() && lw->currentItem()->text() == str)
@@ -152,24 +175,61 @@ void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
 }
 
 
-void GuiSpellchecker::updateView()
+void SpellcheckerWidget::updateView()
+{
+       BufferView * bv = d->gv_->documentBufferView();
+       setEnabled(bv != 0);
+       if (bv && hasFocus() && d->start_.empty()) {
+               d->start_ = bv->cursor();
+               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) {
+               dv_->hide();
+               return false;
+       }
+       dispatch(FuncRequest(LFUN_BUFFER_BEGIN));
+       wrap_around_ = true;
+       return true;
+}
+
+bool SpellcheckerWidget::Private::isWrapAround(DocIterator cursor) const
 {
-       if (hasFocus() && d->count_ == 0)
-               check();
+       return wrap_around_ && start_.buffer() == cursor.buffer() && start_ < cursor;
 }
 
 
-void GuiSpellchecker::forward()
+void SpellcheckerWidget::Private::forward()
 {
-       DocIterator from = bufferview()->cursor();
+       BufferView * bv = gv_->documentBufferView();
+       DocIterator from = bv->cursor();
 
        dispatch(FuncRequest(LFUN_ESCAPE));
        dispatch(FuncRequest(LFUN_CHAR_FORWARD));
-       d->stuck_ = from == bufferview()->cursor();
+       if (bv->cursor().depth() <= 1 && bv->cursor().atLastPos()) {
+               continueFromBeginning();
+               return;
+       }
+       if (from == bv->cursor()) {
+               //FIXME we must be at the end of a cell
+               dispatch(FuncRequest(LFUN_CHAR_FORWARD));
+       }
+       if (isWrapAround(bv->cursor())) {
+               dv_->hide();
+       }
 }
-       
-       
-void GuiSpellchecker::on_languageCO_activated(int index)
+
+
+void SpellcheckerWidget::on_languageCO_activated(int index)
 {
        string const lang =
                fromqstr(d->ui.languageCO->itemData(index).toString());
@@ -177,197 +237,220 @@ void GuiSpellchecker::on_languageCO_activated(int index)
                // nothing changed
                return;
        dispatch(FuncRequest(LFUN_LANGUAGE, lang));
-       check();
+       d->check();
+}
+
+
+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->wrap_around_ = false;
+       d->incheck_ = false;
+       return true;
 }
 
 
-void GuiSpellchecker::on_ignoreAllPB_clicked()
+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_);
-       forward();
-       check();
+       d->forward();
+       d->check();
+       d->canCheck();
 }
 
 
-void GuiSpellchecker::on_addPB_clicked()
+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_);
-       forward();
-       check();
+       d->forward();
+       d->check();
+       d->canCheck();
 }
 
 
-void GuiSpellchecker::on_ignorePB_clicked()
+void SpellcheckerWidget::on_ignorePB_clicked()
 {
-       forward();
-       check();
+       /// ignore this occurrence of word
+       if (d->inCheck())
+               return;
+       LYXERR(Debug::GUI, "Spellchecker: ignore button");
+       d->forward();
+       d->check();
+       d->canCheck();
 }
 
 
-void GuiSpellchecker::on_findNextPB_clicked()
+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 GuiSpellchecker::on_replacePB_clicked()
+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));
-       forward();
-       check();
+       dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
+       d->forward();
+       d->check();
+       d->canCheck();
 }
 
 
-void GuiSpellchecker::on_replaceAllPB_clicked()
+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));
-       forward();
-       check(); // continue spellchecking
+
+       LYXERR(Debug::GUI, "Replace all (" << replacement << ")");
+       dispatch(FuncRequest(LFUN_WORD_REPLACE, datastring));
+       d->forward();
+       d->check(); // continue spellchecking
+       d->canCheck();
 }
 
 
-void GuiSpellchecker::updateSuggestions(docstring_list & words)
+void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
 {
-       QString const suggestion = toqstr(d->word_.word());
-       d->ui.wordED->setText(suggestion);
-       QListWidget * lw = d->ui.suggestionsLW;
+       QString const suggestion = toqstr(word_.word());
+       ui.wordED->setText(suggestion);
+       QListWidget * lw = ui.suggestionsLW;
        lw->clear();
 
        if (words.empty()) {
-               on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
+               p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
                return;
        }
        for (size_t i = 0; i != words.size(); ++i)
                lw->addItem(toqstr(words[i]));
 
-       on_suggestionsLW_itemClicked(lw->item(0));
+       p->on_suggestionsLW_itemClicked(lw->item(0));
        lw->setCurrentRow(0);
 }
 
 
-bool GuiSpellchecker::initialiseParams(string const &)
+void SpellcheckerWidget::Private::setLanguage(Language const * lang)
 {
-       LYXERR(Debug::GUI, "Spellchecker::initialiseParams");
-
-       if (!theSpellChecker())
-               return false;
-
-       DocIterator const begin = doc_iterator_begin(&buffer());
-       Cursor const & cur = bufferview()->cursor();
-       d->progress_ = countWords(begin, cur);
-       d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()));
-       d->count_ = 0;
-       return true;
+       int const pos = ui.languageCO->findData(toqstr(lang->lang()));
+       if (pos != -1)
+               ui.languageCO->setCurrentIndex(pos);
 }
 
 
-void GuiSpellchecker::check()
+void SpellcheckerWidget::Private::check()
 {
-       LYXERR(Debug::GUI, "Check the spelling of the words starting at " << d->progress_);
-
-       // last move forward failed
-       if (d->stuck_) {
-               d->stuck_ = false;
-               showSummary();
+       BufferView * bv = gv_->documentBufferView();
+       if (!bv || bv->buffer().text().empty())
                return;
-       }
-       
-       DocIterator from = bufferview()->cursor();
+
+       DocIterator from = bv->cursor();
        DocIterator to;
        WordLangTuple word_lang;
        docstring_list suggestions;
 
+       LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
        int progress;
        try {
-               progress = buffer().spellCheck(from, to, word_lang, suggestions);
+               progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
        } catch (ExceptionMessage const & message) {
                if (message.type_ == WarningException) {
                        Alert::warning(message.title_, message.details_);
-                       close();
                        return;
                }
                throw message;
        }
-       d->count_ += progress;
-       d->progress_ += progress;
-       LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"" <<
-                  " at position " << d->progress_);
 
        // end of document
-       if (from == doc_iterator_end(&buffer())) {
-               showSummary();
+       if (from == doc_iterator_end(&bv->buffer())) {
+               if (wrap_around_ || start_ == doc_iterator_begin(&bv->buffer())) {
+                       dv_->hide();
+                       return;
+               }
+               if (continueFromBeginning())
+                       check();
                return;
        }
 
-       // current misspelled word has to be counted too.
-       ++d->count_;
-       ++d->progress_;
-
-       if (!isVisible())
-               show();
+       if (isWrapAround(from)) {
+               dv_->hide();
+               return;
+       }
 
-       d->word_ = word_lang;
+       word_ = word_lang;
 
-       int const progress_bar = d->total_
-               ? int(100.0 * float(d->progress_)/d->total_) : 100;
-       LYXERR(Debug::GUI, "Updating spell progress." <<
-                  " Now we have " << progress_bar << " percent.");
-       // set progress bar
-       d->ui.spellcheckPR->setValue(progress_bar);
        // set suggestions
        updateSuggestions(suggestions);
        // set language
-       int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
-       if (pos != -1)
-               d->ui.languageCO->setCurrentIndex(pos);
+       setLanguage(word_lang.lang());
 
        // FIXME LFUN
        // If we used a LFUN, dispatch would do all of this for us
        int const size = to.pos() - from.pos();
-       BufferView * bv = const_cast<BufferView *>(bufferview());
        bv->putSelectionAt(from, size, false);
-       bv->processUpdateFlags(Update::Force | Update::FitCursor);
-       
+       bv->processUpdateFlags(Update::Force | Update::FitCursor);      
 }
 
 
-void GuiSpellchecker::showSummary()
+GuiSpellchecker::GuiSpellchecker(GuiView & parent,
+               Qt::DockWidgetArea area, Qt::WindowFlags flags)
+       : DockView(parent, "spellchecker", qt_("Spellchecker"),
+                  area, flags)
 {
-       if (d->count_ == 0) {
-               close();
-               return;
-       }
+       widget_ = new SpellcheckerWidget(&parent, this);
+       setWidget(widget_);
+       setFocusProxy(widget_);
+}
+
+
+GuiSpellchecker::~GuiSpellchecker()
+{
+       setFocusProxy(0);
+       delete widget_;
+}
 
-       docstring message;
-       if (d->count_ != 1)
-               message = bformat(_("%1$d words checked."), d->count_);
-       else
-               message = _("One word checked.");
 
-       close();
-       Alert::information(_("Spelling check completed"), message);
+void GuiSpellchecker::updateView()
+{
+       widget_->updateView();
 }
 
 
 Dialog * createGuiSpellchecker(GuiView & lv) 
 { 
-       GuiSpellchecker * gui = new GuiSpellchecker(lv);
+       GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
 #ifdef Q_WS_MACX
        gui->setFloating(true);
 #endif