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