]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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
17 #include "FuncRequest.h"
18 #include "lyxfind.h"
19
20 #include "support/debug.h"
21 #include "support/gettext.h"
22
23 #include <QHeaderView>
24 #include <QLineEdit>
25 #include <QPushButton>
26 #include <QTreeWidget>
27 #include <QTreeWidgetItem>
28
29
30 using namespace std;
31
32 namespace lyx {
33 namespace frontend {
34
35 GuiThesaurus::GuiThesaurus(GuiView & lv)
36         : GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
37 {
38         setupUi(this);
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::entryChanged()
75 {
76         updateLists();
77 }
78
79
80 void GuiThesaurus::selectionChanged()
81 {
82         int const col = meaningsTV->currentColumn();
83         if (col < 0 || isBufferReadonly())
84                 return;
85
86         replaceED->setText(meaningsTV->currentItem()->text(col));
87         replacePB->setEnabled(true);
88         changed();
89 }
90
91
92 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
93 {
94         selectionChanged();
95 }
96
97
98 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
99 {
100         entryED->setText(item->text(col));
101         selectionChanged();
102         updateLists();
103 }
104
105
106 void GuiThesaurus::updateLists()
107 {
108         meaningsTV->clear();
109         meaningsTV->setUpdatesEnabled(false);
110
111         Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text()));
112
113         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
114                 cit != meanings.end(); ++cit) {
115                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
116                 i->setText(0, toqstr(cit->first));
117                 meaningsTV->expandItem(i);
118                 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
119                         cit2 != cit->second.end(); ++cit2) {
120                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
121                                 i2->setText(0, toqstr(*cit2));
122                         }
123         }
124
125         meaningsTV->setUpdatesEnabled(true);
126         meaningsTV->update();
127 }
128
129
130 void GuiThesaurus::updateContents()
131 {
132         entryED->setText(toqstr(text_));
133         replaceED->setText("");
134         updateLists();
135 }
136
137
138 void GuiThesaurus::replaceClicked()
139 {
140         replace(qstring_to_ucs4(replaceED->text()));
141 }
142
143
144 bool GuiThesaurus::initialiseParams(string const & data)
145 {
146         text_ = from_utf8(data);
147         return true;
148 }
149
150
151 void GuiThesaurus::clearParams()
152 {
153         text_.erase();
154 }
155
156
157 void GuiThesaurus::replace(docstring const & newstr)
158 {
159         /* FIXME: this is not suitable ! We need to have a "lock"
160          * on a particular charpos in a paragraph that is broken on
161          * deletion/change !
162          */
163         docstring const data =
164                 replace2string(text_, newstr,
165                                      true,  // case sensitive
166                                      true,  // match word
167                                      false, // all words
168                                      true); // forward
169         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
170 }
171
172
173 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str)
174 {
175         if (str != laststr_)
176                 meanings_ = thesaurus.lookup(str);
177         return meanings_;
178 }
179
180
181 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
182
183
184 } // namespace frontend
185 } // namespace lyx
186
187
188 #include "GuiThesaurus_moc.cpp"