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());
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);
122 void GuiBranches::apply(BufferParams & params) const
124 params.branchlist() = branchlist_;
128 void GuiBranches::on_addBranchPB_pressed()
130 QString const new_branch = newBranchLE->text();
131 if (!new_branch.isEmpty()) {
132 branchlist_.add(qstring_to_ucs4(new_branch));
133 newBranchLE->clear();
139 void GuiBranches::on_removePB_pressed()
141 QTreeWidgetItem * selItem = branchesTW->currentItem();
144 sel_branch = selItem->text(0);
145 if (!sel_branch.isEmpty()) {
146 branchlist_.remove(qstring_to_ucs4(sel_branch));
147 newBranchLE->clear();
153 void GuiBranches::on_renamePB_pressed()
155 QTreeWidgetItem * selItem = branchesTW->currentItem();
158 sel_branch = selItem->text(0);
159 if (!sel_branch.isEmpty()) {
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)
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?"),
171 if (frontend::Alert::prompt(_("Branch already exists"),
172 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
173 success = branchlist_.rename(oldname, newname, true);
175 success = branchlist_.rename(oldname, newname);
176 newBranchLE->clear();
180 Alert::error(_("Renaming failed"),
181 _("The branch could not be renamed."));
184 renameBranches(oldname, newname);
189 void GuiBranches::on_activatePB_pressed()
191 toggleBranch(branchesTW->currentItem());
195 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
204 void GuiBranches::on_branchesTW_itemSelectionChanged()
206 bool const have_sel =
207 !branchesTW->selectedItems().isEmpty();
208 removePB->setEnabled(have_sel);
209 renamePB->setEnabled(have_sel);
210 colorPB->setEnabled(have_sel);
211 activatePB->setEnabled(have_sel);
215 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
220 QString sel_branch = item->text(0);
221 if (sel_branch.isEmpty())
224 bool const selected = (item->text(1) == qt_("Yes"));
225 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
226 if (branch && branch->setSelected(!selected)) {
227 newBranchLE->clear();
233 void GuiBranches::on_colorPB_clicked()
235 toggleColor(branchesTW->currentItem());
239 void GuiBranches::toggleColor(QTreeWidgetItem * item)
244 QString sel_branch = item->text(0);
245 if (sel_branch.isEmpty())
248 docstring current_branch = qstring_to_ucs4(sel_branch);
249 Branch * branch = branchlist_.find(current_branch);
253 QColor const initial = rgb2qcolor(branch->color());
254 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
258 // add the color to the branchlist
259 branch->setColor(fromqstr(ncol.name()));
260 newBranchLE->clear();
265 void GuiBranches::on_unknownPB_pressed()
267 undef_->branchesLW->clear();
268 for (int i = 0; i != unknown_branches_.count(); ++i) {
269 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
270 undef_->branchesLW->addItem(unknown_branches_[i]);
272 unknownBranchSelChanged();
277 void GuiBranches::addUnknown()
279 QList<QListWidgetItem *> selItems =
280 undef_->branchesLW->selectedItems();
282 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
283 for (; it != selItems.end() ; ++it) {
284 QListWidgetItem const * new_branch = *it;
286 branchlist_.add(qstring_to_ucs4(new_branch->text()));
293 void GuiBranches::addAllUnknown()
295 undef_->branchesLW->selectAll();
300 void GuiBranches::unknownBranchSelChanged()
302 undef_->addSelectedPB->setEnabled(
303 !undef_->branchesLW->selectedItems().isEmpty());
307 } // namespace frontend
310 #include "moc_GuiBranches.cpp"