]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
getting rid of superfluous lyx::support:: statements.
[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 "support/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 namespace std;
29
30 namespace lyx {
31 namespace frontend {
32
33 GuiThesaurus::GuiThesaurus(GuiView & lv)
34         : GuiDialog(lv, "thesaurus")
35 {
36         setupUi(this);
37         setViewTitle(_("Thesaurus"));
38
39         meaningsTV->setColumnCount(1);
40         meaningsTV->header()->hide();
41
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)));
58
59         bc().setCancel(closePB);
60         bc().setApply(replacePB);
61         bc().addReadOnly(replaceED);
62         bc().addReadOnly(replacePB);
63         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
64 }
65
66
67 void GuiThesaurus::change_adaptor()
68 {
69         changed();
70 }
71
72
73 void GuiThesaurus::closeEvent(QCloseEvent * e)
74 {
75         slotClose();
76         GuiDialog::closeEvent(e);
77 }
78
79
80 void GuiThesaurus::entryChanged()
81 {
82         updateLists();
83 }
84
85
86 void GuiThesaurus::selectionChanged()
87 {
88         int const col = meaningsTV->currentColumn();
89         if (col < 0 || isBufferReadonly())
90                 return;
91
92         replaceED->setText(meaningsTV->currentItem()->text(col));
93         replacePB->setEnabled(true);
94         changed();
95 }
96
97
98 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
99 {
100         selectionChanged();
101 }
102
103
104 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
105 {
106         entryED->setText(item->text(col));
107         selectionChanged();
108         updateLists();
109 }
110
111
112 void GuiThesaurus::updateLists()
113 {
114         meaningsTV->clear();
115         meaningsTV->setUpdatesEnabled(false);
116
117         Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text()));
118
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));
128                         }
129         }
130
131         meaningsTV->setUpdatesEnabled(true);
132         meaningsTV->update();
133 }
134
135
136 void GuiThesaurus::updateContents()
137 {
138         entryED->setText(toqstr(text_));
139         replaceED->setText("");
140         updateLists();
141 }
142
143
144 void GuiThesaurus::replaceClicked()
145 {
146         replace(qstring_to_ucs4(replaceED->text()));
147 }
148
149
150 bool GuiThesaurus::initialiseParams(string const & data)
151 {
152         text_ = from_utf8(data);
153         return true;
154 }
155
156
157 void GuiThesaurus::clearParams()
158 {
159         text_.erase();
160 }
161
162
163 void GuiThesaurus::replace(docstring const & newstr)
164 {
165         /* FIXME: this is not suitable ! We need to have a "lock"
166          * on a particular charpos in a paragraph that is broken on
167          * deletion/change !
168          */
169         docstring const data =
170                 replace2string(text_, newstr,
171                                      true,  // case sensitive
172                                      true,  // match word
173                                      false, // all words
174                                      true); // forward
175         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
176 }
177
178
179 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str)
180 {
181         if (str != laststr_)
182                 meanings_ = thesaurus.lookup(str);
183         return meanings_;
184 }
185
186
187 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
188
189
190 } // namespace frontend
191 } // namespace lyx
192
193
194 #include "GuiThesaurus_moc.cpp"