]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
Fix the tab ordering of GuiDocument components.
[lyx.git] / src / frontends / qt4 / GuiIndices.cpp
1 /**
2  * \file GuiIndices.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Jürgen Spitzmüller
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiIndices.h"
16
17 #include "ColorCache.h"
18 #include "GuiApplication.h"
19 #include "Validator.h"
20 #include "qt_helpers.h"
21
22 #include "frontends/alert.h"
23
24 #include "BufferParams.h"
25 #include "LyXRC.h"
26
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 #include <QTreeWidget>
31 #include <QTreeWidgetItem>
32 #include <QPixmap>
33 #include <QIcon>
34 #include <QColor>
35 #include <QColorDialog>
36
37
38 using namespace std;
39 using namespace lyx::support;
40
41
42 namespace lyx {
43 namespace frontend {
44
45
46 GuiIndices::GuiIndices(QWidget * parent)
47         : QWidget(parent)
48 {
49         setupUi(this);
50         indicesTW->setColumnCount(2);
51         indicesTW->headerItem()->setText(0, qt_("Name"));
52         indicesTW->headerItem()->setText(1, qt_("Label Color"));
53         indicesTW->setSortingEnabled(true);
54
55         // NOTE: we do not provide "custom" here for security reasons!
56         indexCO->clear();
57         indexCO->addItem(qt_("Default"), QString("default"));
58         for (set<string>::const_iterator it = lyxrc.index_alternatives.begin();
59                              it != lyxrc.index_alternatives.end(); ++it) {
60                 QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
61                 indexCO->addItem(command, command);
62         }
63
64         indexOptionsLE->setValidator(new NoNewLineValidator(indexOptionsLE));
65         newIndexLE->setValidator(new NoNewLineValidator(newIndexLE));
66 }
67
68 void GuiIndices::update(BufferParams const & params)
69 {
70         indiceslist_ = params.indiceslist();
71         multipleIndicesCB->setChecked(params.use_indices);
72         bool const state = params.use_indices;
73         indicesTW->setEnabled(state);
74         newIndexLE->setEnabled(state);
75         newIndexLA->setEnabled(state);
76         addIndexPB->setEnabled(state);
77         availableLA->setEnabled(state);
78         removePB->setEnabled(state);
79         colorPB->setEnabled(state);
80
81         string command;
82         string options =
83                 split(params.index_command, command, ' ');
84
85         int const pos = indexCO->findData(toqstr(command));
86         if (pos != -1) {
87                 indexCO->setCurrentIndex(pos);
88                 indexOptionsLE->setText(toqstr(options).trimmed());
89         } else {
90                 // We reset to default if we do not know the specified compiler
91                 // This is for security reasons
92                 indexCO->setCurrentIndex(indexCO->findData(toqstr("default")));
93                 indexOptionsLE->clear();
94         }
95         indexOptionsLE->setEnabled(
96                 indexCO->currentIndex() != 0);
97
98         updateView();
99 }
100
101
102 void GuiIndices::updateView()
103 {
104         // store the selected index
105         QTreeWidgetItem * item = indicesTW->currentItem();
106         QString sel_index;
107         if (item != 0)
108                 sel_index = item->text(0);
109
110         indicesTW->clear();
111
112         IndicesList::const_iterator it = indiceslist_.begin();
113         IndicesList::const_iterator const end = indiceslist_.end();
114         for (; it != end; ++it) {
115                 QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
116
117                 QString const iname = toqstr(it->index());
118                 newItem->setText(0, iname);
119
120                 QColor const itemcolor = rgb2qcolor(it->color());
121                 if (itemcolor.isValid()) {
122                         QPixmap coloritem(30, 10);
123                         coloritem.fill(itemcolor);
124                         newItem->setIcon(1, QIcon(coloritem));
125                 }
126                 // restore selected index
127                 if (iname == sel_index) {
128                         indicesTW->setCurrentItem(newItem);
129                         indicesTW->setItemSelected(newItem, true);
130                 }
131         }
132         indicesTW->resizeColumnToContents(0);
133         bool const have_sel =
134                 !indicesTW->selectedItems().isEmpty();
135         removePB->setEnabled(have_sel);
136         renamePB->setEnabled(have_sel);
137         colorPB->setEnabled(have_sel);
138         // emit signal
139         changed();
140 }
141
142
143 void GuiIndices::apply(BufferParams & params) const
144 {
145         params.use_indices = multipleIndicesCB->isChecked();
146         params.indiceslist() = indiceslist_;
147
148         string const index_command =
149                 fromqstr(indexCO->itemData(
150                         indexCO->currentIndex()).toString());
151         string const index_options = fromqstr(indexOptionsLE->text());
152         if (index_command == "default" || index_options.empty())
153                 params.index_command = index_command;
154         else
155                 params.index_command = index_command + " " + index_options;
156 }
157
158
159 void GuiIndices::on_indexCO_activated(int n)
160 {
161         indexOptionsLE->setEnabled(
162                 indexCO->itemData(n).toString() != "default");
163         changed();
164 }
165
166
167 void GuiIndices::on_indexOptionsLE_textChanged(QString)
168 {
169         changed();
170 }
171
172
173 void GuiIndices::on_addIndexPB_pressed()
174 {
175         QString const new_index = newIndexLE->text();
176         if (!new_index.isEmpty()) {
177                 indiceslist_.add(qstring_to_ucs4(new_index));
178                 newIndexLE->clear();
179                 updateView();
180         }
181 }
182
183
184 void GuiIndices::on_removePB_pressed()
185 {
186         QTreeWidgetItem * selItem = indicesTW->currentItem();
187         QString sel_index;
188         if (selItem != 0)
189                 sel_index = selItem->text(0);
190         if (!sel_index.isEmpty()) {
191                 if (indiceslist_.find(qstring_to_ucs4(sel_index)) == 
192                     indiceslist_.findShortcut(from_ascii("idx"))) {
193                         Alert::error(_("Cannot remove standard index"), 
194                               _("The default index cannot be removed."));
195                               return;
196                 }
197                 indiceslist_.remove(qstring_to_ucs4(sel_index));
198                 newIndexLE->clear();
199                 updateView();
200         }
201 }
202
203
204 void GuiIndices::on_renamePB_clicked()
205 {
206         QTreeWidgetItem * selItem = indicesTW->currentItem();
207         QString sel_index;
208         if (selItem != 0)
209                 sel_index = selItem->text(0);
210         if (!sel_index.isEmpty()) {
211                 docstring newname;
212                 docstring const oldname = qstring_to_ucs4(sel_index);
213                 bool success = false;
214                 if (Alert::askForText(newname, _("Enter new index name"), oldname)) {
215                         if (newname.empty() || oldname == newname)
216                                 return;
217                         success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
218                         newIndexLE->clear();
219                         updateView();
220                         if (!success)
221                                 Alert::error(_("Renaming failed"), 
222                                       _("The index could not be renamed. "
223                                         "Check if the new name already exists."));
224                 }
225         }
226 }
227
228
229 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
230 {
231         toggleColor(item);
232 }
233
234
235 void GuiIndices::on_indicesTW_itemSelectionChanged()
236 {
237         bool const have_sel =
238                 !indicesTW->selectedItems().isEmpty();
239         removePB->setEnabled(have_sel);
240         renamePB->setEnabled(have_sel);
241         colorPB->setEnabled(have_sel);
242 }
243
244
245 void GuiIndices::on_colorPB_clicked()
246 {
247         toggleColor(indicesTW->currentItem());
248 }
249
250
251 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
252 {
253         bool const have_sel =
254                 !indicesTW->selectedItems().isEmpty();
255         indicesTW->setEnabled(state);
256         newIndexLE->setEnabled(state);
257         newIndexLA->setEnabled(state);
258         addIndexPB->setEnabled(state);
259         availableLA->setEnabled(state);
260         removePB->setEnabled(state && have_sel);
261         colorPB->setEnabled(state && have_sel);
262         renamePB->setEnabled(state && have_sel);
263         // emit signal
264         changed();
265 }
266
267
268 void GuiIndices::toggleColor(QTreeWidgetItem * item)
269 {
270         if (item == 0)
271                 return;
272
273         QString sel_index = item->text(0);
274         if (sel_index.isEmpty())
275                 return;
276
277         docstring current_index = qstring_to_ucs4(sel_index);
278         Index * index = indiceslist_.find(current_index);
279         if (!index)
280                 return;
281
282         QColor const initial = rgb2qcolor(index->color());
283         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
284         if (!ncol.isValid())
285                 return;
286
287         // add the color to the indiceslist
288         index->setColor(fromqstr(ncol.name()));
289         newIndexLE->clear();
290         updateView();
291 }
292
293 } // namespace frontend
294 } // namespace lyx
295
296 #include "moc_GuiIndices.cpp"