]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QBibtex.C
Remove quite a few compiler warnings:
[lyx.git] / src / frontends / qt4 / QBibtex.C
1 /**
2  * \file QBibtex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Herbert Voß
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QBibtex.h"
16 #include "QBibtexDialog.h"
17 #include "ui/QBibtexAddUi.h"
18 #include "Qt2BC.h"
19 #include "qt_helpers.h"
20 #include "validators.h"
21
22 #include "lyxrc.h"
23
24 #include "controllers/ControlBibtex.h"
25
26 #include "support/filetools.h" // changeExtension
27 #include "support/lstrings.h"
28
29 #include <QPushButton>
30 #include <QListWidget>
31 #include <QCheckBox>
32
33
34 using lyx::support::changeExtension;
35 using lyx::support::split;
36 using lyx::support::trim;
37
38 using std::vector;
39 using std::string;
40
41 namespace lyx {
42 namespace frontend {
43
44 typedef QController<ControlBibtex, QView<QBibtexDialog> > base_class;
45
46 QBibtex::QBibtex(Dialog & parent)
47         : base_class(parent, _("BibTeX Bibliography"))
48 {
49 }
50
51
52 void QBibtex::build_dialog()
53 {
54         dialog_.reset(new QBibtexDialog(this));
55
56         bcview().setOK(dialog_->okPB);
57         bcview().setCancel(dialog_->closePB);
58         bcview().addReadOnly(dialog_->databaseLW);
59         bcview().addReadOnly(dialog_->stylePB);
60         bcview().addReadOnly(dialog_->styleCB);
61         bcview().addReadOnly(dialog_->bibtocCB);
62         bcview().addReadOnly(dialog_->addBibPB);
63         bcview().addReadOnly(dialog_->deletePB);
64 }
65
66
67 void QBibtex::update_contents()
68 {
69         PathValidator * path_validator =
70                 getPathValidator(dialog_->add_->bibED);
71         if (path_validator)
72                 path_validator->setChecker(kernel().docType(), lyxrc);
73
74         bool bibtopic = controller().usingBibtopic();
75
76         dialog_->databaseLW->clear();
77
78         string bibs(controller().params().getContents());
79         string bib;
80
81         while (!bibs.empty()) {
82                 bibs = split(bibs, bib, ',');
83                 bib = trim(bib);
84                 if (!bib.empty())
85                         dialog_->databaseLW->addItem(toqstr(bib));
86         }
87
88         dialog_->add_->bibLW->clear();
89
90         vector<string> bib_str;
91         controller().getBibFiles(bib_str);
92         for (vector<string>::const_iterator it = bib_str.begin();
93                 it != bib_str.end(); ++it) {
94                 string bibItem(changeExtension(*it, ""));
95                 dialog_->add_->bibLW->addItem(toqstr(bibItem));
96         }
97
98         string bibstyle(controller().getStylefile());
99
100         dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
101         dialog_->bibtocCB->setEnabled(!bibtopic);
102
103         string btprint(controller().params().getSecOptions());
104         int btp = 0;
105         if (btprint == "btPrintNotCited")
106                 btp = 1;
107         else if (btprint == "btPrintAll")
108                 btp = 2;
109
110         dialog_->btPrintCO->setCurrentItem(btp);
111         dialog_->btPrintCO->setEnabled(bibtopic);
112
113         dialog_->styleCB->clear();
114
115         int item_nr(-1);
116
117         vector<string> str;
118         controller().getBibStyles(str);
119         for (vector<string>::const_iterator it = str.begin();
120                 it != str.end(); ++it) {
121                 string item(changeExtension(*it, ""));
122                 if (item == bibstyle)
123                         item_nr = int(it - str.begin());
124                 dialog_->styleCB->insertItem(toqstr(item));
125         }
126
127         if (item_nr == -1 && !bibstyle.empty()) {
128                 dialog_->styleCB->insertItem(toqstr(bibstyle));
129                 item_nr = dialog_->styleCB->count() - 1;
130         }
131
132         if (item_nr != -1)
133                 dialog_->styleCB->setCurrentItem(item_nr);
134         else
135                 dialog_->styleCB->clearEdit();
136 }
137
138
139 void QBibtex::apply()
140 {
141         string dbs(fromqstr(dialog_->databaseLW->item(0)->text()));
142
143         unsigned int maxCount = dialog_->databaseLW->count();
144         for (unsigned int i = 1; i < maxCount; i++) {
145                 dbs += ',';
146                 dbs += fromqstr(dialog_->databaseLW->item(i)->text());
147         }
148
149         controller().params().setContents(dbs);
150
151         string const bibstyle(fromqstr(dialog_->styleCB->currentText()));
152         bool const bibtotoc(dialog_->bibtocCB->isChecked());
153
154         if (bibtotoc && (!bibstyle.empty())) {
155                 // both bibtotoc and style
156                 controller().params().setOptions("bibtotoc," + bibstyle);
157         } else if (bibtotoc) {
158                 // bibtotoc and no style
159                 controller().params().setOptions("bibtotoc");
160         } else {
161                 // only style. An empty one is valid, because some
162                 // documentclasses have an own \bibliographystyle{}
163                 // command!
164                 controller().params().setOptions(bibstyle);
165         }
166
167         // bibtopic allows three kinds of sections:
168         // 1. sections that include all cited references of the database(s)
169         // 2. sections that include all uncited references of the database(s)
170         // 3. sections that include all references of the database(s), cited or not
171         int btp = dialog_->btPrintCO->currentItem();
172
173         switch (btp) {
174         case 0:
175                 controller().params().setSecOptions("btPrintCited");
176                 break;
177         case 1:
178                 controller().params().setSecOptions("btPrintNotCited");
179                 break;
180         case 2:
181                 controller().params().setSecOptions("btPrintAll");
182                 break;
183         }
184
185         if (!controller().usingBibtopic())
186                 controller().params().setSecOptions("");
187 }
188
189
190 bool QBibtex::isValid()
191 {
192         return dialog_->databaseLW->count() != 0;
193 }
194
195 } // namespace frontend
196 } // namespace lyx