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