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 "CiteEnginesList.h"
22 #include "FuncRequest.h"
24 #include "qt_helpers.h"
25 #include "Validator.h"
27 #include "ui_BibtexAddUi.h"
29 #include "ButtonPolicy.h"
31 #include "frontends/alert.h"
33 #include "insets/InsetBibtex.h"
35 #include "support/debug.h"
36 #include "support/ExceptionMessage.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h" // changeExtension
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
42 #include <QDialogButtonBox>
43 #include <QPushButton>
44 #include <QListWidget>
49 using namespace lyx::support;
55 GuiBibtex::GuiBibtex(GuiView & lv)
56 : GuiDialog(lv, "bibtex", qt_("BibTeX Bibliography")),
57 params_(insetCode("bibtex"))
61 QDialog::setModal(true);
62 setWindowModality(Qt::WindowModal);
64 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
65 this, SLOT(slotButtonBox(QAbstractButton *)));
66 connect(stylePB, SIGNAL(clicked()),
67 this, SLOT(browsePressed()));
68 connect(deletePB, SIGNAL(clicked()),
69 this, SLOT(deletePressed()));
70 connect(upPB, SIGNAL(clicked()),
71 this, SLOT(upPressed()));
72 connect(downPB, SIGNAL(clicked()),
73 this, SLOT(downPressed()));
74 connect(styleCB, SIGNAL(editTextChanged(QString)),
75 this, SLOT(change_adaptor()));
76 connect(databaseLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
77 this, SLOT(databaseChanged()));
78 connect(bibtocCB, SIGNAL(clicked()),
79 this, SLOT(change_adaptor()));
80 connect(btPrintCO, SIGNAL(activated(int)),
81 this, SLOT(change_adaptor()));
82 connect(addBibPB, SIGNAL(clicked()),
83 this, SLOT(addPressed()));
84 connect(rescanPB, SIGNAL(clicked()),
85 this, SLOT(rescanClicked()));
86 connect(biblatexOptsLE, SIGNAL(textChanged(QString)),
87 this, SLOT(change_adaptor()));
88 connect(bibEncodingCO, SIGNAL(activated(int)),
89 this, SLOT(change_adaptor()));
91 add_ = new GuiBibtexAddDialog(this);
92 add_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
93 add_bc_.setOK(add_->buttonBox->button(QDialogButtonBox::Ok));
94 add_bc_.setCancel(add_->buttonBox->button(QDialogButtonBox::Cancel));
95 add_bc_.addCheckedLineEdit(add_->bibED, 0);
97 connect(add_->bibED, SIGNAL(textChanged(QString)),
98 this, SLOT(bibEDChanged()));
99 connect(add_->buttonBox, SIGNAL(clicked(QAbstractButton *)),
100 this, SLOT(addBBClicked(QAbstractButton *)));
101 connect(add_->rescanPB, SIGNAL(clicked()),
102 this, SLOT(rescanClicked()));
103 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
104 this, SLOT(addDatabase()));
105 connect(add_->bibLW, SIGNAL(itemActivated(QListWidgetItem *)),
106 add_, SLOT(accept()));
107 connect(add_->bibLW, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
108 this, SLOT(availableChanged()));
109 connect(add_->browsePB, SIGNAL(clicked()),
110 this, SLOT(browseBibPressed()));
112 add_->bibLW->setToolTip(formatToolTip(qt_("This list consists of all databases that are indexed by LaTeX and thus are found without a file path. "
113 "This is usually everything in the bib/ subdirectory of LaTeX's texmf tree. "
114 "If you want to reuse your own database, this is the place you should store it.")));
116 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
117 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
118 bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
119 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
120 bc().addReadOnly(databaseLW);
121 bc().addReadOnly(stylePB);
122 bc().addReadOnly(styleCB);
123 bc().addReadOnly(bibtocCB);
124 bc().addReadOnly(addBibPB);
125 bc().addReadOnly(bibEncodingCO);
126 // Delete/Up/Down are handled with more conditions in
127 // databaseChanged().
129 // Always put the default encoding in the first position.
130 bibEncodingCO->addItem(qt_("Document Encoding"), "default");
131 QMap<QString, QString> encodinglist;
132 for (auto const & encvar : encodings) {
133 if (!encvar.unsafe() && !encvar.guiName().empty())
134 encodinglist.insert(qt_(encvar.guiName()), toqstr(encvar.name()));
136 QMap<QString, QString>::const_iterator it = encodinglist.constBegin();
137 while (it != encodinglist.constEnd()) {
138 bibEncodingCO->addItem(it.key(), it.value());
142 // Make sure the delete/up/down buttons are disabled if necessary.
147 void GuiBibtex::addBBClicked(QAbstractButton * button)
149 switch (add_->buttonBox->standardButton(button)) {
150 case QDialogButtonBox::Ok:
154 case QDialogButtonBox::Cancel:
163 void GuiBibtex::bibEDChanged()
165 // Indicate to the button controller that the contents have
166 // changed. The actual test of validity is carried out by
167 // the checkedLineEdit.
168 add_bc_.setValid(true);
172 void GuiBibtex::change_adaptor()
178 void GuiBibtex::browsePressed()
180 QString const file = browseBst(QString());
185 QString const filen = changeExtension(file, "");
186 bool present = false;
187 unsigned int pres = 0;
189 for (int i = 0; i != styleCB->count(); ++i) {
190 if (styleCB->itemText(i) == filen) {
197 styleCB->insertItem(0, filen);
199 styleCB->setCurrentIndex(pres);
204 void GuiBibtex::browseBibPressed()
206 QString const file = browseBib(QString()).trimmed();
211 QString const f = changeExtension(file, "");
212 bool present = false;
214 for (int i = 0; i < add_->bibLW->count(); ++i) {
215 if (add_->bibLW->item(i)->text() == f)
220 add_->bibLW->addItem(f);
224 add_->bibED->setText(f);
228 void GuiBibtex::addPressed()
230 add_bc_.setValid(false);
235 void GuiBibtex::addDatabase()
237 int const sel = add_->bibLW->currentRow();
238 QString const file = add_->bibED->text().trimmed();
240 if (sel < 0 && file.isEmpty())
243 // Add the selected browser_bib keys to browser_database
244 // multiple selections are possible
245 for (int i = 0; i != add_->bibLW->count(); ++i) {
246 QListWidgetItem * const item = add_->bibLW->item(i);
247 if (add_->bibLW->isItemSelected(item)) {
248 add_->bibLW->setItemSelected(item, false);
249 QList<QListWidgetItem *> matches =
250 databaseLW->findItems(item->text(), Qt::MatchExactly);
251 if (matches.empty()) {
252 QString label = item->text();
253 QListWidgetItem * db = new QListWidgetItem(label);
254 db->setFlags(db->flags() | Qt::ItemIsSelectable);
255 databaseLW->addItem(db);
260 if (!file.isEmpty()) {
261 add_->bibED->clear();
262 QString const f = changeExtension(file, "");
263 QList<QListWidgetItem *> matches =
264 databaseLW->findItems(f, Qt::MatchExactly);
265 if (matches.empty()) {
266 QListWidgetItem * db = new QListWidgetItem(f);
267 db->setFlags(db->flags() | Qt::ItemIsSelectable);
268 databaseLW->addItem(db);
277 void GuiBibtex::deletePressed()
279 QListWidgetItem *cur = databaseLW->takeItem(databaseLW->currentRow());
288 void GuiBibtex::upPressed()
290 int row = databaseLW->currentRow();
291 QListWidgetItem *cur = databaseLW->takeItem(row);
292 databaseLW->insertItem(row - 1, cur);
293 databaseLW->setCurrentItem(cur);
298 void GuiBibtex::downPressed()
300 int row = databaseLW->currentRow();
301 QListWidgetItem *cur = databaseLW->takeItem(row);
302 databaseLW->insertItem(row + 1, cur);
303 databaseLW->setCurrentItem(cur);
308 void GuiBibtex::rescanClicked()
315 void GuiBibtex::databaseChanged()
317 bool readOnly = isBufferReadonly();
318 int count = databaseLW->count();
319 int row = databaseLW->currentRow();
320 deletePB->setEnabled(!readOnly && row != -1);
321 upPB->setEnabled(!readOnly && count > 1 && row > 0);
322 downPB->setEnabled(!readOnly && count > 1 && row < count - 1);
326 void GuiBibtex::availableChanged()
328 add_bc_.setValid(true);
332 void GuiBibtex::updateContents()
334 bool bibtopic = usingBibtopic();
335 bool biblatex = usingBiblatex();
338 setTitle(qt_("Biblatex Bibliography"));
340 setTitle(qt_("BibTeX Bibliography"));
344 docstring bibs = params_["bibfiles"];
347 while (!bibs.empty()) {
348 bibs = split(bibs, bib, ',');
351 QListWidgetItem * db = new QListWidgetItem(toqstr(bib));
352 db->setFlags(db->flags() | Qt::ItemIsSelectable);
353 databaseLW->addItem(db);
357 add_->bibLW->clear();
359 QStringList bibfiles = bibFiles();
360 for (int i = 0; i != bibfiles.count(); ++i)
361 add_->bibLW->addItem(changeExtension(bibfiles[i], ""));
363 QString const bibstyle = styleFile();
365 bibtocCB->setChecked(bibtotoc() && !bibtopic);
366 bibtocCB->setEnabled(!bibtopic);
369 btPrintCO->addItem(qt_("all cited references"), toqstr("btPrintCited"));
371 btPrintCO->addItem(qt_("all uncited references"), toqstr("btPrintNotCited"));
372 btPrintCO->addItem(qt_("all references"), toqstr("btPrintAll"));
373 if (usingBiblatex() && !buffer().masterParams().multibib.empty())
374 btPrintCO->addItem(qt_("all reference units"), toqstr("bibbysection"));
376 docstring btprint = params_["btprint"];
379 btprint = from_ascii("btPrintCited");
380 btPrintCO->setCurrentIndex(btPrintCO->findData(toqstr(btprint)));
382 docstring encoding = params_["encoding"];
383 if (encoding.empty())
385 encoding = from_ascii("default");
386 bibEncodingCO->setCurrentIndex(bibEncodingCO->findData(toqstr(encoding)));
388 // Only useful for biblatex
389 biblatexOptsLA->setVisible(biblatex);
390 biblatexOptsLE->setVisible(biblatex);
392 // only useful for BibTeX
393 bstGB->setVisible(!biblatex);
400 QStringList const str = bibStyles();
401 for (int i = 0; i != str.count(); ++i) {
402 QString item = changeExtension(str[i], "");
403 if (item == bibstyle)
405 styleCB->addItem(item);
408 if (item_nr == -1 && !bibstyle.isEmpty()) {
409 styleCB->addItem(bibstyle);
410 item_nr = styleCB->count() - 1;
415 styleCB->setCurrentIndex(item_nr);
417 styleCB->clearEditText();
419 biblatexOptsLE->setText(toqstr(params_["biblatexopts"]));
423 void GuiBibtex::applyView()
427 unsigned int maxCount = databaseLW->count();
428 for (unsigned int i = 0; i < maxCount; i++) {
431 QString item = databaseLW->item(i)->text();
432 docstring bibfile = qstring_to_ucs4(item);
436 params_["bibfiles"] = dbs;
438 docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());
439 bool const bibtotoc = bibtocCB->isChecked();
441 if (bibtotoc && !bibstyle.empty()) {
442 // both bibtotoc and style
443 params_["options"] = "bibtotoc," + bibstyle;
444 } else if (bibtotoc) {
445 // bibtotoc and no style
446 params_["options"] = from_ascii("bibtotoc");
448 // only style. An empty one is valid, because some
449 // documentclasses have an own \bibliographystyle{}
451 params_["options"] = bibstyle;
454 params_["biblatexopts"] = qstring_to_ucs4(biblatexOptsLE->text());
456 params_["btprint"] = qstring_to_ucs4(btPrintCO->itemData(btPrintCO->currentIndex()).toString());
458 params_["encoding"] = qstring_to_ucs4(bibEncodingCO->itemData(bibEncodingCO->currentIndex()).toString());
462 bool GuiBibtex::isValid()
464 return databaseLW->count() != 0;
468 QString GuiBibtex::browseBib(QString const & in_name) const
470 QString const label1 = qt_("D&ocuments");
471 QString const dir1 = toqstr(lyxrc.document_path);
472 QStringList const filter(qt_("BibTeX Databases (*.bib)"));
473 return browseRelToParent(in_name, bufferFilePath(),
474 qt_("Select a BibTeX database to add"), filter, false, label1, dir1);
478 QString GuiBibtex::browseBst(QString const & in_name) const
480 QString const label1 = qt_("D&ocuments");
481 QString const dir1 = toqstr(lyxrc.document_path);
482 QStringList const filter(qt_("BibTeX Styles (*.bst)"));
483 return browseRelToParent(in_name, bufferFilePath(),
484 qt_("Select a BibTeX style"), filter, false, label1, dir1);
488 QStringList GuiBibtex::bibStyles() const
490 QStringList sdata = texFileList("bstFiles.lst");
491 // test whether we have a valid list, otherwise run rescan
492 if (sdata.isEmpty()) {
494 sdata = texFileList("bstFiles.lst");
496 for (int i = 0; i != sdata.size(); ++i)
497 sdata[i] = onlyFileName(sdata[i]);
498 // sort on filename only (no path)
504 QStringList GuiBibtex::bibFiles() const
506 QStringList sdata = texFileList("bibFiles.lst");
507 // test whether we have a valid list, otherwise run rescan
508 if (sdata.isEmpty()) {
510 sdata = texFileList("bibFiles.lst");
512 for (int i = 0; i != sdata.size(); ++i)
513 sdata[i] = onlyFileName(sdata[i]);
514 // sort on filename only (no path)
520 void GuiBibtex::rescanBibStyles() const
523 rescanTexStyles("bib");
525 rescanTexStyles("bst bib");
529 bool GuiBibtex::usingBibtopic() const
531 return buffer().params().useBibtopic();
535 bool GuiBibtex::bibtotoc() const
537 return prefixIs(to_utf8(params_["options"]), "bibtotoc");
541 bool GuiBibtex::usingBiblatex() const
543 return buffer().masterBuffer()->params().useBiblatex();
547 QString GuiBibtex::styleFile() const
549 // the different bibtex packages have (and need) their
550 // own "plain" stylefiles
551 QString defaultstyle = toqstr(buffer().params().defaultBiblioStyle());
553 QString bst = toqstr(params_["options"]);
556 int pos = bst.indexOf(',');
559 // docstring bibtotoc = from_ascii("bibtotoc");
560 // bst = split(bst, bibtotoc, ',');
561 bst = bst.mid(pos + 1);
567 // propose default style file for new insets
568 // existing insets might have (legally) no bst files
569 // (if the class already provides a style)
570 if (bst.isEmpty() && params_["bibfiles"].empty())
577 bool GuiBibtex::initialiseParams(std::string const & sdata)
579 InsetCommand::string2params(sdata, params_);
584 void GuiBibtex::dispatchParams()
586 std::string const lfun = InsetCommand::params2string(params_);
587 dispatch(FuncRequest(getLfun(), lfun));
592 Dialog * createGuiBibtex(GuiView & lv) { return new GuiBibtex(lv); }
595 } // namespace frontend
598 #include "moc_GuiBibtex.cpp"