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);
120 void GuiBranches::apply(BufferParams & params) const
122 params.branchlist() = branchlist_;
126 void GuiBranches::on_addBranchPB_pressed()
128 QString const new_branch = newBranchLE->text();
129 if (!new_branch.isEmpty()) {
130 branchlist_.add(qstring_to_ucs4(new_branch));
131 newBranchLE->clear();
137 void GuiBranches::on_removePB_pressed()
139 QTreeWidgetItem * selItem = branchesTW->currentItem();
142 sel_branch = selItem->text(0);
143 if (!sel_branch.isEmpty()) {
144 branchlist_.remove(qstring_to_ucs4(sel_branch));
145 newBranchLE->clear();
151 void GuiBranches::on_renamePB_pressed()
153 QTreeWidgetItem * selItem = branchesTW->currentItem();
156 sel_branch = selItem->text(0);
157 if (!sel_branch.isEmpty()) {
159 docstring const oldname = qstring_to_ucs4(sel_branch);
160 bool success = false;
161 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
162 if (newname.empty() || oldname == newname)
164 if (branchlist_.find(newname)) {
165 docstring text = support::bformat(
166 _("A branch with the name \"%1$s\" already exists.\n"
167 "Do you want to merge branch \"%2$s\" with that one?"),
169 if (frontend::Alert::prompt(_("Branch already exists"),
170 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
171 success = branchlist_.rename(oldname, newname, true);
173 success = branchlist_.rename(oldname, newname);
174 newBranchLE->clear();
178 Alert::error(_("Renaming failed"),
179 _("The branch could not be renamed."));
182 renameBranches(oldname, newname);
187 void GuiBranches::on_activatePB_pressed()
189 toggleBranch(branchesTW->currentItem());
193 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
202 void GuiBranches::on_branchesTW_itemSelectionChanged()
204 bool const have_sel =
205 !branchesTW->selectedItems().isEmpty();
206 removePB->setEnabled(have_sel);
207 renamePB->setEnabled(have_sel);
211 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
216 QString sel_branch = item->text(0);
217 if (sel_branch.isEmpty())
220 bool const selected = (item->text(1) == qt_("Yes"));
221 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
222 if (branch && branch->setSelected(!selected)) {
223 newBranchLE->clear();
229 void GuiBranches::on_colorPB_clicked()
231 toggleColor(branchesTW->currentItem());
235 void GuiBranches::toggleColor(QTreeWidgetItem * item)
240 QString sel_branch = item->text(0);
241 if (sel_branch.isEmpty())
244 docstring current_branch = qstring_to_ucs4(sel_branch);
245 Branch * branch = branchlist_.find(current_branch);
249 QColor const initial = rgb2qcolor(branch->color());
250 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
254 // add the color to the branchlist
255 branch->setColor(fromqstr(ncol.name()));
256 newBranchLE->clear();
261 void GuiBranches::on_unknownPB_pressed()
263 undef_->branchesLW->clear();
264 for (int i = 0; i != unknown_branches_.count(); ++i) {
265 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
266 undef_->branchesLW->addItem(unknown_branches_[i]);
268 unknownBranchSelChanged();
273 void GuiBranches::addUnknown()
275 QList<QListWidgetItem *> selItems =
276 undef_->branchesLW->selectedItems();
278 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
279 for (; it != selItems.end() ; ++it) {
280 QListWidgetItem const * new_branch = *it;
282 branchlist_.add(qstring_to_ucs4(new_branch->text()));
289 void GuiBranches::addAllUnknown()
291 undef_->branchesLW->selectAll();
296 void GuiBranches::unknownBranchSelChanged()
298 undef_->addSelectedPB->setEnabled(
299 !undef_->branchesLW->selectedItems().isEmpty());
303 } // namespace frontend
306 #include "moc_GuiBranches.cpp"