]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiThesaurus.cpp
#10571 improved handling of WM's signal when switching from or to full-screen window
[features.git] / src / frontends / qt / 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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiThesaurus.h"
15 #include "GuiApplication.h"
16
17 #include "qt_helpers.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "FuncRequest.h"
23 #include "Language.h"
24 #include "lyxfind.h"
25 #include "WordLangTuple.h"
26
27 #include "support/debug.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <QAbstractItemModel>
32 #include <QCompleter>
33 #include <QDialogButtonBox>
34 #include <QHeaderView>
35 #include <QLineEdit>
36 #include <QPushButton>
37 #include <QTreeWidget>
38 #include <QTreeWidgetItem>
39
40
41 using namespace lyx::support;
42 using namespace std;
43
44 namespace lyx {
45 namespace frontend {
46
47 GuiThesaurus::GuiThesaurus(GuiView & lv)
48         : GuiDialog(lv, "thesaurus", qt_("Thesaurus"))
49 {
50         setupUi(this);
51
52         meaningsTV->setColumnCount(1);
53         meaningsTV->header()->hide();
54
55         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
56                 this, SLOT(slotButtonBox(QAbstractButton *)));
57         connect(replaceED, SIGNAL(returnPressed()),
58                 this, SLOT(replaceClicked()));
59         connect(replaceED, SIGNAL(textChanged(QString)),
60                 this, SLOT(change_adaptor()));
61         connect(entryCO, SIGNAL(editTextChanged(const QString &)),
62                 this, SLOT(entryChanged()));
63         connect(entryCO, SIGNAL(activated(int)),
64                 this, SLOT(entryChanged()));
65         connect(lookupPB, SIGNAL(clicked()),
66                 this, SLOT(entryChanged()));
67         connect(replacePB, SIGNAL(clicked()),
68                 this, SLOT(replaceClicked()));
69         connect(languageCO, SIGNAL(activated(int)),
70                 this, SLOT(entryChanged()));
71         connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
72                 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
73         connect(meaningsTV, SIGNAL(itemSelectionChanged()),
74                 this, SLOT(selectionChanged()));
75         connect(meaningsTV, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
76                 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
77
78         // language
79         QAbstractItemModel * language_model = guiApp->languageModel();
80         // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
81         language_model->sort(0);
82         languageCO->setModel(language_model);
83         languageCO->setModelColumn(2);
84
85         //bug #8138
86         if (entryCO->completer())
87                 entryCO->completer()->setCompletionMode(QCompleter::PopupCompletion);
88
89         bc().setCancel(buttonBox->button(QDialogButtonBox::Close));
90         bc().setApply(replacePB, true);
91         bc().addReadOnly(replaceED);
92         bc().addReadOnly(replacePB);
93         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
94
95         setFocusProxy(entryCO);
96 }
97
98 void GuiThesaurus::checkStatus()
99 {
100         if (!isBufferAvailable()) {
101                 // deactivate the thesaurus if we have no buffer
102                 enableView(false);
103                 return;
104         }
105         updateView();
106 }
107
108 void GuiThesaurus::change_adaptor()
109 {
110         changed();
111 }
112
113
114 void GuiThesaurus::entryChanged()
115 {
116         updateLists();
117 }
118
119
120 void GuiThesaurus::selectionChanged()
121 {
122         int const col = meaningsTV->currentColumn();
123         if (col < 0 || isBufferReadonly())
124                 return;
125
126         QString item = meaningsTV->currentItem()->text(col);
127         // cut out the classification in brackets:
128         // "hominid (generic term)" -> "hominid"
129         QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
130         // This is for items with classifications at the beginning:
131         // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
132         QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
133         int pos = re.indexIn(item);
134         if (pos > -1)
135                 item = re.cap(1).trimmed();
136         pos = rex.indexIn(item);
137         if (pos > -1)
138                 item = rex.cap(2).trimmed();
139         replaceED->setText(item);
140         replacePB->setEnabled(!isBufferReadonly());
141         changed();
142 }
143
144
145 void GuiThesaurus::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
146 {
147         selectionChanged();
148 }
149
150
151 void GuiThesaurus::selectionClicked(QTreeWidgetItem * item, int col)
152 {
153         QString str = item->text(col);
154         // cut out the classification in brackets:
155         // "hominid (generic term)" -> "hominid"
156         QRegExp re("^([^\\(\\)]+)\\b\\(?.*\\)?.*$");
157         // This is for items with classifications at the beginning:
158         // "(noun) man" -> "man"; "(noun) male (generic term)" -> "male"
159         QRegExp rex("^(\\(.+\\))\\s*([^\\(\\)]+)\\s*\\(?.*\\)?.*$");
160         int pos = re.indexIn(str);
161         if (pos > -1)
162                 str = re.cap(1).trimmed();
163         pos = rex.indexIn(str);
164         if (pos > -1)
165                 str = rex.cap(2).trimmed();
166         entryCO->insertItem(0, str);
167         entryCO->setCurrentIndex(0);
168
169         selectionChanged();
170         updateLists();
171 }
172
173
174 void GuiThesaurus::updateLists()
175 {
176         meaningsTV->clear();
177
178         if (entryCO->currentText().isEmpty())
179                 return;
180
181         meaningsTV->setUpdatesEnabled(false);
182
183         QString const lang = languageCO->itemData(
184                 languageCO->currentIndex()).toString();
185         Language * language = const_cast<Language*>(lyx::languages.getLanguage(fromqstr(lang)));
186         docstring const lang_code = from_ascii(language->code());
187
188         Thesaurus::Meanings meanings =
189                 getMeanings(WordLangTuple(qstring_to_ucs4(entryCO->currentText()), language));
190
191         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
192                 cit != meanings.end(); ++cit) {
193                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
194                 i->setText(0, toqstr(cit->first));
195                 meaningsTV->expandItem(i);
196                 for (vector<docstring>::const_iterator cit2 = cit->second.begin();
197                         cit2 != cit->second.end(); ++cit2) {
198                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
199                                 i2->setText(0, toqstr(*cit2));
200                         }
201                 meaningsTV->setEnabled(true);
202                 lookupPB->setEnabled(true);
203                 bool const readonly = isBufferReadonly();
204                 replaceED->setEnabled(!readonly);
205                 replacePB->setEnabled(!readonly);
206         }
207
208         if (meanings.empty()) {
209                 if (!thesaurus.thesaurusAvailable(lang_code)) {
210                         QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
211                         i->setText(0, qt_("No thesaurus available for this language!"));
212                         meaningsTV->setEnabled(false);
213                         lookupPB->setEnabled(false);
214                         replaceED->setEnabled(false);
215                         replacePB->setEnabled(false);
216                 }
217         }
218
219         meaningsTV->setUpdatesEnabled(true);
220         meaningsTV->update();
221 }
222
223
224 void GuiThesaurus::updateContents()
225 {
226         entryCO->clear();
227         entryCO->addItem(toqstr(text_));
228         entryCO->setCurrentIndex(0);
229         replaceED->setText("");
230         int const pos = languageCO->findData(toqstr(lang_));
231         if (pos != -1)
232                 languageCO->setCurrentIndex(pos);
233         updateLists();
234 }
235
236
237 void GuiThesaurus::replaceClicked()
238 {
239         replace(qstring_to_ucs4(replaceED->text()));
240 }
241
242
243 bool GuiThesaurus::initialiseParams(string const & sdata)
244 {
245         string arg;
246         string const lang = rsplit(sdata, arg, ' ');
247         if (prefixIs(lang, "lang=")) {
248                 lang_ = from_utf8(split(lang, '='));
249                 text_ = from_utf8(arg);
250         } else {
251                 text_ = from_utf8(sdata);
252                 if (bufferview())
253                         lang_ = from_ascii(
254                                 bufferview()->buffer().params().language->lang());
255         }
256         return true;
257 }
258
259
260 void GuiThesaurus::clearParams()
261 {
262         text_.erase();
263         lang_.erase();
264 }
265
266
267 void GuiThesaurus::replace(docstring const & newstr)
268 {
269         /* FIXME: this is not suitable ! We need to have a "lock"
270          * on a particular charpos in a paragraph that is broken on
271          * deletion/change !
272          */
273         docstring const sdata =
274                 replace2string(newstr, text_,
275                                      true,  // case sensitive
276                                      true,  // match word
277                                      false, // all words
278                                      true); // forward
279         dispatch(FuncRequest(LFUN_WORD_REPLACE, sdata));
280 }
281
282
283 Thesaurus::Meanings const & GuiThesaurus::getMeanings(WordLangTuple const & wl)
284 {
285         if (wl.word() != laststr_)
286                 meanings_ = thesaurus.lookup(wl);
287         return meanings_;
288 }
289
290
291 Dialog * createGuiThesaurus(GuiView & lv) { return new GuiThesaurus(lv); }
292
293
294 } // namespace frontend
295 } // namespace lyx
296
297
298 #include "moc_GuiThesaurus.cpp"