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