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