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