]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSpellchecker.cpp
15756d51c7a6de18dc372d578b0b29a13812207f
[lyx.git] / src / frontends / qt4 / GuiSpellchecker.cpp
1 /**
2  * \file GuiSpellchecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiSpellchecker.h"
14 #include "Qt2BC.h"
15 #include "qt_helpers.h"
16
17 #include <QProgressBar>
18 #include <QLineEdit>
19 #include <QPushButton>
20 #include <QListWidget>
21 #include <QListWidgetItem>
22 #include <QCloseEvent>
23 #include <QSyntaxHighlighter>
24 #include <QTextCharFormat>
25 #include <QTextDocument>
26
27
28 using std::string;
29
30 namespace lyx {
31 namespace frontend {
32
33 /////////////////////////////////////////////////////////////////////
34 //
35 // GuiSpellCheckerDialog
36 //
37 /////////////////////////////////////////////////////////////////////
38
39
40 GuiSpellcheckerDialog::GuiSpellcheckerDialog(GuiSpellchecker * form)
41         : form_(form)
42 {
43         setupUi(this);
44
45         connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
46
47         connect(replaceCO, SIGNAL(highlighted(const QString &)),
48                 this, SLOT(replaceChanged(const QString &)));
49         connect(replacePB, SIGNAL(clicked()),
50                 this, SLOT(replaceClicked()));
51         connect(ignorePB, SIGNAL(clicked()),
52                 this, SLOT(ignoreClicked()));
53         connect(replacePB_3, SIGNAL(clicked()),
54                 this, SLOT(acceptClicked()));
55         connect(addPB, SIGNAL(clicked()),
56                 this, SLOT(addClicked()));
57         connect(suggestionsLW, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
58                 this, SLOT(replaceClicked() ) );
59         connect(suggestionsLW, SIGNAL(itemClicked(QListWidgetItem*)),
60                 this, SLOT(suggestionChanged(QListWidgetItem*)));
61 }
62
63
64 void GuiSpellcheckerDialog::acceptClicked()
65 {
66         form_->accept();
67 }
68
69 void GuiSpellcheckerDialog::addClicked()
70 {
71         form_->add();
72 }
73
74 void GuiSpellcheckerDialog::replaceClicked()
75 {
76         form_->replace();
77 }
78
79 void GuiSpellcheckerDialog::ignoreClicked()
80 {
81         form_->ignore();
82 }
83
84 void GuiSpellcheckerDialog::suggestionChanged(QListWidgetItem * item)
85 {
86         if (replaceCO->count() != 0)
87                 replaceCO->setItemText(0, item->text());
88         else
89                 replaceCO->addItem(item->text());
90
91         replaceCO->setCurrentIndex(0);
92 }
93
94 void GuiSpellcheckerDialog::replaceChanged(const QString & str)
95 {
96         if (suggestionsLW->currentItem()->text() == str)
97                 return;
98
99         for (int i = 0; i < suggestionsLW->count(); ++i) {
100                 if (suggestionsLW->item(i)->text() == str) {
101                         suggestionsLW->setCurrentRow(i);
102                         break;
103                 }
104         }
105 }
106
107
108 void GuiSpellcheckerDialog::closeEvent(QCloseEvent * e)
109 {
110         form_->slotWMHide();
111         e->accept();
112 }
113
114
115 void GuiSpellcheckerDialog::reject()
116 {
117         form_->slotWMHide();
118         QDialog::reject();
119 }
120
121
122
123 /////////////////////////////////////////////////////////////////////
124 //
125 // GuiSpellChecker
126 //
127 /////////////////////////////////////////////////////////////////////
128
129
130 GuiSpellchecker::GuiSpellchecker(Dialog & parent)
131         : GuiView<GuiSpellcheckerDialog>(parent, _("Spellchecker"))
132 {}
133
134
135 void GuiSpellchecker::build_dialog()
136 {
137         dialog_.reset(new GuiSpellcheckerDialog(this));
138
139         bcview().setCancel(dialog_->closePB);
140         dialog_->wordED->setReadOnly(true);
141 }
142
143
144 void GuiSpellchecker::update_contents()
145 {
146         if (isVisible() || controller().exitEarly())
147                 controller().check();
148 }
149
150
151 void GuiSpellchecker::accept()
152 {
153         controller().ignoreAll();
154 }
155
156
157 void GuiSpellchecker::add()
158 {
159         controller().insert();
160 }
161
162
163 void GuiSpellchecker::ignore()
164 {
165         controller().check();
166 }
167
168
169 void GuiSpellchecker::replace()
170 {
171         controller().replace(qstring_to_ucs4(dialog_->replaceCO->currentText()));
172 }
173
174
175 void GuiSpellchecker::partialUpdate(int s)
176 {
177         ControlSpellchecker::State const state =
178                 static_cast<ControlSpellchecker::State>(s);
179
180         switch (state) {
181
182         case ControlSpellchecker::SPELL_PROGRESSED:
183                 dialog_->spellcheckPR->setValue(controller().getProgress());
184                 break;
185
186         case ControlSpellchecker::SPELL_FOUND_WORD: {
187                 dialog_->wordED->setText(toqstr(controller().getWord()));
188                 dialog_->suggestionsLW->clear();
189
190                 docstring w;
191                 while (!(w = controller().getSuggestion()).empty()) {
192                         dialog_->suggestionsLW->addItem(toqstr(w));
193                 }
194
195                 if (dialog_->suggestionsLW->count() == 0) {
196                         dialog_->suggestionChanged(new QListWidgetItem(dialog_->wordED->text()));
197                 } else {
198                         dialog_->suggestionChanged(dialog_->suggestionsLW->item(0));
199                 }
200
201                 dialog_->suggestionsLW->setCurrentRow(0);
202         }
203                 break;
204
205         }
206 }
207
208 } // namespace frontend
209 } // namespace lyx
210
211 #include "GuiSpellchecker_moc.cpp"