]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBranches.cpp
b3af3ed2936cb9f9ca0d979e7d1da38dd94b4684
[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->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         bool const have_sel =
112                 !branchesTW->selectedItems().isEmpty();
113         removePB->setEnabled(have_sel);
114         renamePB->setEnabled(have_sel);
115         colorPB->setEnabled(have_sel);
116         activatePB->setEnabled(have_sel);
117         // emit signal
118         changed();
119 }
120
121
122 void GuiBranches::apply(BufferParams & params) const
123 {
124         params.branchlist() = branchlist_;
125 }
126
127
128 void GuiBranches::on_addBranchPB_pressed()
129 {
130         QString const new_branch = newBranchLE->text();
131         if (!new_branch.isEmpty()) {
132                 branchlist_.add(qstring_to_ucs4(new_branch));
133                 newBranchLE->clear();
134                 updateView();
135         }
136 }
137
138
139 void GuiBranches::on_removePB_pressed()
140 {
141         QTreeWidgetItem * selItem = branchesTW->currentItem();
142         QString sel_branch;
143         if (selItem != 0)
144                 sel_branch = selItem->text(0);
145         if (!sel_branch.isEmpty()) {
146                 branchlist_.remove(qstring_to_ucs4(sel_branch));
147                 newBranchLE->clear();
148                 updateView();
149         }
150 }
151
152
153 void GuiBranches::on_renamePB_pressed()
154 {
155         QTreeWidgetItem * selItem = branchesTW->currentItem();
156         QString sel_branch;
157         if (selItem != 0)
158                 sel_branch = selItem->text(0);
159         if (!sel_branch.isEmpty()) {
160                 docstring newname;
161                 docstring const oldname = qstring_to_ucs4(sel_branch);
162                 bool success = false;
163                 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
164                         if (newname.empty() || oldname == newname)
165                                 return;
166                         if (branchlist_.find(newname)) {
167                                 docstring text = support::bformat(
168                                         _("A branch with the name \"%1$s\" already exists.\n"
169                                           "Do you want to merge branch \"%2$s\" with that one?"),
170                                         newname, oldname);
171                                 if (frontend::Alert::prompt(_("Branch already exists"),
172                                           text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
173                                         success = branchlist_.rename(oldname, newname, true);
174                         } else
175                                 success = branchlist_.rename(oldname, newname);
176                         newBranchLE->clear();
177                         updateView();
178
179                         if (!success)
180                                 Alert::error(_("Renaming failed"), 
181                                       _("The branch could not be renamed."));
182                         else
183                                 // emit signal
184                                 renameBranches(oldname, newname);
185                 }
186         }
187 }
188
189
190 void GuiBranches::on_activatePB_pressed()
191 {
192         toggleBranch(branchesTW->currentItem());
193 }
194
195
196 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
197 {
198         if (col < 2)
199                 toggleBranch(item);
200         else
201                 toggleColor(item);
202 }
203
204
205 void GuiBranches::on_branchesTW_itemSelectionChanged()
206 {
207         bool const have_sel =
208                 !branchesTW->selectedItems().isEmpty();
209         removePB->setEnabled(have_sel);
210         renamePB->setEnabled(have_sel);
211         colorPB->setEnabled(have_sel);
212         activatePB->setEnabled(have_sel);
213 }
214
215
216 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
217 {
218         if (item == 0)
219                 return;
220
221         QString sel_branch = item->text(0);
222         if (sel_branch.isEmpty())
223                 return;
224
225         bool const selected = (item->text(1) == qt_("Yes"));
226         Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
227         if (branch && branch->setSelected(!selected)) {
228                 newBranchLE->clear();
229                 updateView();
230         }
231 }
232
233
234 void GuiBranches::on_colorPB_clicked()
235 {
236         toggleColor(branchesTW->currentItem());
237 }
238
239
240 void GuiBranches::toggleColor(QTreeWidgetItem * item)
241 {
242         if (item == 0)
243                 return;
244
245         QString sel_branch = item->text(0);
246         if (sel_branch.isEmpty())
247                 return;
248
249         docstring current_branch = qstring_to_ucs4(sel_branch);
250         Branch * branch = branchlist_.find(current_branch);
251         if (!branch)
252                 return;
253
254         QColor const initial = rgb2qcolor(branch->color());
255         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
256         if (!ncol.isValid())
257                 return;
258
259         // add the color to the branchlist
260         branch->setColor(fromqstr(ncol.name()));
261         newBranchLE->clear();
262         updateView();
263 }
264
265
266 void GuiBranches::on_unknownPB_pressed()
267 {
268         undef_->branchesLW->clear();
269         for (int i = 0; i != unknown_branches_.count(); ++i) {
270                 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
271                         undef_->branchesLW->addItem(unknown_branches_[i]);
272         }
273         unknownBranchSelChanged();
274         undef_->exec();
275 }
276
277
278 void GuiBranches::addUnknown()
279 {
280         QList<QListWidgetItem *> selItems =
281                 undef_->branchesLW->selectedItems();
282         
283         QList<QListWidgetItem *>::const_iterator it = selItems.begin();
284         for (; it != selItems.end() ; ++it) {
285                 QListWidgetItem const * new_branch = *it;
286                 if (new_branch) {
287                         branchlist_.add(qstring_to_ucs4(new_branch->text()));
288                         updateView();
289                 }
290         }
291 }
292
293
294 void GuiBranches::addAllUnknown()
295 {
296         undef_->branchesLW->selectAll();
297         addUnknown();
298 }
299
300
301 void GuiBranches::unknownBranchSelChanged()
302 {
303         undef_->addSelectedPB->setEnabled(
304                 !undef_->branchesLW->selectedItems().isEmpty());
305 }
306
307
308 } // namespace frontend
309 } // namespace lyx
310
311 #include "moc_GuiBranches.cpp"