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