]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiBranches.cpp
* GuiBranches.{cpp,h}: UI polishment.
[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         // emit signal
116         changed();
117 }
118
119
120 void GuiBranches::apply(BufferParams & params) const
121 {
122         params.branchlist() = branchlist_;
123 }
124
125
126 void GuiBranches::on_addBranchPB_pressed()
127 {
128         QString const new_branch = newBranchLE->text();
129         if (!new_branch.isEmpty()) {
130                 branchlist_.add(qstring_to_ucs4(new_branch));
131                 newBranchLE->clear();
132                 updateView();
133         }
134 }
135
136
137 void GuiBranches::on_removePB_pressed()
138 {
139         QTreeWidgetItem * selItem = branchesTW->currentItem();
140         QString sel_branch;
141         if (selItem != 0)
142                 sel_branch = selItem->text(0);
143         if (!sel_branch.isEmpty()) {
144                 branchlist_.remove(qstring_to_ucs4(sel_branch));
145                 newBranchLE->clear();
146                 updateView();
147         }
148 }
149
150
151 void GuiBranches::on_renamePB_pressed()
152 {
153         QTreeWidgetItem * selItem = branchesTW->currentItem();
154         QString sel_branch;
155         if (selItem != 0)
156                 sel_branch = selItem->text(0);
157         if (!sel_branch.isEmpty()) {
158                 docstring newname;
159                 docstring const oldname = qstring_to_ucs4(sel_branch);
160                 bool success = false;
161                 if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
162                         if (newname.empty() || oldname == newname)
163                                 return;
164                         if (branchlist_.find(newname)) {
165                                 docstring text = support::bformat(
166                                         _("A branch with the name \"%1$s\" already exists.\n"
167                                           "Do you want to merge branch \"%2$s\" with that one?"),
168                                         newname, oldname);
169                                 if (frontend::Alert::prompt(_("Branch already exists"),
170                                           text, 0, 1, _("&Merge"), _("&Cancel")) == 0)
171                                         success = branchlist_.rename(oldname, newname, true);
172                         } else
173                                 success = branchlist_.rename(oldname, newname);
174                         newBranchLE->clear();
175                         updateView();
176                 }
177                 if (!success)
178                         Alert::error(_("Renaming failed"), 
179                               _("The branch could not be renamed."));
180                 else
181                         // emit signal
182                         renameBranches(oldname, newname);
183         }
184 }
185
186
187 void GuiBranches::on_activatePB_pressed()
188 {
189         toggleBranch(branchesTW->currentItem());
190 }
191
192
193 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
194 {
195         if (col < 2)
196                 toggleBranch(item);
197         else
198                 toggleColor(item);
199 }
200
201
202 void GuiBranches::on_branchesTW_itemSelectionChanged()
203 {
204         bool const have_sel =
205                 !branchesTW->selectedItems().isEmpty();
206         removePB->setEnabled(have_sel);
207         renamePB->setEnabled(have_sel);
208 }
209
210
211 void GuiBranches::toggleBranch(QTreeWidgetItem * item)
212 {
213         if (item == 0)
214                 return;
215
216         QString sel_branch = item->text(0);
217         if (sel_branch.isEmpty())
218                 return;
219
220         bool const selected = (item->text(1) == qt_("Yes"));
221         Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
222         if (branch && branch->setSelected(!selected)) {
223                 newBranchLE->clear();
224                 updateView();
225         }
226 }
227
228
229 void GuiBranches::on_colorPB_clicked()
230 {
231         toggleColor(branchesTW->currentItem());
232 }
233
234
235 void GuiBranches::toggleColor(QTreeWidgetItem * item)
236 {
237         if (item == 0)
238                 return;
239
240         QString sel_branch = item->text(0);
241         if (sel_branch.isEmpty())
242                 return;
243
244         docstring current_branch = qstring_to_ucs4(sel_branch);
245         Branch * branch = branchlist_.find(current_branch);
246         if (!branch)
247                 return;
248
249         QColor const initial = rgb2qcolor(branch->color());
250         QColor ncol = QColorDialog::getColor(initial, qApp->focusWidget());
251         if (!ncol.isValid())
252                 return;
253
254         // add the color to the branchlist
255         branch->setColor(fromqstr(ncol.name()));
256         newBranchLE->clear();
257         updateView();
258 }
259
260
261 void GuiBranches::on_unknownPB_pressed()
262 {
263         undef_->branchesLW->clear();
264         for (int i = 0; i != unknown_branches_.count(); ++i) {
265                 if (branchesTW->findItems(unknown_branches_[i], Qt::MatchExactly, 0).empty())
266                         undef_->branchesLW->addItem(unknown_branches_[i]);
267         }
268         unknownBranchSelChanged();
269         undef_->exec();
270 }
271
272
273 void GuiBranches::addUnknown()
274 {
275         QList<QListWidgetItem *> selItems =
276                 undef_->branchesLW->selectedItems();
277         
278         QList<QListWidgetItem *>::const_iterator it = selItems.begin();
279         for (; it != selItems.end() ; ++it) {
280                 QListWidgetItem const * new_branch = *it;
281                 if (new_branch) {
282                         branchlist_.add(qstring_to_ucs4(new_branch->text()));
283                         updateView();
284                 }
285         }
286 }
287
288
289 void GuiBranches::addAllUnknown()
290 {
291         undef_->branchesLW->selectAll();
292         addUnknown();
293 }
294
295
296 void GuiBranches::unknownBranchSelChanged()
297 {
298         undef_->addSelectedPB->setEnabled(
299                 !undef_->branchesLW->selectedItems().isEmpty());
300 }
301
302
303 } // namespace frontend
304 } // namespace lyx
305
306 #include "moc_GuiBranches.cpp"