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);
55 undef_ = new BranchesUnknownDialog(this);
56 undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
57 undef_bc_.setCancel(undef_->cancelPB);
59 connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
60 this, SLOT(unknownBranchSelChanged()));
61 connect(undef_->addSelectedPB, SIGNAL(clicked()),
62 this, SLOT(addUnknown()));
63 connect(undef_->addAllPB, SIGNAL(clicked()),
64 this, SLOT(addAllUnknown()));
65 connect(undef_->addSelectedPB, SIGNAL(clicked()),
66 undef_, SLOT(accept()));
67 connect(undef_->addAllPB, SIGNAL(clicked()),
68 undef_, SLOT(accept()));
69 connect(undef_->cancelPB, SIGNAL(clicked()),
70 undef_, SLOT(reject()));
73 void GuiBranches::update(BufferParams const & params)
75 branchlist_ = params.branchlist();
80 void GuiBranches::updateView()
82 // store the selected branch
83 QTreeWidgetItem * item = branchesTW->currentItem();
86 sel_branch = item->text(0);
90 BranchList::const_iterator it = branchlist_.begin();
91 BranchList::const_iterator const end = branchlist_.end();
92 for (; it != end; ++it) {
93 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
95 QString const bname = toqstr(it->branch());
96 newItem->setText(0, bname);
97 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
99 QColor const itemcolor = rgb2qcolor(it->color());
100 if (itemcolor.isValid()) {
101 QPixmap coloritem(30, 10);
102 coloritem.fill(itemcolor);
103 newItem->setIcon(2, QIcon(coloritem));
105 newItem->setText(3, it->hasFilenameSuffix() ? qt_("Yes") : qt_("No"));
106 // restore selected branch
107 if (bname == sel_branch) {
108 branchesTW->setCurrentItem(newItem);
109 branchesTW->setItemSelected(newItem, true);
112 unknownPB->setEnabled(!unknown_branches_.isEmpty());
113 bool const have_sel =
114 !branchesTW->selectedItems().isEmpty();
115 removePB->setEnabled(have_sel);
116 renamePB->setEnabled(have_sel);
117 colorPB->setEnabled(have_sel);
118 activatePB->setEnabled(have_sel);
119 suffixPB->setEnabled(have_sel);
125 void GuiBranches::apply(BufferParams & params) const
127 params.branchlist() = branchlist_;
131 void GuiBranches::on_addBranchPB_pressed()
133 QString const new_branch = newBranchLE->text();
134 if (!new_branch.isEmpty()) {
135 branchlist_.add(qstring_to_ucs4(new_branch));
136 newBranchLE->clear();
142 void GuiBranches::on_removePB_pressed()
144 QTreeWidgetItem * selItem = branchesTW->currentItem();
147 sel_branch = selItem->text(0);
148 if (!sel_branch.isEmpty()) {
149 branchlist_.remove(qstring_to_ucs4(sel_branch));
150 newBranchLE->clear();
156 void GuiBranches::on_renamePB_pressed()
158 QTreeWidgetItem * selItem = branchesTW->currentItem();
161 sel_branch = selItem->text(0);
162 if (!sel_branch.isEmpty()) {
164 docstring const oldname = qstring_to_ucs4(sel_branch);
165 bool success = false;
166 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
167 if (newname.empty() || oldname == newname)
169 if (branchlist_.find(newname)) {
170 docstring text = support::bformat(
171 _("A branch with the name \"%1$s\" already exists.\n"
172 "Do you want to merge branch \"%2$s\" with that one?"),
174 if (frontend::Alert::prompt(_("Branch already exists"),
175 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
176 success = branchlist_.rename(oldname, newname, true);
178 success = branchlist_.rename(oldname, newname);
179 newBranchLE->clear();
183 Alert::error(_("Renaming failed"),
184 _("The branch could not be renamed."));
187 renameBranches(oldname, newname);
193 void GuiBranches::on_activatePB_pressed()
195 toggleBranch(branchesTW->currentItem());
199 void GuiBranches::on_suffixPB_pressed()
201 toggleSuffix(branchesTW->currentItem());
205 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
216 void GuiBranches::on_branchesTW_itemSelectionChanged()
218 bool const have_sel =
219 !branchesTW->selectedItems().isEmpty();
220 removePB->setEnabled(have_sel);
221 renamePB->setEnabled(have_sel);
222 colorPB->setEnabled(have_sel);
223 activatePB->setEnabled(have_sel);
224 suffixPB->setEnabled(have_sel);
228 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
233 QString sel_branch = item->text(0);
234 if (sel_branch.isEmpty())
237 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
238 if (branch && branch->setSelected(!branch->isSelected())) {
239 newBranchLE->clear();
245 void GuiBranches::on_colorPB_clicked()
247 toggleColor(branchesTW->currentItem());
251 void GuiBranches::toggleColor(QTreeWidgetItem * item)
256 QString sel_branch = item->text(0);
257 if (sel_branch.isEmpty())
260 docstring current_branch = qstring_to_ucs4(sel_branch);
261 Branch * branch = branchlist_.find(current_branch);
265 QColor const initial = rgb2qcolor(branch->color());
266 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
270 // add the color to the branchlist
271 branch->setColor(fromqstr(ncol.name()));
272 newBranchLE->clear();
277 void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
282 QString sel_branch = item->text(0);
283 if (sel_branch.isEmpty())
286 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
288 branch->setFilenameSuffix(!branch->hasFilenameSuffix());
289 newBranchLE->clear();
295 void GuiBranches::on_unknownPB_pressed()
297 undef_->branchesLW->clear();
298 for (int i = 0; i != unknown_branches_.count(); ++i) {
299 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
300 undef_->branchesLW->addItem(unknown_branches_[i]);
302 unknownBranchSelChanged();
307 void GuiBranches::addUnknown()
309 QList<QListWidgetItem *> selItems =
310 undef_->branchesLW->selectedItems();
312 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
313 for (; it != selItems.end() ; ++it) {
314 QListWidgetItem const * new_branch = *it;
316 branchlist_.add(qstring_to_ucs4(new_branch->text()));
323 void GuiBranches::addAllUnknown()
325 undef_->branchesLW->selectAll();
330 void GuiBranches::unknownBranchSelChanged()
332 undef_->addSelectedPB->setEnabled(
333 !undef_->branchesLW->selectedItems().isEmpty());
337 } // namespace frontend
340 #include "moc_GuiBranches.cpp"