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 "QBranches.h"
16 #include "lcolorcache.h"
17 #include "validators.h"
18 #include "qt_helpers.h"
20 #include "bufferparams.h"
21 #include "controllers/ControlDocument.h"
23 #include "support/lstrings.h"
25 #include <QTreeWidget>
26 #include <QTreeWidgetItem>
30 #include <QColorDialog>
38 QBranches::QBranches(QWidget * parent, Qt::WFlags f)
42 branchesTW->setColumnCount(3);
43 branchesTW->headerItem()->setText(0, qt_("Name"));
44 branchesTW->headerItem()->setText(1, qt_("Activated"));
45 branchesTW->headerItem()->setText(2, qt_("Color"));
48 QBranches::~QBranches()
52 void QBranches::update(BufferParams const & params)
54 branchlist_ = params.branchlist();
58 void QBranches::update()
61 // store the selected branch
62 QTreeWidgetItem * item =
63 branchesTW->currentItem();
66 sel_branch = item->text(0);
70 BranchList::const_iterator it = branchlist_.begin();
71 BranchList::const_iterator const end = branchlist_.end();
72 for (; it != end; ++it) {
73 QTreeWidgetItem * newItem =
74 new QTreeWidgetItem(branchesTW);
76 QString const bname = toqstr(it->getBranch());
77 newItem->setText(0, bname);
79 QString const sel = it->getSelected() ? qt_("Yes") : qt_("No");
80 newItem->setText(1, sel);
82 QColor const itemcolor = rgb2qcolor(it->getColor());
83 if (itemcolor.isValid()) {
84 QPixmap coloritem(30, 10);
85 coloritem.fill(itemcolor);
86 newItem->setIcon(2, QIcon(coloritem));
88 // restore selected branch
89 if (bname == sel_branch)
90 branchesTW->setItemSelected(newItem, true);
95 void QBranches::apply(BufferParams & params) const
97 params.branchlist() = branchlist_;
100 void QBranches::on_addBranchPB_pressed()
102 QString const new_branch = newBranchLE->text();
103 if (!new_branch.isEmpty()) {
104 branchlist_.add(fromqstr(new_branch));
105 newBranchLE->clear();
111 void QBranches::on_removePB_pressed()
113 QTreeWidgetItem * selItem =
114 branchesTW->currentItem();
117 sel_branch = selItem->text(0);
118 if (!sel_branch.isEmpty()) {
119 branchlist_.remove(fromqstr(sel_branch));
120 newBranchLE->clear();
126 void QBranches::on_activatePB_pressed()
128 toggleBranch(branchesTW->currentItem());
132 void QBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
138 void QBranches::toggleBranch(QTreeWidgetItem * item)
143 QString sel_branch = item->text(0);
144 if (!sel_branch.isEmpty()) {
145 bool const selected = item->text(1) == qt_("Yes");
146 Branch * branch = branchlist_.find(fromqstr(sel_branch));
147 if (branch && branch->setSelected(!selected)) {
148 newBranchLE->clear();
155 void QBranches::on_colorPB_clicked()
157 QTreeWidgetItem * selItem =
158 branchesTW->currentItem();
161 sel_branch = selItem->text(0);
162 if (!sel_branch.isEmpty()) {
163 string current_branch = fromqstr(sel_branch);
165 branchlist_.find(current_branch);
169 QColor const initial = rgb2qcolor(branch->getColor());
170 QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget()));
172 // add the color to the branchlist
173 branch->setColor(fromqstr(ncol.name()));
174 newBranchLE->clear();
180 } // namespace frontend