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