]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
7267b070d1b1923ae882badca453d13e8a1778f0
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiThesaurus.h"
14
15 #include "qt_helpers.h"
16 #include "debug.h"
17
18 #include <QCloseEvent>
19 #include <QHeaderView>
20 #include <QLineEdit>
21 #include <QPushButton>
22 #include <QTreeWidget>
23 #include <QTreeWidgetItem>
24
25 #include "lyxfind.h"
26 #include "FuncRequest.h"
27
28 using std::string;
29
30
31 namespace lyx {
32 namespace frontend {
33
34 GuiThesaurus::GuiThesaurus(GuiView & lv)
35         : GuiDialog(lv, "thesaurus")
36 {
37         setupUi(this);
38         setViewTitle(_("Thesaurus"));
39
40         meaningsTV->setColumnCount(1);
41         meaningsTV->header()->hide();
42
43         connect(closePB, SIGNAL(clicked()),
44                 this, SLOT(slotClose()));
45         connect(replaceED, SIGNAL(returnPressed()),
46                 this, SLOT(replaceClicked()));
47         connect(replaceED, SIGNAL(textChanged(QString)),
48                 this, SLOT(change_adaptor()));
49         connect(entryED, SIGNAL(returnPressed()),
50                 this, SLOT(entryChanged()));
51         connect(replacePB, SIGNAL(clicked()),
52                 this, SLOT(replaceClicked()));
53         connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
54                 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
55         connect(meaningsTV, SIGNAL(itemSelectionChanged()),
56                 this, SLOT(selectionChanged()));
57         connect(meaningsTV, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
58                 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
59
60         bc().setCancel(closePB);
61         bc().setApply(replacePB);
62         bc().addReadOnly(replaceED);
63         bc().addReadOnly(replacePB);
64         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
65 }
66
67
68 void GuiThesaurus::change_adaptor()
69 {
70         changed();
71 }
72
73
74 void GuiThesaurus::closeEvent(QCloseEvent * e)
75 {
76         slotClose();
77         GuiDialog::closeEvent(e);
78 }
79
80
81 void GuiThesaurus::entryChanged()
82 {
83         updateLists();
84 }
85
86
87 void GuiThesaurus::selectionChanged()
88 {
89         int const col = meaningsTV->currentColumn();
90         if (col < 0 || isBufferReadonly())
91                 return;
92
93         replaceED->setText(meaningsTV->currentItem()->text(col));
94         replacePB->setEnabled(true);
95         changed();
96 }
97
98
99 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
100 {
101         selectionChanged();
102 }
103
104
105 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
106 {
107         entryED->setText(item->text(col));
108         selectionChanged();
109         updateLists();
110 }
111
112
113 void GuiThesaurus::updateLists()
114 {
115         meaningsTV->clear();
116         meaningsTV->setUpdatesEnabled(false);
117
118         Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text()));
119
120         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
121                 cit != meanings.end(); ++cit) {
122                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
123                 i->setText(0, toqstr(cit->first));
124                 meaningsTV->expandItem(i);
125                 for (std::vector<docstring>::const_iterator cit2 = cit->second.begin();
126                         cit2 != cit->second.end(); ++cit2) {
127                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
128                                 i2->setText(0, toqstr(*cit2));
129                         }
130         }
131
132         meaningsTV->setUpdatesEnabled(true);
133         meaningsTV->update();
134 }
135
136
137 void GuiThesaurus::updateContents()
138 {
139         entryED->setText(toqstr(text_));
140         replaceED->setText("");
141         updateLists();
142 }
143
144
145 void GuiThesaurus::replaceClicked()
146 {
147         replace(qstring_to_ucs4(replaceED->text()));
148 }
149
150
151 bool GuiThesaurus::initialiseParams(string const & data)
152 {
153         text_ = from_utf8(data);
154         return true;
155 }
156
157
158 void GuiThesaurus::clearParams()
159 {
160         text_.erase();
161 }
162
163
164 void GuiThesaurus::replace(docstring const & newstr)
165 {
166         /* FIXME: this is not suitable ! We need to have a "lock"
167          * on a particular charpos in a paragraph that is broken on
168          * deletion/change !
169          */
170         docstring const data =
171                 replace2string(text_, newstr,
172                                      true,  // case sensitive
173                                      true,  // match word
174                                      false, // all words
175                                      true); // forward
176         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
177 }
178
179
180 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str)
181 {
182         if (str != laststr_)
183                 meanings_ = thesaurus.lookup(str);
184         return meanings_;
185 }
186
187
188 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
189
190
191 } // namespace frontend
192 } // namespace lyx
193
194
195 #include "GuiThesaurus_moc.cpp"