2 * \file GuiThesaurus.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "GuiThesaurus.h"
15 #include "GuiApplication.h"
17 #include "qt_helpers.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "FuncRequest.h"
25 #include "WordLangTuple.h"
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
31 #include <QAbstractItemModel>
33 #include <QDialogButtonBox>
34 #include <QHeaderView>
36 #include <QPushButton>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItem>
41 using namespace lyx::support;
47 GuiThesaurus::GuiThesaurus(GuiView & lv)
48 : GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
52 meaningsTV->setColumnCount(1);
53 meaningsTV->header()->hide();
55 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
56 this, SLOT(slotButtonBox(QAbstractButton *)));
57 connect(replaceED, SIGNAL(returnPressed()),
58 this, SLOT(replaceClicked()));
59 connect(replaceED, SIGNAL(textChanged(QString)),
60 this, SLOT(change_adaptor()));
61 connect(entryCO, SIGNAL(editTextChanged(const QString &)),
62 this, SLOT(entryChanged()));
63 connect(entryCO, SIGNAL(activated(int)),
64 this, SLOT(entryChanged()));
65 connect(lookupPB, SIGNAL(clicked()),
66 this, SLOT(entryChanged()));
67 connect(replacePB, SIGNAL(clicked()),
68 this, SLOT(replaceClicked()));
69 connect(languageCO, SIGNAL(activated(int)),
70 this, SLOT(entryChanged()));
71 connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
72 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
73 connect(meaningsTV, SIGNAL(itemSelectionChanged()),
74 this, SLOT(selectionChanged()));
75 connect(meaningsTV, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
76 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
79 QAbstractItemModel * language_model = guiApp->languageModel();
80 // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
81 language_model->sort(0);
82 languageCO->setModel(language_model);
83 languageCO->setModelColumn(2);
86 if (entryCO->completer())
87 entryCO->completer()->setCompletionMode(QCompleter::PopupCompletion);
89 bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
90 bc().setApply(replacePB);
91 bc().addReadOnly(replaceED);
92 bc().addReadOnly(replacePB);
93 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
96 void GuiThesaurus::checkStatus()
98 if (!isBufferAvailable()) {
99 // deactivate the thesaurus if we have no buffer
106 void GuiThesaurus::change_adaptor()
112 void GuiThesaurus::entryChanged()
118 void GuiThesaurus::selectionChanged()
120 int const col = meaningsTV->currentColumn();
121 if (col < 0 || isBufferReadonly())
124 QString item = meaningsTV->currentItem()->text(col);
125 // cut out the classification in brackets:
126 // "hominid (generic term)" -> "hominid"
127 QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
128 // This is for items with classifications at the beginning:
129 // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
130 QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
131 int pos = re.indexIn(item);
133 item = re.cap(1).trimmed();
134 pos = rex.indexIn(item);
136 item = rex.cap(2).trimmed();
137 replaceED->setText(item);
138 replacePB->setEnabled(!isBufferReadonly());
143 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
149 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
151 QString str = item->text(col);
152 // cut out the classification in brackets:
153 // "hominid (generic term)" -> "hominid"
154 QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
155 // This is for items with classifications at the beginning:
156 // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
157 QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
158 int pos = re.indexIn(str);
160 str = re.cap(1).trimmed();
161 pos = rex.indexIn(str);
163 str = rex.cap(2).trimmed();
164 entryCO->insertItem(0, str);
165 entryCO->setCurrentIndex(0);
172 void GuiThesaurus::updateLists()
176 if (entryCO->currentText().isEmpty())
179 meaningsTV->setUpdatesEnabled(false);
181 QString const lang = languageCO->itemData(
182 languageCO->currentIndex()).toString();
183 Language * language = const_cast<Language*>(lyx::languages.getLanguage(fromqstr(lang)));
184 docstring const lang_code = from_ascii(language->code());
186 Thesaurus::Meanings meanings =
187 getMeanings(WordLangTuple(qstring_to_ucs4(entryCO->currentText()), language));
189 for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
190 cit != meanings.end(); ++cit) {
191 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
192 i->setText(0, toqstr(cit->first));
193 meaningsTV->expandItem(i);
194 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
195 cit2 != cit->second.end(); ++cit2) {
196 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
197 i2->setText(0, toqstr(*cit2));
199 meaningsTV->setEnabled(true);
200 lookupPB->setEnabled(true);
201 bool const readonly = isBufferReadonly();
202 replaceED->setEnabled(!readonly);
203 replacePB->setEnabled(!readonly);
206 if (meanings.empty()) {
207 if (!thesaurus.thesaurusAvailable(lang_code)) {
208 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
209 i->setText(0, qt_("No thesaurus available for this language!"));
210 meaningsTV->setEnabled(false);
211 lookupPB->setEnabled(false);
212 replaceED->setEnabled(false);
213 replacePB->setEnabled(false);
217 meaningsTV->setUpdatesEnabled(true);
218 meaningsTV->update();
222 void GuiThesaurus::updateContents()
225 entryCO->addItem(toqstr(text_));
226 entryCO->setCurrentIndex(0);
227 replaceED->setText("");
228 int const pos = languageCO->findData(toqstr(lang_));
230 languageCO->setCurrentIndex(pos);
235 void GuiThesaurus::replaceClicked()
237 replace(qstring_to_ucs4(replaceED->text()));
241 bool GuiThesaurus::initialiseParams(string const & sdata)
244 string const lang = rsplit(sdata, arg, ' ');
245 if (prefixIs(lang, "lang=")) {
246 lang_ = from_utf8(split(lang, '='));
247 text_ = from_utf8(arg);
249 text_ = from_utf8(sdata);
252 bufferview()->buffer().params().language->lang());
258 void GuiThesaurus::clearParams()
265 void GuiThesaurus::replace(docstring const & newstr)
267 /* FIXME: this is not suitable ! We need to have a "lock"
268 * on a particular charpos in a paragraph that is broken on
271 docstring const sdata =
272 replace2string(newstr, text_,
273 true, // case sensitive
277 dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
281 Thesaurus::Meanings const & GuiThesaurus::getMeanings(WordLangTuple const & wl)
283 if (wl.word() != laststr_)
284 meanings_ = thesaurus.lookup(wl);
289 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
292 } // namespace frontend
296 #include "moc_GuiThesaurus.cpp"