]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBranches.cpp
Ease the pain with unknown branches:
[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 "ui_BranchesUnknownUi.h"
22
23 #include "Buffer.h"
24 #include "BufferParams.h"
25
26 #include "support/lstrings.h"
27
28 #include <QListWidget>
29 #include <QTreeWidget>
30 #include <QTreeWidgetItem>
31 #include <QPixmap>
32 #include <QIcon>
33 #include <QColor>
34 #include <QColorDialog>
35
36
37 namespace lyx {
38 namespace frontend {
39
40
41 GuiBranches::GuiBranches(QWidget * parent)
42         : QWidget(parent)
43 {
44         setupUi(this);
45         branchesTW->setColumnCount(3);
46         branchesTW->headerItem()->setText(0, qt_("Branch"));
47         branchesTW->headerItem()->setText(1, qt_("Activated"));
48         branchesTW->headerItem()->setText(2, qt_("Color"));
49         branchesTW->setSortingEnabled(true);
50
51         undef_ = new BranchesUnknownDialog(this);
52         undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
53         undef_bc_.setCancel(undef_->cancelPB);
54
55         connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
56                 this, SLOT(unknownBranchSelChanged()));
57         connect(undef_->addSelectedPB, SIGNAL(clicked()),
58                 this, SLOT(addUnknown()));
59         connect(undef_->addAllPB, SIGNAL(clicked()),
60                 this, SLOT(addAllUnknown()));
61         connect(undef_->addSelectedPB, SIGNAL(clicked()),
62                 undef_, SLOT(accept()));
63         connect(undef_->addAllPB, SIGNAL(clicked()),
64                 undef_, SLOT(accept()));
65         connect(undef_->cancelPB, SIGNAL(clicked()),
66                 undef_, SLOT(reject()));
67 }
68
69 void GuiBranches::update(BufferParams const & params)
70 {
71         branchlist_ = params.branchlist();
72         updateView();
73 }
74
75
76 void GuiBranches::updateView()
77 {
78         // store the selected branch
79         QTreeWidgetItem * item = branchesTW->currentItem();
80         QString sel_branch;
81         if (item != 0)
82                 sel_branch = item->text(0);
83
84         branchesTW->clear();
85
86         BranchList::const_iterator it = branchlist_.begin();
87         BranchList::const_iterator const end = branchlist_.end();
88         for (; it != end; ++it) {
89                 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
90
91                 QString const bname = toqstr(it->branch());
92                 newItem->setText(0, bname);
93                 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
94
95                 QColor const itemcolor = rgb2qcolor(it->color());
96                 if (itemcolor.isValid()) {
97                         QPixmap coloritem(30, 10);
98                         coloritem.fill(itemcolor);
99                         newItem->setIcon(2, QIcon(coloritem));
100                 }
101                 // restore selected branch
102                 if (bname == sel_branch) {
103                         branchesTW->setCurrentItem(newItem);
104                         branchesTW->setItemSelected(newItem, true);
105                 }
106         }
107         unknownPB->setEnabled(!unknown_branches_.isEmpty());
108         // emit signal
109         changed();
110 }
111
112
113 void GuiBranches::apply(BufferParams & params) const
114 {
115         params.branchlist() = branchlist_;
116 }
117
118
119 void GuiBranches::on_addBranchPB_pressed()
120 {
121         QString const new_branch = newBranchLE->text();
122         if (!new_branch.isEmpty()) {
123                 branchlist_.add(qstring_to_ucs4(new_branch));
124                 newBranchLE->clear();
125                 updateView();
126         }
127 }
128
129
130 void GuiBranches::on_removePB_pressed()
131 {
132         QTreeWidgetItem * selItem = branchesTW->currentItem();
133         QString sel_branch;
134         if (selItem != 0)
135                 sel_branch = selItem->text(0);
136         if (!sel_branch.isEmpty()) {
137                 branchlist_.remove(qstring_to_ucs4(sel_branch));
138                 newBranchLE->clear();
139                 updateView();
140         }
141 }
142
143
144 void GuiBranches::on_activatePB_pressed()
145 {
146         toggleBranch(branchesTW->currentItem());
147 }
148
149
150 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
151 {
152         if (col < 2)
153                 toggleBranch(item);
154         else
155                 toggleColor(item);
156 }
157
158
159 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
160 {
161         if (item == 0)
162                 return;
163
164         QString sel_branch = item->text(0);
165         if (sel_branch.isEmpty())
166                 return;
167
168         bool const selected = (item->text(1) == qt_("Yes"));
169         Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
170         if (branch && branch->setSelected(!selected)) {
171                 newBranchLE->clear();
172                 updateView();
173         }
174 }
175
176
177 void GuiBranches::on_colorPB_clicked()
178 {
179         toggleColor(branchesTW->currentItem());
180 }
181
182
183 void GuiBranches::toggleColor(QTreeWidgetItem * item)
184 {
185         if (item == 0)
186                 return;
187
188         QString sel_branch = item->text(0);
189         if (sel_branch.isEmpty())
190                 return;
191
192         docstring current_branch = qstring_to_ucs4(sel_branch);
193         Branch * branch = branchlist_.find(current_branch);
194         if (!branch)
195                 return;
196
197         QColor const initial = rgb2qcolor(branch->color());
198         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
199         if (!ncol.isValid())
200                 return;
201
202         // add the color to the branchlist
203         branch->setColor(fromqstr(ncol.name()));
204         newBranchLE->clear();
205         updateView();
206 }
207
208
209 void GuiBranches::on_unknownPB_pressed()
210 {
211         undef_->branchesLW->clear();
212         for (int i = 0; i != unknown_branches_.count(); ++i) {
213                 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
214                         undef_->branchesLW->addItem(unknown_branches_[i]);
215         }
216         unknownBranchSelChanged();
217         undef_->exec();
218 }
219
220
221 void GuiBranches::addUnknown()
222 {
223         QList<QListWidgetItem *> selItems =
224                 undef_->branchesLW->selectedItems();
225         
226         QList<QListWidgetItem *>::const_iterator it = selItems.begin();
227         for (it ; it != selItems.end() ; ++it) {
228                 QListWidgetItem const * new_branch = *it;
229                 if (new_branch) {
230                         branchlist_.add(qstring_to_ucs4(new_branch->text()));
231                         updateView();
232                 }
233         }
234 }
235
236
237 void GuiBranches::addAllUnknown()
238 {
239         undef_->branchesLW->selectAll();
240         addUnknown();
241 }
242
243
244 void GuiBranches::unknownBranchSelChanged()
245 {
246         undef_->addSelectedPB->setEnabled(
247                 !undef_->branchesLW->selectedItems().isEmpty());
248 }
249
250
251 } // namespace frontend
252 } // namespace lyx
253
254 #include "moc_GuiBranches.cpp"