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