]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
Fix handling of the add branch textfield in GuiBranches
[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/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <QAbstractItemModel>
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         bc().setCancel(closePB);
84         bc().setApply(replacePB);
85         bc().addReadOnly(replaceED);
86         bc().addReadOnly(replacePB);
87         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
88 }
89
90 void GuiThesaurus::checkStatus()
91 {
92         if (!isBufferAvailable()) {
93                 // deactivate the thesaurus if we have no buffer
94                 enableView(false);
95                 return;
96         }
97         updateView();
98 }
99         
100 void GuiThesaurus::change_adaptor()
101 {
102         changed();
103 }
104
105
106 void GuiThesaurus::entryChanged()
107 {
108         updateLists();
109 }
110
111
112 void GuiThesaurus::selectionChanged()
113 {
114         int const col = meaningsTV->currentColumn();
115         if (col < 0 || isBufferReadonly())
116                 return;
117
118         QString item = meaningsTV->currentItem()->text(col);
119         // cut out the classification in brackets:
120         // "hominid (generic term)" -> "hominid"
121         QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
122         // This is for items with classifications at the beginning:
123         // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
124         QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
125         int pos = re.indexIn(item);
126         if (pos > -1)
127                 item = re.cap(1).trimmed();
128         pos = rex.indexIn(item);
129         if (pos > -1)
130                 item = rex.cap(2).trimmed();
131         replaceED->setText(item);
132         replacePB->setEnabled(!isBufferReadonly());
133         changed();
134 }
135
136
137 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
138 {
139         selectionChanged();
140 }
141
142
143 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
144 {
145         QString str = item->text(col);
146         // cut out the classification in brackets:
147         // "hominid (generic term)" -> "hominid"
148         QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
149         // This is for items with classifications at the beginning:
150         // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
151         QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
152         int pos = re.indexIn(str);
153         if (pos > -1)
154                 str = re.cap(1).trimmed();
155         pos = rex.indexIn(str);
156         if (pos > -1)
157                 str = rex.cap(2).trimmed();
158         entryCO->insertItem(0, str);
159         entryCO->setCurrentIndex(0);
160
161         selectionChanged();
162         updateLists();
163 }
164
165
166 void GuiThesaurus::updateLists()
167 {
168         meaningsTV->clear();
169
170         if (entryCO->currentText().isEmpty())
171                 return;
172
173         meaningsTV->setUpdatesEnabled(false);
174
175         QString const lang = languageCO->itemData(
176                 languageCO->currentIndex()).toString();
177         Language * language = const_cast<Language*>(lyx::languages.getLanguage(fromqstr(lang)));
178         docstring const lang_code = from_ascii(language->code());
179
180         Thesaurus::Meanings meanings =
181                 getMeanings(WordLangTuple(qstring_to_ucs4(entryCO->currentText()), language));
182
183         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
184                 cit != meanings.end(); ++cit) {
185                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
186                 i->setText(0, toqstr(cit->first));
187                 meaningsTV->expandItem(i);
188                 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
189                         cit2 != cit->second.end(); ++cit2) {
190                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
191                                 i2->setText(0, toqstr(*cit2));
192                         }
193                 meaningsTV->setEnabled(true);
194                 lookupPB->setEnabled(true);
195                 bool const readonly = isBufferReadonly();
196                 replaceED->setEnabled(!readonly);
197                 replacePB->setEnabled(!readonly);
198         }
199
200         if (meanings.empty()) {
201                 if (!thesaurus.thesaurusAvailable(lang_code)) {
202                         QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
203                         i->setText(0, qt_("No thesaurus available for this language!"));
204                         meaningsTV->setEnabled(false);
205                         lookupPB->setEnabled(false);
206                         replaceED->setEnabled(false);
207                         replacePB->setEnabled(false);
208                 }
209         }
210
211         meaningsTV->setUpdatesEnabled(true);
212         meaningsTV->update();
213 }
214
215
216 void GuiThesaurus::updateContents()
217 {
218         entryCO->clear();
219         entryCO->addItem(toqstr(text_));
220         entryCO->setCurrentIndex(0);
221         replaceED->setText("");
222         int const pos = languageCO->findData(toqstr(lang_));
223         if (pos != -1)
224                 languageCO->setCurrentIndex(pos);
225         updateLists();
226 }
227
228
229 void GuiThesaurus::replaceClicked()
230 {
231         replace(qstring_to_ucs4(replaceED->text()));
232 }
233
234
235 bool GuiThesaurus::initialiseParams(string const & data)
236 {
237         string arg;
238         string const lang = rsplit(data, arg, ' ');
239         if (prefixIs(lang, "lang=")) {
240                 lang_ = from_utf8(split(lang, '='));
241                 text_ = from_utf8(arg);
242         } else {
243                 text_ = from_utf8(data);
244                 if (bufferview())
245                         lang_ = from_ascii(
246                                 bufferview()->buffer().params().language->lang());
247         }
248         return true;
249 }
250
251
252 void GuiThesaurus::clearParams()
253 {
254         text_.erase();
255         lang_.erase();
256 }
257
258
259 void GuiThesaurus::replace(docstring const & newstr)
260 {
261         /* FIXME: this is not suitable ! We need to have a "lock"
262          * on a particular charpos in a paragraph that is broken on
263          * deletion/change !
264          */
265         docstring const data =
266                 replace2string(newstr, text_,
267                                      true,  // case sensitive
268                                      true,  // match word
269                                      false, // all words
270                                      true); // forward
271         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
272 }
273
274
275 Thesaurus::Meanings const & GuiThesaurus::getMeanings(WordLangTuple const & wl)
276 {
277         if (wl.word() != laststr_)
278                 meanings_ = thesaurus.lookup(wl);
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"