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