]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QThesaurus.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / QThesaurus.cpp
1 /**
2  * \file QThesaurus.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 "QThesaurus.h"
14 #include "Qt2BC.h"
15 #include "qt_helpers.h"
16 #include "debug.h"
17
18 #include "controllers/ControlThesaurus.h"
19
20 #include <QHeaderView>
21 #include <QLineEdit>
22 #include <QPushButton>
23 #include <QTreeWidget>
24 #include <QTreeWidgetItem>
25
26 using std::string;
27
28 namespace lyx {
29 namespace frontend {
30
31 /////////////////////////////////////////////////////////////////////
32 //
33 // QTheasurusDialog
34 //
35 /////////////////////////////////////////////////////////////////////
36
37 QThesaurusDialog::QThesaurusDialog(QThesaurus * form)
38         : form_(form)
39 {
40         setupUi(this);
41
42         meaningsTV->setColumnCount(1);
43         meaningsTV->header()->hide();
44
45         connect(closePB, SIGNAL(clicked()),
46                 form, SLOT(slotClose()));
47         connect(replaceED, SIGNAL(returnPressed()),
48                 this, SLOT(replaceClicked()));
49         connect(replaceED, SIGNAL(textChanged(const QString &)),
50                 this, SLOT(change_adaptor() ) );
51         connect(entryED, SIGNAL(returnPressed()),
52                 this, SLOT(entryChanged()));
53         connect(replacePB, SIGNAL(clicked()),
54                 this, SLOT(replaceClicked()));
55         connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
56                 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
57         connect(meaningsTV, SIGNAL(itemSelectionChanged()),
58                 this, SLOT(selectionChanged()));
59         connect(meaningsTV, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
60                 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
61 }
62
63
64 void QThesaurusDialog::change_adaptor()
65 {
66         form_->changed();
67 }
68
69
70 void QThesaurusDialog::closeEvent(QCloseEvent * e)
71 {
72         form_->slotWMHide();
73         e->accept();
74 }
75
76
77 void QThesaurusDialog::entryChanged()
78 {
79         updateLists();
80 }
81
82
83 void QThesaurusDialog::replaceClicked()
84 {
85         form_->replace();
86 }
87
88
89 void QThesaurusDialog::selectionChanged()
90 {
91         int const col = meaningsTV->currentColumn();
92         if (col<0 || form_->readOnly())
93                 return;
94
95         replaceED->setText(meaningsTV->currentItem()->text(col));
96         replacePB->setEnabled(true);
97         form_->changed();
98 }
99
100
101 void QThesaurusDialog::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
102 {
103         selectionChanged();
104 }
105
106
107 void QThesaurusDialog::selectionClicked(QTreeWidgetItem * item, int col)
108 {
109         entryED->setText(item->text(col));
110         selectionChanged();
111         updateLists();
112 }
113
114
115 void QThesaurusDialog::updateLists()
116 {
117         meaningsTV->clear();
118         meaningsTV->setUpdatesEnabled(false);
119
120         Thesaurus::Meanings meanings = form_->controller().getMeanings(qstring_to_ucs4(entryED->text()));
121
122         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
123                 cit != meanings.end(); ++cit) {
124                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
125                 i->setText(0, toqstr(cit->first));
126                 meaningsTV->expandItem(i);
127                 for (std::vector<docstring>::const_iterator cit2 = cit->second.begin();
128                         cit2 != cit->second.end(); ++cit2) {
129                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
130                                 i2->setText(0, toqstr(*cit2));
131                         }
132         }
133
134         meaningsTV->setUpdatesEnabled(true);
135         meaningsTV->update();
136 }
137
138
139 /////////////////////////////////////////////////////////////////////
140 //
141 // QThesuarus
142 //
143 /////////////////////////////////////////////////////////////////////
144
145 typedef QController<ControlThesaurus, QView<QThesaurusDialog> > ThesaurusBase;
146
147 QThesaurus::QThesaurus(Dialog & parent)
148         : ThesaurusBase(parent, _("Thesaurus"))
149 {
150 }
151
152
153 void QThesaurus::build_dialog()
154 {
155         dialog_.reset(new QThesaurusDialog(this));
156
157         bcview().setCancel(dialog_->closePB);
158         bcview().setApply(dialog_->replacePB);
159         bcview().addReadOnly(dialog_->replaceED);
160         bcview().addReadOnly(dialog_->replacePB);
161 }
162
163
164 void QThesaurus::update_contents()
165 {
166         dialog_->entryED->setText(toqstr(controller().text()));
167         dialog_->replaceED->setText("");
168         dialog_->updateLists();
169 }
170
171
172 void QThesaurus::replace()
173 {
174         controller().replace(qstring_to_ucs4(dialog_->replaceED->text()));
175 }
176
177 } // namespace frontend
178 } // namespace lyx
179
180
181 #include "QThesaurus_moc.cpp"