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