]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBranches.cpp
HTML output for InsetMathCancel.
[lyx.git] / src / frontends / qt4 / GuiBranches.cpp
1 /**
2  * \file GuiBranches.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 Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiBranches.h"
15
16 #include "ColorCache.h"
17 #include "GuiApplication.h"
18 #include "Validator.h"
19 #include "qt_helpers.h"
20
21 #include "ui_BranchesUnknownUi.h"
22
23 #include "frontends/alert.h"
24
25 #include "Buffer.h"
26 #include "BufferParams.h"
27
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <QListWidget>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
34 #include <QPixmap>
35 #include <QIcon>
36 #include <QColor>
37 #include <QColorDialog>
38
39
40 namespace lyx {
41 namespace frontend {
42
43
44 GuiBranches::GuiBranches(QWidget * parent)
45         : QWidget(parent)
46 {
47         setupUi(this);
48         branchesTW->setColumnCount(3);
49         branchesTW->headerItem()->setText(0, qt_("Branch"));
50         branchesTW->headerItem()->setText(1, qt_("Activated"));
51         branchesTW->headerItem()->setText(2, qt_("Color"));
52         branchesTW->headerItem()->setText(3, qt_("Filename Suffix"));
53         branchesTW->setSortingEnabled(true);
54         branchesTW->resizeColumnToContents(1);
55         branchesTW->resizeColumnToContents(2);
56
57         undef_ = new BranchesUnknownDialog(this);
58         undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
59         undef_bc_.setCancel(undef_->cancelPB);
60
61         connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
62                 this, SLOT(unknownBranchSelChanged()));
63         connect(undef_->addSelectedPB, SIGNAL(clicked()),
64                 this, SLOT(addUnknown()));
65         connect(undef_->addAllPB, SIGNAL(clicked()),
66                 this, SLOT(addAllUnknown()));
67         connect(undef_->addSelectedPB, SIGNAL(clicked()),
68                 undef_, SLOT(accept()));
69         connect(undef_->addAllPB, SIGNAL(clicked()),
70                 undef_, SLOT(accept()));
71         connect(undef_->cancelPB, SIGNAL(clicked()),
72                 undef_, SLOT(reject()));
73
74         newBranchLE->setValidator(new NoNewLineValidator(newBranchLE));
75 }
76
77 void GuiBranches::update(BufferParams const & params)
78 {
79         branchlist_ = params.branchlist();
80         updateView();
81 }
82
83
84 void GuiBranches::updateView()
85 {
86         // store the selected branch
87         QTreeWidgetItem * item = branchesTW->currentItem();
88         QString sel_branch;
89         if (item != 0)
90                 sel_branch = item->text(0);
91
92         branchesTW->clear();
93
94         BranchList::const_iterator it = branchlist_.begin();
95         BranchList::const_iterator const end = branchlist_.end();
96         for (; it != end; ++it) {
97                 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
98
99                 QString const bname = toqstr(it->branch());
100                 newItem->setText(0, bname);
101                 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
102
103                 QColor const itemcolor = rgb2qcolor(it->color());
104                 if (itemcolor.isValid()) {
105                         QPixmap coloritem(30, 10);
106                         coloritem.fill(itemcolor);
107                         newItem->setIcon(2, QIcon(coloritem));
108                 }
109                 newItem->setText(3, it->hasFileNameSuffix() ? qt_("Yes") : qt_("No"));
110                 // restore selected branch
111                 if (bname == sel_branch) {
112                         branchesTW->setCurrentItem(newItem);
113                         branchesTW->setItemSelected(newItem, true);
114                 }
115         }
116         unknownPB->setEnabled(!unknown_branches_.isEmpty());
117         bool const have_sel =
118                 !branchesTW->selectedItems().isEmpty();
119         removePB->setEnabled(have_sel);
120         renamePB->setEnabled(have_sel);
121         colorPB->setEnabled(have_sel);
122         activatePB->setEnabled(have_sel);
123         suffixPB->setEnabled(have_sel);
124         // emit signal
125         changed();
126 }
127
128
129 void GuiBranches::apply(BufferParams & params) const
130 {
131         params.branchlist() = branchlist_;
132 }
133
134
135 void GuiBranches::on_addBranchPB_pressed()
136 {
137         QString const new_branch = newBranchLE->text();
138         if (!new_branch.isEmpty()) {
139                 branchlist_.add(qstring_to_ucs4(new_branch));
140                 newBranchLE->clear();
141                 updateView();
142         }
143 }
144
145
146 void GuiBranches::on_removePB_pressed()
147 {
148         QTreeWidgetItem * selItem = branchesTW->currentItem();
149         QString sel_branch;
150         if (selItem != 0)
151                 sel_branch = selItem->text(0);
152         if (!sel_branch.isEmpty()) {
153                 branchlist_.remove(qstring_to_ucs4(sel_branch));
154                 newBranchLE->clear();
155                 updateView();
156         }
157 }
158
159
160 void GuiBranches::on_renamePB_pressed()
161 {
162         QTreeWidgetItem * selItem = branchesTW->currentItem();
163         QString sel_branch;
164         if (selItem != 0)
165                 sel_branch = selItem->text(0);
166         if (!sel_branch.isEmpty()) {
167                 docstring newname;
168                 docstring const oldname = qstring_to_ucs4(sel_branch);
169                 bool success = false;
170                 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
171                         if (newname.empty() || oldname == newname)
172                                 return;
173                         if (branchlist_.find(newname)) {
174                                 docstring text = support::bformat(
175                                         _("A branch with the name \"%1$s\" already exists.\n"
176                                           "Do you want to merge branch \"%2$s\" with that one?"),
177                                         newname, oldname);
178                                 if (frontend::Alert::prompt(_("Branch already exists"),
179                                           text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
180                                         success = branchlist_.rename(oldname, newname, true);
181                         } else
182                                 success = branchlist_.rename(oldname, newname);
183                         newBranchLE->clear();
184                         updateView();
185
186                         if (!success)
187                                 Alert::error(_("Renaming failed"), 
188                                       _("The branch could not be renamed."));
189                         else
190                                 // emit signal
191                                 renameBranches(oldname, newname);
192                 }
193         }
194 }
195
196
197 void GuiBranches::on_activatePB_pressed()
198 {
199         toggleBranch(branchesTW->currentItem());
200 }
201
202
203 void GuiBranches::on_suffixPB_pressed()
204 {
205         toggleSuffix(branchesTW->currentItem());
206 }
207
208
209 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
210 {
211         if (col < 2)
212                 toggleBranch(item);
213         else if (col == 2)
214                 toggleColor(item);
215         else if (col == 3)
216                 toggleSuffix(item);
217 }
218
219
220 void GuiBranches::on_branchesTW_itemSelectionChanged()
221 {
222         bool const have_sel =
223                 !branchesTW->selectedItems().isEmpty();
224         removePB->setEnabled(have_sel);
225         renamePB->setEnabled(have_sel);
226         colorPB->setEnabled(have_sel);
227         activatePB->setEnabled(have_sel);
228         suffixPB->setEnabled(have_sel);
229 }
230
231
232 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
233 {
234         if (item == 0)
235                 return;
236
237         QString sel_branch = item->text(0);
238         if (sel_branch.isEmpty())
239                 return;
240
241         Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
242         if (branch && branch->setSelected(!branch->isSelected())) {
243                 newBranchLE->clear();
244                 updateView();
245         }
246 }
247
248
249 void GuiBranches::on_colorPB_clicked()
250 {
251         toggleColor(branchesTW->currentItem());
252 }
253
254
255 void GuiBranches::toggleColor(QTreeWidgetItem * item)
256 {
257         if (item == 0)
258                 return;
259
260         QString sel_branch = item->text(0);
261         if (sel_branch.isEmpty())
262                 return;
263
264         docstring current_branch = qstring_to_ucs4(sel_branch);
265         Branch * branch = branchlist_.find(current_branch);
266         if (!branch)
267                 return;
268
269         QColor const initial = rgb2qcolor(branch->color());
270         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
271         if (!ncol.isValid())
272                 return;
273
274         // add the color to the branchlist
275         branch->setColor(fromqstr(ncol.name()));
276         newBranchLE->clear();
277         updateView();
278 }
279
280
281 void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
282 {
283         if (item == 0)
284                 return;
285
286         QString sel_branch = item->text(0);
287         if (sel_branch.isEmpty())
288                 return;
289
290         Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
291         if (branch) {
292                 branch->setFileNameSuffix(!branch->hasFileNameSuffix());
293                 newBranchLE->clear();
294                 updateView();
295         }
296 }
297
298
299 void GuiBranches::on_unknownPB_pressed()
300 {
301         undef_->branchesLW->clear();
302         for (int i = 0; i != unknown_branches_.count(); ++i) {
303                 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
304                         undef_->branchesLW->addItem(unknown_branches_[i]);
305         }
306         unknownBranchSelChanged();
307         undef_->exec();
308 }
309
310
311 void GuiBranches::addUnknown()
312 {
313         QList<QListWidgetItem *> selItems =
314                 undef_->branchesLW->selectedItems();
315         
316         QList<QListWidgetItem *>::const_iterator it = selItems.begin();
317         for (; it != selItems.end() ; ++it) {
318                 QListWidgetItem const * new_branch = *it;
319                 if (new_branch) {
320                         branchlist_.add(qstring_to_ucs4(new_branch->text()));
321                         updateView();
322                 }
323         }
324 }
325
326
327 void GuiBranches::addAllUnknown()
328 {
329         undef_->branchesLW->selectAll();
330         addUnknown();
331 }
332
333
334 void GuiBranches::unknownBranchSelChanged()
335 {
336         undef_->addSelectedPB->setEnabled(
337                 !undef_->branchesLW->selectedItems().isEmpty());
338 }
339
340
341 } // namespace frontend
342 } // namespace lyx
343
344 #include "moc_GuiBranches.cpp"