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()));
74 newBranchLE->setValidator(new NoNewLineValidator(newBranchLE));
77 void GuiBranches::update(BufferParams const & params)
79 branchlist_ = params.branchlist();
84 void GuiBranches::updateView()
86 // store the selected branch
87 QTreeWidgetItem * item = branchesTW->currentItem();
90 sel_branch = item->text(0);
94 BranchList::const_iterator it = branchlist_.begin();
95 BranchList::const_iterator const end = branchlist_.end();
96 for (; it != end; ++it) {
97 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
99 QString const bname = toqstr(it->branch());
100 newItem->setText(0, bname);
101 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
103 QColor const itemcolor = rgb2qcolor(it->color());
104 if (itemcolor.isValid()) {
105 QPixmap coloritem(30, 10);
106 coloritem.fill(itemcolor);
107 newItem->setIcon(2, QIcon(coloritem));
109 newItem->setText(3, it->hasFileNameSuffix() ? qt_("Yes") : qt_("No"));
110 // restore selected branch
111 if (bname == sel_branch) {
112 branchesTW->setCurrentItem(newItem);
113 branchesTW->setItemSelected(newItem, true);
116 unknownPB->setEnabled(!unknown_branches_.isEmpty());
117 bool const have_sel =
118 !branchesTW->selectedItems().isEmpty();
119 removePB->setEnabled(have_sel);
120 renamePB->setEnabled(have_sel);
121 colorPB->setEnabled(have_sel);
122 activatePB->setEnabled(have_sel);
123 suffixPB->setEnabled(have_sel);
129 void GuiBranches::apply(BufferParams & params) const
131 params.branchlist() = branchlist_;
135 void GuiBranches::on_addBranchPB_pressed()
137 QString const new_branch = newBranchLE->text();
138 if (!new_branch.isEmpty()) {
139 branchlist_.add(qstring_to_ucs4(new_branch));
140 newBranchLE->clear();
146 void GuiBranches::on_removePB_pressed()
148 QTreeWidgetItem * selItem = branchesTW->currentItem();
151 sel_branch = selItem->text(0);
152 if (!sel_branch.isEmpty()) {
153 branchlist_.remove(qstring_to_ucs4(sel_branch));
154 newBranchLE->clear();
160 void GuiBranches::on_renamePB_pressed()
162 QTreeWidgetItem * selItem = branchesTW->currentItem();
165 sel_branch = selItem->text(0);
166 if (!sel_branch.isEmpty()) {
168 docstring const oldname = qstring_to_ucs4(sel_branch);
169 bool success = false;
170 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
171 if (newname.empty() || oldname == newname)
173 if (branchlist_.find(newname)) {
174 docstring text = support::bformat(
175 _("A branch with the name \"%1$s\" already exists.\n"
176 "Do you want to merge branch \"%2$s\" with that one?"),
178 if (frontend::Alert::prompt(_("Branch already exists"),
179 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
180 success = branchlist_.rename(oldname, newname, true);
182 success = branchlist_.rename(oldname, newname);
183 newBranchLE->clear();
187 Alert::error(_("Renaming failed"),
188 _("The branch could not be renamed."));
191 renameBranches(oldname, newname);
197 void GuiBranches::on_activatePB_pressed()
199 toggleBranch(branchesTW->currentItem());
203 void GuiBranches::on_suffixPB_pressed()
205 toggleSuffix(branchesTW->currentItem());
209 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
220 void GuiBranches::on_branchesTW_itemSelectionChanged()
222 bool const have_sel =
223 !branchesTW->selectedItems().isEmpty();
224 removePB->setEnabled(have_sel);
225 renamePB->setEnabled(have_sel);
226 colorPB->setEnabled(have_sel);
227 activatePB->setEnabled(have_sel);
228 suffixPB->setEnabled(have_sel);
232 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
237 QString sel_branch = item->text(0);
238 if (sel_branch.isEmpty())
241 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
242 if (branch && branch->setSelected(!branch->isSelected())) {
243 newBranchLE->clear();
249 void GuiBranches::on_colorPB_clicked()
251 toggleColor(branchesTW->currentItem());
255 void GuiBranches::toggleColor(QTreeWidgetItem * item)
260 QString sel_branch = item->text(0);
261 if (sel_branch.isEmpty())
264 docstring current_branch = qstring_to_ucs4(sel_branch);
265 Branch * branch = branchlist_.find(current_branch);
269 QColor const initial = rgb2qcolor(branch->color());
270 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
274 // add the color to the branchlist
275 branch->setColor(fromqstr(ncol.name()));
276 newBranchLE->clear();
281 void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
286 QString sel_branch = item->text(0);
287 if (sel_branch.isEmpty())
290 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
292 branch->setFileNameSuffix(!branch->hasFileNameSuffix());
293 newBranchLE->clear();
299 void GuiBranches::on_unknownPB_pressed()
301 undef_->branchesLW->clear();
302 for (int i = 0; i != unknown_branches_.count(); ++i) {
303 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
304 undef_->branchesLW->addItem(unknown_branches_[i]);
306 unknownBranchSelChanged();
311 void GuiBranches::addUnknown()
313 QList<QListWidgetItem *> selItems =
314 undef_->branchesLW->selectedItems();
316 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
317 for (; it != selItems.end() ; ++it) {
318 QListWidgetItem const * new_branch = *it;
320 branchlist_.add(qstring_to_ucs4(new_branch->text()));
327 void GuiBranches::addAllUnknown()
329 undef_->branchesLW->selectAll();
334 void GuiBranches::unknownBranchSelChanged()
336 undef_->addSelectedPB->setEnabled(
337 !undef_->branchesLW->selectedItems().isEmpty());
341 } // namespace frontend
344 #include "moc_GuiBranches.cpp"