Cleanup the SpellChecker dialog (fixes #7375 and #7379):

* Transform Spellchecker.ui into a simple QWidget
* Pimpl all we can
* Remove progress bar
* Remove Close button (as for all other dock widgets)
* Allow the dialog to stay even for empty document
* Disable the dialog when closing all documents
* always work against the current document Bufferview (as opposed to embedded bufferview).


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38018 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2011-03-23 23:10:12 +00:00
parent dd23151da9
commit 70c603a1b2
4 changed files with 369 additions and 391 deletions

View File

@ -72,6 +72,8 @@ public:
bool empty() const { return slices_.empty(); } bool empty() const { return slices_.empty(); }
/// is this the end position? /// is this the end position?
bool atEnd() const { return slices_.empty(); } bool atEnd() const { return slices_.empty(); }
/// is this the last possible position?
bool atLastPos() const { return pit() == lastpit() && pos() == lastpos(); }
// //
// access to slice at tip // access to slice at tip

View File

@ -41,8 +41,9 @@
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/textutils.h" #include "support/textutils.h"
#include <QListWidgetItem>
#include <QKeyEvent> #include <QKeyEvent>
#include <QListWidgetItem>
#include <QMessageBox>
#include "SpellChecker.h" #include "SpellChecker.h"
@ -55,27 +56,34 @@ namespace lyx {
namespace frontend { namespace frontend {
struct GuiSpellchecker::Private struct SpellcheckerWidget::Private
{ {
Private() : progress_(0), count_(0), stuck_(false) {} Private(SpellcheckerWidget * parent)
: p(parent) {}
/// 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();
///
Ui::SpellcheckerUi ui; Ui::SpellcheckerUi ui;
///
SpellcheckerWidget * p;
///
GuiView * gv_;
/// current word being checked and lang code /// current word being checked and lang code
WordLangTuple word_; WordLangTuple word_;
/// values for progress
int total_;
int progress_;
/// word count
int count_;
/// flag for last move forward success
bool stuck_;
}; };
GuiSpellchecker::GuiSpellchecker(GuiView & lv) SpellcheckerWidget::SpellcheckerWidget(GuiView * gv, QWidget * parent)
: DockView(lv, "spellchecker", qt_("Spellchecker"), : QWidget(parent), d(new Private(this))
Qt::RightDockWidgetArea), d(new GuiSpellchecker::Private)
{ {
d->ui.setupUi(this); d->ui.setupUi(this);
d->gv_ = gv;
connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)), connect(d->ui.suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(on_replacePB_clicked())); this, SLOT(on_replacePB_clicked()));
@ -93,19 +101,13 @@ GuiSpellchecker::GuiSpellchecker(GuiView & lv)
} }
GuiSpellchecker::~GuiSpellchecker() SpellcheckerWidget::~SpellcheckerWidget()
{ {
delete d; delete d;
} }
void GuiSpellchecker::on_closePB_clicked() bool SpellcheckerWidget::eventFilter(QObject *obj, QEvent *event)
{
close();
}
bool GuiSpellchecker::eventFilter(QObject *obj, QEvent *event)
{ {
if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) { if (obj == d->ui.suggestionsLW && event->type() == QEvent::KeyPress) {
QKeyEvent *e = static_cast<QKeyEvent *> (event); QKeyEvent *e = static_cast<QKeyEvent *> (event);
@ -126,7 +128,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) if (d->ui.replaceCO->count() != 0)
d->ui.replaceCO->setItemText(0, item->text()); d->ui.replaceCO->setItemText(0, item->text());
@ -137,7 +139,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; QListWidget * lw = d->ui.suggestionsLW;
if (lw->currentItem() && lw->currentItem()->text() == str) if (lw->currentItem() && lw->currentItem()->text() == str)
@ -152,24 +154,48 @@ void GuiSpellchecker::on_replaceCO_highlighted(const QString & str)
} }
void GuiSpellchecker::updateView() void SpellcheckerWidget::updateView()
{ {
if (hasFocus() && d->count_ == 0) BufferView * bv = d->gv_->documentBufferView();
check(); setEnabled(bv != 0);
if (bv && hasFocus())
d->check();
} }
void GuiSpellchecker::forward() bool SpellcheckerWidget::Private::continueFromBeginning()
{ {
DocIterator from = bufferview()->cursor(); 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;
}
void SpellcheckerWidget::Private::forward()
{
BufferView * bv = gv_->documentBufferView();
DocIterator from = bv->cursor();
dispatch(FuncRequest(LFUN_ESCAPE)); dispatch(FuncRequest(LFUN_ESCAPE));
dispatch(FuncRequest(LFUN_CHAR_FORWARD)); dispatch(FuncRequest(LFUN_CHAR_FORWARD));
d->stuck_ = from == bufferview()->cursor(); if (bv->cursor().atLastPos()) {
continueFromBeginning();
return;
}
if (from == bv->cursor()) {
//FIXME we must be at the end of a cell
dispatch(FuncRequest(LFUN_CHAR_FORWARD));
}
} }
void GuiSpellchecker::on_languageCO_activated(int index) void SpellcheckerWidget::on_languageCO_activated(int index)
{ {
string const lang = string const lang =
fromqstr(d->ui.languageCO->itemData(index).toString()); fromqstr(d->ui.languageCO->itemData(index).toString());
@ -177,37 +203,37 @@ void GuiSpellchecker::on_languageCO_activated(int index)
// nothing changed // nothing changed
return; return;
dispatch(FuncRequest(LFUN_LANGUAGE, lang)); dispatch(FuncRequest(LFUN_LANGUAGE, lang));
check(); d->check();
} }
void GuiSpellchecker::on_ignoreAllPB_clicked() void SpellcheckerWidget::on_ignoreAllPB_clicked()
{ {
/// replace all occurrences of word /// replace all occurrences of word
if (d->word_.lang() && !d->word_.word().empty()) if (d->word_.lang() && !d->word_.word().empty())
theSpellChecker()->accept(d->word_); theSpellChecker()->accept(d->word_);
forward(); d->forward();
check(); d->check();
} }
void GuiSpellchecker::on_addPB_clicked() void SpellcheckerWidget::on_addPB_clicked()
{ {
/// insert word in personal dictionary /// insert word in personal dictionary
theSpellChecker()->insert(d->word_); theSpellChecker()->insert(d->word_);
forward(); d->forward();
check(); d->check();
} }
void GuiSpellchecker::on_ignorePB_clicked() void SpellcheckerWidget::on_ignorePB_clicked()
{ {
forward(); d->forward();
check(); d->check();
} }
void GuiSpellchecker::on_findNextPB_clicked() void SpellcheckerWidget::on_findNextPB_clicked()
{ {
docstring const data = find2string( docstring const data = find2string(
qstring_to_ucs4(d->ui.wordED->text()), qstring_to_ucs4(d->ui.wordED->text()),
@ -216,7 +242,7 @@ void GuiSpellchecker::on_findNextPB_clicked()
} }
void GuiSpellchecker::on_replacePB_clicked() void SpellcheckerWidget::on_replacePB_clicked()
{ {
docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText()); docstring const replacement = qstring_to_ucs4(d->ui.replaceCO->currentText());
docstring const data = replace2string( docstring const data = replace2string(
@ -225,149 +251,115 @@ void GuiSpellchecker::on_replacePB_clicked()
LYXERR(Debug::GUI, "Replace (" << replacement << ")"); LYXERR(Debug::GUI, "Replace (" << replacement << ")");
dispatch(FuncRequest(LFUN_WORD_REPLACE, data)); dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
forward(); d->forward();
check(); d->check();
} }
void GuiSpellchecker::on_replaceAllPB_clicked() void SpellcheckerWidget::on_replaceAllPB_clicked()
{ {
docstring const data = replace2string( docstring const data = replace2string(
qstring_to_ucs4(d->ui.replaceCO->currentText()), qstring_to_ucs4(d->ui.replaceCO->currentText()),
qstring_to_ucs4(d->ui.wordED->text()), qstring_to_ucs4(d->ui.wordED->text()),
true, true, true, true); true, true, true, true);
dispatch(FuncRequest(LFUN_WORD_REPLACE, data)); dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
forward(); d->forward();
check(); // continue spellchecking d->check(); // continue spellchecking
} }
void GuiSpellchecker::updateSuggestions(docstring_list & words) void SpellcheckerWidget::Private::updateSuggestions(docstring_list & words)
{ {
QString const suggestion = toqstr(d->word_.word()); QString const suggestion = toqstr(word_.word());
d->ui.wordED->setText(suggestion); ui.wordED->setText(suggestion);
QListWidget * lw = d->ui.suggestionsLW; QListWidget * lw = ui.suggestionsLW;
lw->clear(); lw->clear();
if (words.empty()) { if (words.empty()) {
on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion)); p->on_suggestionsLW_itemClicked(new QListWidgetItem(suggestion));
return; return;
} }
for (size_t i = 0; i != words.size(); ++i) for (size_t i = 0; i != words.size(); ++i)
lw->addItem(toqstr(words[i])); lw->addItem(toqstr(words[i]));
on_suggestionsLW_itemClicked(lw->item(0)); p->on_suggestionsLW_itemClicked(lw->item(0));
lw->setCurrentRow(0); lw->setCurrentRow(0);
} }
bool GuiSpellchecker::initialiseParams(string const &) void SpellcheckerWidget::Private::check()
{ {
LYXERR(Debug::GUI, "Spellchecker::initialiseParams"); BufferView * bv = gv_->documentBufferView();
if (!bv)
if (!theSpellChecker())
return false;
DocIterator const begin = doc_iterator_begin(&buffer());
Cursor const & cur = bufferview()->cursor();
d->progress_ = countWords(begin, cur, false);
d->total_ = d->progress_ + countWords(cur, doc_iterator_end(&buffer()), false);
d->count_ = 0;
return true;
}
void GuiSpellchecker::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();
return; return;
}
DocIterator from = bufferview()->cursor(); DocIterator from = bv->cursor();
DocIterator to; DocIterator to;
WordLangTuple word_lang; WordLangTuple word_lang;
docstring_list suggestions; docstring_list suggestions;
int progress; int progress;
try { try {
progress = buffer().spellCheck(from, to, word_lang, suggestions); progress = bv->buffer().spellCheck(from, to, word_lang, suggestions);
} catch (ExceptionMessage const & message) { } catch (ExceptionMessage const & message) {
if (message.type_ == WarningException) { if (message.type_ == WarningException) {
Alert::warning(message.title_, message.details_); Alert::warning(message.title_, message.details_);
close();
return; return;
} }
throw message; throw message;
} }
d->count_ += progress;
d->progress_ += progress;
LYXERR(Debug::GUI, "Found word \"" << word_lang.word() << "\"" <<
" at position " << d->progress_);
// end of document // end of document
if (from == doc_iterator_end(&buffer())) { if (from == doc_iterator_end(&bv->buffer())) {
showSummary(); if (continueFromBeginning())
check();
return; return;
} }
// current misspelled word has to be counted too. word_ = word_lang;
++d->count_;
++d->progress_;
if (!isVisible())
show();
d->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 // set suggestions
updateSuggestions(suggestions); updateSuggestions(suggestions);
// set language // set language
int const pos = d->ui.languageCO->findData(toqstr(word_lang.lang()->lang())); int const pos = ui.languageCO->findData(toqstr(word_lang.lang()->lang()));
if (pos != -1) if (pos != -1)
d->ui.languageCO->setCurrentIndex(pos); ui.languageCO->setCurrentIndex(pos);
// FIXME LFUN // FIXME LFUN
// If we used a LFUN, dispatch would do all of this for us // If we used a LFUN, dispatch would do all of this for us
int const size = to.pos() - from.pos(); int const size = to.pos() - from.pos();
BufferView * bv = const_cast<BufferView *>(bufferview());
bv->putSelectionAt(from, size, false); 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) { widget_ = new SpellcheckerWidget(&parent);
close(); setWidget(widget_);
return; setFocusProxy(widget_);
} }
docstring message;
if (d->count_ != 1)
message = bformat(_("%1$d words checked."), d->count_);
else
message = _("One word checked.");
close(); GuiSpellchecker::~GuiSpellchecker()
Alert::information(_("Spelling check completed"), message); {
setFocusProxy(0);
delete widget_;
}
void GuiSpellchecker::updateView()
{
widget_->updateView();
} }
Dialog * createGuiSpellchecker(GuiView & lv) Dialog * createGuiSpellchecker(GuiView & lv)
{ {
GuiSpellchecker * gui = new GuiSpellchecker(lv); GuiSpellchecker * gui = new GuiSpellchecker(lv, Qt::RightDockWidgetArea);
#ifdef Q_WS_MACX #ifdef Q_WS_MACX
gui->setFloating(true); gui->setFloating(true);
#endif #endif

View File

@ -25,16 +25,19 @@ class docstring_list;
namespace frontend { namespace frontend {
class GuiSpellchecker : public DockView class SpellcheckerWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
GuiSpellchecker(GuiView & parent); SpellcheckerWidget(GuiView * gv, QWidget * parent = 0);
~GuiSpellchecker(); ~SpellcheckerWidget();
///
void updateView();
///
bool initialiseParams(std::string const & data);
private Q_SLOTS: private Q_SLOTS:
void on_closePB_clicked();
void on_findNextPB_clicked(); void on_findNextPB_clicked();
void on_replaceAllPB_clicked(); void on_replaceAllPB_clicked();
void on_suggestionsLW_itemClicked(QListWidgetItem *); void on_suggestionsLW_itemClicked(QListWidgetItem *);
@ -46,31 +49,36 @@ private Q_SLOTS:
void on_replacePB_clicked(); void on_replacePB_clicked();
private: private:
/// update from controller ///
void updateSuggestions(docstring_list & words);
///{
void updateView();
bool initialiseParams(std::string const & data);
void clearParams() {}
void dispatchParams() {}
bool isBufferDependent() const { return true; }
bool needBufferOpen() const { return true; }
///}
/// move to next position after current word
void forward();
/// check text until next misspelled/unknown word
void check();
/// show count of checked words at normal exit
void showSummary();
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);
struct Private; struct Private;
Private * const d; Private * const d;
}; };
class GuiSpellchecker : public DockView
{
Q_OBJECT
public:
GuiSpellchecker(
GuiView & parent, ///< the main window where to dock.
Qt::DockWidgetArea area = Qt::RightDockWidgetArea, ///< Position of the dock (and also drawer)
Qt::WindowFlags flags = 0);
~GuiSpellchecker();
private:
///{
void updateView();
bool initialiseParams(std::string const &) { return true; }
void clearParams() {}
void dispatchParams() {}
bool isBufferDependent() const { return false; }
///}
/// The encapsulated widget.
SpellcheckerWidget * widget_;
};
} // namespace frontend } // namespace frontend
} // namespace lyx } // namespace lyx

View File

@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>SpellcheckerUi</class> <class>SpellcheckerUi</class>
<widget class="QDockWidget" name="SpellcheckerUi" > <widget class="QWidget" name="SpellcheckerUi">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>264</width> <width>237</width>
<height>388</height> <height>342</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string/> <string>Spell Checker</string>
</property> </property>
<widget class="QWidget" name="dockWidgetContents" >
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2"> <item row="0" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
@ -29,7 +29,7 @@
<item> <item>
<widget class="QComboBox" name="languageCO"> <widget class="QComboBox" name="languageCO">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Fixed" hsizetype="Expanding" > <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
@ -68,7 +68,7 @@
<item row="3" column="1"> <item row="3" column="1">
<widget class="QPushButton" name="findNextPB"> <widget class="QPushButton" name="findNextPB">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>100</horstretch> <horstretch>100</horstretch>
<verstretch>32</verstretch> <verstretch>32</verstretch>
</sizepolicy> </sizepolicy>
@ -116,7 +116,7 @@
<item row="5" column="1"> <item row="5" column="1">
<widget class="QPushButton" name="replacePB"> <widget class="QPushButton" name="replacePB">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>100</horstretch> <horstretch>100</horstretch>
<verstretch>32</verstretch> <verstretch>32</verstretch>
</sizepolicy> </sizepolicy>
@ -132,7 +132,7 @@
<item row="6" column="0"> <item row="6" column="0">
<widget class="QLabel" name="TextLabel2"> <widget class="QLabel" name="TextLabel2">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Maximum" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
@ -148,7 +148,7 @@
<item row="6" column="1"> <item row="6" column="1">
<widget class="QPushButton" name="replaceAllPB"> <widget class="QPushButton" name="replaceAllPB">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>100</horstretch> <horstretch>100</horstretch>
<verstretch>32</verstretch> <verstretch>32</verstretch>
</sizepolicy> </sizepolicy>
@ -161,13 +161,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item rowspan="4" row="7" column="0" > <item row="7" column="0" rowspan="4">
<widget class="QListWidget" name="suggestionsLW"/> <widget class="QListWidget" name="suggestionsLW"/>
</item> </item>
<item row="7" column="1"> <item row="7" column="1">
<widget class="QPushButton" name="ignorePB"> <widget class="QPushButton" name="ignorePB">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Maximum" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>100</horstretch> <horstretch>100</horstretch>
<verstretch>32</verstretch> <verstretch>32</verstretch>
</sizepolicy> </sizepolicy>
@ -183,7 +183,7 @@
<item row="8" column="1"> <item row="8" column="1">
<widget class="QPushButton" name="ignoreAllPB"> <widget class="QPushButton" name="ignoreAllPB">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Maximum" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>100</horstretch> <horstretch>100</horstretch>
<verstretch>32</verstretch> <verstretch>32</verstretch>
</sizepolicy> </sizepolicy>
@ -199,7 +199,7 @@
<item row="9" column="1"> <item row="9" column="1">
<widget class="QPushButton" name="addPB"> <widget class="QPushButton" name="addPB">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy vsizetype="Maximum" hsizetype="Preferred" > <sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>100</horstretch> <horstretch>100</horstretch>
<verstretch>32</verstretch> <verstretch>32</verstretch>
</sizepolicy> </sizepolicy>
@ -228,32 +228,8 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="11" column="0" >
<widget class="QProgressBar" name="spellcheckPR" >
<property name="value" >
<number>24</number>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="11" column="1" >
<widget class="QPushButton" name="closePB" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Preferred" hsizetype="Preferred" >
<horstretch>100</horstretch>
<verstretch>32</verstretch>
</sizepolicy>
</property>
<property name="text" >
<string>&amp;Close</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>