]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBranches.cpp
thrid attempt at changing the naming pattern of the intermediated 'mocced' files
[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 "ColorCache.h"
17 #include "GuiApplication.h"
18 #include "Validator.h"
19 #include "qt_helpers.h"
20
21 #include "BufferParams.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)
38         : QWidget(parent)
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         updateView();
52 }
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 = new QTreeWidgetItem(branchesTW);
69
70                 QString const bname = toqstr(it->branch());
71                 newItem->setText(0, bname);
72                 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
73
74                 QColor const itemcolor = rgb2qcolor(it->color());
75                 if (itemcolor.isValid()) {
76                         QPixmap coloritem(30, 10);
77                         coloritem.fill(itemcolor);
78                         newItem->setIcon(2, QIcon(coloritem));
79                 }
80                 // restore selected branch
81                 if (bname == sel_branch) {
82                         branchesTW->setCurrentItem(newItem);
83                         branchesTW->setItemSelected(newItem, true);
84                 }
85         }
86         // emit signal
87         changed();
88 }
89
90
91 void GuiBranches::apply(BufferParams & params) const
92 {
93         params.branchlist() = branchlist_;
94 }
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                 updateView();
104         }
105 }
106
107
108 void GuiBranches::on_removePB_pressed()
109 {
110         QTreeWidgetItem * selItem = branchesTW->currentItem();
111         QString sel_branch;
112         if (selItem != 0)
113                 sel_branch = selItem->text(0);
114         if (!sel_branch.isEmpty()) {
115                 branchlist_.remove(qstring_to_ucs4(sel_branch));
116                 newBranchLE->clear();
117                 updateView();
118         }
119 }
120
121
122 void GuiBranches::on_activatePB_pressed()
123 {
124         toggleBranch(branchesTW->currentItem());
125 }
126
127
128 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
129 {
130         if (col < 2)
131                 toggleBranch(item);
132         else
133                 toggleColor(item);
134 }
135
136
137 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
138 {
139         if (item == 0)
140                 return;
141
142         QString sel_branch = item->text(0);
143         if (sel_branch.isEmpty())
144                 return;
145
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 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                 return;
169
170         docstring current_branch = qstring_to_ucs4(sel_branch);
171         Branch * branch = branchlist_.find(current_branch);
172         if (!branch)
173                 return;
174
175         QColor const initial = rgb2qcolor(branch->color());
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 "moc_GuiBranches.cpp"