]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiThesaurus.cpp
cosmetics
[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 "ControlThesaurus.h"
16 #include "qt_helpers.h"
17 #include "debug.h"
18
19 #include <QCloseEvent>
20 #include <QHeaderView>
21 #include <QLineEdit>
22 #include <QPushButton>
23 #include <QTreeWidget>
24 #include <QTreeWidgetItem>
25
26 using std::string;
27
28
29 namespace lyx {
30 namespace frontend {
31
32 GuiThesaurusDialog::GuiThesaurusDialog(LyXView & lv)
33         : GuiDialog(lv, "thesaurus")
34 {
35         setupUi(this);
36         setViewTitle(_("Thesaurus"));
37         setController(new ControlThesaurus(*this));
38
39         meaningsTV->setColumnCount(1);
40         meaningsTV->header()->hide();
41
42         connect(closePB, SIGNAL(clicked()),
43                 this, SLOT(slotClose()));
44         connect(replaceED, SIGNAL(returnPressed()),
45                 this, SLOT(replaceClicked()));
46         connect(replaceED, SIGNAL(textChanged(const QString &)),
47                 this, SLOT(change_adaptor() ) );
48         connect(entryED, SIGNAL(returnPressed()),
49                 this, SLOT(entryChanged()));
50         connect(replacePB, SIGNAL(clicked()),
51                 this, SLOT(replaceClicked()));
52         connect(meaningsTV, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
53                 this, SLOT(itemClicked(QTreeWidgetItem *, int)));
54         connect(meaningsTV, SIGNAL(itemSelectionChanged()),
55                 this, SLOT(selectionChanged()));
56         connect(meaningsTV, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
57                 this, SLOT(selectionClicked(QTreeWidgetItem *, int)));
58
59         bc().setCancel(closePB);
60         bc().setApply(replacePB);
61         bc().addReadOnly(replaceED);
62         bc().addReadOnly(replacePB);
63         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
64 }
65
66
67 ControlThesaurus & GuiThesaurusDialog::controller()
68 {
69         return static_cast<ControlThesaurus &>(GuiDialog::controller());
70 }
71
72 void GuiThesaurusDialog::change_adaptor()
73 {
74         changed();
75 }
76
77
78 void GuiThesaurusDialog::closeEvent(QCloseEvent * e)
79 {
80         slotClose();
81         e->accept();
82 }
83
84
85 void GuiThesaurusDialog::entryChanged()
86 {
87         updateLists();
88 }
89
90
91 void GuiThesaurusDialog::selectionChanged()
92 {
93         int const col = meaningsTV->currentColumn();
94         if (col < 0 || controller().isBufferReadonly())
95                 return;
96
97         replaceED->setText(meaningsTV->currentItem()->text(col));
98         replacePB->setEnabled(true);
99         changed();
100 }
101
102
103 void GuiThesaurusDialog::itemClicked(QTreeWidgetItem * /*item*/, int /*col*/)
104 {
105         selectionChanged();
106 }
107
108
109 void GuiThesaurusDialog::selectionClicked(QTreeWidgetItem * item, int col)
110 {
111         entryED->setText(item->text(col));
112         selectionChanged();
113         updateLists();
114 }
115
116
117 void GuiThesaurusDialog::updateLists()
118 {
119         meaningsTV->clear();
120         meaningsTV->setUpdatesEnabled(false);
121
122         Thesaurus::Meanings meanings = controller().getMeanings(qstring_to_ucs4(entryED->text()));
123
124         for (Thesaurus::Meanings::const_iterator cit = meanings.begin();
125                 cit != meanings.end(); ++cit) {
126                 QTreeWidgetItem * i = new QTreeWidgetItem(meaningsTV);
127                 i->setText(0, toqstr(cit->first));
128                 meaningsTV->expandItem(i);
129                 for (std::vector<docstring>::const_iterator cit2 = cit->second.begin();
130                         cit2 != cit->second.end(); ++cit2) {
131                                 QTreeWidgetItem * i2 = new QTreeWidgetItem(i);
132                                 i2->setText(0, toqstr(*cit2));
133                         }
134         }
135
136         meaningsTV->setUpdatesEnabled(true);
137         meaningsTV->update();
138 }
139
140
141 void GuiThesaurusDialog::updateContents()
142 {
143         entryED->setText(toqstr(controller().text()));
144         replaceED->setText("");
145         updateLists();
146 }
147
148
149 void GuiThesaurusDialog::replaceClicked()
150 {
151         controller().replace(qstring_to_ucs4(replaceED->text()));
152 }
153
154 } // namespace frontend
155 } // namespace lyx
156
157
158 #include "GuiThesaurus_moc.cpp"