2 * \file GuiBranches.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "GuiBranches.h"
16 #include "ColorCache.h"
17 #include "GuiApplication.h"
18 #include "Validator.h"
19 #include "qt_helpers.h"
21 #include "ui_BranchesUnknownUi.h"
23 #include "frontends/alert.h"
26 #include "BufferParams.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
31 #include <QListWidget>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
37 #include <QColorDialog>
44 GuiBranches::GuiBranches(QWidget * parent)
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);
54 undef_ = new BranchesUnknownDialog(this);
55 undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
56 undef_bc_.setCancel(undef_->cancelPB);
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()));
72 void GuiBranches::update(BufferParams const & params)
74 branchlist_ = params.branchlist();
79 void GuiBranches::updateView()
81 // store the selected branch
82 QTreeWidgetItem * item = branchesTW->currentItem();
85 sel_branch = item->text(0);
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);
94 QString const bname = toqstr(it->branch());
95 newItem->setText(0, bname);
96 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
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));
104 // restore selected branch
105 if (bname == sel_branch) {
106 branchesTW->setCurrentItem(newItem);
107 branchesTW->setItemSelected(newItem, true);
110 unknownPB->setEnabled(!unknown_branches_.isEmpty());
116 void GuiBranches::apply(BufferParams & params) const
118 params.branchlist() = branchlist_;
122 void GuiBranches::on_addBranchPB_pressed()
124 QString const new_branch = newBranchLE->text();
125 if (!new_branch.isEmpty()) {
126 branchlist_.add(qstring_to_ucs4(new_branch));
127 newBranchLE->clear();
133 void GuiBranches::on_removePB_pressed()
135 QTreeWidgetItem * selItem = branchesTW->currentItem();
138 sel_branch = selItem->text(0);
139 if (!sel_branch.isEmpty()) {
140 branchlist_.remove(qstring_to_ucs4(sel_branch));
141 newBranchLE->clear();
147 void GuiBranches::on_renamePB_pressed()
149 QTreeWidgetItem * selItem = branchesTW->currentItem();
152 sel_branch = selItem->text(0);
153 if (!sel_branch.isEmpty()) {
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?"),
163 if (frontend::Alert::prompt(_("Branch already exists"),
164 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
165 success = branchlist_.rename(oldname, newname, true);
167 success = branchlist_.rename(oldname, newname);
168 newBranchLE->clear();
172 Alert::error(_("Renaming failed"),
173 _("The branch could not be renamed."));
176 renameBranches(oldname, newname);
181 void GuiBranches::on_activatePB_pressed()
183 toggleBranch(branchesTW->currentItem());
187 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
196 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
201 QString sel_branch = item->text(0);
202 if (sel_branch.isEmpty())
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();
214 void GuiBranches::on_colorPB_clicked()
216 toggleColor(branchesTW->currentItem());
220 void GuiBranches::toggleColor(QTreeWidgetItem * item)
225 QString sel_branch = item->text(0);
226 if (sel_branch.isEmpty())
229 docstring current_branch = qstring_to_ucs4(sel_branch);
230 Branch * branch = branchlist_.find(current_branch);
234 QColor const initial = rgb2qcolor(branch->color());
235 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
239 // add the color to the branchlist
240 branch->setColor(fromqstr(ncol.name()));
241 newBranchLE->clear();
246 void GuiBranches::on_unknownPB_pressed()
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]);
253 unknownBranchSelChanged();
258 void GuiBranches::addUnknown()
260 QList<QListWidgetItem *> selItems =
261 undef_->branchesLW->selectedItems();
263 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
264 for (; it != selItems.end() ; ++it) {
265 QListWidgetItem const * new_branch = *it;
267 branchlist_.add(qstring_to_ucs4(new_branch->text()));
274 void GuiBranches::addAllUnknown()
276 undef_->branchesLW->selectAll();
281 void GuiBranches::unknownBranchSelChanged()
283 undef_->addSelectedPB->setEnabled(
284 !undef_->branchesLW->selectedItems().isEmpty());
288 } // namespace frontend
291 #include "moc_GuiBranches.cpp"