]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
* GuiBranches.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 }
63
64 void GuiIndices::update(BufferParams const & params)
65 {
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);
76
77         string command;
78         string options =
79                 split(params.index_command, command, ' ');
80
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();
88         }
89         indexOptionsED->setEnabled(
90                 indexCO->currentIndex() != 0);
91
92         updateView();
93 }
94
95
96 void GuiIndices::updateView()
97 {
98         // store the selected index
99         QTreeWidgetItem * item = indicesTW->currentItem();
100         QString sel_index;
101         if (item != 0)
102                 sel_index = item->text(0);
103
104         indicesTW->clear();
105
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);
110
111                 QString const iname = toqstr(it->index());
112                 newItem->setText(0, iname);
113
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));
119                 }
120                 // restore selected index
121                 if (iname == sel_index) {
122                         indicesTW->setCurrentItem(newItem);
123                         indicesTW->setItemSelected(newItem, true);
124                 }
125         }
126         bool const have_sel =
127                 !indicesTW->selectedItems().isEmpty();
128         removePB->setEnabled(have_sel);
129         renamePB->setEnabled(have_sel);
130         colorPB->setEnabled(have_sel);
131         // emit signal
132         changed();
133 }
134
135
136 void GuiIndices::apply(BufferParams & params) const
137 {
138         params.use_indices = multipleIndicesCB->isChecked();
139         params.indiceslist() = indiceslist_;
140
141         string const index_command =
142                 fromqstr(indexCO->itemData(
143                         indexCO->currentIndex()).toString());
144         string const index_options = fromqstr(indexOptionsED->text());
145         if (index_command == "default" || index_options.empty())
146                 params.index_command = index_command;
147         else
148                 params.index_command = index_command + " " + index_options;
149 }
150
151
152 void GuiIndices::on_indexCO_activated(int n)
153 {
154         indexOptionsED->setEnabled(n != 0);
155         changed();
156 }
157
158
159 void GuiIndices::on_indexOptionsED_textChanged(QString)
160 {
161         changed();
162 }
163
164
165 void GuiIndices::on_addIndexPB_pressed()
166 {
167         QString const new_index = newIndexLE->text();
168         if (!new_index.isEmpty()) {
169                 indiceslist_.add(qstring_to_ucs4(new_index));
170                 newIndexLE->clear();
171                 updateView();
172         }
173 }
174
175
176 void GuiIndices::on_removePB_pressed()
177 {
178         QTreeWidgetItem * selItem = indicesTW->currentItem();
179         QString sel_index;
180         if (selItem != 0)
181                 sel_index = selItem->text(0);
182         if (!sel_index.isEmpty()) {
183                 if (indiceslist_.find(qstring_to_ucs4(sel_index)) == 
184                     indiceslist_.findShortcut(from_ascii("idx"))) {
185                         Alert::error(_("Cannot remove standard index"), 
186                               _("The default index cannot be removed."));
187                               return;
188                 }
189                 indiceslist_.remove(qstring_to_ucs4(sel_index));
190                 newIndexLE->clear();
191                 updateView();
192         }
193 }
194
195
196 void GuiIndices::on_renamePB_clicked()
197 {
198         QTreeWidgetItem * selItem = indicesTW->currentItem();
199         QString sel_index;
200         if (selItem != 0)
201                 sel_index = selItem->text(0);
202         if (!sel_index.isEmpty()) {
203                 docstring newname;
204                 docstring const oldname = qstring_to_ucs4(sel_index);
205                 bool success = false;
206                 if (Alert::askForText(newname, _("Enter new index name"), oldname)) {
207                         if (newname.empty() || oldname == newname)
208                                 return;
209                         success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
210                         newIndexLE->clear();
211                         updateView();
212                 }
213                 if (!success)
214                         Alert::error(_("Renaming failed"), 
215                               _("The index could not be renamed. "
216                                 "Check if the new name already exists."));
217         }
218 }
219
220
221 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
222 {
223         toggleColor(item);
224 }
225
226
227 void GuiIndices::on_indicesTW_itemSelectionChanged()
228 {
229         bool const have_sel =
230                 !indicesTW->selectedItems().isEmpty();
231         removePB->setEnabled(have_sel);
232         renamePB->setEnabled(have_sel);
233         colorPB->setEnabled(have_sel);
234 }
235
236
237 void GuiIndices::on_colorPB_clicked()
238 {
239         toggleColor(indicesTW->currentItem());
240 }
241
242
243 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
244 {
245         bool const have_sel =
246                 !indicesTW->selectedItems().isEmpty();
247         indicesTW->setEnabled(state);
248         newIndexLE->setEnabled(state);
249         newIndexLA->setEnabled(state);
250         addIndexPB->setEnabled(state);
251         availableLA->setEnabled(state);
252         removePB->setEnabled(state && have_sel);
253         colorPB->setEnabled(state && have_sel);
254         renamePB->setEnabled(state && have_sel);
255         // emit signal
256         changed();
257 }
258
259
260 void GuiIndices::toggleColor(QTreeWidgetItem * item)
261 {
262         if (item == 0)
263                 return;
264
265         QString sel_index = item->text(0);
266         if (sel_index.isEmpty())
267                 return;
268
269         docstring current_index = qstring_to_ucs4(sel_index);
270         Index * index = indiceslist_.find(current_index);
271         if (!index)
272                 return;
273
274         QColor const initial = rgb2qcolor(index->color());
275         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
276         if (!ncol.isValid())
277                 return;
278
279         // add the color to the indiceslist
280         index->setColor(fromqstr(ncol.name()));
281         newIndexLE->clear();
282         updateView();
283 }
284
285 } // namespace frontend
286 } // namespace lyx
287
288 #include "moc_GuiIndices.cpp"