]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
Fix the tab ordering of GuiDocument components.
[lyx.git] / src / frontends / qt4 / GuiThesaurus.cpp
1 /**
2  * \file GuiThesaurus.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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiThesaurus.h"
15 #include "GuiApplication.h"
16
17 #include "qt_helpers.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "FuncRequest.h"
23 #include "Language.h"
24 #include "lyxfind.h"
25
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 #include <QAbstractItemModel>
31 #include <QHeaderView>
32 #include <QLineEdit>
33 #include <QPushButton>
34 #include <QTreeWidget>
35 #include <QTreeWidgetItem>
36
37
38 using namespace lyx::support;
39 using namespace std;
40
41 namespace lyx {
42 namespace frontend {
43
44 GuiThesaurus::GuiThesaurus(GuiView & lv)
45         : GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
46 {
47         setupUi(this);
48
49         meaningsTV->setColumnCount(1);
50         meaningsTV->header()->hide();
51
52         connect(closePB, SIGNAL(clicked()),
53                 this, SLOT(slotClose()));
54         connect(replaceED, SIGNAL(returnPressed()),
55                 this, SLOT(replaceClicked()));
56         connect(replaceED, SIGNAL(textChanged(QString)),
57                 this, SLOT(change_adaptor()));
58         connect(entryCO, SIGNAL(editTextChanged(const QString &)),
59                 this, SLOT(entryChanged()));
60         connect(entryCO, SIGNAL(activated(int)),
61                 this, SLOT(entryChanged()));
62         connect(lookupPB, SIGNAL(clicked()),
63                 this, SLOT(entryChanged()));
64         connect(replacePB, SIGNAL(clicked()),
65                 this, SLOT(replaceClicked()));
66         connect(languageCO, SIGNAL(activated(int)),
67                 this, SLOT(entryChanged()));
68         connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
69                 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
70         connect(meaningsTV, SIGNAL(itemSelectionChanged()),
71                 this, SLOT(selectionChanged()));
72         connect(meaningsTV, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
73                 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
74
75         // language
76         QAbstractItemModel * language_model = guiApp->languageModel();
77         // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
78         language_model->sort(0);
79         languageCO->setModel(language_model);
80         languageCO->setModelColumn(2);
81
82         bc().setCancel(closePB);
83         bc().setApply(replacePB);
84         bc().addReadOnly(replaceED);
85         bc().addReadOnly(replacePB);
86         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
87 }
88
89 void GuiThesaurus::checkStatus()
90 {
91         if (!isBufferAvailable()) {
92                 // deactivate the thesaurus if we have no buffer
93                 enableView(false);
94                 return;
95         }
96         updateView();
97 }
98         
99 void GuiThesaurus::change_adaptor()
100 {
101         changed();
102 }
103
104
105 void GuiThesaurus::entryChanged()
106 {
107         updateLists();
108 }
109
110
111 void GuiThesaurus::selectionChanged()
112 {
113         int const col = meaningsTV->currentColumn();
114         if (col < 0 || isBufferReadonly())
115                 return;
116
117         QString item = meaningsTV->currentItem()->text(col);
118         // cut out the classification in brackets:
119         // "hominid (generic term)" -> "hominid"
120         QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
121         // This is for items with classifications at the beginning:
122         // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
123         QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
124         int pos = re.indexIn(item);
125         if (pos > -1)
126                 item = re.cap(1).trimmed();
127         pos = rex.indexIn(item);
128         if (pos > -1)
129                 item = rex.cap(2).trimmed();
130         replaceED->setText(item);
131         replacePB->setEnabled(!isBufferReadonly());
132         changed();
133 }
134
135
136 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
137 {
138         selectionChanged();
139 }
140
141
142 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
143 {
144         QString str = item->text(col);
145         // cut out the classification in brackets:
146         // "hominid (generic term)" -> "hominid"
147         QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
148         // This is for items with classifications at the beginning:
149         // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
150         QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
151         int pos = re.indexIn(str);
152         if (pos > -1)
153                 str = re.cap(1).trimmed();
154         pos = rex.indexIn(str);
155         if (pos > -1)
156                 str = rex.cap(2).trimmed();
157         entryCO->insertItem(0, str);
158         entryCO->setCurrentIndex(0);
159
160         selectionChanged();
161         updateLists();
162 }
163
164
165 void GuiThesaurus::updateLists()
166 {
167         meaningsTV->clear();
168
169         if (entryCO->currentText().isEmpty())
170                 return;
171
172         meaningsTV->setUpdatesEnabled(false);
173
174         QString const lang = languageCO->itemData(
175                 languageCO->currentIndex()).toString();
176         docstring const lang_code =
177                 from_ascii(lyx::languages.getLanguage(fromqstr(lang))->code());
178
179         Thesaurus::Meanings meanings =
180                 getMeanings(qstring_to_ucs4(entryCO->currentText()), lang_code);
181
182         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
183                 cit != meanings.end(); ++cit) {
184                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
185                 i->setText(0, toqstr(cit->first));
186                 meaningsTV->expandItem(i);
187                 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
188                         cit2 != cit->second.end(); ++cit2) {
189                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
190                                 i2->setText(0, toqstr(*cit2));
191                         }
192                 meaningsTV->setEnabled(true);
193                 lookupPB->setEnabled(true);
194                 bool const readonly = isBufferReadonly();
195                 replaceED->setEnabled(!readonly);
196                 replacePB->setEnabled(!readonly);
197         }
198
199         if (meanings.empty()) {
200                 if (!thesaurus.thesaurusAvailable(lang_code)) {
201                         QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
202                         i->setText(0, qt_("No thesaurus available for this language!"));
203                         meaningsTV->setEnabled(false);
204                         lookupPB->setEnabled(false);
205                         replaceED->setEnabled(false);
206                         replacePB->setEnabled(false);
207                 }
208         }
209
210         meaningsTV->setUpdatesEnabled(true);
211         meaningsTV->update();
212 }
213
214
215 void GuiThesaurus::updateContents()
216 {
217         entryCO->clear();
218         entryCO->addItem(toqstr(text_));
219         entryCO->setCurrentIndex(0);
220         replaceED->setText("");
221         int const pos = languageCO->findData(toqstr(lang_));
222         if (pos != -1)
223                 languageCO->setCurrentIndex(pos);
224         updateLists();
225 }
226
227
228 void GuiThesaurus::replaceClicked()
229 {
230         replace(qstring_to_ucs4(replaceED->text()));
231 }
232
233
234 bool GuiThesaurus::initialiseParams(string const & data)
235 {
236         string arg;
237         string const lang = rsplit(data, arg, ' ');
238         if (prefixIs(lang, "lang=")) {
239                 lang_ = from_utf8(split(lang, '='));
240                 text_ = from_utf8(arg);
241         } else {
242                 text_ = from_utf8(data);
243                 if (bufferview())
244                         lang_ = from_ascii(
245                                 bufferview()->buffer().params().language->lang());
246         }
247         return true;
248 }
249
250
251 void GuiThesaurus::clearParams()
252 {
253         text_.erase();
254         lang_.erase();
255 }
256
257
258 void GuiThesaurus::replace(docstring const & newstr)
259 {
260         /* FIXME: this is not suitable ! We need to have a "lock"
261          * on a particular charpos in a paragraph that is broken on
262          * deletion/change !
263          */
264         docstring const data =
265                 replace2string(newstr, text_,
266                                      true,  // case sensitive
267                                      true,  // match word
268                                      false, // all words
269                                      true); // forward
270         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
271 }
272
273
274 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str,
275         docstring const & lang)
276 {
277         if (str != laststr_)
278                 meanings_ = thesaurus.lookup(str, lang);
279         return meanings_;
280 }
281
282
283 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
284
285
286 } // namespace frontend
287 } // namespace lyx
288
289
290 #include "moc_GuiThesaurus.cpp"