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