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