]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiBranches.cpp
9f73cd4c7c2a94ccc03d6366655e1a780860dd02
[features.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 "frontends/alert.h"
24
25 #include "Buffer.h"
26 #include "BufferParams.h"
27
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 #include <QListWidget>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
34 #include <QPixmap>
35 #include <QIcon>
36 #include <QColor>
37 #include <QColorDialog>
38
39
40 namespace lyx {
41 namespace frontend {
42
43
44 GuiBranches::GuiBranches(QWidget * parent)
45         : QWidget(parent)
46 {
47         setupUi(this);
48         branchesTW->setColumnCount(3);
49         branchesTW->headerItem()->setText(0, qt_("Branch"));
50         branchesTW->headerItem()->setText(1, qt_("Activated"));
51         branchesTW->headerItem()->setText(2, qt_("Color"));
52         branchesTW->setSortingEnabled(true);
53
54         undef_ = new BranchesUnknownDialog(this);
55         undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
56         undef_bc_.setCancel(undef_->cancelPB);
57
58         connect(undef_->branchesLW, SIGNAL(itemSelectionChanged()),
59                 this, SLOT(unknownBranchSelChanged()));
60         connect(undef_->addSelectedPB, SIGNAL(clicked()),
61                 this, SLOT(addUnknown()));
62         connect(undef_->addAllPB, SIGNAL(clicked()),
63                 this, SLOT(addAllUnknown()));
64         connect(undef_->addSelectedPB, SIGNAL(clicked()),
65                 undef_, SLOT(accept()));
66         connect(undef_->addAllPB, SIGNAL(clicked()),
67                 undef_, SLOT(accept()));
68         connect(undef_->cancelPB, SIGNAL(clicked()),
69                 undef_, SLOT(reject()));
70 }
71
72 void GuiBranches::update(BufferParams const & params)
73 {
74         branchlist_ = params.branchlist();
75         updateView();
76 }
77
78
79 void GuiBranches::updateView()
80 {
81         // store the selected branch
82         QTreeWidgetItem * item = branchesTW->currentItem();
83         QString sel_branch;
84         if (item != 0)
85                 sel_branch = item->text(0);
86
87         branchesTW->clear();
88
89         BranchList::const_iterator it = branchlist_.begin();
90         BranchList::const_iterator const end = branchlist_.end();
91         for (; it != end; ++it) {
92                 QTreeWidgetItem * newItem = new QTreeWidgetItem(branchesTW);
93
94                 QString const bname = toqstr(it->branch());
95                 newItem->setText(0, bname);
96                 newItem->setText(1, it->isSelected() ? qt_("Yes") : qt_("No"));
97
98                 QColor const itemcolor = rgb2qcolor(it->color());
99                 if (itemcolor.isValid()) {
100                         QPixmap coloritem(30, 10);
101                         coloritem.fill(itemcolor);
102                         newItem->setIcon(2, QIcon(coloritem));
103                 }
104                 // restore selected branch
105                 if (bname == sel_branch) {
106                         branchesTW->setCurrentItem(newItem);
107                         branchesTW->setItemSelected(newItem, true);
108                 }
109         }
110         unknownPB->setEnabled(!unknown_branches_.isEmpty());
111         bool const have_sel =
112                 !branchesTW->selectedItems().isEmpty();
113         removePB->setEnabled(have_sel);
114         renamePB->setEnabled(have_sel);
115         colorPB->setEnabled(have_sel);
116         activatePB->setEnabled(have_sel);
117         // emit signal
118         changed();
119 }
120
121
122 void GuiBranches::apply(BufferParams & params) const
123 {
124         params.branchlist() = branchlist_;
125 }
126
127
128 void GuiBranches::on_addBranchPB_pressed()
129 {
130         QString const new_branch = newBranchLE->text();
131         if (!new_branch.isEmpty()) {
132                 branchlist_.add(qstring_to_ucs4(new_branch));
133                 newBranchLE->clear();
134                 updateView();
135         }
136 }
137
138
139 void GuiBranches::on_removePB_pressed()
140 {
141         QTreeWidgetItem * selItem = branchesTW->currentItem();
142         QString sel_branch;
143         if (selItem != 0)
144                 sel_branch = selItem->text(0);
145         if (!sel_branch.isEmpty()) {
146                 branchlist_.remove(qstring_to_ucs4(sel_branch));
147                 newBranchLE->clear();
148                 updateView();
149         }
150 }
151
152
153 void GuiBranches::on_renamePB_pressed()
154 {
155         QTreeWidgetItem * selItem = branchesTW->currentItem();
156         QString sel_branch;
157         if (selItem != 0)
158                 sel_branch = selItem->text(0);
159         if (!sel_branch.isEmpty()) {
160                 docstring newname;
161                 docstring const oldname = qstring_to_ucs4(sel_branch);
162                 bool success = false;
163                 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
164                         if (newname.empty() || oldname == newname)
165                                 return;
166                         if (branchlist_.find(newname)) {
167                                 docstring text = support::bformat(
168                                         _("A branch with the name \"%1$s\" already exists.\n"
169                                           "Do you want to merge branch \"%2$s\" with that one?"),
170                                         newname, oldname);
171                                 if (frontend::Alert::prompt(_("Branch already exists"),
172                                           text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
173                                         success = branchlist_.rename(oldname, newname, true);
174                         } else
175                                 success = branchlist_.rename(oldname, newname);
176                         newBranchLE->clear();
177                         updateView();
178                 }
179                 if (!success)
180                         Alert::error(_("Renaming failed"), 
181                               _("The branch could not be renamed."));
182                 else
183                         // emit signal
184                         renameBranches(oldname, newname);
185         }
186 }
187
188
189 void GuiBranches::on_activatePB_pressed()
190 {
191         toggleBranch(branchesTW->currentItem());
192 }
193
194
195 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
196 {
197         if (col < 2)
198                 toggleBranch(item);
199         else
200                 toggleColor(item);
201 }
202
203
204 void GuiBranches::on_branchesTW_itemSelectionChanged()
205 {
206         bool const have_sel =
207                 !branchesTW->selectedItems().isEmpty();
208         removePB->setEnabled(have_sel);
209         renamePB->setEnabled(have_sel);
210         colorPB->setEnabled(have_sel);
211         activatePB->setEnabled(have_sel);
212 }
213
214
215 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
216 {
217         if (item == 0)
218                 return;
219
220         QString sel_branch = item->text(0);
221         if (sel_branch.isEmpty())
222                 return;
223
224         bool const selected = (item->text(1) == qt_("Yes"));
225         Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
226         if (branch && branch->setSelected(!selected)) {
227                 newBranchLE->clear();
228                 updateView();
229         }
230 }
231
232
233 void GuiBranches::on_colorPB_clicked()
234 {
235         toggleColor(branchesTW->currentItem());
236 }
237
238
239 void GuiBranches::toggleColor(QTreeWidgetItem * item)
240 {
241         if (item == 0)
242                 return;
243
244         QString sel_branch = item->text(0);
245         if (sel_branch.isEmpty())
246                 return;
247
248         docstring current_branch = qstring_to_ucs4(sel_branch);
249         Branch * branch = branchlist_.find(current_branch);
250         if (!branch)
251                 return;
252
253         QColor const initial = rgb2qcolor(branch->color());
254         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
255         if (!ncol.isValid())
256                 return;
257
258         // add the color to the branchlist
259         branch->setColor(fromqstr(ncol.name()));
260         newBranchLE->clear();
261         updateView();
262 }
263
264
265 void GuiBranches::on_unknownPB_pressed()
266 {
267         undef_->branchesLW->clear();
268         for (int i = 0; i != unknown_branches_.count(); ++i) {
269                 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
270                         undef_->branchesLW->addItem(unknown_branches_[i]);
271         }
272         unknownBranchSelChanged();
273         undef_->exec();
274 }
275
276
277 void GuiBranches::addUnknown()
278 {
279         QList<QListWidgetItem *> selItems =
280                 undef_->branchesLW->selectedItems();
281         
282         QList<QListWidgetItem *>::const_iterator it = selItems.begin();
283         for (; it != selItems.end() ; ++it) {
284                 QListWidgetItem const * new_branch = *it;
285                 if (new_branch) {
286                         branchlist_.add(qstring_to_ucs4(new_branch->text()));
287                         updateView();
288                 }
289         }
290 }
291
292
293 void GuiBranches::addAllUnknown()
294 {
295         undef_->branchesLW->selectAll();
296         addUnknown();
297 }
298
299
300 void GuiBranches::unknownBranchSelChanged()
301 {
302         undef_->addSelectedPB->setEnabled(
303                 !undef_->branchesLW->selectedItems().isEmpty());
304 }
305
306
307 } // namespace frontend
308 } // namespace lyx
309
310 #include "moc_GuiBranches.cpp"