]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBranches.C
* Painter.h:
[lyx.git] / src / frontends / qt4 / QBranches.C
1 /**
2  * \file QBranches.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QBranches.h"
15
16 #include "GuiApplication.h"
17 #include "validators.h"
18 #include "qt_helpers.h"
19
20 #include "bufferparams.h"
21 #include "controllers/ControlDocument.h"
22
23 #include "support/lstrings.h"
24
25 #include <QTreeWidget>
26 #include <QTreeWidgetItem>
27 #include <QPixmap>
28 #include <QIcon>
29 #include <QColor>
30 #include <QColorDialog>
31
32 using std::string;
33
34 namespace lyx {
35 namespace frontend {
36
37
38 QBranches::QBranches(QWidget * parent, Qt::WFlags f)
39         : QWidget(parent, f)
40 {
41         setupUi(this);
42         branchesTW->setColumnCount(2);
43         branchesTW->headerItem()->setText(0, qt_("Branch"));
44         branchesTW->headerItem()->setText(1, qt_("Activated"));
45 }
46
47 void QBranches::update(BufferParams const & params)
48 {
49         branchlist_ = params.branchlist();
50         update();
51 }
52
53 void QBranches::update()
54 {
55         // store the selected branch
56         QTreeWidgetItem * item = branchesTW->currentItem();
57         QString sel_branch;
58         if (item != 0)
59                 sel_branch = item->text(0);
60
61         branchesTW->clear();
62
63         BranchList::const_iterator it = branchlist_.begin();
64         BranchList::const_iterator const end = branchlist_.end();
65         for (; it != end; ++it) {
66                 QTreeWidgetItem * newItem =
67                         new QTreeWidgetItem(branchesTW);
68
69                 QString const bname = toqstr(it->getBranch());
70                 newItem->setText(0, bname);
71
72                 QString const sel = it->getSelected() ? qt_("Yes") : qt_("No");
73                 newItem->setText(1, sel);
74
75                 QColor const itemcolor = rgb2qcolor(it->getColor());
76                 if (itemcolor.isValid()) {
77                         QPixmap coloritem(32, 32);
78                         coloritem.fill(itemcolor);
79                         newItem->setIcon(0, QIcon(coloritem));
80                 }
81                 // restore selected branch
82                 if (bname == sel_branch)
83                         branchesTW->setItemSelected(newItem, true);
84         }
85         // emit signal
86         changed();
87 }
88
89 void QBranches::apply(BufferParams & params) const
90 {
91         params.branchlist() = branchlist_;
92 }
93
94 void QBranches::on_addBranchPB_pressed()
95 {
96         QString const new_branch = newBranchLE->text();
97         if (!new_branch.isEmpty()) {
98                 branchlist_.add(fromqstr(new_branch));
99                 newBranchLE->clear();
100                 update();
101         }
102 }
103
104
105 void QBranches::on_removePB_pressed()
106 {
107         QTreeWidgetItem * selItem =
108                 branchesTW->currentItem();
109         QString sel_branch;
110         if (selItem != 0)
111                 sel_branch = selItem->text(0);
112         if (!sel_branch.isEmpty()) {
113                 branchlist_.remove(fromqstr(sel_branch));
114                 newBranchLE->clear();
115                 update();
116         }
117 }
118
119
120 void QBranches::on_activatePB_pressed()
121 {
122         toggleBranch(branchesTW->currentItem());
123 }
124
125
126 void QBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
127 {
128         toggleBranch(item);
129 }
130
131
132 void QBranches::toggleBranch(QTreeWidgetItem * item)
133 {
134         if (item == 0)
135                 return;
136
137         QString sel_branch = item->text(0);
138         if (!sel_branch.isEmpty()) {
139                 bool const selected = item->text(1) == qt_("Yes");
140                 Branch * branch = branchlist_.find(fromqstr(sel_branch));
141                 if (branch && branch->setSelected(!selected)) {
142                         newBranchLE->clear();
143                         update();
144                 }
145         }
146 }
147
148
149 void QBranches::on_colorPB_clicked()
150 {
151         QTreeWidgetItem * selItem =
152                 branchesTW->currentItem();
153         QString sel_branch;
154         if (selItem != 0)
155                 sel_branch = selItem->text(0);
156         if (!sel_branch.isEmpty()) {
157                 string current_branch = fromqstr(sel_branch);
158                 Branch * branch =
159                         branchlist_.find(current_branch);
160                 if (!branch)
161                         return;
162
163                 QColor const initial = rgb2qcolor(branch->getColor());
164                 QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget()));
165                 if (ncol.isValid()){
166                         // add the color to the branchlist
167                         branch->setColor(fromqstr(ncol.name()));
168                         newBranchLE->clear();
169                         update();
170                 }
171         }
172 }
173
174 } // namespace frontend
175 } // namespace lyx
176
177 #include "QBranches_moc.cpp"