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