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);
64 indexOptionsLE->setValidator(new NoNewLineValidator(indexOptionsLE));
65 newIndexLE->setValidator(new NoNewLineValidator(newIndexLE));
68 void GuiIndices::update(BufferParams const & params)
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);
83 split(params.index_command, command, ' ');
85 int const pos = indexCO->findData(toqstr(command));
87 indexCO->setCurrentIndex(pos);
88 indexOptionsLE->setText(toqstr(options).trimmed());
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();
95 indexOptionsLE->setEnabled(
96 indexCO->currentIndex() != 0);
102 void GuiIndices::updateView()
104 // store the selected index
105 QTreeWidgetItem * item = indicesTW->currentItem();
108 sel_index = item->text(0);
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);
117 QString const iname = toqstr(it->index());
118 newItem->setText(0, iname);
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));
126 // restore selected index
127 if (iname == sel_index) {
128 indicesTW->setCurrentItem(newItem);
129 indicesTW->setItemSelected(newItem, true);
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);
143 void GuiIndices::apply(BufferParams & params) const
145 params.use_indices = multipleIndicesCB->isChecked();
146 params.indiceslist() = indiceslist_;
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;
155 params.index_command = index_command + " " + index_options;
159 void GuiIndices::on_indexCO_activated(int n)
161 indexOptionsLE->setEnabled(
162 indexCO->itemData(n).toString() != "default");
167 void GuiIndices::on_indexOptionsLE_textChanged(QString)
173 void GuiIndices::on_addIndexPB_pressed()
175 QString const new_index = newIndexLE->text();
176 if (!new_index.isEmpty()) {
177 indiceslist_.add(qstring_to_ucs4(new_index));
184 void GuiIndices::on_removePB_pressed()
186 QTreeWidgetItem * selItem = indicesTW->currentItem();
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."));
197 indiceslist_.remove(qstring_to_ucs4(sel_index));
204 void GuiIndices::on_renamePB_clicked()
206 QTreeWidgetItem * selItem = indicesTW->currentItem();
209 sel_index = selItem->text(0);
210 if (!sel_index.isEmpty()) {
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)
217 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
221 Alert::error(_("Renaming failed"),
222 _("The index could not be renamed. "
223 "Check if the new name already exists."));
229 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
235 void GuiIndices::on_indicesTW_itemSelectionChanged()
237 bool const have_sel =
238 !indicesTW->selectedItems().isEmpty();
239 removePB->setEnabled(have_sel);
240 renamePB->setEnabled(have_sel);
241 colorPB->setEnabled(have_sel);
245 void GuiIndices::on_colorPB_clicked()
247 toggleColor(indicesTW->currentItem());
251 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
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);
268 void GuiIndices::toggleColor(QTreeWidgetItem * item)
273 QString sel_index = item->text(0);
274 if (sel_index.isEmpty())
277 docstring current_index = qstring_to_ucs4(sel_index);
278 Index * index = indiceslist_.find(current_index);
282 QColor const initial = rgb2qcolor(index->color());
283 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
287 // add the color to the indiceslist
288 index->setColor(fromqstr(ncol.name()));
293 } // namespace frontend
296 #include "moc_GuiIndices.cpp"