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