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"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
29 #include <QTreeWidget>
30 #include <QTreeWidgetItem>
34 #include <QColorDialog>
41 GuiIndices::GuiIndices(QWidget * parent)
45 indicesTW->setColumnCount(2);
46 indicesTW->headerItem()->setText(0, qt_("Name"));
47 indicesTW->headerItem()->setText(1, qt_("Label Color"));
48 indicesTW->setSortingEnabled(true);
51 void GuiIndices::update(BufferParams const & params)
53 indiceslist_ = params.indiceslist();
54 multipleIndicesCB->setChecked(
56 bool const state = params.use_indices;
57 indicesTW->setEnabled(state);
58 newIndexLE->setEnabled(state);
59 newIndexLA->setEnabled(state);
60 addIndexPB->setEnabled(state);
61 availableLA->setEnabled(state);
62 removePB->setEnabled(state);
63 colorPB->setEnabled(state);
68 void GuiIndices::updateView()
70 // store the selected index
71 QTreeWidgetItem * item = indicesTW->currentItem();
74 sel_index = item->text(0);
78 IndicesList::const_iterator it = indiceslist_.begin();
79 IndicesList::const_iterator const end = indiceslist_.end();
80 for (; it != end; ++it) {
81 QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
83 QString const iname = toqstr(it->index());
84 newItem->setText(0, iname);
86 QColor const itemcolor = rgb2qcolor(it->color());
87 if (itemcolor.isValid()) {
88 QPixmap coloritem(30, 10);
89 coloritem.fill(itemcolor);
90 newItem->setIcon(1, QIcon(coloritem));
92 // restore selected index
93 if (iname == sel_index) {
94 indicesTW->setCurrentItem(newItem);
95 indicesTW->setItemSelected(newItem, true);
103 void GuiIndices::apply(BufferParams & params) const
105 params.use_indices = multipleIndicesCB->isChecked();
106 params.indiceslist() = indiceslist_;
110 void GuiIndices::on_addIndexPB_pressed()
112 QString const new_index = newIndexLE->text();
113 if (!new_index.isEmpty()) {
114 indiceslist_.add(qstring_to_ucs4(new_index));
121 void GuiIndices::on_removePB_pressed()
123 QTreeWidgetItem * selItem = indicesTW->currentItem();
126 sel_index = selItem->text(0);
127 if (!sel_index.isEmpty()) {
128 if (indiceslist_.find(qstring_to_ucs4(sel_index)) ==
129 indiceslist_.findShortcut(from_ascii("idx"))) {
130 Alert::error(_("Cannot remove standard index"),
131 _("The default index cannot be removed."));
134 indiceslist_.remove(qstring_to_ucs4(sel_index));
141 void GuiIndices::on_renamePB_clicked()
143 QTreeWidgetItem * selItem = indicesTW->currentItem();
146 sel_index = selItem->text(0);
147 if (!sel_index.isEmpty()) {
149 bool success = false;
150 if (Alert::askForText(newname, _("Enter new index name"))) {
151 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
156 Alert::error(_("Renaming failed"),
157 _("The index could not be renamed. "
158 "Check if the new name already exists."));
163 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
169 void GuiIndices::on_colorPB_clicked()
171 toggleColor(indicesTW->currentItem());
175 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
177 indicesTW->setEnabled(state);
178 newIndexLE->setEnabled(state);
179 newIndexLA->setEnabled(state);
180 addIndexPB->setEnabled(state);
181 availableLA->setEnabled(state);
182 removePB->setEnabled(state);
183 colorPB->setEnabled(state);
189 void GuiIndices::toggleColor(QTreeWidgetItem * item)
194 QString sel_index = item->text(0);
195 if (sel_index.isEmpty())
198 docstring current_index = qstring_to_ucs4(sel_index);
199 Index * index = indiceslist_.find(current_index);
203 QColor const initial = rgb2qcolor(index->color());
204 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
208 // add the color to the indiceslist
209 index->setColor(fromqstr(ncol.name()));
214 } // namespace frontend
217 #include "moc_GuiIndices.cpp"