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"
32 #include <QListWidget>
33 #include <QTreeWidget>
34 #include <QTreeWidgetItem>
38 #include <QColorDialog>
48 GuiBranches::GuiBranches(QWidget * parent)
52 branchesTW->setColumnCount(3);
53 branchesTW->headerItem()->setText(0, qt_("Branch"));
54 branchesTW->headerItem()->setText(1, qt_("Activated"));
55 branchesTW->headerItem()->setText(2, qt_("Color"));
56 branchesTW->headerItem()->setText(3, qt_("Filename Suffix"));
57 branchesTW->setSortingEnabled(true);
58 branchesTW->resizeColumnToContents(1);
59 branchesTW->resizeColumnToContents(2);
61 undef_ = new BranchesUnknownDialog(this);
62 undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
63 undef_bc_.setCancel(undef_->cancelPB);
65 connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
66 this, SLOT(unknownBranchSelChanged()));
67 connect(undef_->addSelectedPB, SIGNAL(clicked()),
68 this, SLOT(addUnknown()));
69 connect(undef_->addAllPB, SIGNAL(clicked()),
70 this, SLOT(addAllUnknown()));
71 connect(undef_->addSelectedPB, SIGNAL(clicked()),
72 undef_, SLOT(accept()));
73 connect(undef_->addAllPB, SIGNAL(clicked()),
74 undef_, SLOT(accept()));
75 connect(undef_->cancelPB, SIGNAL(clicked()),
76 undef_, SLOT(reject()));
78 newBranchLE->installEventFilter(this);
79 newBranchLE->setValidator(new NoNewLineValidator(newBranchLE));
83 bool GuiBranches::eventFilter(QObject * obj, QEvent * event)
85 QEvent::Type etype = event->type();
86 if (etype == QEvent::KeyPress
88 && addBranchPB->isEnabled()) {
89 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
90 int const keyPressed = keyEvent->key();
91 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
93 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
95 on_addBranchPB_pressed();
96 } else if (keyModifiers == Qt::ControlModifier
97 || keyModifiers == Qt::KeypadModifier
98 || keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
99 on_addBranchPB_pressed();
100 newBranchLE->clearFocus();
107 return QObject::eventFilter(obj, event);
111 void GuiBranches::update(BufferParams const & params)
113 branchlist_ = params.branchlist();
118 void GuiBranches::updateView()
120 // store the selected branch
121 QTreeWidgetItem * item = branchesTW->currentItem();
124 sel_branch = item->text(0);
128 BranchList::const_iterator it = branchlist_.begin();
129 BranchList::const_iterator const end = branchlist_.end();
130 for (; it != end; ++it) {
131 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
133 QString const bname = toqstr(it->branch());
134 newItem->setText(0, bname);
135 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
137 QColor const itemcolor = rgb2qcolor(it->color());
138 if (itemcolor.isValid()) {
139 QPixmap coloritem(30, 10);
140 coloritem.fill(itemcolor);
141 newItem->setIcon(2, QIcon(coloritem));
143 newItem->setText(3, it->hasFileNameSuffix() ? qt_("Yes") : qt_("No"));
144 // restore selected branch
145 if (bname == sel_branch) {
146 branchesTW->setCurrentItem(newItem);
147 newItem->setSelected(true);
150 unknownPB->setEnabled(!unknown_branches_.isEmpty());
151 bool const have_sel =
152 !branchesTW->selectedItems().isEmpty();
153 removePB->setEnabled(have_sel);
154 renamePB->setEnabled(have_sel);
155 colorPB->setEnabled(have_sel);
156 activatePB->setEnabled(have_sel);
157 suffixPB->setEnabled(have_sel);
163 void GuiBranches::apply(BufferParams & params) const
165 params.branchlist() = branchlist_;
169 void GuiBranches::on_newBranchLE_textChanged(QString)
171 QString const new_branch = newBranchLE->text();
172 addBranchPB->setEnabled(!new_branch.isEmpty());
176 void GuiBranches::on_addBranchPB_pressed()
178 QString const new_branch = newBranchLE->text();
179 branchlist_.add(qstring_to_ucs4(new_branch));
180 newBranchLE->clear();
181 addBranchPB->setEnabled(false);
186 void GuiBranches::on_removePB_pressed()
188 QTreeWidgetItem * selItem = branchesTW->currentItem();
191 sel_branch = selItem->text(0);
192 if (!sel_branch.isEmpty()) {
193 branchlist_.remove(qstring_to_ucs4(sel_branch));
194 newBranchLE->clear();
200 void GuiBranches::on_renamePB_pressed()
202 QTreeWidgetItem * selItem = branchesTW->currentItem();
205 sel_branch = selItem->text(0);
206 if (!sel_branch.isEmpty()) {
208 docstring const oldname = qstring_to_ucs4(sel_branch);
209 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
210 if (newname.empty() || oldname == newname)
212 bool success = false;
213 if (branchlist_.find(newname)) {
214 docstring text = support::bformat(
215 _("A branch with the name \"%1$s\" already exists.\n"
216 "Do you want to merge branch \"%2$s\" with that one?"),
218 if (frontend::Alert::prompt(_("Branch already exists"),
219 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
220 success = branchlist_.rename(oldname, newname, true);
222 success = branchlist_.rename(oldname, newname);
223 newBranchLE->clear();
227 Alert::error(_("Renaming failed"),
228 _("The branch could not be renamed."));
231 renameBranches(oldname, newname);
237 void GuiBranches::on_activatePB_pressed()
239 toggleBranch(branchesTW->currentItem());
243 void GuiBranches::on_suffixPB_pressed()
245 toggleSuffix(branchesTW->currentItem());
249 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
260 void GuiBranches::on_branchesTW_itemSelectionChanged()
262 bool const have_sel =
263 !branchesTW->selectedItems().isEmpty();
264 removePB->setEnabled(have_sel);
265 renamePB->setEnabled(have_sel);
266 colorPB->setEnabled(have_sel);
267 activatePB->setEnabled(have_sel);
268 suffixPB->setEnabled(have_sel);
272 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
277 QString sel_branch = item->text(0);
278 if (sel_branch.isEmpty())
281 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
282 if (branch && branch->setSelected(!branch->isSelected())) {
283 newBranchLE->clear();
289 void GuiBranches::on_colorPB_clicked()
291 toggleColor(branchesTW->currentItem());
295 void GuiBranches::toggleColor(QTreeWidgetItem * item)
300 QString sel_branch = item->text(0);
301 if (sel_branch.isEmpty())
304 docstring current_branch = qstring_to_ucs4(sel_branch);
305 Branch * branch = branchlist_.find(current_branch);
309 QColor const initial = rgb2qcolor(branch->color());
310 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
314 // add the color to the branchlist
315 branch->setColor(fromqstr(ncol.name()));
316 newBranchLE->clear();
321 void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
326 QString sel_branch = item->text(0);
327 if (sel_branch.isEmpty())
330 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
332 branch->setFileNameSuffix(!branch->hasFileNameSuffix());
333 newBranchLE->clear();
339 void GuiBranches::on_unknownPB_pressed()
341 undef_->branchesLW->clear();
342 for (int i = 0; i != unknown_branches_.count(); ++i) {
343 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
344 undef_->branchesLW->addItem(unknown_branches_[i]);
346 unknownBranchSelChanged();
351 void GuiBranches::addUnknown()
353 QList<QListWidgetItem *> selItems =
354 undef_->branchesLW->selectedItems();
356 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
357 for (; it != selItems.end() ; ++it) {
358 QListWidgetItem const * new_branch = *it;
360 branchlist_.add(qstring_to_ucs4(new_branch->text()));
367 void GuiBranches::addAllUnknown()
369 undef_->branchesLW->selectAll();
374 void GuiBranches::unknownBranchSelChanged()
376 undef_->addSelectedPB->setEnabled(
377 !undef_->branchesLW->selectedItems().isEmpty());
381 } // namespace frontend
384 #include "moc_GuiBranches.cpp"