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"
16 #include "support/debug.h"
18 #include <QCloseEvent>
19 #include <QHeaderView>
21 #include <QPushButton>
22 #include <QTreeWidget>
23 #include <QTreeWidgetItem>
26 #include "FuncRequest.h"
33 GuiThesaurus::GuiThesaurus(GuiView & lv)
34 : GuiDialog(lv, "thesaurus")
37 setViewTitle(_("Thesaurus"));
39 meaningsTV->setColumnCount(1);
40 meaningsTV->header()->hide();
42 connect(closePB, SIGNAL(clicked()),
43 this, SLOT(slotClose()));
44 connect(replaceED, SIGNAL(returnPressed()),
45 this, SLOT(replaceClicked()));
46 connect(replaceED, SIGNAL(textChanged(QString)),
47 this, SLOT(change_adaptor()));
48 connect(entryED, SIGNAL(returnPressed()),
49 this, SLOT(entryChanged()));
50 connect(replacePB, SIGNAL(clicked()),
51 this, SLOT(replaceClicked()));
52 connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
53 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
54 connect(meaningsTV, SIGNAL(itemSelectionChanged()),
55 this, SLOT(selectionChanged()));
56 connect(meaningsTV, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
57 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
59 bc().setCancel(closePB);
60 bc().setApply(replacePB);
61 bc().addReadOnly(replaceED);
62 bc().addReadOnly(replacePB);
63 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
67 void GuiThesaurus::change_adaptor()
73 void GuiThesaurus::closeEvent(QCloseEvent * e)
76 GuiDialog::closeEvent(e);
80 void GuiThesaurus::entryChanged()
86 void GuiThesaurus::selectionChanged()
88 int const col = meaningsTV->currentColumn();
89 if (col < 0 || isBufferReadonly())
92 replaceED->setText(meaningsTV->currentItem()->text(col));
93 replacePB->setEnabled(true);
98 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
104 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
106 entryED->setText(item->text(col));
112 void GuiThesaurus::updateLists()
115 meaningsTV->setUpdatesEnabled(false);
117 Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text()));
119 for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
120 cit != meanings.end(); ++cit) {
121 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
122 i->setText(0, toqstr(cit->first));
123 meaningsTV->expandItem(i);
124 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
125 cit2 != cit->second.end(); ++cit2) {
126 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
127 i2->setText(0, toqstr(*cit2));
131 meaningsTV->setUpdatesEnabled(true);
132 meaningsTV->update();
136 void GuiThesaurus::updateContents()
138 entryED->setText(toqstr(text_));
139 replaceED->setText("");
144 void GuiThesaurus::replaceClicked()
146 replace(qstring_to_ucs4(replaceED->text()));
150 bool GuiThesaurus::initialiseParams(string const & data)
152 text_ = from_utf8(data);
157 void GuiThesaurus::clearParams()
163 void GuiThesaurus::replace(docstring const & newstr)
165 /* FIXME: this is not suitable ! We need to have a "lock"
166 * on a particular charpos in a paragraph that is broken on
169 docstring const data =
170 replace2string(text_, newstr,
171 true, // case sensitive
175 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
179 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str)
182 meanings_ = thesaurus.lookup(str);
187 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
190 } // namespace frontend
194 #include "GuiThesaurus_moc.cpp"