]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBranches.C
enable Font cache only for MacOSX and inline width() for other platform.
[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 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 signal
90         changed();
91 }
92
93 void QBranches::apply(BufferParams & params) const
94 {
95         params.branchlist() = branchlist_;
96 }
97
98 void QBranches::on_addBranchPB_pressed()
99 {
100         QString const new_branch = newBranchLE->text();
101         if (!new_branch.isEmpty()) {
102                 branchlist_.add(fromqstr(new_branch));
103                 newBranchLE->clear();
104                 update();
105         }
106 }
107
108
109 void QBranches::on_removePB_pressed()
110 {
111         QTreeWidgetItem * selItem =
112                 branchesTW->currentItem();
113         QString sel_branch;
114         if (selItem != 0)
115                 sel_branch = selItem->text(0);
116         if (!sel_branch.isEmpty()) {
117                 branchlist_.remove(fromqstr(sel_branch));
118                 newBranchLE->clear();
119                 update();
120         }
121 }
122
123
124 void QBranches::on_activatePB_pressed()
125 {
126         toggleBranch(branchesTW->currentItem());
127 }
128
129
130 void QBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int /*col*/)
131 {
132         toggleBranch(item);
133 }
134
135
136 void QBranches::toggleBranch(QTreeWidgetItem * item)
137 {
138         if (item == 0)
139                 return;
140
141         QString sel_branch = item->text(0);
142         if (!sel_branch.isEmpty()) {
143                 bool const selected = item->text(1) == qt_("Yes");
144                 Branch * branch = branchlist_.find(fromqstr(sel_branch));
145                 if (branch && branch->setSelected(!selected)) {
146                         newBranchLE->clear();
147                         update();
148                 }
149         }
150 }
151
152
153 void QBranches::on_colorPB_clicked()
154 {
155         QTreeWidgetItem * selItem =
156                 branchesTW->currentItem();
157         QString sel_branch;
158         if (selItem != 0)
159                 sel_branch = selItem->text(0);
160         if (!sel_branch.isEmpty()) {
161                 string current_branch = fromqstr(sel_branch);
162                 Branch * branch =
163                         branchlist_.find(current_branch);
164                 if (!branch)
165                         return;
166
167                 QColor const initial = rgb2qcolor(branch->getColor());
168                 QColor ncol(QColorDialog::getColor(initial, qApp->focusWidget()));
169                 if (ncol.isValid()){
170                         // add the color to the branchlist
171                         branch->setColor(fromqstr(ncol.name()));
172                         newBranchLE->clear();
173                         update();
174                 }
175         }
176 }
177
178 } // namespace frontend
179 } // namespace lyx
180
181 #include "QBranches_moc.cpp"