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)
47 : QWidget(parent), readonly_(false),
51 indicesTW->setColumnCount(2);
52 indicesTW->headerItem()->setText(0, qt_("Name"));
53 indicesTW->headerItem()->setText(1, qt_("Label Color"));
54 indicesTW->setSortingEnabled(true);
56 // NOTE: we do not provide "custom" here for security reasons!
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);
65 indexOptionsLE->setValidator(new NoNewLineValidator(indexOptionsLE));
66 newIndexLE->setValidator(new NoNewLineValidator(newIndexLE));
70 void GuiIndices::updateWidgets()
73 !indicesTW->selectedItems().isEmpty();
75 indexCO->setEnabled(!readonly_);
76 indexOptionsLE->setEnabled(
77 indexCO->itemData(indexCO->currentIndex()).toString() != "default");
78 indexOptionsLE->setReadOnly(readonly_);
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_);
94 void GuiIndices::update(BufferParams const & params, bool const readonly)
96 use_indices_ = params.use_indices;
98 indiceslist_ = params.indiceslist();
99 multipleIndicesCB->setChecked(params.use_indices);
105 split(params.index_command, command, ' ');
107 int const pos = indexCO->findData(toqstr(command));
109 indexCO->setCurrentIndex(pos);
110 indexOptionsLE->setText(toqstr(options).trimmed());
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();
122 void GuiIndices::updateView()
124 // store the selected index
125 QTreeWidgetItem * item = indicesTW->currentItem();
128 sel_index = item->text(0);
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);
137 QString const iname = toqstr(it->index());
138 newItem->setText(0, iname);
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));
146 // restore selected index
147 if (iname == sel_index) {
148 indicesTW->setCurrentItem(newItem);
149 newItem->setSelected(true);
152 indicesTW->resizeColumnToContents(0);
160 void GuiIndices::apply(BufferParams & params) const
162 params.use_indices = multipleIndicesCB->isChecked();
163 params.indiceslist() = indiceslist_;
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;
172 params.index_command = index_command + " " + index_options;
176 void GuiIndices::on_indexCO_activated(int)
183 void GuiIndices::on_newIndexLE_textChanged(QString)
190 void GuiIndices::on_indexOptionsLE_textChanged(QString)
196 void GuiIndices::on_addIndexPB_pressed()
198 QString const new_index = newIndexLE->text();
199 if (!new_index.isEmpty()) {
200 indiceslist_.add(qstring_to_ucs4(new_index));
207 void GuiIndices::on_removePB_pressed()
209 QTreeWidgetItem * selItem = indicesTW->currentItem();
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."));
220 indiceslist_.remove(qstring_to_ucs4(sel_index));
227 void GuiIndices::on_renamePB_clicked()
229 QTreeWidgetItem * selItem = indicesTW->currentItem();
232 sel_index = selItem->text(0);
233 if (!sel_index.isEmpty()) {
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)
239 bool success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
243 Alert::error(_("Renaming failed"),
244 _("The index could not be renamed. "
245 "Check if the new name already exists."));
251 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
257 void GuiIndices::on_indicesTW_itemSelectionChanged()
263 void GuiIndices::on_colorPB_clicked()
265 toggleColor(indicesTW->currentItem());
269 void GuiIndices::on_multipleIndicesCB_toggled(bool const b)
278 void GuiIndices::toggleColor(QTreeWidgetItem * item)
283 QString sel_index = item->text(0);
284 if (sel_index.isEmpty())
287 docstring current_index = qstring_to_ucs4(sel_index);
288 Index * index = indiceslist_.find(current_index);
292 QColor const initial = rgb2qcolor(index->color());
293 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
297 // add the color to the indiceslist
298 index->setColor(fromqstr(ncol.name()));
303 } // namespace frontend
306 #include "moc_GuiIndices.cpp"