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