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