]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QBibtex.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / 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/QBibtexAddDialogBase.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 <qcombobox.h>
30 #include <qpushbutton.h>
31 #include <qlistbox.h>
32 #include <qcheckbox.h>
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_->databaseLB);
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_->databaseLB->clear();
78
79         string bibs(controller().params().getContents());
80         string bib;
81
82         while (!bibs.empty()) {
83                 bibs = split(bibs, bib, ',');
84                 bib = trim(bib);
85                 if (!bib.empty())
86                         dialog_->databaseLB->insertItem(toqstr(bib));
87         }
88
89         dialog_->add_->bibLB->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_->bibLB->insertItem(toqstr(bibItem));
97         }
98
99         dialog_->bibtocCB->setChecked(controller().bibtotoc() && !bibtopic);
100         dialog_->bibtocCB->setEnabled(!bibtopic);
101
102         string btprint(controller().params().getSecOptions());
103         int btp = 0;
104         if (btprint == "btPrintNotCited")
105                 btp = 1;
106         else if (btprint == "btPrintAll")
107                 btp = 2;
108
109         dialog_->btPrintCO->setCurrentItem(btp);
110         dialog_->btPrintCO->setEnabled(bibtopic);
111
112         dialog_->styleCB->clear();
113
114         int item_nr(-1);
115         string bibstyle(controller().getStylefile());
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_->databaseLB->text(0)));
142
143         unsigned int maxCount = dialog_->databaseLB->count();
144         for (unsigned int i = 1; i < maxCount; i++) {
145                 dbs += ',';
146                 dbs += fromqstr(dialog_->databaseLB->text(i));
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_->databaseLB->count() != 0;
193 }
194
195 } // namespace frontend
196 } // namespace lyx