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>
45 GuiBranches::GuiBranches(QWidget * parent)
49 branchesTW->setColumnCount(3);
50 branchesTW->headerItem()->setText(0, qt_("Branch"));
51 branchesTW->headerItem()->setText(1, qt_("Activated"));
52 branchesTW->headerItem()->setText(2, qt_("Color"));
53 branchesTW->headerItem()->setText(3, qt_("Filename Suffix"));
54 branchesTW->setSortingEnabled(true);
55 branchesTW->resizeColumnToContents(1);
56 branchesTW->resizeColumnToContents(2);
58 undef_ = new BranchesUnknownDialog(this);
59 undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
60 undef_bc_.setCancel(undef_->cancelPB);
62 connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
63 this, SLOT(unknownBranchSelChanged()));
64 connect(undef_->addSelectedPB, SIGNAL(clicked()),
65 this, SLOT(addUnknown()));
66 connect(undef_->addAllPB, SIGNAL(clicked()),
67 this, SLOT(addAllUnknown()));
68 connect(undef_->addSelectedPB, SIGNAL(clicked()),
69 undef_, SLOT(accept()));
70 connect(undef_->addAllPB, SIGNAL(clicked()),
71 undef_, SLOT(accept()));
72 connect(undef_->cancelPB, SIGNAL(clicked()),
73 undef_, SLOT(reject()));
75 newBranchLE->installEventFilter(this);
76 newBranchLE->setValidator(new NoNewLineValidator(newBranchLE));
80 bool GuiBranches::eventFilter(QObject * obj, QEvent * event)
82 QEvent::Type etype = event->type();
83 if (etype == QEvent::KeyPress
85 && addBranchPB->isEnabled()) {
86 QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
87 int const keyPressed = keyEvent->key();
88 Qt::KeyboardModifiers const keyModifiers = keyEvent->modifiers();
90 if (keyPressed == Qt::Key_Enter || keyPressed == Qt::Key_Return) {
92 on_addBranchPB_pressed();
93 } else if (keyModifiers == Qt::ControlModifier
94 || keyModifiers == Qt::KeypadModifier
95 || keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
96 on_addBranchPB_pressed();
97 newBranchLE->clearFocus();
104 return QObject::eventFilter(obj, event);
108 void GuiBranches::update(BufferParams const & params)
110 branchlist_ = params.branchlist();
115 void GuiBranches::updateView()
117 // store the selected branch
118 QTreeWidgetItem * item = branchesTW->currentItem();
121 sel_branch = item->text(0);
125 BranchList::const_iterator it = branchlist_.begin();
126 BranchList::const_iterator const end = branchlist_.end();
127 for (; it != end; ++it) {
128 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
130 QString const bname = toqstr(it->branch());
131 newItem->setText(0, bname);
132 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
134 QColor const itemcolor = rgb2qcolor(it->color());
135 if (itemcolor.isValid()) {
136 QPixmap coloritem(30, 10);
137 coloritem.fill(itemcolor);
138 newItem->setIcon(2, QIcon(coloritem));
140 newItem->setText(3, it->hasFileNameSuffix() ? qt_("Yes") : qt_("No"));
141 // restore selected branch
142 if (bname == sel_branch) {
143 branchesTW->setCurrentItem(newItem);
144 branchesTW->setItemSelected(newItem, true);
147 unknownPB->setEnabled(!unknown_branches_.isEmpty());
148 bool const have_sel =
149 !branchesTW->selectedItems().isEmpty();
150 removePB->setEnabled(have_sel);
151 renamePB->setEnabled(have_sel);
152 colorPB->setEnabled(have_sel);
153 activatePB->setEnabled(have_sel);
154 suffixPB->setEnabled(have_sel);
160 void GuiBranches::apply(BufferParams & params) const
162 params.branchlist() = branchlist_;
166 void GuiBranches::on_newBranchLE_textChanged(QString)
168 QString const new_branch = newBranchLE->text();
169 addBranchPB->setEnabled(!new_branch.isEmpty());
173 void GuiBranches::on_addBranchPB_pressed()
175 QString const new_branch = newBranchLE->text();
176 branchlist_.add(qstring_to_ucs4(new_branch));
177 newBranchLE->clear();
178 addBranchPB->setEnabled(false);
183 void GuiBranches::on_removePB_pressed()
185 QTreeWidgetItem * selItem = branchesTW->currentItem();
188 sel_branch = selItem->text(0);
189 if (!sel_branch.isEmpty()) {
190 branchlist_.remove(qstring_to_ucs4(sel_branch));
191 newBranchLE->clear();
197 void GuiBranches::on_renamePB_pressed()
199 QTreeWidgetItem * selItem = branchesTW->currentItem();
202 sel_branch = selItem->text(0);
203 if (!sel_branch.isEmpty()) {
205 docstring const oldname = qstring_to_ucs4(sel_branch);
206 bool success = false;
207 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
208 if (newname.empty() || oldname == newname)
210 if (branchlist_.find(newname)) {
211 docstring text = support::bformat(
212 _("A branch with the name \"%1$s\" already exists.\n"
213 "Do you want to merge branch \"%2$s\" with that one?"),
215 if (frontend::Alert::prompt(_("Branch already exists"),
216 text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
217 success = branchlist_.rename(oldname, newname, true);
219 success = branchlist_.rename(oldname, newname);
220 newBranchLE->clear();
224 Alert::error(_("Renaming failed"),
225 _("The branch could not be renamed."));
228 renameBranches(oldname, newname);
234 void GuiBranches::on_activatePB_pressed()
236 toggleBranch(branchesTW->currentItem());
240 void GuiBranches::on_suffixPB_pressed()
242 toggleSuffix(branchesTW->currentItem());
246 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
257 void GuiBranches::on_branchesTW_itemSelectionChanged()
259 bool const have_sel =
260 !branchesTW->selectedItems().isEmpty();
261 removePB->setEnabled(have_sel);
262 renamePB->setEnabled(have_sel);
263 colorPB->setEnabled(have_sel);
264 activatePB->setEnabled(have_sel);
265 suffixPB->setEnabled(have_sel);
269 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
274 QString sel_branch = item->text(0);
275 if (sel_branch.isEmpty())
278 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
279 if (branch && branch->setSelected(!branch->isSelected())) {
280 newBranchLE->clear();
286 void GuiBranches::on_colorPB_clicked()
288 toggleColor(branchesTW->currentItem());
292 void GuiBranches::toggleColor(QTreeWidgetItem * item)
297 QString sel_branch = item->text(0);
298 if (sel_branch.isEmpty())
301 docstring current_branch = qstring_to_ucs4(sel_branch);
302 Branch * branch = branchlist_.find(current_branch);
306 QColor const initial = rgb2qcolor(branch->color());
307 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
311 // add the color to the branchlist
312 branch->setColor(fromqstr(ncol.name()));
313 newBranchLE->clear();
318 void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
323 QString sel_branch = item->text(0);
324 if (sel_branch.isEmpty())
327 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
329 branch->setFileNameSuffix(!branch->hasFileNameSuffix());
330 newBranchLE->clear();
336 void GuiBranches::on_unknownPB_pressed()
338 undef_->branchesLW->clear();
339 for (int i = 0; i != unknown_branches_.count(); ++i) {
340 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
341 undef_->branchesLW->addItem(unknown_branches_[i]);
343 unknownBranchSelChanged();
348 void GuiBranches::addUnknown()
350 QList<QListWidgetItem *> selItems =
351 undef_->branchesLW->selectedItems();
353 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
354 for (; it != selItems.end() ; ++it) {
355 QListWidgetItem const * new_branch = *it;
357 branchlist_.add(qstring_to_ucs4(new_branch->text()));
364 void GuiBranches::addAllUnknown()
366 undef_->branchesLW->selectAll();
371 void GuiBranches::unknownBranchSelChanged()
373 undef_->addSelectedPB->setEnabled(
374 !undef_->branchesLW->selectedItems().isEmpty());
378 } // namespace frontend
381 #include "moc_GuiBranches.cpp"