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"
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
30 #include <QAbstractItemModel>
31 #include <QHeaderView>
33 #include <QPushButton>
34 #include <QTreeWidget>
35 #include <QTreeWidgetItem>
38 using namespace lyx::support;
44 GuiThesaurus::GuiThesaurus(GuiView & lv)
45 : GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
49 meaningsTV->setColumnCount(1);
50 meaningsTV->header()->hide();
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)));
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);
82 bc().setCancel(closePB);
83 bc().setApply(replacePB);
84 bc().addReadOnly(replaceED);
85 bc().addReadOnly(replacePB);
86 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
90 void GuiThesaurus::change_adaptor()
96 void GuiThesaurus::entryChanged()
102 void GuiThesaurus::selectionChanged()
104 int const col = meaningsTV->currentColumn();
105 if (col < 0 || isBufferReadonly())
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);
117 item = re.cap(1).trimmed();
118 pos = rex.indexIn(item);
120 item = rex.cap(2).trimmed();
121 replaceED->setText(item);
122 replacePB->setEnabled(true);
127 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
133 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
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);
144 str = re.cap(1).trimmed();
145 pos = rex.indexIn(str);
147 str = rex.cap(2).trimmed();
148 entryCO->insertItem(0, str);
149 entryCO->setCurrentIndex(0);
156 void GuiThesaurus::updateLists()
159 meaningsTV->setUpdatesEnabled(false);
161 QString const lang = languageCO->itemData(
162 languageCO->currentIndex()).toString();
163 docstring const lang_code =
164 from_ascii(lyx::languages.getLanguage(fromqstr(lang))->code());
166 Thesaurus::Meanings meanings =
167 getMeanings(qstring_to_ucs4(entryCO->currentText()), lang_code);
169 for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
170 cit != meanings.end(); ++cit) {
171 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
172 i->setText(0, toqstr(cit->first));
173 meaningsTV->expandItem(i);
174 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
175 cit2 != cit->second.end(); ++cit2) {
176 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
177 i2->setText(0, toqstr(*cit2));
179 meaningsTV->setEnabled(true);
180 lookupPB->setEnabled(true);
181 replaceED->setEnabled(true);
182 replacePB->setEnabled(true);
185 if (meanings.empty()) {
186 if (!thesaurus.thesaurusAvailable(lang_code)) {
187 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
188 i->setText(0, qt_("No thesaurus available for this language!"));
189 meaningsTV->setEnabled(false);
190 lookupPB->setEnabled(false);
191 replaceED->setEnabled(false);
192 replacePB->setEnabled(false);
196 meaningsTV->setUpdatesEnabled(true);
197 meaningsTV->update();
201 void GuiThesaurus::updateContents()
204 entryCO->addItem(toqstr(text_));
205 entryCO->setCurrentIndex(0);
206 replaceED->setText("");
207 int const pos = languageCO->findData(toqstr(lang_));
209 languageCO->setCurrentIndex(pos);
214 void GuiThesaurus::replaceClicked()
216 replace(qstring_to_ucs4(replaceED->text()));
220 bool GuiThesaurus::initialiseParams(string const & data)
223 string const lang = rsplit(data, arg, ' ');
224 if (prefixIs(lang, "lang=")) {
225 lang_ = from_utf8(split(lang, '='));
226 text_ = from_utf8(arg);
228 text_ = from_utf8(data);
231 bufferview()->buffer().params().language->lang());
237 void GuiThesaurus::clearParams()
244 void GuiThesaurus::replace(docstring const & newstr)
246 /* FIXME: this is not suitable ! We need to have a "lock"
247 * on a particular charpos in a paragraph that is broken on
250 docstring const data =
251 replace2string(newstr, text_,
252 true, // case sensitive
256 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
260 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str,
261 docstring const & lang)
264 meanings_ = thesaurus.lookup(str, lang);
269 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
272 } // namespace frontend
276 #include "moc_GuiThesaurus.cpp"