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->headerItem()->setText(3, qt_("Filename Suffix"));
53 branchesTW->setSortingEnabled(true);
54 branchesTW->resizeColumnToContents(1);
55 branchesTW->resizeColumnToContents(2);
57 undef_ = new BranchesUnknownDialog(this);
58 undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
59 undef_bc_.setCancel(undef_->cancelPB);
61 connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
62 this, SLOT(unknownBranchSelChanged()));
63 connect(undef_->addSelectedPB, SIGNAL(clicked()),
64 this, SLOT(addUnknown()));
65 connect(undef_->addAllPB, SIGNAL(clicked()),
66 this, SLOT(addAllUnknown()));
67 connect(undef_->addSelectedPB, SIGNAL(clicked()),
68 undef_, SLOT(accept()));
69 connect(undef_->addAllPB, SIGNAL(clicked()),
70 undef_, SLOT(accept()));
71 connect(undef_->cancelPB, SIGNAL(clicked()),
72 undef_, SLOT(reject()));
75 void GuiBranches::update(BufferParams const & params)
77 branchlist_ = params.branchlist();
82 void GuiBranches::updateView()
84 // store the selected branch
85 QTreeWidgetItem * item = branchesTW->currentItem();
88 sel_branch = item->text(0);
92 BranchList::const_iterator it = branchlist_.begin();
93 BranchList::const_iterator const end = branchlist_.end();
94 for (; it != end; ++it) {
95 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
97 QString const bname = toqstr(it->branch());
98 newItem->setText(0, bname);
99 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
101 QColor const itemcolor = rgb2qcolor(it->color());
102 if (itemcolor.isValid()) {
103 QPixmap coloritem(30, 10);
104 coloritem.fill(itemcolor);
105 newItem->setIcon(2, QIcon(coloritem));
107 newItem->setText(3, it->hasFilenameSuffix() ? qt_("Yes") : qt_("No"));
108 // restore selected branch
109 if (bname == sel_branch) {
110 branchesTW->setCurrentItem(newItem);
111 branchesTW->setItemSelected(newItem, true);
114 unknownPB->setEnabled(!unknown_branches_.isEmpty());
115 bool const have_sel =
116 !branchesTW->selectedItems().isEmpty();
117 removePB->setEnabled(have_sel);
118 renamePB->setEnabled(have_sel);
119 colorPB->setEnabled(have_sel);
120 activatePB->setEnabled(have_sel);
121 suffixPB->setEnabled(have_sel);
127 void GuiBranches::apply(BufferParams & params) const
129 params.branchlist() = branchlist_;
133 void GuiBranches::on_addBranchPB_pressed()
135 QString const new_branch = newBranchLE->text();
136 if (!new_branch.isEmpty()) {
137 branchlist_.add(qstring_to_ucs4(new_branch));
138 newBranchLE->clear();
144 void GuiBranches::on_removePB_pressed()
146 QTreeWidgetItem * selItem = branchesTW->currentItem();
149 sel_branch = selItem->text(0);
150 if (!sel_branch.isEmpty()) {
151 branchlist_.remove(qstring_to_ucs4(sel_branch));
152 newBranchLE->clear();
158 void GuiBranches::on_renamePB_pressed()
160 QTreeWidgetItem * selItem = branchesTW->currentItem();
163 sel_branch = selItem->text(0);
164 if (!sel_branch.isEmpty()) {
166 docstring const oldname = qstring_to_ucs4(sel_branch);
167 bool success = false;
168 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
169 if (newname.empty() || oldname == newname)
171 if (branchlist_.find(newname)) {
172 docstring text = support::bformat(
173 _("A branch with the name \"%1$s\" already exists.\n"
174 "Do you want to merge branch \"%2$s\" with that one?"),
176 if (frontend::Alert::prompt(_("Branch already exists"),
177 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
178 success = branchlist_.rename(oldname, newname, true);
180 success = branchlist_.rename(oldname, newname);
181 newBranchLE->clear();
185 Alert::error(_("Renaming failed"),
186 _("The branch could not be renamed."));
189 renameBranches(oldname, newname);
195 void GuiBranches::on_activatePB_pressed()
197 toggleBranch(branchesTW->currentItem());
201 void GuiBranches::on_suffixPB_pressed()
203 toggleSuffix(branchesTW->currentItem());
207 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
218 void GuiBranches::on_branchesTW_itemSelectionChanged()
220 bool const have_sel =
221 !branchesTW->selectedItems().isEmpty();
222 removePB->setEnabled(have_sel);
223 renamePB->setEnabled(have_sel);
224 colorPB->setEnabled(have_sel);
225 activatePB->setEnabled(have_sel);
226 suffixPB->setEnabled(have_sel);
230 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
235 QString sel_branch = item->text(0);
236 if (sel_branch.isEmpty())
239 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
240 if (branch && branch->setSelected(!branch->isSelected())) {
241 newBranchLE->clear();
247 void GuiBranches::on_colorPB_clicked()
249 toggleColor(branchesTW->currentItem());
253 void GuiBranches::toggleColor(QTreeWidgetItem * item)
258 QString sel_branch = item->text(0);
259 if (sel_branch.isEmpty())
262 docstring current_branch = qstring_to_ucs4(sel_branch);
263 Branch * branch = branchlist_.find(current_branch);
267 QColor const initial = rgb2qcolor(branch->color());
268 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
272 // add the color to the branchlist
273 branch->setColor(fromqstr(ncol.name()));
274 newBranchLE->clear();
279 void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
284 QString sel_branch = item->text(0);
285 if (sel_branch.isEmpty())
288 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
290 branch->setFilenameSuffix(!branch->hasFilenameSuffix());
291 newBranchLE->clear();
297 void GuiBranches::on_unknownPB_pressed()
299 undef_->branchesLW->clear();
300 for (int i = 0; i != unknown_branches_.count(); ++i) {
301 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
302 undef_->branchesLW->addItem(unknown_branches_[i]);
304 unknownBranchSelChanged();
309 void GuiBranches::addUnknown()
311 QList<QListWidgetItem *> selItems =
312 undef_->branchesLW->selectedItems();
314 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
315 for (; it != selItems.end() ; ++it) {
316 QListWidgetItem const * new_branch = *it;
318 branchlist_.add(qstring_to_ucs4(new_branch->text()));
325 void GuiBranches::addAllUnknown()
327 undef_->branchesLW->selectAll();
332 void GuiBranches::unknownBranchSelChanged()
334 undef_->addSelectedPB->setEnabled(
335 !undef_->branchesLW->selectedItems().isEmpty());
339 } // namespace frontend
342 #include "moc_GuiBranches.cpp"