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