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(params.use_indices);
55 bool const state = params.use_indices;
56 indicesTW->setEnabled(state);
57 newIndexLE->setEnabled(state);
58 newIndexLA->setEnabled(state);
59 addIndexPB->setEnabled(state);
60 availableLA->setEnabled(state);
61 removePB->setEnabled(state);
62 colorPB->setEnabled(state);
67 void GuiIndices::updateView()
69 // store the selected index
70 QTreeWidgetItem * item = indicesTW->currentItem();
73 sel_index = item->text(0);
77 IndicesList::const_iterator it = indiceslist_.begin();
78 IndicesList::const_iterator const end = indiceslist_.end();
79 for (; it != end; ++it) {
80 QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
82 QString const iname = toqstr(it->index());
83 newItem->setText(0, iname);
85 QColor const itemcolor = rgb2qcolor(it->color());
86 if (itemcolor.isValid()) {
87 QPixmap coloritem(30, 10);
88 coloritem.fill(itemcolor);
89 newItem->setIcon(1, QIcon(coloritem));
91 // restore selected index
92 if (iname == sel_index) {
93 indicesTW->setCurrentItem(newItem);
94 indicesTW->setItemSelected(newItem, true);
102 void GuiIndices::apply(BufferParams & params) const
104 params.use_indices = multipleIndicesCB->isChecked();
105 params.indiceslist() = indiceslist_;
109 void GuiIndices::on_addIndexPB_pressed()
111 QString const new_index = newIndexLE->text();
112 if (!new_index.isEmpty()) {
113 indiceslist_.add(qstring_to_ucs4(new_index));
120 void GuiIndices::on_removePB_pressed()
122 QTreeWidgetItem * selItem = indicesTW->currentItem();
125 sel_index = selItem->text(0);
126 if (!sel_index.isEmpty()) {
127 if (indiceslist_.find(qstring_to_ucs4(sel_index)) ==
128 indiceslist_.findShortcut(from_ascii("idx"))) {
129 Alert::error(_("Cannot remove standard index"),
130 _("The default index cannot be removed."));
133 indiceslist_.remove(qstring_to_ucs4(sel_index));
140 void GuiIndices::on_renamePB_clicked()
142 QTreeWidgetItem * selItem = indicesTW->currentItem();
145 sel_index = selItem->text(0);
146 if (!sel_index.isEmpty()) {
148 bool success = false;
149 if (Alert::askForText(newname, _("Enter new index name"))) {
150 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
155 Alert::error(_("Renaming failed"),
156 _("The index could not be renamed. "
157 "Check if the new name already exists."));
162 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
168 void GuiIndices::on_colorPB_clicked()
170 toggleColor(indicesTW->currentItem());
174 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
176 indicesTW->setEnabled(state);
177 newIndexLE->setEnabled(state);
178 newIndexLA->setEnabled(state);
179 addIndexPB->setEnabled(state);
180 availableLA->setEnabled(state);
181 removePB->setEnabled(state);
182 colorPB->setEnabled(state);
188 void GuiIndices::toggleColor(QTreeWidgetItem * item)
193 QString sel_index = item->text(0);
194 if (sel_index.isEmpty())
197 docstring current_index = qstring_to_ucs4(sel_index);
198 Index * index = indiceslist_.find(current_index);
202 QColor const initial = rgb2qcolor(index->color());
203 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
207 // add the color to the indiceslist
208 index->setColor(fromqstr(ncol.name()));
213 } // namespace frontend
216 #include "moc_GuiIndices.cpp"