]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
Fix doubling of bibtex and index alternatives everytime preferences are saved.
[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         // emit signal
127         changed();
128 }
129
130
131 void GuiIndices::apply(BufferParams & params) const
132 {
133         params.use_indices = multipleIndicesCB->isChecked();
134         params.indiceslist() = indiceslist_;
135
136         string const index_command =
137                 fromqstr(indexCO->itemData(
138                         indexCO->currentIndex()).toString());
139         string const index_options = fromqstr(indexOptionsED->text());
140         if (index_command == "default" || index_options.empty())
141                 params.index_command = index_command;
142         else
143                 params.index_command = index_command + " " + index_options;
144 }
145
146
147 void GuiIndices::on_indexCO_activated(int n)
148 {
149         indexOptionsED->setEnabled(n != 0);
150         changed();
151 }
152
153
154 void GuiIndices::on_indexOptionsED_textChanged(QString)
155 {
156         changed();
157 }
158
159
160 void GuiIndices::on_addIndexPB_pressed()
161 {
162         QString const new_index = newIndexLE->text();
163         if (!new_index.isEmpty()) {
164                 indiceslist_.add(qstring_to_ucs4(new_index));
165                 newIndexLE->clear();
166                 updateView();
167         }
168 }
169
170
171 void GuiIndices::on_removePB_pressed()
172 {
173         QTreeWidgetItem * selItem = indicesTW->currentItem();
174         QString sel_index;
175         if (selItem != 0)
176                 sel_index = selItem->text(0);
177         if (!sel_index.isEmpty()) {
178                 if (indiceslist_.find(qstring_to_ucs4(sel_index)) == 
179                     indiceslist_.findShortcut(from_ascii("idx"))) {
180                         Alert::error(_("Cannot remove standard index"), 
181                               _("The default index cannot be removed."));
182                               return;
183                 }
184                 indiceslist_.remove(qstring_to_ucs4(sel_index));
185                 newIndexLE->clear();
186                 updateView();
187         }
188 }
189
190
191 void GuiIndices::on_renamePB_clicked()
192 {
193         QTreeWidgetItem * selItem = indicesTW->currentItem();
194         QString sel_index;
195         if (selItem != 0)
196                 sel_index = selItem->text(0);
197         if (!sel_index.isEmpty()) {
198                 docstring newname;
199                 bool success = false;
200                 if (Alert::askForText(newname, _("Enter new index name"))) {
201                         success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
202                         newIndexLE->clear();
203                         updateView();
204                 }
205                 if (!success)
206                         Alert::error(_("Renaming failed"), 
207                               _("The index could not be renamed. "
208                                 "Check if the new name already exists."));
209         }
210 }
211
212
213 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
214 {
215         toggleColor(item);
216 }
217
218
219 void GuiIndices::on_colorPB_clicked()
220 {
221         toggleColor(indicesTW->currentItem());
222 }
223
224
225 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
226 {
227         indicesTW->setEnabled(state);
228         newIndexLE->setEnabled(state);
229         newIndexLA->setEnabled(state);
230         addIndexPB->setEnabled(state);
231         availableLA->setEnabled(state);
232         removePB->setEnabled(state);
233         colorPB->setEnabled(state);
234         // emit signal
235         changed();
236 }
237
238
239 void GuiIndices::toggleColor(QTreeWidgetItem * item)
240 {
241         if (item == 0)
242                 return;
243
244         QString sel_index = item->text(0);
245         if (sel_index.isEmpty())
246                 return;
247
248         docstring current_index = qstring_to_ucs4(sel_index);
249         Index * index = indiceslist_.find(current_index);
250         if (!index)
251                 return;
252
253         QColor const initial = rgb2qcolor(index->color());
254         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
255         if (!ncol.isValid())
256                 return;
257
258         // add the color to the indiceslist
259         index->setColor(fromqstr(ncol.name()));
260         newIndexLE->clear();
261         updateView();
262 }
263
264 } // namespace frontend
265 } // namespace lyx
266
267 #include "moc_GuiIndices.cpp"