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>
40 using namespace lyx::support;
46 GuiBibtex::GuiBibtex(GuiView & lv)
47 : GuiCommand(lv, "bibtex")
51 setViewTitle( _("BibTeX Bibliography"));
53 QDialog::setModal(true);
55 connect(okPB, SIGNAL(clicked()),
56 this, SLOT(slotOK()));
57 connect(closePB, SIGNAL(clicked()),
58 this, SLOT(slotClose()));
59 connect(stylePB, SIGNAL(clicked()),
60 this, SLOT(browsePressed()));
61 connect(deletePB, SIGNAL(clicked()),
62 this, SLOT(deletePressed()));
63 connect(styleCB, SIGNAL(editTextChanged(QString)),
64 this, SLOT(change_adaptor()));
65 connect(databaseLW, SIGNAL(itemSelectionChanged()),
66 this, SLOT(databaseChanged()));
67 connect(bibtocCB, SIGNAL(clicked()),
68 this, SLOT(change_adaptor()));
69 connect(btPrintCO, SIGNAL(activated(int)),
70 this, SLOT(change_adaptor()));
71 connect(addBibPB, SIGNAL(clicked()),
72 this, SLOT(addPressed()));
74 add_ = new GuiBibtexAddDialog(this);
75 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
76 add_bc_.setOK(add_->addPB);
77 add_bc_.setCancel(add_->closePB);
78 add_bc_.addCheckedLineEdit(add_->bibED, 0);
80 connect(add_->bibED, SIGNAL(textChanged(QString)),
81 this, SLOT(bibEDChanged()));
82 connect(add_->addPB, SIGNAL(clicked()),
83 this, SLOT(addDatabase()));
84 connect(add_->addPB, SIGNAL(clicked()),
85 add_, SLOT(accept()));
86 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
87 this, SLOT(addDatabase()));
88 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
89 add_, SLOT(accept()));
90 connect(add_->bibLW, SIGNAL(itemSelectionChanged()),
91 this, SLOT(availableChanged()));
92 connect(add_->browsePB, SIGNAL(clicked()),
93 this, SLOT(browseBibPressed()));
94 connect(add_->closePB, SIGNAL(clicked()),
95 add_, SLOT(reject()));
97 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
99 bc().setCancel(closePB);
100 bc().addReadOnly(databaseLW);
101 bc().addReadOnly(stylePB);
102 bc().addReadOnly(styleCB);
103 bc().addReadOnly(bibtocCB);
104 bc().addReadOnly(addBibPB);
105 bc().addReadOnly(deletePB);
109 void GuiBibtex::bibEDChanged()
111 // Indicate to the button controller that the contents have
112 // changed. The actual test of validity is carried out by
113 // the checkedLineEdit.
114 add_bc_.setValid(true);
118 void GuiBibtex::change_adaptor()
124 void GuiBibtex::browsePressed()
126 docstring const file = browseBst(docstring());
130 docstring const filen = from_utf8(changeExtension(to_utf8(file), ""));
131 bool present = false;
132 unsigned int pres = 0;
134 for (int i = 0; i != styleCB->count(); ++i) {
135 if (qstring_to_ucs4(styleCB->itemText(i)) == filen) {
142 styleCB->insertItem(0, toqstr(filen));
144 styleCB->setCurrentIndex(pres);
150 void GuiBibtex::browseBibPressed()
152 docstring const file = trim(browseBib(docstring()));
156 QString const f = toqstr(changeExtension(to_utf8(file), ""));
157 bool present = false;
159 for (int i = 0; i < add_->bibLW->count(); ++i) {
160 if (add_->bibLW->item(i)->text() == f)
165 add_->bibLW->addItem(f);
169 add_->bibED->setText(f);
174 void GuiBibtex::addPressed()
176 add_bc_.setValid(false);
181 void GuiBibtex::addDatabase()
183 int const sel = add_->bibLW->currentRow();
184 docstring const file = trim(qstring_to_ucs4(add_->bibED->text()));
186 if (sel < 0 && file.empty())
189 // Add the selected browser_bib keys to browser_database
190 // multiple selections are possible
191 for (int i = 0; i != add_->bibLW->count(); ++i) {
192 QListWidgetItem * const item = add_->bibLW->item(i);
193 if (add_->bibLW->isItemSelected(item)) {
194 add_->bibLW->setItemSelected(item, false);
195 QList<QListWidgetItem *> matches =
196 databaseLW->findItems(item->text(), Qt::MatchExactly);
197 if (matches.empty()) {
198 QString label = item->text();
199 QListWidgetItem * db = new QListWidgetItem(label);
200 db->setFlags(db->flags() | Qt::ItemIsSelectable
201 | Qt::ItemIsUserCheckable);
202 db->setCheckState(Qt::Checked);
203 databaseLW->addItem(db);
209 add_->bibED->clear();
210 QString const f = toqstr(from_utf8(changeExtension(to_utf8(file), "")));
211 QList<QListWidgetItem *> matches =
212 databaseLW->findItems(f, Qt::MatchExactly);
213 if (matches.empty()) {
214 QListWidgetItem * db = new QListWidgetItem(f);
215 db->setFlags(db->flags() | Qt::ItemIsSelectable
216 | Qt::ItemIsUserCheckable);
217 db->setCheckState(Qt::Checked);
218 databaseLW->addItem(db);
226 void GuiBibtex::deletePressed()
228 databaseLW->takeItem(databaseLW->currentRow());
234 void GuiBibtex::databaseChanged()
236 deletePB->setEnabled(!isBufferReadonly() && databaseLW->currentRow() != -1);
240 void GuiBibtex::availableChanged()
242 add_bc_.setValid(true);
246 void GuiBibtex::closeEvent(QCloseEvent *e)
253 void GuiBibtex::updateContents()
255 bool bibtopic = usingBibtopic();
259 docstring bibs = params_["bibfiles"];
260 docstring embs = params_["embed"];
264 while (!bibs.empty()) {
265 bibs = split(bibs, bib, ',');
266 embs = split(embs, emb, ',');
269 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
270 db->setFlags(db->flags() | Qt::ItemIsSelectable
271 | Qt::ItemIsUserCheckable);
272 db->setCheckState(emb.empty() ? Qt::Unchecked : Qt::Checked);
273 databaseLW->addItem(db);
277 add_->bibLW->clear();
279 vector<string> bib_str;
280 getBibFiles(bib_str);
281 for (vector<string>::const_iterator it = bib_str.begin();
282 it != bib_str.end(); ++it) {
283 string bibItem(changeExtension(*it, ""));
284 add_->bibLW->addItem(toqstr(bibItem));
287 string bibstyle = getStylefile();
289 bibtocCB->setChecked(bibtotoc() && !bibtopic);
290 bibtocCB->setEnabled(!bibtopic);
292 if (!bibtopic && btPrintCO->count() == 3)
293 btPrintCO->removeItem(1);
294 else if (bibtopic && btPrintCO->count() < 3)
295 btPrintCO->insertItem(1, qt_("all uncited references", 0));
297 docstring btprint = params_["btprint"];
299 if ((bibtopic && btprint == "btPrintNotCited") ||
300 (!bibtopic && btprint == "btPrintAll"))
302 else if (bibtopic && btprint == "btPrintAll")
305 btPrintCO->setCurrentIndex(btp);
313 for (vector<string>::const_iterator it = str.begin();
314 it != str.end(); ++it) {
315 string item(changeExtension(*it, ""));
316 if (item == bibstyle)
317 item_nr = int(it - str.begin());
318 styleCB->addItem(toqstr(item));
321 if (item_nr == -1 && !bibstyle.empty()) {
322 styleCB->addItem(toqstr(bibstyle));
323 item_nr = styleCB->count() - 1;
327 styleCB->setCurrentIndex(item_nr);
329 styleCB->clearEditText();
333 void GuiBibtex::applyView()
335 docstring dbs = qstring_to_ucs4(databaseLW->item(0)->text());
336 docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false");
338 unsigned int maxCount = databaseLW->count();
339 for (unsigned int i = 1; i < maxCount; i++) {
341 dbs += qstring_to_ucs4(databaseLW->item(i)->text());
343 emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false");
346 params_["bibfiles"] = dbs;
347 params_["embed"] = emb;
349 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
350 bool const bibtotoc = bibtocCB->isChecked();
352 if (bibtotoc && (!bibstyle.empty())) {
353 // both bibtotoc and style
354 params_["options"] = "bibtotoc," + bibstyle;
355 } else if (bibtotoc) {
356 // bibtotoc and no style
357 params_["options"] = from_ascii("bibtotoc");
359 // only style. An empty one is valid, because some
360 // documentclasses have an own \bibliographystyle{}
362 params_["options"] = bibstyle;
365 int btp = btPrintCO->currentIndex();
367 if (usingBibtopic()) {
368 // bibtopic allows three kinds of sections:
369 // 1. sections that include all cited references of the database(s)
370 // 2. sections that include all uncited references of the database(s)
371 // 3. sections that include all references of the database(s), cited or not
374 params_["btprint"] = from_ascii("btPrintCited");
377 params_["btprint"] = from_ascii("btPrintNotCited");
380 params_["btprint"] = from_ascii("btPrintAll");
386 params_["btprint"] = docstring();
390 params_["btprint"] = from_ascii("btPrintAll");
397 bool GuiBibtex::isValid()
399 return databaseLW->count() != 0;
403 docstring const GuiBibtex::browseBib(docstring const & in_name) const
406 docstring const label1 = _("Documents|#o#O");
407 docstring const dir1 = from_utf8(lyxrc.document_path);
408 FileFilterList const filter(_("BibTeX Databases (*.bib)"));
409 return browseRelFile(in_name, from_utf8(bufferFilepath()),
410 _("Select a BibTeX database to add"), filter, false, label1, dir1);
414 docstring const GuiBibtex::browseBst(docstring const & in_name) const
417 docstring const label1 = _("Documents|#o#O");
418 docstring const dir1 = from_utf8(lyxrc.document_path);
419 FileFilterList const filter(_("BibTeX Styles (*.bst)"));
420 return browseRelFile(in_name, from_utf8(bufferFilepath()),
421 _("Select a BibTeX style"), filter, false, label1, dir1);
425 void GuiBibtex::getBibStyles(vector<string> & data) const
429 getTexFileList("bstFiles.lst", data);
430 // test, if we have a valid list, otherwise run rescan
433 getTexFileList("bstFiles.lst", data);
435 vector<string>::iterator it = data.begin();
436 vector<string>::iterator end = data.end();
437 for (; it != end; ++it) {
438 *it = onlyFilename(*it);
440 // sort on filename only (no path)
441 sort(data.begin(), data.end());
445 void GuiBibtex::getBibFiles(vector<string> & data) const
449 getTexFileList("bibFiles.lst", data);
450 // test, if we have a valid list, otherwise run rescan
453 getTexFileList("bibFiles.lst", data);
455 vector<string>::iterator it = data.begin();
456 vector<string>::iterator end = data.end();
457 for (; it != end; ++it) {
458 *it = onlyFilename(*it);
460 // sort on filename only (no path)
461 sort(data.begin(), data.end());
465 void GuiBibtex::rescanBibStyles() const
471 bool GuiBibtex::usingBibtopic() const
473 return buffer().params().use_bibtopic;
477 bool GuiBibtex::bibtotoc() const
479 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
483 string const GuiBibtex::getStylefile() const
485 // the different bibtex packages have (and need) their
486 // own "plain" stylefiles
487 biblio::CiteEngine const engine = buffer().params().getEngine();
488 docstring defaultstyle;
490 case biblio::ENGINE_BASIC:
491 defaultstyle = from_ascii("plain");
493 case biblio::ENGINE_NATBIB_AUTHORYEAR:
494 defaultstyle = from_ascii("plainnat");
496 case biblio::ENGINE_NATBIB_NUMERICAL:
497 defaultstyle = from_ascii("plainnat");
499 case biblio::ENGINE_JURABIB:
500 defaultstyle = from_ascii("jurabib");
504 docstring bst = params_["options"];
507 if (contains(bst, ',')) {
508 docstring bibtotoc = from_ascii("bibtotoc");
509 bst = split(bst, bibtotoc, ',');
514 // propose default style file for new insets
515 // existing insets might have (legally) no bst files
516 // (if the class already provides a style)
517 if (bst.empty() && params_["bibfiles"].empty())
525 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
528 } // namespace frontend
531 #include "GuiBibtex_moc.cpp"