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