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);
56 indexCO->addItem(qt_("Default"), QString("default"));
57 for (set<string>::const_iterator it = lyxrc.index_alternatives.begin();
58 it != lyxrc.index_alternatives.end(); ++it) {
59 QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
60 indexCO->addItem(command, command);
64 void GuiIndices::update(BufferParams const & params)
66 indiceslist_ = params.indiceslist();
67 multipleIndicesCB->setChecked(params.use_indices);
68 bool const state = params.use_indices;
69 indicesTW->setEnabled(state);
70 newIndexLE->setEnabled(state);
71 newIndexLA->setEnabled(state);
72 addIndexPB->setEnabled(state);
73 availableLA->setEnabled(state);
74 removePB->setEnabled(state);
75 colorPB->setEnabled(state);
79 split(params.index_command, command, ' ');
81 int const pos = indexCO->findData(toqstr(command));
83 indexCO->setCurrentIndex(pos);
84 indexOptionsED->setText(toqstr(options).trimmed());
86 indexCO->setCurrentIndex(0);
87 indexOptionsED->clear();
89 indexOptionsED->setEnabled(
90 indexCO->currentIndex() != 0);
96 void GuiIndices::updateView()
98 // store the selected index
99 QTreeWidgetItem * item = indicesTW->currentItem();
102 sel_index = item->text(0);
106 IndicesList::const_iterator it = indiceslist_.begin();
107 IndicesList::const_iterator const end = indiceslist_.end();
108 for (; it != end; ++it) {
109 QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
111 QString const iname = toqstr(it->index());
112 newItem->setText(0, iname);
114 QColor const itemcolor = rgb2qcolor(it->color());
115 if (itemcolor.isValid()) {
116 QPixmap coloritem(30, 10);
117 coloritem.fill(itemcolor);
118 newItem->setIcon(1, QIcon(coloritem));
120 // restore selected index
121 if (iname == sel_index) {
122 indicesTW->setCurrentItem(newItem);
123 indicesTW->setItemSelected(newItem, true);
126 indicesTW->resizeColumnToContents(0);
127 bool const have_sel =
128 !indicesTW->selectedItems().isEmpty();
129 removePB->setEnabled(have_sel);
130 renamePB->setEnabled(have_sel);
131 colorPB->setEnabled(have_sel);
137 void GuiIndices::apply(BufferParams & params) const
139 params.use_indices = multipleIndicesCB->isChecked();
140 params.indiceslist() = indiceslist_;
142 string const index_command =
143 fromqstr(indexCO->itemData(
144 indexCO->currentIndex()).toString());
145 string const index_options = fromqstr(indexOptionsED->text());
146 if (index_command == "default" || index_options.empty())
147 params.index_command = index_command;
149 params.index_command = index_command + " " + index_options;
153 void GuiIndices::on_indexCO_activated(int n)
155 indexOptionsED->setEnabled(n != 0);
160 void GuiIndices::on_indexOptionsED_textChanged(QString)
166 void GuiIndices::on_addIndexPB_pressed()
168 QString const new_index = newIndexLE->text();
169 if (!new_index.isEmpty()) {
170 indiceslist_.add(qstring_to_ucs4(new_index));
177 void GuiIndices::on_removePB_pressed()
179 QTreeWidgetItem * selItem = indicesTW->currentItem();
182 sel_index = selItem->text(0);
183 if (!sel_index.isEmpty()) {
184 if (indiceslist_.find(qstring_to_ucs4(sel_index)) ==
185 indiceslist_.findShortcut(from_ascii("idx"))) {
186 Alert::error(_("Cannot remove standard index"),
187 _("The default index cannot be removed."));
190 indiceslist_.remove(qstring_to_ucs4(sel_index));
197 void GuiIndices::on_renamePB_clicked()
199 QTreeWidgetItem * selItem = indicesTW->currentItem();
202 sel_index = selItem->text(0);
203 if (!sel_index.isEmpty()) {
205 docstring const oldname = qstring_to_ucs4(sel_index);
206 bool success = false;
207 if (Alert::askForText(newname, _("Enter new index name"), oldname)) {
208 if (newname.empty() || oldname == newname)
210 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
214 Alert::error(_("Renaming failed"),
215 _("The index could not be renamed. "
216 "Check if the new name already exists."));
222 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
228 void GuiIndices::on_indicesTW_itemSelectionChanged()
230 bool const have_sel =
231 !indicesTW->selectedItems().isEmpty();
232 removePB->setEnabled(have_sel);
233 renamePB->setEnabled(have_sel);
234 colorPB->setEnabled(have_sel);
238 void GuiIndices::on_colorPB_clicked()
240 toggleColor(indicesTW->currentItem());
244 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
246 bool const have_sel =
247 !indicesTW->selectedItems().isEmpty();
248 indicesTW->setEnabled(state);
249 newIndexLE->setEnabled(state);
250 newIndexLA->setEnabled(state);
251 addIndexPB->setEnabled(state);
252 availableLA->setEnabled(state);
253 removePB->setEnabled(state && have_sel);
254 colorPB->setEnabled(state && have_sel);
255 renamePB->setEnabled(state && have_sel);
261 void GuiIndices::toggleColor(QTreeWidgetItem * item)
266 QString sel_index = item->text(0);
267 if (sel_index.isEmpty())
270 docstring current_index = qstring_to_ucs4(sel_index);
271 Index * index = indiceslist_.find(current_index);
275 QColor const initial = rgb2qcolor(index->color());
276 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
280 // add the color to the indiceslist
281 index->setColor(fromqstr(ncol.name()));
286 } // namespace frontend
289 #include "moc_GuiIndices.cpp"