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