]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
dc076319f051a042e4e43bb5ff156b02128e3de9
[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 #include "Qt2BC.h"
15 #include "qt_helpers.h"
16 #include "debug.h"
17
18 #include <QCloseEvent>
19 #include <QHeaderView>
20 #include <QLineEdit>
21 #include <QPushButton>
22 #include <QTreeWidget>
23 #include <QTreeWidgetItem>
24
25 using std::string;
26
27 namespace lyx {
28 namespace frontend {
29
30 /////////////////////////////////////////////////////////////////////
31 //
32 // QTheasurusDialog
33 //
34 /////////////////////////////////////////////////////////////////////
35
36 GuiThesaurusDialog::GuiThesaurusDialog(GuiThesaurus * form)
37         : form_(form)
38 {
39         setupUi(this);
40
41         meaningsTV->setColumnCount(1);
42         meaningsTV->header()->hide();
43
44         connect(closePB, SIGNAL(clicked()),
45                 form, SLOT(slotClose()));
46         connect(replaceED, SIGNAL(returnPressed()),
47                 this, SLOT(replaceClicked()));
48         connect(replaceED, SIGNAL(textChanged(const 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
62
63 void GuiThesaurusDialog::change_adaptor()
64 {
65         form_->changed();
66 }
67
68
69 void GuiThesaurusDialog::closeEvent(QCloseEvent * e)
70 {
71         form_->slotWMHide();
72         e->accept();
73 }
74
75
76 void GuiThesaurusDialog::entryChanged()
77 {
78         updateLists();
79 }
80
81
82 void GuiThesaurusDialog::replaceClicked()
83 {
84         form_->replace();
85 }
86
87
88 void GuiThesaurusDialog::selectionChanged()
89 {
90         int const col = meaningsTV->currentColumn();
91         if (col<0 || form_->readOnly())
92                 return;
93
94         replaceED->setText(meaningsTV->currentItem()->text(col));
95         replacePB->setEnabled(true);
96         form_->changed();
97 }
98
99
100 void GuiThesaurusDialog::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
101 {
102         selectionChanged();
103 }
104
105
106 void GuiThesaurusDialog::selectionClicked(QTreeWidgetItem * item, int col)
107 {
108         entryED->setText(item->text(col));
109         selectionChanged();
110         updateLists();
111 }
112
113
114 void GuiThesaurusDialog::updateLists()
115 {
116         meaningsTV->clear();
117         meaningsTV->setUpdatesEnabled(false);
118
119         Thesaurus::Meanings meanings = form_->controller().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 (std::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 /////////////////////////////////////////////////////////////////////
139 //
140 // GuiThesaurus
141 //
142 /////////////////////////////////////////////////////////////////////
143
144 GuiThesaurus::GuiThesaurus(Dialog & parent)
145         : GuiView<GuiThesaurusDialog>(parent, _("Thesaurus"))
146 {
147 }
148
149
150 void GuiThesaurus::build_dialog()
151 {
152         dialog_.reset(new GuiThesaurusDialog(this));
153
154         bcview().setCancel(dialog_->closePB);
155         bcview().setApply(dialog_->replacePB);
156         bcview().addReadOnly(dialog_->replaceED);
157         bcview().addReadOnly(dialog_->replacePB);
158 }
159
160
161 void GuiThesaurus::update_contents()
162 {
163         dialog_->entryED->setText(toqstr(controller().text()));
164         dialog_->replaceED->setText("");
165         dialog_->updateLists();
166 }
167
168
169 void GuiThesaurus::replace()
170 {
171         controller().replace(qstring_to_ucs4(dialog_->replaceED->text()));
172 }
173
174 } // namespace frontend
175 } // namespace lyx
176
177
178 #include "GuiThesaurus_moc.cpp"