]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBibtex.cpp
'using namespace std' instead of 'using std::xxx'
[lyx.git] / src / frontends / qt4 / GuiBibtex.cpp
1 /**
2  * \file GuiBibtex.cpp
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 Angus Leeming
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiBibtex.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "support/debug.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
24 #include "LyXRC.h"
25 #include "support/gettext.h"
26
27 #include "ButtonPolicy.h"
28
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
31 #include "support/FileFilterList.h"
32
33 #include <QPushButton>
34 #include <QListWidget>
35 #include <QCheckBox>
36 #include <QCloseEvent>
37 #include <QLineEdit>
38
39 using namespace std;
40
41 namespace lyx {
42 namespace frontend {
43
44 using support::changeExtension;
45 using support::contains;
46 using support::FileFilterList;
47 using support::onlyFilename;
48 using support::prefixIs;
49 using support::split;
50 using support::trim;
51
52
53 GuiBibtex::GuiBibtex(GuiView & lv)
54         : GuiCommand(lv, "bibtex")
55 {
56         setupUi(this);
57
58         setViewTitle( _("BibTeX Bibliography"));
59
60         QDialog::setModal(true);
61
62         connect(okPB, SIGNAL(clicked()),
63                 this, SLOT(slotOK()));
64         connect(closePB, SIGNAL(clicked()),
65                 this, SLOT(slotClose()));
66         connect(stylePB, SIGNAL(clicked()),
67                 this, SLOT(browsePressed()));
68         connect(deletePB, SIGNAL(clicked()),
69                 this, SLOT(deletePressed()));
70         connect(styleCB, SIGNAL(editTextChanged(QString)),
71                 this, SLOT(change_adaptor()));
72         connect(databaseLW, SIGNAL(itemSelectionChanged()),
73                 this, SLOT(databaseChanged()));
74         connect(bibtocCB, SIGNAL(clicked()),
75                 this, SLOT(change_adaptor()));
76         connect(btPrintCO, SIGNAL(activated(int)),
77                 this, SLOT(change_adaptor()));
78         connect(addBibPB, SIGNAL(clicked()),
79                 this, SLOT(addPressed()));
80
81         add_ = new GuiBibtexAddDialog(this);
82         add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
83         add_bc_.setOK(add_->addPB);
84         add_bc_.setCancel(add_->closePB);
85         add_bc_.addCheckedLineEdit(add_->bibED, 0);
86
87         connect(add_->bibED, SIGNAL(textChanged(QString)),
88                 this, SLOT(bibEDChanged()));
89         connect(add_->addPB, SIGNAL(clicked()),
90                 this, SLOT(addDatabase()));
91         connect(add_->addPB, SIGNAL(clicked()),
92                 add_, SLOT(accept()));
93         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
94                 this, SLOT(addDatabase()));
95         connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
96                 add_, SLOT(accept()));
97         connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
98                 this, SLOT(availableChanged()));
99         connect(add_->browsePB, SIGNAL(clicked()),
100                 this, SLOT(browseBibPressed()));
101         connect(add_->closePB, SIGNAL(clicked()),
102                 add_, SLOT(reject()));
103
104         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
105         bc().setOK(okPB);
106         bc().setCancel(closePB);
107         bc().addReadOnly(databaseLW);
108         bc().addReadOnly(stylePB);
109         bc().addReadOnly(styleCB);
110         bc().addReadOnly(bibtocCB);
111         bc().addReadOnly(addBibPB);
112         bc().addReadOnly(deletePB);
113 }
114
115
116 void GuiBibtex::bibEDChanged()
117 {
118         // Indicate to the button controller that the contents have
119         // changed. The actual test of validity is carried out by
120         // the checkedLineEdit.
121         add_bc_.setValid(true);
122 }
123
124
125 void GuiBibtex::change_adaptor()
126 {
127         changed();
128 }
129
130
131 void GuiBibtex::browsePressed()
132 {
133         docstring const file = browseBst(docstring());
134
135         if (!file.empty()) {
136                 // FIXME UNICODE
137                 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
138                 bool present = false;
139                 unsigned int pres = 0;
140
141                 for (int i = 0; i != styleCB->count(); ++i) {
142                         if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
143                                 present = true;
144                                 pres = i;
145                         }
146                 }
147
148                 if (!present)
149                         styleCB->insertItem(0, toqstr(filen));
150
151                 styleCB->setCurrentIndex(pres);
152                 changed();
153         }
154 }
155
156
157 void GuiBibtex::browseBibPressed()
158 {
159         docstring const file = trim(browseBib(docstring()));
160
161         if (!file.empty()) {
162                 // FIXME UNICODE
163                 QString const f = toqstr(changeExtension(to_utf8(file), ""));
164                 bool present = false;
165
166                 for (int i = 0; i < add_->bibLW->count(); ++i) {
167                         if (add_->bibLW->item(i)->text() == f)
168                                 present = true;
169                 }
170
171                 if (!present) {
172                         add_->bibLW->addItem(f);
173                         changed();
174                 }
175
176                 add_->bibED->setText(f);
177         }
178 }
179
180
181 void GuiBibtex::addPressed()
182 {
183         add_bc_.setValid(false);
184         add_->exec();
185 }
186
187
188 void GuiBibtex::addDatabase()
189 {
190         int const sel = add_->bibLW->currentRow();
191         docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
192
193         if (sel < 0 && file.empty())
194                 return;
195
196         // Add the selected browser_bib keys to browser_database
197         // multiple selections are possible
198         for (int i = 0; i != add_->bibLW->count(); ++i) {
199                 QListWidgetItem * const item = add_->bibLW->item(i);
200                 if (add_->bibLW->isItemSelected(item)) {
201                         add_->bibLW->setItemSelected(item, false);
202                         QList<QListWidgetItem *> matches =
203                                 databaseLW->findItems(item->text(), Qt::MatchExactly);
204                         if (matches.empty())
205                                 databaseLW->addItem(item->text());
206                 }
207         }
208
209         if (!file.empty()) {
210                 add_->bibED->clear();
211                 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
212                 QList<QListWidgetItem *> matches =
213                         databaseLW->findItems(f, Qt::MatchExactly);
214                 if (matches.empty())
215                         databaseLW->addItem(f);
216         }
217
218         changed();
219 }
220
221
222 void GuiBibtex::deletePressed()
223 {
224         databaseLW->takeItem(databaseLW->currentRow());
225         changed();
226 }
227
228
229
230 void GuiBibtex::databaseChanged()
231 {
232         deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
233 }
234
235
236 void GuiBibtex::availableChanged()
237 {
238         add_bc_.setValid(true);
239 }
240
241
242 void GuiBibtex::closeEvent(QCloseEvent *e)
243 {
244         slotClose();
245         e->accept();
246 }
247
248
249 void GuiBibtex::updateContents()
250 {
251         bool bibtopic = usingBibtopic();
252
253         databaseLW->clear();
254
255         docstring bibs = params_["bibfiles"];
256         docstring bib;
257
258         while (!bibs.empty()) {
259                 bibs = split(bibs, bib, ',');
260                 bib = trim(bib);
261                 if (!bib.empty())
262                         databaseLW->addItem(toqstr(bib));
263         }
264
265         add_->bibLW->clear();
266
267         vector<string> bib_str;
268         getBibFiles(bib_str);
269         for (vector<string>::const_iterator it = bib_str.begin();
270                 it != bib_str.end(); ++it) {
271                 string bibItem(changeExtension(*it, ""));
272                 add_->bibLW->addItem(toqstr(bibItem));
273         }
274
275         string bibstyle = getStylefile();
276
277         bibtocCB->setChecked(bibtotoc() && !bibtopic);
278         bibtocCB->setEnabled(!bibtopic);
279
280         docstring btprint = params_["btprint"];
281         int btp = 0;
282         if (btprint == "btPrintNotCited")
283                 btp = 1;
284         else if (btprint == "btPrintAll")
285                 btp = 2;
286
287         btPrintCO->setCurrentIndex(btp);
288         btPrintCO->setEnabled(bibtopic);
289
290         styleCB->clear();
291
292         int item_nr(-1);
293
294         vector<string> str;
295         getBibStyles(str);
296         for (vector<string>::const_iterator it = str.begin();
297                 it != str.end(); ++it) {
298                 string item(changeExtension(*it, ""));
299                 if (item == bibstyle)
300                         item_nr = int(it - str.begin());
301                 styleCB->addItem(toqstr(item));
302         }
303
304         if (item_nr == -1 && !bibstyle.empty()) {
305                 styleCB->addItem(toqstr(bibstyle));
306                 item_nr = styleCB->count() - 1;
307         }
308
309         if (item_nr != -1)
310                 styleCB->setCurrentIndex(item_nr);
311         else
312                 styleCB->clearEditText();
313 }
314
315
316 void GuiBibtex::applyView()
317 {
318         docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
319
320         unsigned int maxCount = databaseLW->count();
321         for (unsigned int i = 1; i < maxCount; i++) {
322                 dbs += ',';
323                 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
324         }
325
326         params_["bibfiles"] = dbs;
327
328         docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
329         bool const bibtotoc = bibtocCB->isChecked();
330
331         if (bibtotoc && (!bibstyle.empty())) {
332                 // both bibtotoc and style
333                 params_["options"] = "bibtotoc," + bibstyle;
334         } else if (bibtotoc) {
335                 // bibtotoc and no style
336                 params_["options"] = from_ascii("bibtotoc");
337         } else {
338                 // only style. An empty one is valid, because some
339                 // documentclasses have an own \bibliographystyle{}
340                 // command!
341                 params_["options"] = bibstyle;
342         }
343
344         // bibtopic allows three kinds of sections:
345         // 1. sections that include all cited references of the database(s)
346         // 2. sections that include all uncited references of the database(s)
347         // 3. sections that include all references of the database(s), cited or not
348         int btp = btPrintCO->currentIndex();
349
350         switch (btp) {
351         case 0:
352                 params_["btprint"] = from_ascii("btPrintCited");
353                 break;
354         case 1:
355                 params_["btprint"] = from_ascii("btPrintNotCited");
356                 break;
357         case 2:
358                 params_["btprint"] = from_ascii("btPrintAll");
359                 break;
360         }
361
362         if (!usingBibtopic())
363                 params_["btprint"] = docstring();
364 }
365
366
367 bool GuiBibtex::isValid()
368 {
369         return databaseLW->count() != 0;
370 }
371
372
373 docstring const GuiBibtex::browseBib(docstring const & in_name) const
374 {
375         // FIXME UNICODE
376         docstring const label1 = _("Documents|#o#O");
377         docstring const dir1 = from_utf8(lyxrc.document_path);
378         FileFilterList const filter(_("BibTeX Databases (*.bib)"));
379         return browseRelFile(in_name, from_utf8(bufferFilepath()),
380                 _("Select a BibTeX database to add"), filter, false, label1, dir1);
381 }
382
383
384 docstring const GuiBibtex::browseBst(docstring const & in_name) const
385 {
386         // FIXME UNICODE
387         docstring const label1 = _("Documents|#o#O");
388         docstring const dir1 = from_utf8(lyxrc.document_path);
389         FileFilterList const filter(_("BibTeX Styles (*.bst)"));
390         return browseRelFile(in_name, from_utf8(bufferFilepath()),
391                 _("Select a BibTeX style"), filter, false, label1, dir1);
392 }
393
394
395 void GuiBibtex::getBibStyles(vector<string> & data) const
396 {
397         data.clear();
398
399         getTexFileList("bstFiles.lst", data);
400         // test, if we have a valid list, otherwise run rescan
401         if (data.empty()) {
402                 rescanBibStyles();
403                 getTexFileList("bstFiles.lst", data);
404         }
405         vector<string>::iterator it  = data.begin();
406         vector<string>::iterator end = data.end();
407         for (; it != end; ++it) {
408                 *it = onlyFilename(*it);
409         }
410         // sort on filename only (no path)
411         std::sort(data.begin(), data.end());
412 }
413
414
415 void GuiBibtex::getBibFiles(vector<string> & data) const
416 {
417         data.clear();
418
419         getTexFileList("bibFiles.lst", data);
420         // test, if we have a valid list, otherwise run rescan
421         if (data.empty()) {
422                 rescanBibStyles();
423                 getTexFileList("bibFiles.lst", data);
424         }
425         vector<string>::iterator it  = data.begin();
426         vector<string>::iterator end = data.end();
427         for (; it != end; ++it) {
428                 *it = onlyFilename(*it);
429         }
430         // sort on filename only (no path)
431         std::sort(data.begin(), data.end());
432 }
433
434
435 void GuiBibtex::rescanBibStyles() const
436 {
437         rescanTexStyles();
438 }
439
440
441 bool GuiBibtex::usingBibtopic() const
442 {
443         return buffer().params().use_bibtopic;
444 }
445
446
447 bool GuiBibtex::bibtotoc() const
448 {
449         return prefixIs(to_utf8(params_["options"]), "bibtotoc");
450 }
451
452
453 string const GuiBibtex::getStylefile() const
454 {
455         // the different bibtex packages have (and need) their
456         // own "plain" stylefiles
457         biblio::CiteEngine const engine = buffer().params().getEngine();
458         docstring defaultstyle;
459         switch (engine) {
460         case biblio::ENGINE_BASIC:
461                 defaultstyle = from_ascii("plain");
462                 break;
463         case biblio::ENGINE_NATBIB_AUTHORYEAR:
464                 defaultstyle = from_ascii("plainnat");
465                 break;
466         case biblio::ENGINE_NATBIB_NUMERICAL:
467                 defaultstyle = from_ascii("plainnat");
468                 break;
469         case biblio::ENGINE_JURABIB:
470                 defaultstyle = from_ascii("jurabib");
471                 break;
472         }
473
474         docstring bst = params_["options"];
475         if (bibtotoc()){
476                 // bibstyle exists?
477                 if (contains(bst, ',')) {
478                         docstring bibtotoc = from_ascii("bibtotoc");
479                         bst = split(bst, bibtotoc, ',');
480                 } else
481                         bst.erase();
482         }
483
484         // propose default style file for new insets
485         // existing insets might have (legally) no bst files
486         // (if the class already provides a style)
487         if (bst.empty() && params_["bibfiles"].empty())
488                 bst = defaultstyle;
489
490         // FIXME UNICODE
491         return to_utf8(bst);
492 }
493
494
495 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
496
497
498 } // namespace frontend
499 } // namespace lyx
500
501 #include "GuiBibtex_moc.cpp"