3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiIndices.h"
17 #include "ColorCache.h"
18 #include "GuiApplication.h"
19 #include "Validator.h"
20 #include "qt_helpers.h"
22 #include "frontends/alert.h"
24 #include "BufferParams.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
30 #include <QTreeWidget>
31 #include <QTreeWidgetItem>
35 #include <QColorDialog>
39 using namespace lyx::support;
46 GuiIndices::GuiIndices(QWidget * parent)
50 indicesTW->setColumnCount(2);
51 indicesTW->headerItem()->setText(0, qt_("Name"));
52 indicesTW->headerItem()->setText(1, qt_("Label Color"));
53 indicesTW->setSortingEnabled(true);
55 // NOTE: we do not provide "custom" here for security reasons!
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);
65 void GuiIndices::update(BufferParams const & params)
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);
80 split(params.index_command, command, ' ');
82 int const pos = indexCO->findData(toqstr(command));
84 indexCO->setCurrentIndex(pos);
85 indexOptionsED->setText(toqstr(options).trimmed());
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();
92 indexOptionsED->setEnabled(
93 indexCO->currentIndex() != 0);
99 void GuiIndices::updateView()
101 // store the selected index
102 QTreeWidgetItem * item = indicesTW->currentItem();
105 sel_index = item->text(0);
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);
114 QString const iname = toqstr(it->index());
115 newItem->setText(0, iname);
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));
123 // restore selected index
124 if (iname == sel_index) {
125 indicesTW->setCurrentItem(newItem);
126 indicesTW->setItemSelected(newItem, true);
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);
140 void GuiIndices::apply(BufferParams & params) const
142 params.use_indices = multipleIndicesCB->isChecked();
143 params.indiceslist() = indiceslist_;
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;
152 params.index_command = index_command + " " + index_options;
156 void GuiIndices::on_indexCO_activated(int n)
158 indexOptionsED->setEnabled(
159 indexCO->itemData(n).toString() != "default");
164 void GuiIndices::on_indexOptionsED_textChanged(QString)
170 void GuiIndices::on_addIndexPB_pressed()
172 QString const new_index = newIndexLE->text();
173 if (!new_index.isEmpty()) {
174 indiceslist_.add(qstring_to_ucs4(new_index));
181 void GuiIndices::on_removePB_pressed()
183 QTreeWidgetItem * selItem = indicesTW->currentItem();
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."));
194 indiceslist_.remove(qstring_to_ucs4(sel_index));
201 void GuiIndices::on_renamePB_clicked()
203 QTreeWidgetItem * selItem = indicesTW->currentItem();
206 sel_index = selItem->text(0);
207 if (!sel_index.isEmpty()) {
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)
214 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
218 Alert::error(_("Renaming failed"),
219 _("The index could not be renamed. "
220 "Check if the new name already exists."));
226 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
232 void GuiIndices::on_indicesTW_itemSelectionChanged()
234 bool const have_sel =
235 !indicesTW->selectedItems().isEmpty();
236 removePB->setEnabled(have_sel);
237 renamePB->setEnabled(have_sel);
238 colorPB->setEnabled(have_sel);
242 void GuiIndices::on_colorPB_clicked()
244 toggleColor(indicesTW->currentItem());
248 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
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);
265 void GuiIndices::toggleColor(QTreeWidgetItem * item)
270 QString sel_index = item->text(0);
271 if (sel_index.isEmpty())
274 docstring current_index = qstring_to_ucs4(sel_index);
275 Index * index = indiceslist_.find(current_index);
279 QColor const initial = rgb2qcolor(index->color());
280 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
284 // add the color to the indiceslist
285 index->setColor(fromqstr(ncol.name()));
290 } // namespace frontend
293 #include "moc_GuiIndices.cpp"