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"
20 #include "support/debug.h"
21 #include "ui_BibtexAddUi.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "support/gettext.h"
27 #include "ButtonPolicy.h"
29 #include "support/filetools.h" // changeExtension
30 #include "support/lstrings.h"
31 #include "support/FileFilterList.h"
33 #include <QPushButton>
34 #include <QListWidget>
36 #include <QCloseEvent>
47 using support::changeExtension;
48 using support::contains;
49 using support::FileFilterList;
50 using support::onlyFilename;
51 using support::prefixIs;
56 GuiBibtex::GuiBibtex(GuiView & lv)
57 : GuiCommand(lv, "bibtex")
61 setViewTitle( _("BibTeX Bibliography"));
63 QDialog::setModal(true);
65 connect(okPB, SIGNAL(clicked()),
66 this, SLOT(slotOK()));
67 connect(closePB, SIGNAL(clicked()),
68 this, SLOT(slotClose()));
69 connect(stylePB, SIGNAL(clicked()),
70 this, SLOT(browsePressed()));
71 connect(deletePB, SIGNAL(clicked()),
72 this, SLOT(deletePressed()));
73 connect(styleCB, SIGNAL(editTextChanged(QString)),
74 this, SLOT(change_adaptor()));
75 connect(databaseLW, SIGNAL(itemSelectionChanged()),
76 this, SLOT(databaseChanged()));
77 connect(bibtocCB, SIGNAL(clicked()),
78 this, SLOT(change_adaptor()));
79 connect(btPrintCO, SIGNAL(activated(int)),
80 this, SLOT(change_adaptor()));
81 connect(addBibPB, SIGNAL(clicked()),
82 this, SLOT(addPressed()));
84 add_ = new GuiBibtexAddDialog(this);
85 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
86 add_bc_.setOK(add_->addPB);
87 add_bc_.setCancel(add_->closePB);
88 add_bc_.addCheckedLineEdit(add_->bibED, 0);
90 connect(add_->bibED, SIGNAL(textChanged(QString)),
91 this, SLOT(bibEDChanged()));
92 connect(add_->addPB, SIGNAL(clicked()),
93 this, SLOT(addDatabase()));
94 connect(add_->addPB, SIGNAL(clicked()),
95 add_, SLOT(accept()));
96 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
97 this, SLOT(addDatabase()));
98 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
99 add_, SLOT(accept()));
100 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
101 this, SLOT(availableChanged()));
102 connect(add_->browsePB, SIGNAL(clicked()),
103 this, SLOT(browseBibPressed()));
104 connect(add_->closePB, SIGNAL(clicked()),
105 add_, SLOT(reject()));
107 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
109 bc().setCancel(closePB);
110 bc().addReadOnly(databaseLW);
111 bc().addReadOnly(stylePB);
112 bc().addReadOnly(styleCB);
113 bc().addReadOnly(bibtocCB);
114 bc().addReadOnly(addBibPB);
115 bc().addReadOnly(deletePB);
119 void GuiBibtex::bibEDChanged()
121 // Indicate to the button controller that the contents have
122 // changed. The actual test of validity is carried out by
123 // the checkedLineEdit.
124 add_bc_.setValid(true);
128 void GuiBibtex::change_adaptor()
134 void GuiBibtex::browsePressed()
136 docstring const file = browseBst(docstring());
140 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
141 bool present = false;
142 unsigned int pres = 0;
144 for (int i = 0; i != styleCB->count(); ++i) {
145 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
152 styleCB->insertItem(0, toqstr(filen));
154 styleCB->setCurrentIndex(pres);
160 void GuiBibtex::browseBibPressed()
162 docstring const file = trim(browseBib(docstring()));
166 QString const f = toqstr(changeExtension(to_utf8(file), ""));
167 bool present = false;
169 for (int i = 0; i < add_->bibLW->count(); ++i) {
170 if (add_->bibLW->item(i)->text() == f)
175 add_->bibLW->addItem(f);
179 add_->bibED->setText(f);
184 void GuiBibtex::addPressed()
186 add_bc_.setValid(false);
191 void GuiBibtex::addDatabase()
193 int const sel = add_->bibLW->currentRow();
194 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
196 if (sel < 0 && file.empty())
199 // Add the selected browser_bib keys to browser_database
200 // multiple selections are possible
201 for (int i = 0; i != add_->bibLW->count(); ++i) {
202 QListWidgetItem * const item = add_->bibLW->item(i);
203 if (add_->bibLW->isItemSelected(item)) {
204 add_->bibLW->setItemSelected(item, false);
205 QList<QListWidgetItem *> matches =
206 databaseLW->findItems(item->text(), Qt::MatchExactly);
208 databaseLW->addItem(item->text());
213 add_->bibED->clear();
214 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
215 QList<QListWidgetItem *> matches =
216 databaseLW->findItems(f, Qt::MatchExactly);
218 databaseLW->addItem(f);
225 void GuiBibtex::deletePressed()
227 databaseLW->takeItem(databaseLW->currentRow());
233 void GuiBibtex::databaseChanged()
235 deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
239 void GuiBibtex::availableChanged()
241 add_bc_.setValid(true);
245 void GuiBibtex::closeEvent(QCloseEvent *e)
252 void GuiBibtex::updateContents()
254 bool bibtopic = usingBibtopic();
258 docstring bibs = params_["bibfiles"];
261 while (!bibs.empty()) {
262 bibs = split(bibs, bib, ',');
265 databaseLW->addItem(toqstr(bib));
268 add_->bibLW->clear();
270 vector<string> bib_str;
271 getBibFiles(bib_str);
272 for (vector<string>::const_iterator it = bib_str.begin();
273 it != bib_str.end(); ++it) {
274 string bibItem(changeExtension(*it, ""));
275 add_->bibLW->addItem(toqstr(bibItem));
278 string bibstyle = getStylefile();
280 bibtocCB->setChecked(bibtotoc() && !bibtopic);
281 bibtocCB->setEnabled(!bibtopic);
283 docstring btprint = params_["btprint"];
285 if (btprint == "btPrintNotCited")
287 else if (btprint == "btPrintAll")
290 btPrintCO->setCurrentIndex(btp);
291 btPrintCO->setEnabled(bibtopic);
299 for (vector<string>::const_iterator it = str.begin();
300 it != str.end(); ++it) {
301 string item(changeExtension(*it, ""));
302 if (item == bibstyle)
303 item_nr = int(it - str.begin());
304 styleCB->addItem(toqstr(item));
307 if (item_nr == -1 && !bibstyle.empty()) {
308 styleCB->addItem(toqstr(bibstyle));
309 item_nr = styleCB->count() - 1;
313 styleCB->setCurrentIndex(item_nr);
315 styleCB->clearEditText();
319 void GuiBibtex::applyView()
321 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
323 unsigned int maxCount = databaseLW->count();
324 for (unsigned int i = 1; i < maxCount; i++) {
326 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
329 params_["bibfiles"] = dbs;
331 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
332 bool const bibtotoc = bibtocCB->isChecked();
334 if (bibtotoc && (!bibstyle.empty())) {
335 // both bibtotoc and style
336 params_["options"] = "bibtotoc," + bibstyle;
337 } else if (bibtotoc) {
338 // bibtotoc and no style
339 params_["options"] = from_ascii("bibtotoc");
341 // only style. An empty one is valid, because some
342 // documentclasses have an own \bibliographystyle{}
344 params_["options"] = bibstyle;
347 // bibtopic allows three kinds of sections:
348 // 1. sections that include all cited references of the database(s)
349 // 2. sections that include all uncited references of the database(s)
350 // 3. sections that include all references of the database(s), cited or not
351 int btp = btPrintCO->currentIndex();
355 params_["btprint"] = from_ascii("btPrintCited");
358 params_["btprint"] = from_ascii("btPrintNotCited");
361 params_["btprint"] = from_ascii("btPrintAll");
365 if (!usingBibtopic())
366 params_["btprint"] = docstring();
370 bool GuiBibtex::isValid()
372 return databaseLW->count() != 0;
376 docstring const GuiBibtex::browseBib(docstring const & in_name) const
379 docstring const label1 = _("Documents|#o#O");
380 docstring const dir1 = from_utf8(lyxrc.document_path);
381 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
382 return browseRelFile(in_name, from_utf8(bufferFilepath()),
383 _("Select a BibTeX database to add"), filter, false, label1, dir1);
387 docstring const GuiBibtex::browseBst(docstring const & in_name) const
390 docstring const label1 = _("Documents|#o#O");
391 docstring const dir1 = from_utf8(lyxrc.document_path);
392 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
393 return browseRelFile(in_name, from_utf8(bufferFilepath()),
394 _("Select a BibTeX style"), filter, false, label1, dir1);
398 void GuiBibtex::getBibStyles(vector<string> & data) const
402 getTexFileList("bstFiles.lst", data);
403 // test, if we have a valid list, otherwise run rescan
406 getTexFileList("bstFiles.lst", data);
408 vector<string>::iterator it = data.begin();
409 vector<string>::iterator end = data.end();
410 for (; it != end; ++it) {
411 *it = onlyFilename(*it);
413 // sort on filename only (no path)
414 std::sort(data.begin(), data.end());
418 void GuiBibtex::getBibFiles(vector<string> & data) const
422 getTexFileList("bibFiles.lst", data);
423 // test, if we have a valid list, otherwise run rescan
426 getTexFileList("bibFiles.lst", data);
428 vector<string>::iterator it = data.begin();
429 vector<string>::iterator end = data.end();
430 for (; it != end; ++it) {
431 *it = onlyFilename(*it);
433 // sort on filename only (no path)
434 std::sort(data.begin(), data.end());
438 void GuiBibtex::rescanBibStyles() const
444 bool GuiBibtex::usingBibtopic() const
446 return buffer().params().use_bibtopic;
450 bool GuiBibtex::bibtotoc() const
452 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
456 string const GuiBibtex::getStylefile() const
458 // the different bibtex packages have (and need) their
459 // own "plain" stylefiles
460 biblio::CiteEngine const engine = buffer().params().getEngine();
461 docstring defaultstyle;
463 case biblio::ENGINE_BASIC:
464 defaultstyle = from_ascii("plain");
466 case biblio::ENGINE_NATBIB_AUTHORYEAR:
467 defaultstyle = from_ascii("plainnat");
469 case biblio::ENGINE_NATBIB_NUMERICAL:
470 defaultstyle = from_ascii("plainnat");
472 case biblio::ENGINE_JURABIB:
473 defaultstyle = from_ascii("jurabib");
477 docstring bst = params_["options"];
480 if (contains(bst, ',')) {
481 docstring bibtotoc = from_ascii("bibtotoc");
482 bst = split(bst, bibtotoc, ',');
487 // propose default style file for new insets
488 // existing insets might have (legally) no bst files
489 // (if the class already provides a style)
490 if (bst.empty() && params_["bibfiles"].empty())
498 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
501 } // namespace frontend
504 #include "GuiBibtex_moc.cpp"