]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QBibtex.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / qt2 / 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
21 #include "controllers/ControlBibtex.h"
22
23 #include "support/filetools.h" // ChangeExtension
24 #include "support/lstrings.h"
25
26 #include <qcombobox.h>
27 #include <qpushbutton.h>
28 #include <qlistbox.h>
29 #include <qcheckbox.h>
30
31
32 using lyx::support::ChangeExtension;
33 using lyx::support::contains;
34 using lyx::support::prefixIs;
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"))
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_->databaseLB);
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         bool bibtopic = controller().usingBibtopic();
70
71         dialog_->databaseLB->clear();
72
73         string bibs(controller().params().getContents());
74         string bib;
75
76         while (!bibs.empty()) {
77                 bibs = split(bibs, bib, ',');
78                 bib = trim(bib);
79                 if (!bib.empty())
80                         dialog_->databaseLB->insertItem(toqstr(bib));
81         }
82
83         dialog_->add_->bibLB->clear();
84
85         vector<string> bib_str;
86         controller().getBibFiles(bib_str);
87         for (vector<string>::const_iterator it = bib_str.begin();
88                 it != bib_str.end(); ++it) {
89                 string bibItem(ChangeExtension(*it, ""));
90                 dialog_->add_->bibLB->insertItem(toqstr(bibItem));
91         }
92
93         string bibtotoc = "bibtotoc";
94         string bibstyle(controller().params().getOptions());
95
96         // bibtotoc exists?
97         if (prefixIs(bibstyle, bibtotoc)){
98                 // bibstyle exists?
99                 if (contains(bibstyle,','))
100                         bibstyle = split(bibstyle, bibtotoc, ',');
101                 else
102                         bibstyle.erase();
103         }
104
105         if (prefixIs(bibstyle, bibtotoc) && !bibtopic)
106                 dialog_->bibtocCB->setChecked(true);
107         else
108                 dialog_->bibtocCB->setChecked(false);
109
110         dialog_->bibtocCB->setEnabled(!bibtopic);
111
112         string btprint(controller().params().getSecOptions());
113         int btp = 0;
114         if (btprint == "btPrintNotCited")
115                 btp = 1;
116         else if (btprint == "btPrintAll")
117                 btp = 2;
118
119         dialog_->btPrintCO->setCurrentItem(btp);
120         dialog_->btPrintCO->setEnabled(bibtopic);
121
122         dialog_->styleCB->clear();
123
124         int item_nr(-1);
125
126         vector<string> str;
127         controller().getBibStyles(str);
128         for (vector<string>::const_iterator it = str.begin();
129                 it != str.end(); ++it) {
130                 string item(ChangeExtension(*it, ""));
131                 if (item == bibstyle)
132                         item_nr = int(it - str.begin());
133                 dialog_->styleCB->insertItem(toqstr(item));
134         }
135
136         if (item_nr == -1) {
137                 dialog_->styleCB->insertItem(toqstr(bibstyle));
138                 item_nr = dialog_->styleCB->count() - 1;
139         }
140
141         dialog_->styleCB->setCurrentItem(item_nr);
142 }
143
144
145 void QBibtex::apply()
146 {
147         string dbs(fromqstr(dialog_->databaseLB->text(0)));
148
149         unsigned int maxCount = dialog_->databaseLB->count();
150         for (unsigned int i = 1; i < maxCount; i++) {
151                 dbs += ',';
152                 dbs += fromqstr(dialog_->databaseLB->text(i));
153         }
154
155         controller().params().setContents(dbs);
156
157         string const bibstyle(fromqstr(dialog_->styleCB->currentText()));
158         bool const bibtotoc(dialog_->bibtocCB->isChecked());
159
160         if (bibtotoc && (!bibstyle.empty())) {
161                 // both bibtotoc and style
162                 controller().params().setOptions("bibtotoc," + bibstyle);
163         } else if (bibtotoc) {
164                 // bibtotoc and no style
165                 controller().params().setOptions("bibtotoc");
166         } else {
167                 // only style. An empty one is valid, because some
168                 // documentclasses have an own \bibliographystyle{}
169                 // command!
170                 controller().params().setOptions(bibstyle);
171         }
172
173         // bibtopic allows three kinds of sections:
174         // 1. sections that include all cited references of the database(s)
175         // 2. sections that include all uncited references of the database(s)
176         // 3. sections that include all references of the database(s), cited or not
177         int btp = dialog_->btPrintCO->currentItem();
178
179         switch (btp) {
180         case 0:
181                 controller().params().setSecOptions("btPrintCited");
182                 break;
183         case 1:
184                 controller().params().setSecOptions("btPrintNotCited");
185                 break;
186         case 2:
187                 controller().params().setSecOptions("btPrintAll");
188                 break;
189         }
190
191         if (!controller().usingBibtopic())
192                 controller().params().setSecOptions("");
193 }
194
195
196 bool QBibtex::isValid()
197 {
198         return dialog_->databaseLB->count() != 0;
199 }
200
201 } // namespace frontend
202 } // namespace lyx