]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
* GuiDocument.cpp:
[lyx.git] / src / frontends / qt4 / GuiIndices.cpp
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.
5  *
6  * \author Edwin Leuven
7  * \author Jürgen Spitzmüller
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiIndices.h"
16
17 #include "ColorCache.h"
18 #include "GuiApplication.h"
19 #include "Validator.h"
20 #include "qt_helpers.h"
21
22 #include "frontends/alert.h"
23
24 #include "BufferParams.h"
25 #include "LyXRC.h"
26
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 #include <QTreeWidget>
31 #include <QTreeWidgetItem>
32 #include <QPixmap>
33 #include <QIcon>
34 #include <QColor>
35 #include <QColorDialog>
36
37
38 using namespace std;
39 using namespace lyx::support;
40
41
42 namespace lyx {
43 namespace frontend {
44
45
46 GuiIndices::GuiIndices(QWidget * parent)
47         : QWidget(parent)
48 {
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);
54
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);
61         }
62         indexCO->addItem(qt_("Custom"), QString("custom"));
63 }
64
65 void GuiIndices::update(BufferParams const & params)
66 {
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);
77
78         string command;
79         string options =
80                 split(params.index_command, command, ' ');
81
82         int const pos = indexCO->findData(toqstr(command));
83         if (pos != -1) {
84                 indexCO->setCurrentIndex(pos);
85                 indexOptionsED->setText(toqstr(options).trimmed());
86                 indexOptionsLA->setText(qt_("&Options:"));
87         } else {
88                 indexCO->setCurrentIndex(indexCO->findData(toqstr("custom")));
89                 indexOptionsED->setText(toqstr(params.index_command));
90                 indexOptionsLA->setText(qt_("Co&mmand:"));
91         }
92         indexOptionsED->setEnabled(
93                 indexCO->currentIndex() != 0);
94
95         updateView();
96 }
97
98
99 void GuiIndices::updateView()
100 {
101         // store the selected index
102         QTreeWidgetItem * item = indicesTW->currentItem();
103         QString sel_index;
104         if (item != 0)
105                 sel_index = item->text(0);
106
107         indicesTW->clear();
108
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);
113
114                 QString const iname = toqstr(it->index());
115                 newItem->setText(0, iname);
116
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));
122                 }
123                 // restore selected index
124                 if (iname == sel_index) {
125                         indicesTW->setCurrentItem(newItem);
126                         indicesTW->setItemSelected(newItem, true);
127                 }
128         }
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);
135         // emit signal
136         changed();
137 }
138
139
140 void GuiIndices::apply(BufferParams & params) const
141 {
142         params.use_indices = multipleIndicesCB->isChecked();
143         params.indiceslist() = indiceslist_;
144
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;
153         else
154                 params.index_command = index_command + " " + index_options;
155 }
156
157
158 void GuiIndices::on_indexCO_activated(int n)
159 {
160         QString const data = indexCO->itemData(n).toString();
161         indexOptionsED->setEnabled(data != "default");
162         if (data == "custom")
163                 indexOptionsLA->setText(qt_("Co&mmand:"));
164         else
165                 indexOptionsLA->setText(qt_("&Options:"));
166         changed();
167 }
168
169
170 void GuiIndices::on_indexOptionsED_textChanged(QString)
171 {
172         changed();
173 }
174
175
176 void GuiIndices::on_addIndexPB_pressed()
177 {
178         QString const new_index = newIndexLE->text();
179         if (!new_index.isEmpty()) {
180                 indiceslist_.add(qstring_to_ucs4(new_index));
181                 newIndexLE->clear();
182                 updateView();
183         }
184 }
185
186
187 void GuiIndices::on_removePB_pressed()
188 {
189         QTreeWidgetItem * selItem = indicesTW->currentItem();
190         QString sel_index;
191         if (selItem != 0)
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."));
198                               return;
199                 }
200                 indiceslist_.remove(qstring_to_ucs4(sel_index));
201                 newIndexLE->clear();
202                 updateView();
203         }
204 }
205
206
207 void GuiIndices::on_renamePB_clicked()
208 {
209         QTreeWidgetItem * selItem = indicesTW->currentItem();
210         QString sel_index;
211         if (selItem != 0)
212                 sel_index = selItem->text(0);
213         if (!sel_index.isEmpty()) {
214                 docstring newname;
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)
219                                 return;
220                         success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
221                         newIndexLE->clear();
222                         updateView();
223                         if (!success)
224                                 Alert::error(_("Renaming failed"), 
225                                       _("The index could not be renamed. "
226                                         "Check if the new name already exists."));
227                 }
228         }
229 }
230
231
232 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
233 {
234         toggleColor(item);
235 }
236
237
238 void GuiIndices::on_indicesTW_itemSelectionChanged()
239 {
240         bool const have_sel =
241                 !indicesTW->selectedItems().isEmpty();
242         removePB->setEnabled(have_sel);
243         renamePB->setEnabled(have_sel);
244         colorPB->setEnabled(have_sel);
245 }
246
247
248 void GuiIndices::on_colorPB_clicked()
249 {
250         toggleColor(indicesTW->currentItem());
251 }
252
253
254 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
255 {
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);
266         // emit signal
267         changed();
268 }
269
270
271 void GuiIndices::toggleColor(QTreeWidgetItem * item)
272 {
273         if (item == 0)
274                 return;
275
276         QString sel_index = item->text(0);
277         if (sel_index.isEmpty())
278                 return;
279
280         docstring current_index = qstring_to_ucs4(sel_index);
281         Index * index = indiceslist_.find(current_index);
282         if (!index)
283                 return;
284
285         QColor const initial = rgb2qcolor(index->color());
286         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
287         if (!ncol.isValid())
288                 return;
289
290         // add the color to the indiceslist
291         index->setColor(fromqstr(ncol.name()));
292         newIndexLE->clear();
293         updateView();
294 }
295
296 } // namespace frontend
297 } // namespace lyx
298
299 #include "moc_GuiIndices.cpp"