]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
Whitespace.
[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         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();
134 }
135
136
137 void GuiIndices::apply(BufferParams & params) const
138 {
139         params.use_indices = multipleIndicesCB->isChecked();
140         params.indiceslist() = indiceslist_;
141
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;
150 }
151
152
153 void GuiIndices::on_indexCO_activated(int n)
154 {
155         indexOptionsED->setEnabled(n != 0);
156         changed();
157 }
158
159
160 void GuiIndices::on_indexOptionsED_textChanged(QString)
161 {
162         changed();
163 }
164
165
166 void GuiIndices::on_addIndexPB_pressed()
167 {
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();
173         }
174 }
175
176
177 void GuiIndices::on_removePB_pressed()
178 {
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;
189                 }
190                 indiceslist_.remove(qstring_to_ucs4(sel_index));
191                 newIndexLE->clear();
192                 updateView();
193         }
194 }
195
196
197 void GuiIndices::on_renamePB_clicked()
198 {
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();
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
222 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
223 {
224         toggleColor(item);
225 }
226
227
228 void GuiIndices::on_indicesTW_itemSelectionChanged()
229 {
230         bool const have_sel =
231                 !indicesTW->selectedItems().isEmpty();
232         removePB->setEnabled(have_sel);
233         renamePB->setEnabled(have_sel);
234         colorPB->setEnabled(have_sel);
235 }
236
237
238 void GuiIndices::on_colorPB_clicked()
239 {
240         toggleColor(indicesTW->currentItem());
241 }
242
243
244 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
245 {
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();
258 }
259
260
261 void GuiIndices::toggleColor(QTreeWidgetItem * item)
262 {
263         if (item == 0)
264                 return;
265
266         QString sel_index = item->text(0);
267         if (sel_index.isEmpty())
268                 return;
269
270         docstring current_index = qstring_to_ucs4(sel_index);
271         Index * index = indiceslist_.find(current_index);
272         if (!index)
273                 return;
274
275         QColor const initial = rgb2qcolor(index->color());
276         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
277         if (!ncol.isValid())
278                 return;
279
280         // add the color to the indiceslist
281         index->setColor(fromqstr(ncol.name()));
282         newIndexLE->clear();
283         updateView();
284 }
285
286 } // namespace frontend
287 } // namespace lyx
288
289 #include "moc_GuiIndices.cpp"