]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
Fix a crash following the input of an invalid paragraph separation value in the docum...
[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::closeEvent(QCloseEvent * e)
76 {
77         slotClose();
78         GuiDialog::closeEvent(e);
79 }
80
81
82 void GuiThesaurus::entryChanged()
83 {
84         updateLists();
85 }
86
87
88 void GuiThesaurus::selectionChanged()
89 {
90         int const col = meaningsTV->currentColumn();
91         if (col < 0 || isBufferReadonly())
92                 return;
93
94         replaceED->setText(meaningsTV->currentItem()->text(col));
95         replacePB->setEnabled(true);
96         changed();
97 }
98
99
100 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
101 {
102         selectionChanged();
103 }
104
105
106 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
107 {
108         entryED->setText(item->text(col));
109         selectionChanged();
110         updateLists();
111 }
112
113
114 void GuiThesaurus::updateLists()
115 {
116         meaningsTV->clear();
117         meaningsTV->setUpdatesEnabled(false);
118
119         Thesaurus::Meanings meanings = getMeanings(qstring_to_ucs4(entryED->text()));
120
121         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
122                 cit != meanings.end(); ++cit) {
123                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
124                 i->setText(0, toqstr(cit->first));
125                 meaningsTV->expandItem(i);
126                 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
127                         cit2 != cit->second.end(); ++cit2) {
128                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
129                                 i2->setText(0, toqstr(*cit2));
130                         }
131         }
132
133         meaningsTV->setUpdatesEnabled(true);
134         meaningsTV->update();
135 }
136
137
138 void GuiThesaurus::updateContents()
139 {
140         entryED->setText(toqstr(text_));
141         replaceED->setText("");
142         updateLists();
143 }
144
145
146 void GuiThesaurus::replaceClicked()
147 {
148         replace(qstring_to_ucs4(replaceED->text()));
149 }
150
151
152 bool GuiThesaurus::initialiseParams(string const & data)
153 {
154         text_ = from_utf8(data);
155         return true;
156 }
157
158
159 void GuiThesaurus::clearParams()
160 {
161         text_.erase();
162 }
163
164
165 void GuiThesaurus::replace(docstring const & newstr)
166 {
167         /* FIXME: this is not suitable ! We need to have a "lock"
168          * on a particular charpos in a paragraph that is broken on
169          * deletion/change !
170          */
171         docstring const data =
172                 replace2string(text_, newstr,
173                                      true,  // case sensitive
174                                      true,  // match word
175                                      false, // all words
176                                      true); // forward
177         dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
178 }
179
180
181 Thesaurus::Meanings const & GuiThesaurus::getMeanings(docstring const & str)
182 {
183         if (str != laststr_)
184                 meanings_ = thesaurus.lookup(str);
185         return meanings_;
186 }
187
188
189 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
190
191
192 } // namespace frontend
193 } // namespace lyx
194
195
196 #include "GuiThesaurus_moc.cpp"