]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBranches.C
35f5c3ad779ca2afc4bff34f5e71d0b8d9d839e1
[lyx.git] / src / frontends / qt4 / QBranches.C
1 /**
2  * \file QBranches.C
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 "QBranches.h"
15
16 #include "GuiApplication.h"
17 #include "validators.h"
18 #include "qt_helpers.h"
19
20 #include "bufferparams.h"
21 #include "controllers/ControlDocument.h"
22
23 #include "support/lstrings.h"
24
25 #include <QTreeWidget>
26 #include <QTreeWidgetItem>
27 #include <QPixmap>
28 #include <QIcon>
29 #include <QColor>
30 #include <QColorDialog>
31
32
33 namespace lyx {
34 namespace frontend {
35
36
37 QBranches::QBranches(QWidget * parent, Qt::WFlags f)
38         : QWidget(parent, f)
39 {
40         setupUi(this);
41         branchesTW->setColumnCount(2);
42         branchesTW->headerItem()->setText(0, qt_("Branch"));
43         branchesTW->headerItem()->setText(1, qt_("Activated"));
44 }
45
46 void QBranches::update(BufferParams const & params)
47 {
48         branchlist_ = params.branchlist();
49         update();
50 }
51
52 void QBranches::update()
53 {
54         // store the selected branch
55         QTreeWidgetItem * item = branchesTW->currentItem();
56         QString sel_branch;
57         if (item != 0)
58                 sel_branch = item->text(0);
59
60         branchesTW->clear();
61
62         BranchList::const_iterator it = branchlist_.begin();
63         BranchList::const_iterator const end = branchlist_.end();
64         for (; it != end; ++it) {
65                 QTreeWidgetItem * newItem =
66                         new QTreeWidgetItem(branchesTW);
67
68                 QString const bname = toqstr(it->getBranch());
69                 newItem->setText(0, bname);
70
71                 QString const sel = it->getSelected() ? qt_("Yes") : qt_("No");
72                 newItem->setText(1, sel);
73
74                 QColor const itemcolor = rgb2qcolor(it->getColor());
75                 if (itemcolor.isValid()) {
76                         QPixmap coloritem(32, 32);
77                         coloritem.fill(itemcolor);
78                         newItem->setIcon(0, QIcon(coloritem));
79                 }
80                 // restore selected branch
81                 if (bname == sel_branch)
82                         branchesTW->setItemSelected(newItem, true);
83         }
84         // emit signal
85         changed();
86 }
87
88 void QBranches::apply(BufferParams & params) const
89 {
90         params.branchlist() = branchlist_;
91 }
92
93 void QBranches::on_addBranchPB_pressed()
94 {
95         QString const new_branch = newBranchLE->text();
96         if (!new_branch.isEmpty()) {
97                 branchlist_.add(qstring_to_ucs4(new_branch));
98                 newBranchLE->clear();
99                 update();
100         }
101 }
102
103
104 void QBranches::on_removePB_pressed()
105 {
106         QTreeWidgetItem * selItem =
107                 branchesTW->currentItem();
108         QString sel_branch;
109         if (selItem != 0)
110                 sel_branch = selItem->text(0);
111         if (!sel_branch.isEmpty()) {
112                 branchlist_.remove(qstring_to_ucs4(sel_branch));
113                 newBranchLE->clear();
114                 update();
115         }
116 }
117
118
119 void QBranches::on_activatePB_pressed()
120 {
121         toggleBranch(branchesTW->currentItem());
122 }
123
124
125 void QBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
126 {
127         toggleBranch(item);
128 }
129
130
131 void QBranches::toggleBranch(QTreeWidgetItem * item)
132 {
133         if (item == 0)
134                 return;
135
136         QString sel_branch = item->text(0);
137         if (!sel_branch.isEmpty()) {
138                 bool const selected = item->text(1) == qt_("Yes");
139                 Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
140                 if (branch && branch->setSelected(!selected)) {
141                         newBranchLE->clear();
142                         update();
143                 }
144         }
145 }
146
147
148 void QBranches::on_colorPB_clicked()
149 {
150         QTreeWidgetItem * selItem =
151                 branchesTW->currentItem();
152         QString sel_branch;
153         if (selItem != 0)
154                 sel_branch = selItem->text(0);
155         if (!sel_branch.isEmpty()) {
156                 docstring current_branch = qstring_to_ucs4(sel_branch);
157                 Branch * branch =
158                         branchlist_.find(current_branch);
159                 if (!branch)
160                         return;
161
162                 QColor const initial = rgb2qcolor(branch->getColor());
163                 QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget()));
164                 if (ncol.isValid()){
165                         // add the color to the branchlist
166                         branch->setColor(fromqstr(ncol.name()));
167                         newBranchLE->clear();
168                         update();
169                 }
170         }
171 }
172
173 } // namespace frontend
174 } // namespace lyx
175
176 #include "QBranches_moc.cpp"