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