* GuiIndices.cpp: cosmetics (corretly resize TW column).
[lyx.git] / src / frontends / qt4 / GuiIndices.cpp
bloba3f609bd6a44addb216ea80f37bc00ea05e0af34
1 /**
2 * \file GuiIndices.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Edwin Leuven
7 * \author Jürgen Spitzmüller
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
13 #include <config.h>
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"
25 #include "LyXRC.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
30 #include <QTreeWidget>
31 #include <QTreeWidgetItem>
32 #include <QPixmap>
33 #include <QIcon>
34 #include <QColor>
35 #include <QColorDialog>
38 using namespace std;
39 using namespace lyx::support;
42 namespace lyx {
43 namespace frontend {
46 GuiIndices::GuiIndices(QWidget * parent)
47 : QWidget(parent)
49 setupUi(this);
50 indicesTW->setColumnCount(2);
51 indicesTW->headerItem()->setText(0, qt_("Name"));
52 indicesTW->headerItem()->setText(1, qt_("Label Color"));
53 indicesTW->setSortingEnabled(true);
55 indexCO->clear();
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);
77 string command;
78 string options =
79 split(params.index_command, command, ' ');
81 int const pos = indexCO->findData(toqstr(command));
82 if (pos != -1) {
83 indexCO->setCurrentIndex(pos);
84 indexOptionsED->setText(toqstr(options).trimmed());
85 } else {
86 indexCO->setCurrentIndex(0);
87 indexOptionsED->clear();
89 indexOptionsED->setEnabled(
90 indexCO->currentIndex() != 0);
92 updateView();
96 void GuiIndices::updateView()
98 // store the selected index
99 QTreeWidgetItem * item = indicesTW->currentItem();
100 QString sel_index;
101 if (item != 0)
102 sel_index = item->text(0);
104 indicesTW->clear();
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);
132 // emit signal
133 changed();
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;
148 else
149 params.index_command = index_command + " " + index_options;
153 void GuiIndices::on_indexCO_activated(int n)
155 indexOptionsED->setEnabled(n != 0);
156 changed();
160 void GuiIndices::on_indexOptionsED_textChanged(QString)
162 changed();
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));
171 newIndexLE->clear();
172 updateView();
177 void GuiIndices::on_removePB_pressed()
179 QTreeWidgetItem * selItem = indicesTW->currentItem();
180 QString sel_index;
181 if (selItem != 0)
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."));
188 return;
190 indiceslist_.remove(qstring_to_ucs4(sel_index));
191 newIndexLE->clear();
192 updateView();
197 void GuiIndices::on_renamePB_clicked()
199 QTreeWidgetItem * selItem = indicesTW->currentItem();
200 QString sel_index;
201 if (selItem != 0)
202 sel_index = selItem->text(0);
203 if (!sel_index.isEmpty()) {
204 docstring newname;
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)
209 return;
210 success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
211 newIndexLE->clear();
212 updateView();
214 if (!success)
215 Alert::error(_("Renaming failed"),
216 _("The index could not be renamed. "
217 "Check if the new name already exists."));
222 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
224 toggleColor(item);
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);
256 // emit signal
257 changed();
261 void GuiIndices::toggleColor(QTreeWidgetItem * item)
263 if (item == 0)
264 return;
266 QString sel_index = item->text(0);
267 if (sel_index.isEmpty())
268 return;
270 docstring current_index = qstring_to_ucs4(sel_index);
271 Index * index = indiceslist_.find(current_index);
272 if (!index)
273 return;
275 QColor const initial = rgb2qcolor(index->color());
276 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
277 if (!ncol.isValid())
278 return;
280 // add the color to the indiceslist
281 index->setColor(fromqstr(ncol.name()));
282 newIndexLE->clear();
283 updateView();
286 } // namespace frontend
287 } // namespace lyx
289 #include "moc_GuiIndices.cpp"