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"
24 #include "BufferParams.h"
26 #include "support/lstrings.h"
28 #include <QListWidget>
29 #include <QTreeWidget>
30 #include <QTreeWidgetItem>
34 #include <QColorDialog>
41 GuiBranches::GuiBranches(QWidget * parent)
45 branchesTW->setColumnCount(3);
46 branchesTW->headerItem()->setText(0, qt_("Branch"));
47 branchesTW->headerItem()->setText(1, qt_("Activated"));
48 branchesTW->headerItem()->setText(2, qt_("Color"));
49 branchesTW->setSortingEnabled(true);
51 undef_ = new BranchesUnknownDialog(this);
52 undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
53 undef_bc_.setCancel(undef_->cancelPB);
55 connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
56 this, SLOT(unknownBranchSelChanged()));
57 connect(undef_->addSelectedPB, SIGNAL(clicked()),
58 this, SLOT(addUnknown()));
59 connect(undef_->addAllPB, SIGNAL(clicked()),
60 this, SLOT(addAllUnknown()));
61 connect(undef_->addSelectedPB, SIGNAL(clicked()),
62 undef_, SLOT(accept()));
63 connect(undef_->addAllPB, SIGNAL(clicked()),
64 undef_, SLOT(accept()));
65 connect(undef_->cancelPB, SIGNAL(clicked()),
66 undef_, SLOT(reject()));
69 void GuiBranches::update(BufferParams const & params)
71 branchlist_ = params.branchlist();
76 void GuiBranches::updateView()
78 // store the selected branch
79 QTreeWidgetItem * item = branchesTW->currentItem();
82 sel_branch = item->text(0);
86 BranchList::const_iterator it = branchlist_.begin();
87 BranchList::const_iterator const end = branchlist_.end();
88 for (; it != end; ++it) {
89 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
91 QString const bname = toqstr(it->branch());
92 newItem->setText(0, bname);
93 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
95 QColor const itemcolor = rgb2qcolor(it->color());
96 if (itemcolor.isValid()) {
97 QPixmap coloritem(30, 10);
98 coloritem.fill(itemcolor);
99 newItem->setIcon(2, QIcon(coloritem));
101 // restore selected branch
102 if (bname == sel_branch) {
103 branchesTW->setCurrentItem(newItem);
104 branchesTW->setItemSelected(newItem, true);
107 unknownPB->setEnabled(!unknown_branches_.isEmpty());
113 void GuiBranches::apply(BufferParams & params) const
115 params.branchlist() = branchlist_;
119 void GuiBranches::on_addBranchPB_pressed()
121 QString const new_branch = newBranchLE->text();
122 if (!new_branch.isEmpty()) {
123 branchlist_.add(qstring_to_ucs4(new_branch));
124 newBranchLE->clear();
130 void GuiBranches::on_removePB_pressed()
132 QTreeWidgetItem * selItem = branchesTW->currentItem();
135 sel_branch = selItem->text(0);
136 if (!sel_branch.isEmpty()) {
137 branchlist_.remove(qstring_to_ucs4(sel_branch));
138 newBranchLE->clear();
144 void GuiBranches::on_activatePB_pressed()
146 toggleBranch(branchesTW->currentItem());
150 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
159 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
164 QString sel_branch = item->text(0);
165 if (sel_branch.isEmpty())
168 bool const selected = (item->text(1) == qt_("Yes"));
169 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
170 if (branch && branch->setSelected(!selected)) {
171 newBranchLE->clear();
177 void GuiBranches::on_colorPB_clicked()
179 toggleColor(branchesTW->currentItem());
183 void GuiBranches::toggleColor(QTreeWidgetItem * item)
188 QString sel_branch = item->text(0);
189 if (sel_branch.isEmpty())
192 docstring current_branch = qstring_to_ucs4(sel_branch);
193 Branch * branch = branchlist_.find(current_branch);
197 QColor const initial = rgb2qcolor(branch->color());
198 QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
202 // add the color to the branchlist
203 branch->setColor(fromqstr(ncol.name()));
204 newBranchLE->clear();
209 void GuiBranches::on_unknownPB_pressed()
211 undef_->branchesLW->clear();
212 for (int i = 0; i != unknown_branches_.count(); ++i) {
213 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
214 undef_->branchesLW->addItem(unknown_branches_[i]);
216 unknownBranchSelChanged();
221 void GuiBranches::addUnknown()
223 QList<QListWidgetItem *> selItems =
224 undef_->branchesLW->selectedItems();
226 QList<QListWidgetItem *>::const_iterator it = selItems.begin();
227 for (it ; it != selItems.end() ; ++it) {
228 QListWidgetItem const * new_branch = *it;
230 branchlist_.add(qstring_to_ucs4(new_branch->text()));
237 void GuiBranches::addAllUnknown()
239 undef_->branchesLW->selectAll();
244 void GuiBranches::unknownBranchSelChanged()
246 undef_->addSelectedPB->setEnabled(
247 !undef_->branchesLW->selectedItems().isEmpty());
251 } // namespace frontend
254 #include "moc_GuiBranches.cpp"