]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBranches.cpp
move our stuff off the Q* namespace
[lyx.git] / src / frontends / qt4 / GuiBranches.cpp
1 /**
2  * \file GuiBranches.cpp
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 "GuiBranches.h"
15
16 #include "GuiApplication.h"
17 #include "Validator.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
33 namespace lyx {
34 namespace frontend {
35
36
37 GuiBranches::GuiBranches(QWidget * parent, Qt::WFlags f)
38         : QWidget(parent, f)
39 {
40         setupUi(this);
41         branchesTW->setColumnCount(3);
42         branchesTW->headerItem()->setText(0, qt_("Branch"));
43         branchesTW->headerItem()->setText(1, qt_("Activated"));
44         branchesTW->headerItem()->setText(2, qt_("Color"));
45         branchesTW->setSortingEnabled(true);
46 }
47
48 void GuiBranches::update(BufferParams const & params)
49 {
50         branchlist_ = params.branchlist();
51         update();
52 }
53
54 void GuiBranches::update()
55 {
56         // store the selected branch
57         QTreeWidgetItem * item = branchesTW->currentItem();
58         QString sel_branch;
59         if (item != 0)
60                 sel_branch = item->text(0);
61
62         branchesTW->clear();
63
64         BranchList::const_iterator it = branchlist_.begin();
65         BranchList::const_iterator const end = branchlist_.end();
66         for (; it != end; ++it) {
67                 QTreeWidgetItem * newItem =
68                         new QTreeWidgetItem(branchesTW);
69
70                 QString const bname = toqstr(it->getBranch());
71                 newItem->setText(0, bname);
72
73                 QString const sel = it->getSelected() ? qt_("Yes") : qt_("No");
74                 newItem->setText(1, sel);
75
76                 QColor const itemcolor = rgb2qcolor(it->getColor());
77                 if (itemcolor.isValid()) {
78                         QPixmap coloritem(30, 10);
79                         coloritem.fill(itemcolor);
80                         newItem->setIcon(2, QIcon(coloritem));
81                 }
82                 // restore selected branch
83                 if (bname == sel_branch) {
84                         branchesTW->setCurrentItem(newItem);
85                         branchesTW->setItemSelected(newItem, true);
86                 }
87         }
88         // emit signal
89         changed();
90 }
91
92 void GuiBranches::apply(BufferParams & params) const
93 {
94         params.branchlist() = branchlist_;
95 }
96
97 void GuiBranches::on_addBranchPB_pressed()
98 {
99         QString const new_branch = newBranchLE->text();
100         if (!new_branch.isEmpty()) {
101                 branchlist_.add(qstring_to_ucs4(new_branch));
102                 newBranchLE->clear();
103                 update();
104         }
105 }
106
107
108 void GuiBranches::on_removePB_pressed()
109 {
110         QTreeWidgetItem * selItem =
111                 branchesTW->currentItem();
112         QString sel_branch;
113         if (selItem != 0)
114                 sel_branch = selItem->text(0);
115         if (!sel_branch.isEmpty()) {
116                 branchlist_.remove(qstring_to_ucs4(sel_branch));
117                 newBranchLE->clear();
118                 update();
119         }
120 }
121
122
123 void GuiBranches::on_activatePB_pressed()
124 {
125         toggleBranch(branchesTW->currentItem());
126 }
127
128
129 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
130 {
131         if (col < 2)
132                 toggleBranch(item);
133         else
134                 toggleColor(item);
135 }
136
137
138 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
139 {
140         if (item == 0)
141                 return;
142
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(qstring_to_ucs4(sel_branch));
147                 if (branch && branch->setSelected(!selected)) {
148                         newBranchLE->clear();
149                         update();
150                 }
151         }
152 }
153
154
155 void GuiBranches::on_colorPB_clicked()
156 {
157         toggleColor(branchesTW->currentItem());
158 }
159
160
161 void GuiBranches::toggleColor(QTreeWidgetItem * item)
162 {
163         if (item == 0)
164                 return;
165
166         QString sel_branch = item->text(0);
167         if (!sel_branch.isEmpty()) {
168                 docstring current_branch = qstring_to_ucs4(sel_branch);
169                 Branch * branch =
170                         branchlist_.find(current_branch);
171                 if (!branch)
172                         return;
173
174                 QColor const initial = rgb2qcolor(branch->getColor());
175                 QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget()));
176                 if (ncol.isValid()){
177                         // add the color to the branchlist
178                         branch->setColor(fromqstr(ncol.name()));
179                         newBranchLE->clear();
180                         update();
181                 }
182         }
183 }
184
185 } // namespace frontend
186 } // namespace lyx
187
188 #include "GuiBranches_moc.cpp"