3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
16 #include "GuiBibtex.h"
19 #include "BufferParams.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
27 #include "ButtonPolicy.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
31 #include "support/FileFilterList.h"
33 #include "frontend_helpers.h"
36 #include <QPushButton>
37 #include <QListWidget>
39 #include <QCloseEvent>
50 using support::changeExtension;
51 using support::contains;
52 using support::FileFilterList;
53 using support::onlyFilename;
54 using support::prefixIs;
59 GuiBibtex::GuiBibtex(LyXView & lv)
60 : GuiCommand(lv, "bibtex")
64 setViewTitle( _("BibTeX Bibliography"));
66 QDialog::setModal(true);
68 connect(okPB, SIGNAL(clicked()),
69 this, SLOT(slotOK()));
70 connect(closePB, SIGNAL(clicked()),
71 this, SLOT(slotClose()));
72 connect(stylePB, SIGNAL(clicked()),
73 this, SLOT(browsePressed()));
74 connect(deletePB, SIGNAL(clicked()),
75 this, SLOT(deletePressed()));
76 connect(styleCB, SIGNAL(editTextChanged(QString)),
77 this, SLOT(change_adaptor()));
78 connect(databaseLW, SIGNAL(itemSelectionChanged()),
79 this, SLOT(databaseChanged()));
80 connect(bibtocCB, SIGNAL(clicked()),
81 this, SLOT(change_adaptor()));
82 connect(btPrintCO, SIGNAL(activated(int)),
83 this, SLOT(change_adaptor()));
84 connect(addBibPB, SIGNAL(clicked()),
85 this, SLOT(addPressed()));
87 add_ = new GuiBibtexAddDialog(this);
88 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
89 add_bc_.setOK(add_->addPB);
90 add_bc_.setCancel(add_->closePB);
91 add_bc_.addCheckedLineEdit(add_->bibED, 0);
93 connect(add_->bibED, SIGNAL(textChanged(QString)),
94 this, SLOT(bibEDChanged()));
95 connect(add_->addPB, SIGNAL(clicked()),
96 this, SLOT(addDatabase()));
97 connect(add_->addPB, SIGNAL(clicked()),
98 add_, SLOT(accept()));
99 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
100 this, SLOT(addDatabase()));
101 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
102 add_, SLOT(accept()));
103 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
104 this, SLOT(availableChanged()));
105 connect(add_->browsePB, SIGNAL(clicked()),
106 this, SLOT(browseBibPressed()));
107 connect(add_->closePB, SIGNAL(clicked()),
108 add_, SLOT(reject()));
110 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
112 bc().setCancel(closePB);
113 bc().addReadOnly(databaseLW);
114 bc().addReadOnly(stylePB);
115 bc().addReadOnly(styleCB);
116 bc().addReadOnly(bibtocCB);
117 bc().addReadOnly(addBibPB);
118 bc().addReadOnly(deletePB);
122 void GuiBibtex::bibEDChanged()
124 // Indicate to the button controller that the contents have
125 // changed. The actual test of validity is carried out by
126 // the checkedLineEdit.
127 add_bc_.setValid(true);
131 void GuiBibtex::change_adaptor()
137 void GuiBibtex::browsePressed()
139 docstring const file = browseBst(docstring());
143 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
144 bool present = false;
145 unsigned int pres = 0;
147 for (int i = 0; i != styleCB->count(); ++i) {
148 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
155 styleCB->insertItem(0, toqstr(filen));
157 styleCB->setCurrentIndex(pres);
163 void GuiBibtex::browseBibPressed()
165 docstring const file = trim(browseBib(docstring()));
169 QString const f = toqstr(changeExtension(to_utf8(file), ""));
170 bool present = false;
172 for (int i = 0; i < add_->bibLW->count(); ++i) {
173 if (add_->bibLW->item(i)->text() == f)
178 add_->bibLW->addItem(f);
182 add_->bibED->setText(f);
187 void GuiBibtex::addPressed()
189 add_bc_.setValid(false);
194 void GuiBibtex::addDatabase()
196 int const sel = add_->bibLW->currentRow();
197 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
199 if (sel < 0 && file.empty())
202 // Add the selected browser_bib keys to browser_database
203 // multiple selections are possible
204 for (int i = 0; i != add_->bibLW->count(); ++i) {
205 QListWidgetItem * const item = add_->bibLW->item(i);
206 if (add_->bibLW->isItemSelected(item)) {
207 add_->bibLW->setItemSelected(item, false);
208 QList<QListWidgetItem *> matches =
209 databaseLW->findItems(item->text(), Qt::MatchExactly);
211 databaseLW->addItem(item->text());
216 add_->bibED->clear();
217 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
218 QList<QListWidgetItem *> matches =
219 databaseLW->findItems(f, Qt::MatchExactly);
221 databaseLW->addItem(f);
228 void GuiBibtex::deletePressed()
230 databaseLW->takeItem(databaseLW->currentRow());
236 void GuiBibtex::databaseChanged()
238 deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
242 void GuiBibtex::availableChanged()
244 add_bc_.setValid(true);
248 void GuiBibtex::closeEvent(QCloseEvent *e)
255 void GuiBibtex::updateContents()
257 bool bibtopic = usingBibtopic();
261 docstring bibs = params_["bibfiles"];
264 while (!bibs.empty()) {
265 bibs = split(bibs, bib, ',');
268 databaseLW->addItem(toqstr(bib));
271 add_->bibLW->clear();
273 vector<string> bib_str;
274 getBibFiles(bib_str);
275 for (vector<string>::const_iterator it = bib_str.begin();
276 it != bib_str.end(); ++it) {
277 string bibItem(changeExtension(*it, ""));
278 add_->bibLW->addItem(toqstr(bibItem));
281 string bibstyle = getStylefile();
283 bibtocCB->setChecked(bibtotoc() && !bibtopic);
284 bibtocCB->setEnabled(!bibtopic);
286 docstring btprint = params_["btprint"];
288 if (btprint == "btPrintNotCited")
290 else if (btprint == "btPrintAll")
293 btPrintCO->setCurrentIndex(btp);
294 btPrintCO->setEnabled(bibtopic);
302 for (vector<string>::const_iterator it = str.begin();
303 it != str.end(); ++it) {
304 string item(changeExtension(*it, ""));
305 if (item == bibstyle)
306 item_nr = int(it - str.begin());
307 styleCB->addItem(toqstr(item));
310 if (item_nr == -1 && !bibstyle.empty()) {
311 styleCB->addItem(toqstr(bibstyle));
312 item_nr = styleCB->count() - 1;
316 styleCB->setCurrentIndex(item_nr);
318 styleCB->clearEditText();
322 void GuiBibtex::applyView()
324 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
326 unsigned int maxCount = databaseLW->count();
327 for (unsigned int i = 1; i < maxCount; i++) {
329 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
332 params_["bibfiles"] = dbs;
334 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
335 bool const bibtotoc = bibtocCB->isChecked();
337 if (bibtotoc && (!bibstyle.empty())) {
338 // both bibtotoc and style
339 params_["options"] = "bibtotoc," + bibstyle;
340 } else if (bibtotoc) {
341 // bibtotoc and no style
342 params_["options"] = from_ascii("bibtotoc");
344 // only style. An empty one is valid, because some
345 // documentclasses have an own \bibliographystyle{}
347 params_["options"] = bibstyle;
350 // bibtopic allows three kinds of sections:
351 // 1. sections that include all cited references of the database(s)
352 // 2. sections that include all uncited references of the database(s)
353 // 3. sections that include all references of the database(s), cited or not
354 int btp = btPrintCO->currentIndex();
358 params_["btprint"] = from_ascii("btPrintCited");
361 params_["btprint"] = from_ascii("btPrintNotCited");
364 params_["btprint"] = from_ascii("btPrintAll");
368 if (!usingBibtopic())
369 params_["btprint"] = docstring();
373 bool GuiBibtex::isValid()
375 return databaseLW->count() != 0;
379 docstring const GuiBibtex::browseBib(docstring const & in_name) const
382 docstring const label1 = _("Documents|#o#O");
383 docstring const dir1 = from_utf8(lyxrc.document_path);
384 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
385 return browseRelFile(in_name, from_utf8(bufferFilepath()),
386 _("Select a BibTeX database to add"), filter, false, label1, dir1);
390 docstring const GuiBibtex::browseBst(docstring const & in_name) const
393 docstring const label1 = _("Documents|#o#O");
394 docstring const dir1 = from_utf8(lyxrc.document_path);
395 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
396 return browseRelFile(in_name, from_utf8(bufferFilepath()),
397 _("Select a BibTeX style"), filter, false, label1, dir1);
401 void GuiBibtex::getBibStyles(vector<string> & data) const
405 getTexFileList("bstFiles.lst", data);
406 // test, if we have a valid list, otherwise run rescan
409 getTexFileList("bstFiles.lst", data);
411 vector<string>::iterator it = data.begin();
412 vector<string>::iterator end = data.end();
413 for (; it != end; ++it) {
414 *it = onlyFilename(*it);
416 // sort on filename only (no path)
417 std::sort(data.begin(), data.end());
421 void GuiBibtex::getBibFiles(vector<string> & data) const
425 getTexFileList("bibFiles.lst", data);
426 // test, if we have a valid list, otherwise run rescan
429 getTexFileList("bibFiles.lst", data);
431 vector<string>::iterator it = data.begin();
432 vector<string>::iterator end = data.end();
433 for (; it != end; ++it) {
434 *it = onlyFilename(*it);
436 // sort on filename only (no path)
437 std::sort(data.begin(), data.end());
441 void GuiBibtex::rescanBibStyles() const
447 bool GuiBibtex::usingBibtopic() const
449 return buffer().params().use_bibtopic;
453 bool GuiBibtex::bibtotoc() const
455 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
459 string const GuiBibtex::getStylefile() const
461 // the different bibtex packages have (and need) their
462 // own "plain" stylefiles
463 biblio::CiteEngine const engine = buffer().params().getEngine();
464 docstring defaultstyle;
466 case biblio::ENGINE_BASIC:
467 defaultstyle = from_ascii("plain");
469 case biblio::ENGINE_NATBIB_AUTHORYEAR:
470 defaultstyle = from_ascii("plainnat");
472 case biblio::ENGINE_NATBIB_NUMERICAL:
473 defaultstyle = from_ascii("plainnat");
475 case biblio::ENGINE_JURABIB:
476 defaultstyle = from_ascii("jurabib");
480 docstring bst = params_["options"];
483 if (contains(bst, ',')) {
484 docstring bibtotoc = from_ascii("bibtotoc");
485 bst = split(bst, bibtotoc, ',');
490 // propose default style file for new insets
491 // existing insets might have (legally) no bst files
492 // (if the class already provides a style)
493 if (bst.empty() && params_["bibfiles"].empty())
501 Dialog * createGuiBibtex(LyXView & lv) { return new GuiBibtex(lv); }
504 } // namespace frontend
507 #include "GuiBibtex_moc.cpp"