]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIndices.cpp
Introduce splitindex support. File format change.
[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
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28
29 #include <QTreeWidget>
30 #include <QTreeWidgetItem>
31 #include <QPixmap>
32 #include <QIcon>
33 #include <QColor>
34 #include <QColorDialog>
35
36
37 namespace lyx {
38 namespace frontend {
39
40
41 GuiIndices::GuiIndices(QWidget * parent)
42         : QWidget(parent)
43 {
44         setupUi(this);
45         indicesTW->setColumnCount(2);
46         indicesTW->headerItem()->setText(0, qt_("Name"));
47         indicesTW->headerItem()->setText(1, qt_("Label Color"));
48         indicesTW->setSortingEnabled(true);
49 }
50
51 void GuiIndices::update(BufferParams const & params)
52 {
53         indiceslist_ = params.indiceslist();
54         multipleIndicesCB->setChecked(
55                 params.use_indices);
56         bool const state = params.use_indices;
57         indicesTW->setEnabled(state);
58         newIndexLE->setEnabled(state);
59         newIndexLA->setEnabled(state);
60         addIndexPB->setEnabled(state);
61         availableLA->setEnabled(state);
62         removePB->setEnabled(state);
63         colorPB->setEnabled(state);
64         updateView();
65 }
66
67
68 void GuiIndices::updateView()
69 {
70         // store the selected index
71         QTreeWidgetItem * item = indicesTW->currentItem();
72         QString sel_index;
73         if (item != 0)
74                 sel_index = item->text(0);
75
76         indicesTW->clear();
77
78         IndicesList::const_iterator it = indiceslist_.begin();
79         IndicesList::const_iterator const end = indiceslist_.end();
80         for (; it != end; ++it) {
81                 QTreeWidgetItem * newItem = new QTreeWidgetItem(indicesTW);
82
83                 QString const iname = toqstr(it->index());
84                 newItem->setText(0, iname);
85
86                 QColor const itemcolor = rgb2qcolor(it->color());
87                 if (itemcolor.isValid()) {
88                         QPixmap coloritem(30, 10);
89                         coloritem.fill(itemcolor);
90                         newItem->setIcon(1, QIcon(coloritem));
91                 }
92                 // restore selected index
93                 if (iname == sel_index) {
94                         indicesTW->setCurrentItem(newItem);
95                         indicesTW->setItemSelected(newItem, true);
96                 }
97         }
98         // emit signal
99         changed();
100 }
101
102
103 void GuiIndices::apply(BufferParams & params) const
104 {
105         params.use_indices = multipleIndicesCB->isChecked();
106         params.indiceslist() = indiceslist_;
107 }
108
109
110 void GuiIndices::on_addIndexPB_pressed()
111 {
112         QString const new_index = newIndexLE->text();
113         if (!new_index.isEmpty()) {
114                 indiceslist_.add(qstring_to_ucs4(new_index));
115                 newIndexLE->clear();
116                 updateView();
117         }
118 }
119
120
121 void GuiIndices::on_removePB_pressed()
122 {
123         QTreeWidgetItem * selItem = indicesTW->currentItem();
124         QString sel_index;
125         if (selItem != 0)
126                 sel_index = selItem->text(0);
127         if (!sel_index.isEmpty()) {
128                 if (indiceslist_.find(qstring_to_ucs4(sel_index)) == 
129                     indiceslist_.findShortcut(from_ascii("idx"))) {
130                         Alert::error(_("Cannot remove standard index"), 
131                               _("The default index cannot be removed."));
132                               return;
133                 }
134                 indiceslist_.remove(qstring_to_ucs4(sel_index));
135                 newIndexLE->clear();
136                 updateView();
137         }
138 }
139
140
141 void GuiIndices::on_renamePB_clicked()
142 {
143         QTreeWidgetItem * selItem = indicesTW->currentItem();
144         QString sel_index;
145         if (selItem != 0)
146                 sel_index = selItem->text(0);
147         if (!sel_index.isEmpty()) {
148                 docstring newname;
149                 bool success = false;
150                 if (Alert::askForText(newname, _("Enter new index name"))) {
151                         success = indiceslist_.rename(qstring_to_ucs4(sel_index), newname);
152                         newIndexLE->clear();
153                         updateView();
154                 }
155                 if (!success)
156                         Alert::error(_("Renaming failed"), 
157                               _("The index could not be renamed. "
158                                 "Check if the new name already exists."));
159         }
160 }
161
162
163 void GuiIndices::on_indicesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
164 {
165         toggleColor(item);
166 }
167
168
169 void GuiIndices::on_colorPB_clicked()
170 {
171         toggleColor(indicesTW->currentItem());
172 }
173
174
175 void GuiIndices::on_multipleIndicesCB_toggled(bool const state)
176 {
177         indicesTW->setEnabled(state);
178         newIndexLE->setEnabled(state);
179         newIndexLA->setEnabled(state);
180         addIndexPB->setEnabled(state);
181         availableLA->setEnabled(state);
182         removePB->setEnabled(state);
183         colorPB->setEnabled(state);
184         // emit signal
185         changed();
186 }
187
188
189 void GuiIndices::toggleColor(QTreeWidgetItem * item)
190 {
191         if (item == 0)
192                 return;
193
194         QString sel_index = item->text(0);
195         if (sel_index.isEmpty())
196                 return;
197
198         docstring current_index = qstring_to_ucs4(sel_index);
199         Index * index = indiceslist_.find(current_index);
200         if (!index)
201                 return;
202
203         QColor const initial = rgb2qcolor(index->color());
204         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
205         if (!ncol.isValid())
206                 return;
207
208         // add the color to the indiceslist
209         index->setColor(fromqstr(ncol.name()));
210         newIndexLE->clear();
211         updateView();
212 }
213
214 } // namespace frontend
215 } // namespace lyx
216
217 #include "moc_GuiIndices.cpp"