]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
Don't allow newline characters in preference (#5840).
[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
65 void GuiIndices::update(BufferParams const & params)
66 {
67         indiceslist_ = params.indiceslist();
68         multipleIndicesCB->setChecked(params.use_indices);
69         bool const state = params.use_indices;
70         indicesTW->setEnabled(state);
71         newIndexLE->setEnabled(state);
72         newIndexLA->setEnabled(state);
73         addIndexPB->setEnabled(state);
74         availableLA->setEnabled(state);
75         removePB->setEnabled(state);
76         colorPB->setEnabled(state);
77
78         string command;
79         string options =
80                 split(params.index_command, command, ' ');
81
82         int const pos = indexCO->findData(toqstr(command));
83         if (pos != -1) {
84                 indexCO->setCurrentIndex(pos);
85                 indexOptionsED->setText(toqstr(options).trimmed());
86         } else {
87                 // We reset to default if we do not know the specified compiler
88                 // This is for security reasons
89                 indexCO->setCurrentIndex(indexCO->findData(toqstr("default")));
90                 indexOptionsED->clear();
91         }
92         indexOptionsED->setEnabled(
93                 indexCO->currentIndex() != 0);
94
95         updateView();
96 }
97
98
99 void GuiIndices::updateView()
100 {
101         // store the selected index
102         QTreeWidgetItem * item = indicesTW->currentItem();
103         QString sel_index;
104         if (item != 0)
105                 sel_index = item->text(0);
106
107         indicesTW->clear();
108
109         IndicesList::const_iterator it = indiceslist_.begin();
110         IndicesList::const_iterator const end = indiceslist_.end();
111         for (; it != end; ++it) {
112                 QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
113
114                 QString const iname = toqstr(it->index());
115                 newItem->setText(0, iname);
116
117                 QColor const itemcolor = rgb2qcolor(it->color());
118                 if (itemcolor.isValid()) {
119                         QPixmap coloritem(30, 10);
120                         coloritem.fill(itemcolor);
121                         newItem->setIcon(1, QIcon(coloritem));
122                 }
123                 // restore selected index
124                 if (iname == sel_index) {
125                         indicesTW->setCurrentItem(newItem);
126                         indicesTW->setItemSelected(newItem, true);
127                 }
128         }
129         indicesTW->resizeColumnToContents(0);
130         bool const have_sel =
131                 !indicesTW->selectedItems().isEmpty();
132         removePB->setEnabled(have_sel);
133         renamePB->setEnabled(have_sel);
134         colorPB->setEnabled(have_sel);
135         // emit signal
136         changed();
137 }
138
139
140 void GuiIndices::apply(BufferParams & params) const
141 {
142         params.use_indices = multipleIndicesCB->isChecked();
143         params.indiceslist() = indiceslist_;
144
145         string const index_command =
146                 fromqstr(indexCO->itemData(
147                         indexCO->currentIndex()).toString());
148         string const index_options = fromqstr(indexOptionsED->text());
149         if (index_command == "default" || index_options.empty())
150                 params.index_command = index_command;
151         else
152                 params.index_command = index_command + " " + index_options;
153 }
154
155
156 void GuiIndices::on_indexCO_activated(int n)
157 {
158         indexOptionsED->setEnabled(
159                 indexCO->itemData(n).toString() != "default");
160         changed();
161 }
162
163
164 void GuiIndices::on_indexOptionsED_textChanged(QString)
165 {
166         changed();
167 }
168
169
170 void GuiIndices::on_addIndexPB_pressed()
171 {
172         QString const new_index = newIndexLE->text();
173         if (!new_index.isEmpty()) {
174                 indiceslist_.add(qstring_to_ucs4(new_index));
175                 newIndexLE->clear();
176                 updateView();
177         }
178 }
179
180
181 void GuiIndices::on_removePB_pressed()
182 {
183         QTreeWidgetItem * selItem = indicesTW->currentItem();
184         QString sel_index;
185         if (selItem != 0)
186                 sel_index = selItem->text(0);
187         if (!sel_index.isEmpty()) {
188                 if (indiceslist_.find(qstring_to_ucs4(sel_index)) == 
189                     indiceslist_.findShortcut(from_ascii("idx"))) {
190                         Alert::error(_("Cannot remove standard index"), 
191                               _("The default index cannot be removed."));
192                               return;
193                 }
194                 indiceslist_.remove(qstring_to_ucs4(sel_index));
195                 newIndexLE->clear();
196                 updateView();
197         }
198 }
199
200
201 void GuiIndices::on_renamePB_clicked()
202 {
203         QTreeWidgetItem * selItem = indicesTW->currentItem();
204         QString sel_index;
205         if (selItem != 0)
206                 sel_index = selItem->text(0);
207         if (!sel_index.isEmpty()) {
208                 docstring newname;
209                 docstring const oldname = qstring_to_ucs4(sel_index);
210                 bool success = false;
211                 if (Alert::askForText(newname, _("Enter new index name"), oldname)) {
212                         if (newname.empty() || oldname == newname)
213                                 return;
214                         success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
215                         newIndexLE->clear();
216                         updateView();
217                         if (!success)
218                                 Alert::error(_("Renaming failed"), 
219                                       _("The index could not be renamed. "
220                                         "Check if the new name already exists."));
221                 }
222         }
223 }
224
225
226 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
227 {
228         toggleColor(item);
229 }
230
231
232 void GuiIndices::on_indicesTW_itemSelectionChanged()
233 {
234         bool const have_sel =
235                 !indicesTW->selectedItems().isEmpty();
236         removePB->setEnabled(have_sel);
237         renamePB->setEnabled(have_sel);
238         colorPB->setEnabled(have_sel);
239 }
240
241
242 void GuiIndices::on_colorPB_clicked()
243 {
244         toggleColor(indicesTW->currentItem());
245 }
246
247
248 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
249 {
250         bool const have_sel =
251                 !indicesTW->selectedItems().isEmpty();
252         indicesTW->setEnabled(state);
253         newIndexLE->setEnabled(state);
254         newIndexLA->setEnabled(state);
255         addIndexPB->setEnabled(state);
256         availableLA->setEnabled(state);
257         removePB->setEnabled(state && have_sel);
258         colorPB->setEnabled(state && have_sel);
259         renamePB->setEnabled(state && have_sel);
260         // emit signal
261         changed();
262 }
263
264
265 void GuiIndices::toggleColor(QTreeWidgetItem * item)
266 {
267         if (item == 0)
268                 return;
269
270         QString sel_index = item->text(0);
271         if (sel_index.isEmpty())
272                 return;
273
274         docstring current_index = qstring_to_ucs4(sel_index);
275         Index * index = indiceslist_.find(current_index);
276         if (!index)
277                 return;
278
279         QColor const initial = rgb2qcolor(index->color());
280         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
281         if (!ncol.isValid())
282                 return;
283
284         // add the color to the indiceslist
285         index->setColor(fromqstr(ncol.name()));
286         newIndexLE->clear();
287         updateView();
288 }
289
290 } // namespace frontend
291 } // namespace lyx
292
293 #include "moc_GuiIndices.cpp"