2 * \file GuiThesaurus.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "GuiThesaurus.h"
15 #include "qt_helpers.h"
17 #include "FuncRequest.h"
20 #include "support/debug.h"
21 #include "support/gettext.h"
23 #include <QCloseEvent>
24 #include <QHeaderView>
26 #include <QPushButton>
27 #include <QTreeWidget>
28 #include <QTreeWidgetItem>
36 GuiThesaurus::GuiThesaurus(GuiView & lv)
37 : GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
41 meaningsTV->setColumnCount(1);
42 meaningsTV->header()->hide();
44 connect(closePB, SIGNAL(clicked()),
45 this, SLOT(slotClose()));
46 connect(replaceED, SIGNAL(returnPressed()),
47 this, SLOT(replaceClicked()));
48 connect(replaceED, SIGNAL(textChanged(QString)),
49 this, SLOT(change_adaptor()));
50 connect(entryED, SIGNAL(returnPressed()),
51 this, SLOT(entryChanged()));
52 connect(replacePB, SIGNAL(clicked()),
53 this, SLOT(replaceClicked()));
54 connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
55 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
56 connect(meaningsTV, SIGNAL(itemSelectionChanged()),
57 this, SLOT(selectionChanged()));
58 connect(meaningsTV, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
59 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
61 bc().setCancel(closePB);
62 bc().setApply(replacePB);
63 bc().addReadOnly(replaceED);
64 bc().addReadOnly(replacePB);
65 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
69 void GuiThesaurus::change_adaptor()
75 void GuiThesaurus::entryChanged()
81 void GuiThesaurus::selectionChanged()
83 int const col = meaningsTV->currentColumn();
84 if (col < 0 || isBufferReadonly())
87 replaceED->setText(meaningsTV->currentItem()->text(col));
88 replacePB->setEnabled(true);
93 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
99 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
101 entryED->setText(item->text(col));
107 void GuiThesaurus::updateLists()
110 meaningsTV->setUpdatesEnabled(false);
112 Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text()));
114 for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
115 cit != meanings.end(); ++cit) {
116 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
117 i->setText(0, toqstr(cit->first));
118 meaningsTV->expandItem(i);
119 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
120 cit2 != cit->second.end(); ++cit2) {
121 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
122 i2->setText(0, toqstr(*cit2));
126 meaningsTV->setUpdatesEnabled(true);
127 meaningsTV->update();
131 void GuiThesaurus::updateContents()
133 entryED->setText(toqstr(text_));
134 replaceED->setText("");
139 void GuiThesaurus::replaceClicked()
141 replace(qstring_to_ucs4(replaceED->text()));
145 bool GuiThesaurus::initialiseParams(string const & data)
147 text_ = from_utf8(data);
152 void GuiThesaurus::clearParams()
158 void GuiThesaurus::replace(docstring const & newstr)
160 /* FIXME: this is not suitable ! We need to have a "lock"
161 * on a particular charpos in a paragraph that is broken on
164 docstring const data =
165 replace2string(text_, newstr,
166 true, // case sensitive
170 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
174 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str)
177 meanings_ = thesaurus.lookup(str);
182 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
185 } // namespace frontend
189 #include "GuiThesaurus_moc.cpp"