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);
62 indexCO->addItem(qt_("Custom"), QString("custom"));
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());
86 indexOptionsLA->setText(qt_("&Options:"));
88 indexCO->setCurrentIndex(indexCO->findData(toqstr("custom")));
89 indexOptionsED->setText(toqstr(params.index_command));
90 indexOptionsLA->setText(qt_("Co&mmand:"));
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 == "custom")
150 params.index_command = index_options;
151 else if (index_command == "default" || index_options.empty())
152 params.index_command = index_command;
154 params.index_command = index_command + " " + index_options;
158 void GuiIndices::on_indexCO_activated(int n)
160 QString const data = indexCO->itemData(n).toString();
161 indexOptionsED->setEnabled(data != "default");
162 if (data == "custom")
163 indexOptionsLA->setText(qt_("Co&mmand:"));
165 indexOptionsLA->setText(qt_("&Options:"));
170 void GuiIndices::on_indexOptionsED_textChanged(QString)
176 void GuiIndices::on_addIndexPB_pressed()
178 QString const new_index = newIndexLE->text();
179 if (!new_index.isEmpty()) {
180 indiceslist_.add(qstring_to_ucs4(new_index));
187 void GuiIndices::on_removePB_pressed()
189 QTreeWidgetItem * selItem = indicesTW->currentItem();
192 sel_index = selItem->text(0);
193 if (!sel_index.isEmpty()) {
194 if (indiceslist_.find(qstring_to_ucs4(sel_index)) ==
195 indiceslist_.findShortcut(from_ascii("idx"))) {
196 Alert::error(_("Cannot remove standard index"),
197 _("The default index cannot be removed."));
200 indiceslist_.remove(qstring_to_ucs4(sel_index));
207 void GuiIndices::on_renamePB_clicked()
209 QTreeWidgetItem * selItem = indicesTW->currentItem();
212 sel_index = selItem->text(0);
213 if (!sel_index.isEmpty()) {
215 docstring const oldname = qstring_to_ucs4(sel_index);
216 bool success = false;
217 if (Alert::askForText(newname, _("Enter new index name"), oldname)) {
218 if (newname.empty() || oldname == newname)
220 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
224 Alert::error(_("Renaming failed"),
225 _("The index could not be renamed. "
226 "Check if the new name already exists."));
232 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
238 void GuiIndices::on_indicesTW_itemSelectionChanged()
240 bool const have_sel =
241 !indicesTW->selectedItems().isEmpty();
242 removePB->setEnabled(have_sel);
243 renamePB->setEnabled(have_sel);
244 colorPB->setEnabled(have_sel);
248 void GuiIndices::on_colorPB_clicked()
250 toggleColor(indicesTW->currentItem());
254 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
256 bool const have_sel =
257 !indicesTW->selectedItems().isEmpty();
258 indicesTW->setEnabled(state);
259 newIndexLE->setEnabled(state);
260 newIndexLA->setEnabled(state);
261 addIndexPB->setEnabled(state);
262 availableLA->setEnabled(state);
263 removePB->setEnabled(state && have_sel);
264 colorPB->setEnabled(state && have_sel);
265 renamePB->setEnabled(state && have_sel);
271 void GuiIndices::toggleColor(QTreeWidgetItem * item)
276 QString sel_index = item->text(0);
277 if (sel_index.isEmpty())
280 docstring current_index = qstring_to_ucs4(sel_index);
281 Index * index = indiceslist_.find(current_index);
285 QColor const initial = rgb2qcolor(index->color());
286 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
290 // add the color to the indiceslist
291 index->setColor(fromqstr(ncol.name()));
296 } // namespace frontend
299 #include "moc_GuiIndices.cpp"